Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons...

31
Part I – AVL Trees

Transcript of Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons...

Page 1: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Part I – AVL Trees

Page 2: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Unbalanced Binary Search Tree5

4 8

1

2

3

7

6

9

12

11

10

Number of comparisons needed to search for NOV: 6.

Average number of comparisons: 3.5

Page 3: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Skew Binary Search TreeConsider the keys are entered in lexicographic order.

In worst case, searching a skew binary tree corresponds to sequential search in a ordered linked list.

1

2

3

4

5

6

7

8

Page 4: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Binary Search TreeConsider a balanced binary

search tree as illustrated.Number of comparisons

needed to search for NOV: 6.Average number of

comparisons: 3.1

6

4 9

2

1

5 8

3

11

12107

Page 5: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Binary Search Trees:Balanced vs. UnbalancedThe average and maximum search time can be

minimized if the binary search tree is maintained as a complete binary tree all the times.The time becomes O(log n) for an n-node binary

search tree.AVL tree (1962)

Balanced binary search tree with respect to the heights of subtrees.

Any retrievals can be performed in O(log n) time.The resulting tree of insertion or a deletion remains

height-balanced.

Page 6: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Height-BalancedDefinition

An empty tree is height-balanced.If T is nonempty binary tree with TL and TR as its left

and right subtrees respectively. T is height-balanced iff

TL and TR are height-balanced, and

|hL-hR| 1 where h≦ L and hR are heights of TL and TR, respectively.

Page 7: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Examples

5

4 8

1

2

3

7

6

9

12

11

10

6

4 9

2

1

5 8

3

11

12107

Not height-balanced Height-balanced

Page 8: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Balance FactorDefinition:

For every node T, define its balance factor, BF(T), as

BF(T) = hL - hR

where hL and hR are the heights of the left and right subtrees of T.

BF(T) for any node T in an AVL tree is –1, 0, or 1.

Page 9: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Balance Factors for an AVL Tree

0 0

0 0

1

0

-1 0

1

0

-1

1

-1

Page 10: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Construction of an AVL Tree Consider to insert the following numbers:

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

8

0

8

-1

9

0

Insert 8 Insert 9

Page 11: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

10

0

8

-2

9

-1

Insert 10

RR

insert in the right subtree of the

right subtree of A

Consider the nearest parent A

with bf = ±2

10

0

8

0

9

08

9

Page 12: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Insert 2

10

0

8

1

9

1

2

0

Page 13: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Insert 1

LL

insert in the left subtree of the

left subtree of A

Consider the nearest parent A

with bf = ±2

10

0

8

2 9

2

2

1

1

0

8

10

0

2

0 9

1

1

0

8

02

Page 14: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Insert 5

LR

insert in the right subtree of the

left subtree of A

Consider the nearest parent A with bf = ±2

10

0

2

-1 9

2

1

0

8

1

5

0

9

8

9

-1

2

0 8

0

1

0

10

0

5

0

Page 15: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Insert 3

9

-1

2

-1 8

1

1

0

10

1

5

0

3

0

Insert 6

9

-1

2

-1 8

1

1

0

10

0

5

0

3

0

6

0

Page 16: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

RL

insert in the left subtree of

the right subtree of A

Insert 4

9

-1

2

-2 8

2

1

0

10

1

5

0

3

-1

6

0

4

0

Consider the nearest

parent A with bf = ±2

2

3

9

-1

2

1

8

1

1

010

0

5

03

0

6

0

4

0

Page 17: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

LR

Insert 7

9

-1

2

1

8

2

1

010

-1

5

03

-1

6

-1

4

0

7

0

8

5 9

-1

2

1

8

0

1

0

10

0

5

0

3

1

6

-1

4

0

7

0

Page 18: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

RR

Insert 11

9

-2

2

1

8

-1

1

0

10

-1

5

-1

3

1

6

-1

4

0

7

0

11

0

9

10

10

0

2

1

8

0

1

0

9

0

5

0

3

1

6

-1

4

0

7

0

11

0

Page 19: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Insert 12

10

-1

2

1

8

-1

1

0

9

-1

5

0

3

1

6

-1

4

0

7

0

11

-1

12

0

Page 20: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Rotation Types (1)Suppose Y is the new node.

LL: Y is inserted in the left subtree of the left subtree of A.

A

B

CTA

TB

LL

B

C A

TATB

Page 21: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Rotation Types (2)LR: Y is inserted in the right subtree of the left subtree

of A

A

B

C

TA

TCL TCR

LR

C

B A

TATCL TCR

Page 22: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Rotation Types (3)RR: Y is inserted in the right subtree of the right

subtree of A.

A

B

C

TA

TB

RR A

B

C

TA TB

Page 23: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Rotation Types (4)RL: Y is inserted in the left subtree of the right subtree

of A

A

B

C

TA

TCL TCR

RL A B

C

TA TCL TCR

Page 24: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

The Class Definition of AVL Tree

class AvlNode { friend class AVL; public: AvlNode(int k) {data = k; bf = 0; leftChild = NULL; rightChild = NULL; } private: int data; int bf; AvlNode *leftChild, *rightChild; }; class AVL { public: AVL() : root(0) {}; bool Search(int key); bool Insert(int key); bool Delete(int key); private: AvlNode *root; };

