AVL trees BST rotations AVL introduction February 26, 2020 Cinda - - PowerPoint PPT Presentation

avl trees
SMART_READER_LITE
LIVE PREVIEW

AVL trees BST rotations AVL introduction February 26, 2020 Cinda - - PowerPoint PPT Presentation

AVL trees BST rotations AVL introduction February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 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


slide-1
SLIDE 1

AVL trees

BST rotations AVL introduction

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

slide-2
SLIDE 2

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 / Andy Roth / Geoffrey Tien 2 February 26, 2020

50 30 70 50 30 70

slide-3
SLIDE 3

Left rotation

Cinda Heeren / Andy Roth / Geoffrey Tien 3

rotateLeft(x) x y z A B C D x y z A B C D

February 26, 2020

slide-4
SLIDE 4

Right rotation

Cinda Heeren / Andy Roth / Geoffrey Tien 4

rotateRight(z) x y z A B C D x y z A B C D

February 26, 2020

slide-5
SLIDE 5

Left rotation example

Cinda Heeren / Andy Roth / Geoffrey Tien 5

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 26, 2020

slide-6
SLIDE 6

Left rotation example

Cinda Heeren / Andy Roth / Geoffrey Tien 6

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 26, 2020

slide-7
SLIDE 7

Left rotation example

Cinda Heeren / Andy Roth / Geoffrey Tien 7

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 26, 2020

slide-8
SLIDE 8

Left rotation example

Cinda Heeren / Andy Roth / Geoffrey Tien 8

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

February 26, 2020

slide-9
SLIDE 9

Right rotation example

Cinda Heeren / Andy Roth / Geoffrey Tien 9

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 26, 2020

slide-10
SLIDE 10

Right rotation example

Cinda Heeren / Andy Roth / Geoffrey Tien 10

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 26, 2020

slide-11
SLIDE 11

Right rotation example

Cinda Heeren / Andy Roth / Geoffrey Tien 11

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 26, 2020

slide-12
SLIDE 12

Right rotation example

Cinda Heeren / Andy Roth / Geoffrey Tien 12

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 26, 2020

slide-13
SLIDE 13

Exercise

  • Suggest a sequence of rotations which will produce a perfect

binary tree from the starting tree:

Cinda Heeren / Andy Roth / Geoffrey Tien 13

Balancing with rotations

3 7 12 13 18 26 31 root

February 26, 2020

slide-14
SLIDE 14

AVL trees

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14

slide-15
SLIDE 15

BST rotation

Summary

g b h a e i c f d g e h b f i c a d

Cinda Heeren / Andy Roth / Geoffrey Tien 15 February 26, 2020

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

the tree structure

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

slide-16
SLIDE 16

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 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16

43 19 63 57 60 50 78 38 4 21

slide-17
SLIDE 17

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 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17

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-18
SLIDE 18

AVL imbalance

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18

  • 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-19
SLIDE 19

AVL imbalance

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19

  • 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-20
SLIDE 20

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 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20

Maintaining balance

slide-21
SLIDE 21

AVL insertion

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21

Pseudocode

if root is NULL Create new node containing item, assign root to it, and return true else if item is equal to root->data item exists already, return false else if item < root->data Recursively insert the item into the left subtree if height of left subtree has increased (increase variable is true) balance--; if balance == 0, reset increase variable to false if balance < -1 reset increase variable to false perform rebalanceLeft else if item > root->data (symmetric to left subtree case, incrementing balance) rebalanceLeft and rebalanceRight are the rotations to correct the 4 imbalance cases Summary: BST insertion, then fix imbalances moving up towards root

slide-22
SLIDE 22

AVL insertion example

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 22

Insert(65) 47 32 71 93 65

slide-23
SLIDE 23

AVL insertion example

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23

Insert(65) Insert(82) 47 32 71 93 65 82 OK OK OK RR imbalance

slide-24
SLIDE 24

AVL insertion example

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24

Insert(65) Insert(82) 71 47 93 82 32 65 Insert(87) 87 OK OK LR imbalance

slide-25
SLIDE 25

AVL insertion example

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25

Insert(65) Insert(82) 71 47 87 82 32 65 Insert(87) OK 93 OK Complexity? What is the cost of doing one "fix"? How many fixes need to be performed for a single insertion?

slide-26
SLIDE 26

Exercise

  • Starting with an empty AVL tree, insert the keys in the

following order:

– 1, 3, 5, 7, 9, 8, 6, 4, 2

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 26

slide-27
SLIDE 27

Readings for this lesson

  • Carrano & Henry

– Chapter 19.4 (end – rotations) – Chapter 19.5 (AVL trees)

  • Play with some interactive AVL tree applets

– https://www.cs.usfca.edu/~galles/visualization/AVLtree.html – https://visualgo.net/bn/bst

February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 27