Top Banner
15.2 Tree Balancing: AVL trees Order of insertion into binary search tree greatly affects balance best order results in balanced tree worst order results in linked list (lopsided tree) AVL trees are a solution named for creators, Russian mathematicians in the 1960s Georgii Maksimovich Adel'son-Vel'skii Evgenii Mikhailovich Landis height-balanced tree specialized binary search tree that has a balance factor balance factor reflects the height difference of a node's subtrees balance factor is calculated by taking height of left subtree and subtracting height of right subtree balance factor is only allowed to be -1, 0 or 1 keeps height difference to at most 1 tree must be rebalanced when balance factor exceeds these values AVL Tree ADT Member variables a binary search tree that maintains the balance factor Basic Operations use the constructor, empty(), search() and traversals from BST insert an item & rebalance if needed delete an item & rebalance of needed AVL tree node need to add a member variable for balance factor so have data, balance factor and pointers to left & right children Example trees w/ balance factors Rebalance Rotations 4 rotations to restore balance factor AVL Trees Wednesday, October 31, 2007 8:02 PM CS223 Page 1
5

AVL Trees - California State University, Bakersfieldmelissa/cs223-f07/notes/avl.pdf · AVL tree node need to add a member variable for balance factor so have data, balance factor

Jul 25, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AVL Trees - California State University, Bakersfieldmelissa/cs223-f07/notes/avl.pdf · AVL tree node need to add a member variable for balance factor so have data, balance factor

15.2 Tree Balancing: AVL treesOrder of insertion into binary search tree greatly affects balance

best order results in balanced treeworst order results in linked list (lopsided tree)

AVL trees are a solutionnamed for creators, Russian mathematicians in the 1960s

Georgii Maksimovich Adel'son-Vel'skiiEvgenii Mikhailovich Landis

height-balanced treespecialized binary search tree that has a balance factor

balance factor reflects the height difference of a node's subtreesbalance factor is calculated by taking height of left subtree and subtracting height of right subtreebalance factor is only allowed to be -1, 0 or 1

keeps height difference to at most 1tree must be rebalanced when balance factor exceeds these values

AVL Tree ADTMember variables

a binary search tree that maintains the balance factorBasic Operations

use the constructor, empty(), search() and traversals from BSTinsert an item & rebalance if neededdelete an item & rebalance of needed

AVL tree nodeneed to add a member variable for balance factorso have data, balance factor and pointers to left & right children

Example trees w/ balance factors

Rebalance Rotations4 rotations to restore balance factor

AVL Trees Wednesday, October 31, 20078:02 PM

CS223 Page 1

Page 2: AVL Trees - California State University, Bakersfieldmelissa/cs223-f07/notes/avl.pdf · AVL tree node need to add a member variable for balance factor so have data, balance factor

4 rotations to restore balance factorRight Rotation

Left Rotation

Left-Right Rotation

Right-Left Rotation

CS223 Page 2

Page 3: AVL Trees - California State University, Bakersfieldmelissa/cs223-f07/notes/avl.pdf · AVL tree node need to add a member variable for balance factor so have data, balance factor

Rotation Details - Insertion caseApply rotation when node's balance factor is +2 or -2 & is nearest ancestor to inserted nodeCases:

Right rotationinserted node is in left subtree of left child of unbalanced node (+2)

Left rotationinserted node is in right subtree of right child of unbalanced node (-2)

Left-right rotationinserted node is in right subtree of left child of unbalanced node (+2)

Right-left rotationinserted node is in left subtree of right child of unbalanced node (-2)

Rotation PseudocodeRight Rotation

A is unbalanced nodeB is left childset parent of B to A's parentset parent of A to Bset A's left to B's right

(value in B's right is between value of A & value of B)set B's right to A

CS223 Page 3

Page 4: AVL Trees - California State University, Bakersfieldmelissa/cs223-f07/notes/avl.pdf · AVL tree node need to add a member variable for balance factor so have data, balance factor

Left RotationA is unbalanced nodeB is right childset parent of B to A's parentset parent of A to Bset A's right to B's leftset B's left to A

Left Right Rotation

rotate left at B (node A's left child)rotate right at node AAlternate Method:

set C's parent to A's parentset A's parent to Cset B's parent to Cset B's right to X (C's left)set A's left to Y (C's right)set C's left to B

CS223 Page 4

Page 5: AVL Trees - California State University, Bakersfieldmelissa/cs223-f07/notes/avl.pdf · AVL tree node need to add a member variable for balance factor so have data, balance factor

set C's left to B set C's right to B

Right-left rotationrotate right at B (node A's right child)rotate left at node A

Rotation on Deletionmore difficult notations than on insertioncan delete nodes & leaves

Runtimesince tree is balanced, searches are O(log2n)overhead to rebalance

increases inserts delete runtimestudies show 45% of inserts require rotations

approx half are double rotationsif searching is primary operation, fast search outweighs slower insert

CS223 Page 5