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

cs 225
SMART_READER_LITE
LIVE PREVIEW

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

CS 225 Data Structures Feb. 28 AVL L Tre rees Wad ade Fag agen-Ulm lmschneid ider Course Logistics Update CBTF exams will go on as-scheduled: Theory Exam 2 is ongoing Sample Exam available on PL MPs and Lab assignments will be


slide-1
SLIDE 1

CS 225

Data Structures

  • Feb. 28 – AVL

L Tre rees

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2

Course Logistics Update

CBTF exams will go on as-scheduled:

  • Theory Exam 2 is ongoing
  • Sample Exam available on PL

MPs and Lab assignments will be due on schedule:

  • MP4 is released; due March 12, 2018
  • lab_huffman is released later today

My office hours are cancelled today.

slide-3
SLIDE 3

Lab Sections

All lab sections are not meeting this week. Instead, all CAs and non-striking TAs will hold open office hours (using the regular queue, held in the basement):

  • Feel free to use the room to work with your peers on the
  • lab. Staff will be available in open office hours in the

basement of Siebel.

  • An intro video on Huffman trees will be provided.
slide-4
SLIDE 4

13 10 25 38 51 40 84 89 66 95

Left ft Rotation

slide-5
SLIDE 5

13 10 25 38 51 84 89

A B C D

slide-6
SLIDE 6

13 10 25 38 51 84 89

A B C D

84 51 89

A B C D

slide-7
SLIDE 7

13 10 25 38 51 40 84 89 66 95

slide-8
SLIDE 8

13 10 25 37 38 51

slide-9
SLIDE 9

13 10 25 37 38 51

slide-10
SLIDE 10

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

AVL Trees

Three issues for consideration:

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

AVL Tree Rotations

Four templates for rotations:

slide-13
SLIDE 13

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

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

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

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

template <class T> void AVLTree<T>::_insert(const T & x, treeNode<T> * & t ) { if( t == NULL ) { t = new TreeNode<T>( x, 0, NULL, NULL); } else if( x < t->key ) { _insert( x, t->left ); int balance = height(t->right) - height(t->left); int leftBalance = height(t->left->right) - height(t->left->left); if ( balance == -2 ) { if ( leftBalance == -1 ) { rotate_____________( t ); } else { rotate_____________( t ); } } } else if( x > t->key ) { _insert( x, t->right ); int balance = height(t->right) - height(t->left); int rightBalance = height(t->right->right) - height(t->right->left); if( balance == 2 ) { if( rightBalance == 1 ) { rotate_____________( t ); } else { rotate_____________( t ); } } } t->height = 1 + max(height(t->left), height(t->right)); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

slide-18
SLIDE 18

Height-Balanced 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

AVL Tree Analysis

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

slide-21
SLIDE 21

AVL Tree Analysis

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