CS 225 Data Structures Feb. 27 AVL L Tre rees Wad ade Fag - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Feb. 27 AVL L Tre rees Wad ade Fag - - PowerPoint PPT Presentation

CS 225 Data Structures Feb. 27 AVL L Tre rees Wad ade Fag agen-Ulm lmschneid ider Left ft Rotation 38 13 51 10 25 40 84 89 66 95 38 13 51 10 25 84 A 89 B C D 38 84 13 51 51 89 10 25 84 A A C B D 89


slide-1
SLIDE 1

CS 225

Data Structures

  • Feb. 27 – AVL

L Tre rees

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2

13 10 25 38 51 40 84 89 66 95

Left ft Rotation

slide-3
SLIDE 3

13 10 25 38 51 84 89

A B C D

slide-4
SLIDE 4

13 10 25 38 51 84 89

A B C D

84 51 89

A B C D

slide-5
SLIDE 5

13 10 25 38 51 40 84 89 66 95 84 51 89

A B C D

slide-6
SLIDE 6

13 10 25 37 38 51

slide-7
SLIDE 7

13 10 25 37 38 51

slide-8
SLIDE 8

BST Rotation Summary ry

  • Four kinds of rotations (L, R, LR, RL)
  • All rotations are local (subtrees are not impacted)
  • All rotations are constant time: O(1)
  • BST property maintained

GOAL: We call these trees:

slide-9
SLIDE 9

AVL Trees

Three issues for consideration:

  • Rotations
  • Maintaining Height
  • Detecting Imbalance
slide-10
SLIDE 10

AVL Tree Rotations

Four templates for rotations:

slide-11
SLIDE 11

t

t1 t2 t3 t4

Finding the Rotation

slide-12
SLIDE 12

t

t1 t2 t3 t4

Finding the Rotation

t

t1 t2 t3 t4

If an insertion occurred in subtrees t3 or t4 and a subtree was detected at t:

slide-13
SLIDE 13

t

tL tR

Finding the Rotation

slide-14
SLIDE 14

t

tL tR

Finding the Rotation

slide-15
SLIDE 15

t

tL tA tB

Finding the Rotation

slide-16
SLIDE 16

t

t1 t2 t3 t4

Theorem: If an insertion occurred in subtrees t3 or t4 and a subtree was detected at t, then a __________ rotation about t restores the balance of the tree. We gauge this by noting the balance factor of t->right is ______.

Finding the Rotation

slide-17
SLIDE 17

13 10 25 38 51 40 84 89 66

Example:

slide-18
SLIDE 18

t

t1 t2 t3 t4

Finding the Rotation

slide-19
SLIDE 19

t

t1 t2 t3 t4

Finding the Rotation

t

t1 t2 t3 t4

Theorem: If an insertion occurred in subtrees t2 or t3 and a subtree was detected at t:

slide-20
SLIDE 20

t

t1 t2 t3 t4

Theorem: If an insertion occurred in subtrees t2 or t3 and a subtree was detected at t, then a __________ rotation about t restores the balance of the tree. We gauge this by noting the balance factor of t->right is ______.

Finding the Rotation

slide-21
SLIDE 21

In Insertion into an AVL Tree

5 3 6 4 2 8 10 9 12 11 1 7

struct TreeNode { T key; unsigned height; TreeNode *left; TreeNode *right; }; 1 2 3 4 5 6

_insert(6.5)

slide-22
SLIDE 22

Insert (pseudo code): 1: Insert at proper place 2: Check for imbalance 3: Rotate, if necessary 4: Update height

In Insertion into an AVL Tree

5 3 6 4 2 8 10 9 12 11 1 7

struct TreeNode { T key; unsigned height; TreeNode *left; TreeNode *right; }; 1 2 3 4 5 6

_insert(6.5)

slide-23
SLIDE 23

template <typename K, typename V> void AVL<K, D>::_insert(const K & key, const V & data, TreeNode *& cur) { if (cur == NULL) { cur = new TreeNode(key, data); } else if (key < cur->key) { _insert( key, data, cur->left ); } else if (key > cur->key) { _insert( key, data, cur->right );} _ensureBalance(cur); } 151 152 153 157 160 166 167

slide-24
SLIDE 24

template <typename K, typename V> void AVL<K, D>::_ensureBalance(TreeNode *& cur) { // Calculate the balance factor: int balance = height(cur->right) - height(cur->left); // Check if the node is current not in balance: if ( balance == -2 ) { int l_balance = height(cur->left->right) - height(cur->left->left); if ( l_balance == -1 ) { ____________________________; } else { ____________________________; } } else if ( balance == 2 ) { int r_balance = height(cur->right->right) - height(cur->right->left); if( r_balance == 1 ) { _____________________________; } else { _____________________________; } } _updateHeight(cur); }; 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

slide-25
SLIDE 25

5 3 6 4 2 8 10 9 12 11 1 7

slide-26
SLIDE 26

AVL Tree Analysis

We know: insert, remove and find runs in: __________. We will argue that: h = _________.

slide-27
SLIDE 27

AVL Tree Analysis

Definition of big-O: …or, with pictures: