09-BalancingATree
-
Upload
frankjamison -
Category
Documents
-
view
216 -
download
0
Transcript of 09-BalancingATree
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 1/21
Dr. Ahmad R. Hadaegh
A.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page
Balancing
aTree
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 2/21
Dr. Ahmad R. Hadaegh
A.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 2
Balancing a Binary Tree
• A binary tree is height-balanced or simply balanced if the
difference between the left subtree and right subtree of anynode is either 0 or 1
• For example, consider the following binary tree:
B
R
P
K
D
M
Node Height of theleft subtree Height of theright subtree Difference
P 1 1 0
K 1 2 1
B 0 3 3
• Because there is at least one node (node B) withthe difference value of 3, this tree is not balanced
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 3/21
Dr. Ahmad R. Hadaegh
A.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 3
• Suppose we receive a set of data all in one shot and we want toinsert them into a binary tree such that the end result is a balanced
binary search tree.• If we insert the data one by one randomly, the tree may not turn to be a balanced binary search tree.
• One method to solve this problem is to• First sort the data using the best sort algorithm available• Designate the middle element to be the root of the binary tree
• The array would consist of two sub arrays:• one from the first element to the middle element (the root) but not including the middle element
• Another consists of middle + 1 till the last element
• Now the left child of the tree is taken from the middle of thefirst sub-array and its right child from the middle of thesecond sub-array
• Now divide the first sub-array into two other sub-arrays andrepeat the same process
• Similarly divide the other sub-array into two other sub-arraysand repeat the same process
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 4/21
Dr. Ahmad R. Hadaegh
A.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 4
void Tree:: Balance (T data[ ], int first, int last){
if (first <=last){
int middle = (first + last) /2;insert (data[middle]);Balance (data, first, middle-1)Balance(data, middle+1, last);
}}
• The algorithm is:
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 5/21
Dr. Ahmad R. Hadaegh
A.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 5
• Example:• Stream of data: 5 1 9 8 7 0 2 3 4 6• Sorted Data: 0 1 2 3 4 5 6 7 8 9
4
Step 1
Step 2
0 1 2 3 4 5 6 7 8 9
1
4
0 1 2 3 4 5 6 7 8 9
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 6/21
Dr. Ahmad R. Hadaegh
A.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 6
Step 3
1
4
0
0 1 2 3 4 5 6 7 8 9
1
4
20
Step 4 0 1 2 3 4 5 6 7 8 9
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 7/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 7
Step 5 0 1 2 3 4 5 6 7 8 9
1
4
20
3
Step 6 0 1 2 3 4 5 6 7 8 9
1
4
20
3
7
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 8/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 8
Step 7 0 1 2 3 4 5 6 7 8 9
1
4
20
3
7
5
Step 8 0 1 2 3 4 5 6 7 8 9
1
4
20
3
7
5
6
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 9/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 9
Step 9 0 1 2 3 4 5 6 7 8 9
1
4
20
3
7
5
6
8
Step 10 0 1 2 3 4 5 6 7 8 9
1
4
20
3
7
5
6
8
9
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 10/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 10
• What if we already have a balanced binary search tree and wewant to insert another element such that after insertion the treeremains balanced
• One solution is to sort all elements including the new oneagain and then re-insert the elements again to the tree as wediscussed before.
• Another solution proposed by C. Day and later improved by
Q. Stout and B. Warren. Their solution i s known as DSWalgorithm
• DSW algorithm avoids sorting. It acquires deconstructing andreconstructing of the tree
• The building block for the tree transformation in thisalgorithm is the rotation
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 11/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 11
• There are two types of rotations: Left rotation and RightRotation which are symmetrical to one another
• The right rotation of a node called “Child” around its parentnode called “Parent” is performed according to the followingalgorithm
RotateRight (Grandparent, Parent, Child){
- If Parent is not the root of the treeGrandparent of child becomes child’s parent byreplacing the parent
- Right subtree of child becomes left subtree ofchild’s parent
- Node child acquire parent as its right child}
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 12/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 12
Gr
c
a
Par
Ch
b
Gr
c a
Par
Ch
b
• The following is an example of right rotation
Gr
Ch
Par
Right rotation of Charound Par
Left rotation of Ch
around Par
Gr
Ch
Par
a
b ca b
c
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 13/21
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 14/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 14
• In the first phase, the backbone is created using the followingalgorithm:
createBackbone(root, n){
tmp = root;
while (tmp != NULL)
if tmp has a left child{
rotate this child around tmp;set tmp to the child which just became parent
}else
set tmp to its right child}
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 15/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 15
5
30
10
20
40
15
2823
25
tmp5
30
10
20
40
15
2823
25
tmp
5
30
10
20
40
15
2823
25
tmp
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 16/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 16
5
30
10
20
40
15
2823
25
tmp
5
30
10
20
40
15
2823
25
tmp
5
30
10
20
40
15
28
23
25
tmp
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 17/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 17
5
30
10
20
40
15
28
23
25tmp
5
30
10
20
40
15
28
23
25
tmp
5
30
10
20
40
15
28
23
25
tmp
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 18/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 18
5
30
10
20
40
15
28
23
25
tmp
5
30
10
20
40
15
28
23
25 tmp
5
30
10
20
40
15
28
23
25
tmp=NULL
5
30
10
20
40
15
28
23
25tmp
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 19/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 19
• In the second phase, the Backbone is transformed into a tree but this time the tree is perfectly balanced by having leaves ontwo adjacent levels
• In each pass down the backbone, every second node down toa cer tain point is rotated around its parent
• The algorithm is as follows:
createPerfectTree(n){m = 2 lg(n+1) -1Make n – m rotations starting from the top of the backbone
while (m >1){
m = m/2make m rotations starting from the top of the backbone
}}
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 20/21Dr. Ahmad R. HadaeghA.R. H adaegh Cali for ni a State Un iversity San M arcos (CSUSM ) Page 20
5
20
10
15
23
25
30
40
28
n = 9
m = 7
So we need to don – m = 2
rotations
5 20
10
15 23
25
30
40
28
m = 7/2 = 3
So we need to dothree rotations
20
25
30
28
235
10
15
40
25
20
23
30
28 40
5
10
15
m = 3/2 = 1
So we need to doone rotation
m = 1/2 = 0No more rotationis necessary
Step 1 Step 2 Step 3
Step 4
8/12/2019 09-BalancingATree
http://slidepdf.com/reader/full/09-balancingatree 21/21