Balanced binary search trees
Rotations
February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1
Balanced binary search trees Rotations February 08, 2019 Cinda - - PowerPoint PPT Presentation
Balanced binary search trees Rotations February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1 Height of a BST Insert 7 7 Insert 4 4 9 Insert 1 Insert 9 1 5 Insert 5 height = log(5) = 2 This is a complete BST February
February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1
Cinda Heeren / Will Evans / Geoffrey Tien 2
Insert 7 Insert 4 Insert 1 Insert 9 Insert 5 7 4 1 9 5 This is a complete BST height = log(5) = 2
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 3
Insert 9 Insert 1 Insert 7 Insert 4 Insert 5 This is a linked list with extra unused pointers 9 1 7 4 5 height = n – 1 = 4 = O(n)
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 4 February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 5
A C E B D F A C E B D F G
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 6
A C D B F E A B C D
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 7 February 08, 2019
– and the in-order traversal is also preserved
Cinda Heeren / Will Evans / Geoffrey Tien 8 February 08, 2019
50 30 70 50 30 70
Cinda Heeren / Will Evans / Geoffrey Tien 9
rotateLeft(x) x y z A B C D x y z A B C D
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 10
rotateRight(z) x y z A B C D x y z A B C D
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 11
47 32 81 13 40 Left rotation of 32 (referred to as x) 37 44 Create a pointer to x’s right child temp
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 12
47 32 81 13 40 Left rotation of 32 (referred to as x) 37 44 Create a pointer to x’s right child temp Set x’s right child to temp’s left child Detach temp’s left child
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 13
47 32 81 13 40 Left rotation of 32 (referred to as x) 37 44 Create a pointer to x’s right child temp Set x’s right child to temp’s left child Detach temp’s left child Make x the left child of temp Make temp the left child of x’s parent
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 14
Left rotation of 32 (completed) 47 40 81 32 44 13 37
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 15
Right rotation of 47 (referred to as x) 47 32 81 13 40 7 29 37 temp Create a pointer to x’s left child
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 16
Right rotation of 47 (referred to as x) 47 32 81 13 40 7 29 37 Create a pointer to x’s left child Set x’s left child to temp’s right child Detach temp’s right child temp
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 17
Right rotation of 47 (referred to as x) 47 32 81 13 40 7 29 37 Create a pointer to x’s left child Set x’s left child to temp’s right child Detach temp’s right child temp Make x the right child of temp
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 18
32 13 47 7 29 temp 40 81 37 Right rotation of 47 (referred to as x) Create a pointer to x’s left child Set x’s left child to temp’s right child Detach temp’s right child Make x the right child of temp Make x the new root
February 08, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 19
3 7 12 13 18 26 31 root
February 08, 2019
g b h a e i c f d g e h b f i c a d
Cinda Heeren / Will Evans / Geoffrey Tien 20 February 08, 2019
February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 21
43 19 63 57 60 50 78 38 4 21
February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 22
enum balance_type {LEFT_HEAVY = -1, BALANCED = 0, RIGHT_HEAVY = +1}; class AVLNode { public: int data; // or template type AVLNode left; AVLNode right; balance_type balance; AVLNode(int value) { ... } AVLNode(int val, AVLNode left1, AVLNode right1) { ... } }
February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 23
3 3 7 12 16 3 7 12 19 3 16 27 31 44
3 7 5 12 16 3 7 9 12 19 3 16 27 31 44 21 24
February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 24
C B A LL imbalance Solve with a right rotation around C C A B LR imbalance Left rotation around A, becomes LL case A B C RR imbalance Symmetric to left imbalance cases A C B RL imbalance
February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 25
February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 26