Balanced binary search trees Rotations February 08, 2019 Cinda - - PowerPoint PPT Presentation

balanced binary search trees
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Balanced binary search trees

Rotations

February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1

slide-2
SLIDE 2

Height of a BST

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

slide-3
SLIDE 3

Height of a BST

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

slide-4
SLIDE 4

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

Cinda Heeren / Will Evans / Geoffrey Tien 4 February 08, 2019

slide-5
SLIDE 5

"Balanced" binary trees

Cinda Heeren / Will Evans / Geoffrey Tien 5

A C E B D F A C E B D F G

February 08, 2019

slide-6
SLIDE 6

"Imbalanced" binary trees

Cinda Heeren / Will Evans / Geoffrey Tien 6

A C D B F E A B C D

February 08, 2019

slide-7
SLIDE 7

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!)

Cinda Heeren / Will Evans / Geoffrey Tien 7 February 08, 2019

slide-8
SLIDE 8

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

Cinda Heeren / Will Evans / Geoffrey Tien 8 February 08, 2019

50 30 70 50 30 70

slide-9
SLIDE 9

Left rotation

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

slide-10
SLIDE 10

Right rotation

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

slide-11
SLIDE 11

Left rotation example

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

slide-12
SLIDE 12

Left rotation example

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

slide-13
SLIDE 13

Left rotation example

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

slide-14
SLIDE 14

Left rotation example

Cinda Heeren / Will Evans / Geoffrey Tien 14

Left rotation of 32 (completed) 47 40 81 32 44 13 37

February 08, 2019

slide-15
SLIDE 15

Right rotation example

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

slide-16
SLIDE 16

Right rotation example

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

slide-17
SLIDE 17

Right rotation example

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

slide-18
SLIDE 18

Right rotation example

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

slide-19
SLIDE 19

Exercise

  • Suggest a sequence of rotations which will produce a perfect

binary tree from the starting tree:

Cinda Heeren / Will Evans / Geoffrey Tien 19

Balancing with rotations

3 7 12 13 18 26 31 root

February 08, 2019

slide-20
SLIDE 20

BST rotation

Summary

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

  • Rotations preserve the in-order BST property, while altering

the tree structure

– we can use rotations to bring imbalanced trees back into balance

slide-21
SLIDE 21

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

February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 21

43 19 63 57 60 50 78 38 4 21

slide-22
SLIDE 22

AVL nodes

  • 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

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) { ... } }

slide-23
SLIDE 23

AVL imbalance

February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 23

  • Balanced trees:

3 3 7 12 16 3 7 12 19 3 16 27 31 44

  • Imbalanced trees:

3 7 5 12 16 3 7 9 12 19 3 16 27 31 44 21 24

slide-24
SLIDE 24

AVL imbalance

February 08, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 24

  • 4 cases of imbalance

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

slide-25
SLIDE 25

AVL insertion

  • 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

Maintaining balance

slide-26
SLIDE 26

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