# Chapter 10 Efficient Binary Search Trees

Document Sample

```					資料結構課後輔導(AVL Trees)
Height-Balanced
 Definition
 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 hL and hR are heights of TL and TR,
respectively.
Examples
5

6
4                   8

4                     9
1                       7         9

2        5            8        11
2               6                 12

11        1       3             7       10        12
3

10

Not height-balanced                            Height-balanced
Balance Factor
 Definition:
 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.
Balance Factors for an AVL Tree
-1

1                        1

0                1               -1
0

0
0       0       -1           0

0
Construction of an AVL Tree
 Consider to insert the following numbers:
 8, 9, 10, 2, 1, 5, 3, 6, 4, 7, 11, 12

0                      -1

8                      8
0

9

Insert 8                     Insert 9
Consider the
-2                   nearest parent A
with bf = ±2
8                                              0
-1
9
RR
9                               0       0
0                          8       10
10   insert in the right
subtree of the
right subtree of A
Insert 10
1

9
1           0

8           10
0

2

Insert 2
2
Consider the
9        nearest parent A                  1
2       0
with bf = ±2
0       9
8       10                                         0
1

LL                 2           10
2                                            0
0                                                         0

1       8
1

insert in the left
subtree of the
left subtree of A
Insert 1
Consider the
nearest parent                      0
2
A with bf = ±2
0           8
9                                                  -1
-1             0

LR                2               9
2              10                         0                         0
0                                                             0
1

1       5                10
1              8
insert in the right
0
subtree of the
left subtree of A
5

Insert 5
1                                   1

-1            8                     -1            8
-1                                      -1

2                 9                 2                     9
0                               0   0                                   0
1                                   0

1             5                10   1             5                    10

0                                   0                     0

3                                   3                 6

Insert 3                            Insert 6
1
2                 Consider the
nearest
8
-2               8                 parent A with             0                       -1
-1
bf = ±2
3                   9
2                    9                                                                 0
0                                   0                         1               0
1

1                                           RL                2           5                    10
5                    10                     0
0               0
-1                    0
1
3                6              insert in the                4               6
0                         left subtree of
the right
4
subtree of A

Insert 4
2                                               0

8                                               5
8
-1                      -1                         1                    0

3                  9                              3                8
1              -1                      0
LR       1           0       -1           -1

2             5                       10            2           4       6             9
0
0                                     0                                0         0
-1

1                                                   1                                7        10
4             6
0

7

Insert 7
-1

5
1                     -1

3                 8

1           0        -1            -2

2           4        6              9
0                                  0        -1
RR

1                                  7        10   0                     0

11                    5
Insert 11                                  1                        0

3                    8
1           0       -1               0

2           4       6                    10
0                             0       0            0

1                             7           9        11
-1

5
1                            -1

3                        8

1           0           -1                    -1

2                                             10
0               4           6

0        0            -1

1                                7            9        11
0

12

Insert 12
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
B                      LL               A
TA        TBR

TBR          TBL                         TBL       TA

Height (TA) + 2 = Height(TBR) + 1
=> Height (TA) + 1 = Height(TBR)
Height (TBL) + 1 = Height(TBR)
=>Height (TBR) = Height(TA)
Rotation Types (2)
 LR: Y is inserted in the right subtree of the left subtree
of A

A
C

B             TA     LR
B               A

C
TCL       TCR   TA

TCL            TCR
Rotation Types (3)
 RR: Y is inserted in the right subtree of the right
subtree of A.

A                                       B

TA        B              RR              A        C

TB       C
TA       TB
Rotation Types (4)
 RL: Y is inserted in the left subtree of the right subtree
of A

C
A

RL              A                   B
TA         B

C
TA        TCL       TCR

TCL       TCR
The Class Definition of AVL Tree
class AvlNode {
public:
AvlNode(int k) {data = k; bf = 0; leftChild = NULL; rightChild = NULL; }
private:
int data;                                          Store the value of
int bf;                                          balance factor of the
AvlNode *leftChild, *rightChild;                          node
};
class AVL {
public:
AVL() : root(0) {};
bool Search(int key);
bool Insert(int key);
bool Delete(int key);
private:
AvlNode *root;
};
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
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;
}
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
 Adjust balance factors of nodes on path from a to pp.
int d;
AvlNode *b, //child of a
*c; //child of b                                             1
if (a == NULL) {                                                pa
p = root; d = (k > p->key)? -1 : 1;       d=-1
}                                                          -1            8
a                                 -1
else if (k > a->key) {
b = p = a->rightChild;      d = -1;
2         b               9
}                                        0                                                0
0
else {                                                                      p
b = p = a->leftChild;       d = 1;                     1
}                                        1                      5                        10
while (p != y) {
0                         0
if (k > p->key) {
p->bf = -1; p = p->rightChild;                         -1
p           3                     6
}                                                            y
else {
p->bf = 1; p = p->leftChild;
p     4
}
}
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
{                                            a             b
if (b->bf == 1) //rotation type LL
{                                    b    A             B    a
a->leftChild = b->rightChild;
B             C        A
b->rightChild = a;
TA
a->bf = 0;                    C      TB            TB       TA
b->bf = 0;
}
Rotation - LR
else //rotation type LR
{
c = b->rightChild;
b->rightChild = c->leftChild;                     a
a->leftChild = c->rightChild;
A
c->leftChild = b;
c->rightChild = a;                        b
switch (c->bf) {                          B             TA
case 1:
a->bf = -1; b->bf = 0; break;               c
C
case -1:                                                            c
b->bf = 1; a->bf = 0; break;
C
case 0:                              TCL           TCR
b->bf = 0; b->bf = 0; break;                           b                   a
}                                                            B               A
c->bf = 0; b = c; //b is the new root
}                                                                   TCL       TCR       TA
}
else {   //right imbalance. This is symmetric to left imbalance

}
if (pa == NULL)
root = b;                         pa
else if (a == pa->leftChild)
9                                pa
pa->leftChild = b;          a
else                                                                      9
b    8          10                     b
pa->rightChild = b;
return true;                                                      2           10
}                             2                                           a
LL
1
8
1

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 0 posted: 6/5/2013 language: English pages: 27
Zhengqin Wu