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 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 2
Height of a BST Insert 9 9 Insert 1 1 Insert 7 Insert 4 7 Insert 5 height = n – 1 = 4 = O( n ) 4 This is a linked list with extra unused pointers 5 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 3
Balanced binary trees • A binary tree is balanced if – Leaves are all about the same distance from the root – The exact specification varies • Sometimes trees are balanced by comparing the height of nodes – e.g. the height of a node’s right subtree is at most one different from the height of its left subtree (e.g. AVL trees) • Sometimes a tree's height is compared to the number of nodes – e.g. red-black trees February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 4
"Balanced" binary trees A A B C B C D E F D E F G February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 5
"Imbalanced" binary trees A A B C B D E C D F February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 6
Balanced BSTs • It would be ideal if a BST was always close to complete – i.e. balanced • How do we guarantee a balanced BST? – We have to make the structure and / or the insertion and removal algorithms more complex • e.g. red – black trees, AVL trees (next!) February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 7
Rotations • An item must be inserted into a BST at the correct position • The shape of a tree is determined by – The values of the items inserted into the tree – The order in which those values are inserted • This suggests that there is more than one tree (shape) that can contain the same values • A tree’s shape can be altered by rotation while still preserving the BST property – and the in-order traversal is also preserved 70 50 50 30 70 30 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 8
Left rotation rotateLeft(x) x z x y z D y C B C D A B A February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 9
Right rotation rotateRight(z) x z x y z D y C B C D A B A February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 10
Left rotation example Left rotation of 32 (referred to as x ) 47 Create a pointer to x ’s right child 32 81 13 40 temp 37 44 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 11
Left rotation example Left rotation of 32 (referred to as x ) 47 Create a pointer to x ’s right child Set x ’s right child to temp ’s left child 32 81 Detach temp ’s left child 13 40 temp 37 44 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 12
Left rotation example Left rotation of 32 (referred to as x ) 47 Create a pointer to x ’s right child Set x ’s right child to temp ’s left child 32 81 Detach temp ’s left child Make x the left child of temp 13 40 temp Make temp the left child of x ’s parent 37 44 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 13
Left rotation example Left rotation of 32 (completed) 47 40 81 32 44 13 37 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 14
Right rotation example Right rotation of 47 (referred to as x ) 47 Create a pointer to x ’s left child temp 32 81 13 40 7 29 37 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 15
Right rotation example Right rotation of 47 (referred to as x ) 47 Create a pointer to x ’s left child temp Set x ’s left child to temp ’s right child 32 81 Detach temp ’s right child 13 40 7 29 37 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 16
Right rotation example Right rotation of 47 (referred to as x ) 47 Create a pointer to x ’s left child temp Set x ’s left child to temp ’s right child 32 81 Detach temp ’s right child Make x the right child of temp 13 40 7 29 37 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 17
Right rotation example temp Right rotation of 47 (referred to as x ) 32 Create a pointer to x ’s left child Set x ’s left child to temp ’s right child 13 47 Detach temp ’s right child Make x the right child of temp 7 29 40 81 Make x the new root 37 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 18
Exercise Balancing with rotations • Suggest a sequence of rotations which will produce a perfect binary tree from the starting tree: root 3 7 12 13 18 26 31 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 19
BST rotation Summary g g e h b h b f i a e i a f c c d d • Rotations preserve the in-order BST property, while altering the tree structure – we can use rotations to bring imbalanced trees back into balance February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 20
AVL trees • An AVL tree is a balanced BST – Each node's left and right subtrees differ in height by at most 1 – Rebalancing via rotations occurs when an insertion or removal causes excessive height difference • AVL tree nodes contain extra information to support this height information 43 19 63 4 38 57 78 21 50 60 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 21
AVL nodes 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) { ... } } • AVLNode is almost the same as a binary tree node – additional balance field indicates that state of subtree balance at that node – there are many alternate implementations! February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 22
AVL imbalance • Balanced trees: 3 3 12 27 7 3 16 12 31 7 3 19 44 16 • Imbalanced trees: 27 12 3 12 31 3 16 7 3 19 44 7 5 16 21 9 24 February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 23
AVL imbalance • 4 cases of imbalance C C A A B A B C A B C B LL imbalance LR imbalance RR imbalance RL imbalance Solve with a right Left rotation Symmetric to left rotation around C around A, becomes imbalance cases LL case February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 24
AVL insertion Maintaining balance • The best way to keep a tree balanced, is to never let it become imbalanced! • AVL insertion begins with ordinary BST insertion (i.e. a leaf node) followed by rotations to maintain balance – i.e. AVL properties are satisfied before and after insertion – if the balance attribute of a subtree's root node becomes critical (-2 or +2) as a result of inserting the new leaf, rebalance it! February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 25
Readings for this lesson • Carrano & Henry – Chapter 15.3 (Binary search tree) – Chapter 16.3 (Implementation – BST) – Chapter 19.4 (end – rotations) • Next class – Carrano & Henry: Chapter 19.5 (AVL trees) February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 26
Recommend
More recommend