o(log 2 n)

20
07/20/22 ITK 279 1 O(log 2 n) average height of a BST : f(n) : The average internal path length of an n-node BST f(3) : (3+2+3) / 3 = 2.67 0 1 2 0+1+2=3 1 1 0 0+1+1=2 1 2 0+1+2=3

Upload: finn-mack

Post on 31-Dec-2015

15 views

Category:

Documents


0 download

DESCRIPTION

The average height of a BST :. O(log 2 n). f(n) : The average internal path length of an n-node BST. f(3) : (3+2+3) / 3 = 2.67. 0. 0. 1. 1. 1. 1. 2. 2. 0+1+1= 2. 0+1+2= 3. 0+1+2= 3. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: O(log 2 n)

04/19/23 ITK 279 1

O(log2n)The average height of a BST :

f(n) : The average internal path length of an n-node BST

f(3) : (3+2+3) / 3 = 2.67

0

1

2

0+1+2=3

1 1

0

0+1+1=2

1

2

0+1+2=3

Page 2: O(log 2 n)

04/19/23 ITK 279 2

AVL Tree

h

h+1

+1

k+1k

+1-1

j+1j

A BST in which the height difference between the two children of any node is always less than 2.

• Adel’son-Vels’kii and Landis, Soviet Mathematic Doklady, 3:1259-1263, 1962 C. Crane, D. Knuth, et al in 1970’s

Page 3: O(log 2 n)

04/19/23 ITK 279 3

AVL Tree

We dynamically maintain the properties of AVL-tree when we insert (remove) a node by four different operations (rotations)

-- an example of Dynamic Tree

Static Tree

Huffman Code

Dynamic Tree

-- an example of Static Tree

We statically analyze the code and build up an optimal tree for retrieving the code words.

Page 4: O(log 2 n)

04/19/23 ITK 279 4

Four operations : (rotations)

1.RR rotation

2.LL rotation

3.RL rotation

4.LR rotation

performed at the bad node where the difference between the heights of its two children is bigger than 1.

If a node is bad caused by:

1.Right-child’s Right-child

2.Left-child’s Left-child

3.Right-child’s Left-child

4.Left-Child’s Right-child

then perform

Page 5: O(log 2 n)

04/19/23 ITK 279 5

No Rotation is Needed

+1

-10

0

Page 6: O(log 2 n)

04/19/23 ITK 279 6

RR Rotations:

+2

+1

Page 7: O(log 2 n)

04/19/23 ITK 279 7

RR Rotation:+2

+1

R

R

+0

+0

Page 8: O(log 2 n)

04/19/23 ITK 279 8

LL Rotation:

-2

L

L

+0

+0

-1

Page 9: O(log 2 n)

04/19/23 ITK 279 9

Rotations: RL

h+1

h+1h h

+1

0

+0

+2

-1

-1

0

0+1

Page 10: O(log 2 n)

04/19/23 ITK 279 10

Rotations: RL

h+1

h+1h h

+2

-1

-1L

RRL Rotation:

h+1 h+1h h

0

0 +1

Page 11: O(log 2 n)

04/19/23 ITK 279 11

Rotations: LR

h+1

h+1

h h

1

-1

-2L

R

LR Rotation:

h+1 h+1h h

0

0 +1

-1

Page 12: O(log 2 n)

04/19/23 ITK 279 12

+2

+2

R

R

Rotate this sub-tree first

Could be RR or RL, depending on what happens in the blue sub-tree.

Afterwards, examine the red node again to see is another rotation is needed.

Page 13: O(log 2 n)

04/19/23 ITK 279 13

Rotations: LL

h+1h

h

h-1

+1

-1

-1

+2

-2

-2

-1

h-1h-1

-1

0

0

+1

Page 14: O(log 2 n)

04/19/23 ITK 279 14

Example:

10

10

20

20

30

30

RR Rotation:

10

20

30

Page 15: O(log 2 n)

04/19/23 ITK 279 15

Example:40

40

50

50

RR Rotation:10

20

30

40

50

10

20

30

Page 16: O(log 2 n)

04/19/23 ITK 279 16

Example:35

35

RL Rotation:40

50

10

20

30

R

L

35

40

5010

20

30

Page 17: O(log 2 n)

04/19/23 ITK 279 17

Possible complications

h+1

h+1h h

+2

-1

-1

Re-assign the links

Tracking the heightsand balance-factors

Page 18: O(log 2 n)

04/19/23 ITK 279 18

RR rotation in C++

h

h h

+2

+1

R

R

template<typename T> // RR rotation on t;TreeNode<T> * AVLTree<T>::RR(TreeNode<T> * t) { TreeNode<T> * a = t, * b = t->right; a->right = b->left; b->left = a;

a->height -= 2; return b;}

a=t

b

height is an extra member variable in the TreeNode.

Page 19: O(log 2 n)

04/19/23 ITK 279 19

RL rotation in C++

template<typename T> // RL rotation on t;TreeNode<T> * AVLTree<T>::RL(TreeNode<T> * t) { TreeNode<T> *a, *b, *c;

a = t; b = t->right; c = t->right->left;

a->right = c->left; b->left = c->right; c->left = a; c->right = b;

c->height++; b->height--; a->height-=2;

return c;}

a=t

h+1

h+1h h

+2

-1

-1L

R b

c

Page 20: O(log 2 n)

04/19/23 ITK 279 20

,)2log()1log( nhn

32824.0,4402.1

h: Average Heights

n Random Keys

AVL