You are on page 1of 21

Balancing a Tree

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

Balancing a Binary Tree


A binary tree is height-balanced or simply balanced if the difference between the left subtree and right subtree of any node is either 0 or 1 For example, consider the following binary tree:
Node Height of the Height of the left subtree right subtree Difference

P
K B

1
1 0

1
2 3

0
1 3

K
D P

Because there is at least one node (node B) with the difference value of 3, this tree is not balanced
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM)

R
Page 2

Suppose we receive a set of data all in one shot and we want to insert 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 the first sub-array and its right child from the middle of the second sub-array Now divide the first sub-array into two other sub-arrays and repeat the same process Similarly divide the other sub-array into two other sub-arrays and repeat the same process
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 3

The algorithm is:

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); } }

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

Example: Stream of data: 5 1 9 8 7 0 2 3 4 6 Sorted Data: 0123456789

Step 1

0 1 2 3 4 5 6 7 8 9
4

Step 2

2 3

4 4

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

Step 3

2 3

4 4

1 0

Step 4

1 2 3 4 5 6 7 8 9
4

1
0
A.R. Hadaegh Dr. Ahmad R. Hadaegh

2
Page 6

California State University San Marcos (CSUSM)

Step 5

2 3

4
4

1 0

2
3

Step 6

1 2 3 4 5 6 7 8 9
4 1 0 2 3 7

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

Step 7

2 3

4
4

1 0 2 3 5

Step 8

2 3

4
4

1 0
2 5

3
A.R. Hadaegh Dr. Ahmad R. Hadaegh

6
Page 8

California State University San Marcos (CSUSM)

Step 9

2 3

4
4

1 0 2 3 5

7 8 6

Step 10

2 3

4
4

1 0 2 3
A.R. Hadaegh Dr. Ahmad R. Hadaegh

5
6

8
9
Page 9

California State University San Marcos (CSUSM)

What if we already have a balanced binary search tree and we want to insert another element such that after insertion the tree remains balanced One solution is to sort all elements including the new one again and then re-insert the elements again to the tree as we discussed before. Another solution proposed by C. Day and later improved by Q. Stout and B. Warren. Their solution is known as DSW algorithm DSW algorithm avoids sorting. It acquires deconstructing and reconstructing of the tree The building block for the tree transformation in this algorithm is the rotation

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 10

There are two types of rotations: Left rotation and Right Rotation which are symmetrical to one another The right rotation of a node called Child around its parent node called Parent is performed according to the following algorithm RotateRight (Grandparent, Parent, Child) { - If Parent is not the root of the tree Grandparent of child becomes childs parent by replacing the parent - Right subtree of child becomes left subtree of childs parent

- Node child acquire parent as its right child


}

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 11

The following is an example of right rotation


Gr Par Ch b c a
Right rotation of Ch around Par

Gr
Ch b c Par a

Gr Par a b
A.R. Hadaegh Dr. Ahmad R. Hadaegh

Gr
Left rotation of Ch around Par

Ch Par c b
Page 12

Ch c

California State University San Marcos (CSUSM)

DSW algorithm transforms an arbitrary binary search tree into a linked list like tree called a backbone tree Then the backbone tree is transformed in a series of passes into a perfectly balanced tree by repeatedly rotating every second node of the backbone around its parent
5

5
10 20 15 25 23 28 30 40

25 10 15

20
23 25 28 30 40 5 10

20

30

23

28

40

15

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 13

In the first phase, the backbone is created using the following algorithm: 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

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 14

tmp 5 10 5 10 tmp 20 30 25 23 28 40 23 15 25 28 30

5 10 tmp

20
15

15 20
30 40

25
23 28

40

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 15

5
10 tmp 15 20 30 25 23 28 40

5 10 15 20

5 10 15

20 tmp
30

tmp 25

25
23 28

40

23

30

28

40

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 16

5 5 10 10 10 5

15
20

15
tmp 25 20 tmp 25

15 20 tmp 23 30

23

30

23
28 40

25
40

28

30
28 40

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 17

5 10
15 20 tmp 23 25 30 28 40 10 15 20 23

5 10 15 20 23 25

5
10 15 20 23

25
tmp 30 tmp 28 30

25 28 30

28

40

40

40 tmp=NULL

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 18

In the second phase, the Backbone is transformed into a tree but this time the tree is perfectly balanced by having leaves on two adjacent levels In each pass down the backbone, every second node down to a certain point is rotated around its parent The algorithm is as follows:

createPerfectTree(n) { m = 2 lg(n+1) -1 Make n m rotations starting from the top of the backbone while (m >1) { m = m/2 make m rotations starting from the top of the backbone } }
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 19

Step 1
5 10 10

Step 2
20

Step 3
10
25 23 30 28 m = 3/2 = 1 40

15
5 20 15 23 25

20
23 25 28

15

28
30 30 40 n=9 m=7 So we need to do nm=2 rotations
A.R. Hadaegh Dr. Ahmad R. Hadaegh

So we need to do one rotation m = 1/2 = 0 No more rotation is necessary 20 10 5 15


Page 20

Step 4
25 30

40 m = 7/2 = 3 So we need to do three rotations

23

28

40

California State University San Marcos (CSUSM)

Can you do this example now.

1 2 4 3 7 6 8

9
10

Some of the code in this lecture is placed in example 1 for the tree lecture in the web
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 21

You might also like