CS 225 Data Structures Oc October 11 11 AV AVL Analysis G G - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Oc October 11 11 AV AVL Analysis G G - - PowerPoint PPT Presentation

CS 225 Data Structures Oc October 11 11 AV AVL Analysis G G Carl Evans Inser Ins ertio tion n in into an an AVL VL Tree ee _insert(6.5) Insert (pseudo code): 1: Insert at proper place 2: Check for imbalance 3: Rotate, if


slide-1
SLIDE 1

CS 225

Data Structures

Oc October 11 11 – AV AVL Analysis

G G Carl Evans

slide-2
SLIDE 2

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

Ins Inser ertio tion n in into an an AVL VL 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-3
SLIDE 3

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

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

slide-5
SLIDE 5

AV AVL Tree Analysis

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

slide-6
SLIDE 6

AV AVL Tree Analysis

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

n, number of nodes h, height

slide-7
SLIDE 7

AV AVL Tree Analysis

  • The height of the tree, f(n), will always be less than

c × g(n) for all values where n > k.

n, number of nodes h, height

slide-8
SLIDE 8

AV AVL Tree Analysis

n, number of nodes h, height n, number of nodes h, height

slide-9
SLIDE 9

AV AVL Tree Analysis

  • The number of nodes in the tree, f-1(h), will always

be greater than c × g-1(h) for all values where n > k.

n, number of nodes h, height n, number of nodes h, height

slide-10
SLIDE 10

Plan Plan of Actio tion

Since our goal is to find the lower bound on n given h, we can begin by defining a function given h which describes the smallest number of nodes in an AVL tree of height h:

slide-11
SLIDE 11

Si Simp mplify t the R Recurr rrence

N(h) = 1 + N(h - 1) + N(h - 2)

slide-12
SLIDE 12

St State a a T Theor

  • rem

Theorem: An AVL tree of height h has at least __________. Proof: I. Consider an AVL tree and let h denote its height.

  • II. Case: ______________

An AVL tree of height ____ has at least ____ nodes.

slide-13
SLIDE 13

Pr Prove ve a Theorem

  • III. Case: ______________

An AVL tree of height ____ has at least ____ nodes.

slide-14
SLIDE 14

Pr Prove ve a Theorem

  • IV. Case: ______________

By an Inductive Hypothesis (IH): We will show that: An AVL tree of height ____ has at least ____ nodes.

slide-15
SLIDE 15

Pr Prove ve a Theorem

  • V. Using a proof by induction, we have shown that:

…and inverting:

slide-16
SLIDE 16

Su Summa mmary of

  • f Ba

Balanced BS BST

Red-Black Trees

  • Max height: 2 * lg(n)
  • Constant number of rotations on insert, remove, and find

AVL Trees

  • Max height: 1.44 * lg(n)
  • Rotations:
slide-17
SLIDE 17

Su Summa mmary of

  • f Ba

Balanced BS BST

Pros:

  • Running Time:
  • Improvement Over:
  • Great for specific applications:
slide-18
SLIDE 18

Su Summa mmary of

  • f Ba

Balanced BS BST

Cons:

  • Running Time:
  • In-memory Requirement: