09-BalancingATree

21
Dr. Ahmad R. H adae gh A.R. H adaegh Cali for ni a State Un i ve r sit y San M arcos (CSUSM ) Page Balancing a Tree

Transcript of 09-BalancingATree

Page 1: 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

Page 2: 09-BalancingATree

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

Page 3: 09-BalancingATree

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

Page 4: 09-BalancingATree

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:

Page 5: 09-BalancingATree

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

Page 6: 09-BalancingATree

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

Page 7: 09-BalancingATree

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

Page 8: 09-BalancingATree

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

Page 9: 09-BalancingATree

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

Page 10: 09-BalancingATree

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

Page 11: 09-BalancingATree

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}

Page 12: 09-BalancingATree

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

Page 13: 09-BalancingATree

8/12/2019 09-BalancingATree

http://slidepdf.com/reader/full/09-balancingatree 13/21

Page 14: 09-BalancingATree

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}

Page 15: 09-BalancingATree

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

Page 16: 09-BalancingATree

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

Page 17: 09-BalancingATree

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

Page 18: 09-BalancingATree

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

Page 19: 09-BalancingATree

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

}}

Page 20: 09-BalancingATree

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

Page 21: 09-BalancingATree

8/12/2019 09-BalancingATree

http://slidepdf.com/reader/full/09-balancingatree 21/21