CS 225 Data Structures Ma March 4 AV AVL Trees G G Carl Evans - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Ma March 4 AV AVL Trees G G Carl Evans - - PowerPoint PPT Presentation

CS 225 Data Structures Ma March 4 AV AVL Trees G G Carl Evans Resources on the Website https://courses.engr.illinois.edu/cs225/fa2019/pages/lectures.html Le Left R Rot otation on 38 13 51 10 25 40 84 66 89 95 38 13 51


slide-1
SLIDE 1

CS 225

Data Structures

Ma March 4– AV AVL Trees

G G Carl Evans

slide-2
SLIDE 2

Resources on the Website

  • https://courses.engr.illinois.edu/cs225/fa2019/pages/lectures.html
slide-3
SLIDE 3

13 10 25 38 51 40 84 89 66 95

Le Left R Rot

  • tation
  • n
slide-4
SLIDE 4

13 10 25 38 51 84 89

A B C D

slide-5
SLIDE 5

13 10 25 38 51 84 89

A B C D

84 51 89

A B C D

slide-6
SLIDE 6

13 10 25 38 51 40 84 89 66 95

slide-7
SLIDE 7

13 10 25 37 38 51

slide-8
SLIDE 8

13 10 25 37 38 51

slide-9
SLIDE 9

BS BST R Rot

  • tation
  • n Su

Summary

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

AV AVL Trees

Three issues for consideration:

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

AV AVL Tree Rotations

Four templates for rotations:

slide-12
SLIDE 12

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 ______.

Fi Findi nding ng the he Rotation n on n Ins nser ert

slide-13
SLIDE 13

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 ______.

Fi Findi nding ng the he Rotation n on n Ins nser ert

slide-14
SLIDE 14

In Inser ertio tion in into an an AVL Tree ee

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

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

In Inser ertio tion in into an an AVL Tree ee

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

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

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

Hei Heigh ght-Ba Balanced T Tree

Height balance: b = height(TR) - height(TL)

slide-19
SLIDE 19

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

slide-20
SLIDE 20

AV AVL Tree Analysis

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

slide-21
SLIDE 21

AV AVL Tree Analysis

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