class AvlNode { friend class AVL; public: AvlNode(int k) {data = k; bf = 0; leftChild = NULL; rightChild = NULL; } private: int data; int bf; AvlNode *leftChild, *rightChild; }; class AVL { public: AVL() : root(0) {}; bool Search(int key); bool Insert(int key); bool Delete(int key); private: AvlNode *root; };

Store the value of balance factor of the

node

Store the value of balance factor of the

node

Page 25: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Phase 1 bool AVL::Insert(int key) { if (!root) { root = new AvlNode(key); return true; }

//Phase 1: locate insertion point for key AvlNode *a = 0, //most recent node with bf = ± 1 *pa = 0, //parent of a *p = root, //p moves through the tree *pp = 0; //parent of p

bool AVL::Insert(int key) { if (!root) { root = new AvlNode(key); return true; }

//Phase 1: locate insertion point for key AvlNode *a = 0, //most recent node with bf = ± 1 *pa = 0, //parent of a *p = root, //p moves through the tree *pp = 0; //parent of p

Page 26: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

while (p) { if (p->bf != 0) { a = p; pa = pp; } if (k > p->key) { pp = p; p = p->rightChild; } else if (k < p->key) { pp = p; p = p->leftChild; } else return false; }

while (p) { if (p->bf != 0) { a = p; pa = pp; } if (k > p->key) { pp = p; p = p->rightChild; } else if (k < p->key) { pp = p; p = p->leftChild; } else return false; }

Page 27: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Phase 2

//Phase 2: Insert and rebalance //k is not in the tree and may be inserted as the appropriate child of pp. AvlNode *y = new AvlNode(k); if (k < pp->key) pp->leftChild = y; //insert as left Child else pp->rightChild = y; //insert as right Child

//Phase 2: Insert and rebalance //k is not in the tree and may be inserted as the appropriate child of pp. AvlNode *y = new AvlNode(k); if (k < pp->key) pp->leftChild = y; //insert as left Child else pp->rightChild = y; //insert as right Child

Page 28: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Adjust balance factors of nodes on path from a to pp.int d;

AvlNode *b, //child of a *c; //child of b if (a == NULL) { p = root; d = (k > p->key)? -1 : 1;}else if (k > a->key) { b = p = a->rightChild; d = -1; } else { b = p = a->leftChild; d = 1; } while (p != y) { if (k > p->key) { p->bf = -1; p = p->rightChild; } else { p->bf = 1; p = p->leftChild; }}

int d;AvlNode *b, //child of a *c; //child of b if (a == NULL) { p = root; d = (k > p->key)? -1 : 1;}else if (k > a->key) { b = p = a->rightChild; d = -1; } else { b = p = a->leftChild; d = 1; } while (p != y) { if (k > p->key) { p->bf = -1; p = p->rightChild; } else { p->bf = 1; p = p->leftChild; }}

9

-1

2

-1 8

1

1

0

10

0

5

0

3

0

6

0

4

y

a

pa

p

d=-1

1

-1p

p

b

Page 29: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Rotation - LL if (a == NULL) return true; else if (a->bf==0 || a->bf+d == 0) { a->bf += d; return true; } if (d == 1) //left imbalance { if (b->bf == 1) //rotation type LL { a->leftChild = b->rightChild; b->rightChild = a; a->bf = 0; b->bf = 0; }

if (a == NULL) return true; else if (a->bf==0 || a->bf+d == 0) { a->bf += d; return true; } if (d == 1) //left imbalance { if (b->bf == 1) //rotation type LL { a->leftChild = b->rightChild; b->rightChild = a; a->bf = 0; b->bf = 0; }

A

B

CTA

TB

a

b B

C A

a

TATB

b

Page 30: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

Rotation - LR else //rotation type LR { c = b->rightChild; b->rightChild = c->leftChild; a->leftChild = c->rightChild; c->leftChild = b; c->rightChild = a; switch (c->bf) { case 1: a->bf = -1; b->bf = 0; break; case -1: b->bf = 1; a->bf = 0; break; case 0: b->bf = 0; b->bf = 0; break; } c->bf = 0; b = c; //b is the new root }}

else //rotation type LR { c = b->rightChild; b->rightChild = c->leftChild; a->leftChild = c->rightChild; c->leftChild = b; c->rightChild = a; switch (c->bf) { case 1: a->bf = -1; b->bf = 0; break; case -1: b->bf = 1; a->bf = 0; break; case 0: b->bf = 0; b->bf = 0; break; } c->bf = 0; b = c; //b is the new root }}

A

B

C

TA

TCL TCR

a

b

c

C

B A

TATCL TCR

a

c

b

Page 31: Part I – AVL Trees. Unbalanced Binary Search Tree 5 48 1 2 3 7 6 9 12 11 10 Number of comparisons needed to search for NOV: 6. Average number of comparisons:

else { //right imbalance. This is symmetric to left imbalance } if (pa == NULL) root = b; else if (a == pa->leftChild) pa->leftChild = b; else pa->rightChild = b; return true;}

else { //right imbalance. This is symmetric to left imbalance } if (pa == NULL) root = b; else if (a == pa->leftChild) pa->leftChild = b; else pa->rightChild = b; return true;} LL

102

9

1 8

108

9

2

1

8

2

pa

a

b

pa

b

a