Vous êtes sur la page 1sur 15

Tree

Que-8
(i):- Define the terms Internal Path Length and External
Path Length for a binary tree.
(ii):- Prove that for any binary tree of n internal nodes the
External Path Length: Internal Path Length + 2n;
Ans-8(i): Internal Path Length: - Internal Path Length of a
binary tree can be define as sum of Path Length of all the
internal nodes in the tree.
->A node with 1 or more child is called an Internal node.

External Path Length:- External Path Length of a


binary tree can be define as the sum of all the path
length, sum over each path from the root node of a tree
to the External node.
->A node with 0 children is called an External node.

Ans-8(ii): ->To prove that for any binary tree of n nodes External
Path Length: Internal Path Length + 2n;
->Lets take an Example:

Internal Nodes
B

External

Nodes

Internal path length (I) = Sum of path length of all the


Internal nodes in the binary Tree.
Here,
Internal Path Length(I)=path length(B) + path length(C) +
path length(D) + path length(E) + path length(F) + path
length(G).

I=1+1+2+2+2+2
I=10.

Here we have to prove that,


External Path Length (E) = I + 2n;
Here I = Internal path length.

n = total no of nodes.
E=10 + 2(7)
E=24.
So, we can say that for a binary tree External path
length=Internal path length + 2n.

Que-9
Construct AVL tree for the following set of month
March,May,August,April,January,December,July,February,J
une,October,September.

Ans-9:
1.March
0

March

2.May

-1

March

May

3.August
March

Aug

May

4.April
March

Aug

May

0
April

5.January
March

Aug

May

0
April

Jan

6.December
March

Aug

0
April

-1

Now it is not balance

May

Jan

L .R.

0
Dec

Jan

Aug

0
April

Dec

Now it is balance

Mar

-1

May

7.July

Jan

Aug

0
April

Mar

Dec

July

May

May

8.February

Jan

Aug

0
April

-1

Dec

Mar

-1

0
Feb

july

9.June

Jan

Aug

0
April

-1

Mar

Dec

-1

July

-1

May

May

-1

Feb

June

10.October
Jan

Aug

0
April

-1

Dec

Mar

-1

july

-1

0
Feb

0
June

0
Oct

11.September
Jan

Aug

balance

0
April

-1

-1

-1

Mar

It is not
Dec
-1

Feb

May

July

-1

-2

June

Oct

-1

Sept

Jan

Aug

L.L.

0
April

-1

Mar

It is balance
Dec

-1

0
Feb

Que-10

July

-1

0
June

Oct

0
May

0
Sept

In a Height-Balanced Tree,what are the cases causing


imbalance and how rebalancing is done in such cases,
Write a step-by-step method (taking one number at a
time) to develop Height-Balanced Tree (by rebalancing at
each stage in case of imbalance) for the following
sequence of numbers: 12,6,9,11,10,4,7.
Ans-10
the usefulness of height balancing while constructing a
binary
search tree are as follows: -

Height balancing tree is also known as AVL (AdelsonVelskil-Landis) Tree.


This trees are binary search tree in which the height of
the two
Siblings are not permitted to prefer by more then one.
An empty tree is a height balanced tree.
A binary tree is a height balance if and only if absolute
value of |HL-HR|=(-1,0,1).where HL means height of the
left sub tree and HR means Height of the right sub tree.
A binary tree is height balanced if every sub tree of
given tree is height balanced.
Balance factor = Left Height Right Height.
Here 0 & 1 is balance factor.
0

If the tree is not height balance then we have to perform


some rotation.
There are two types of rotations.
1. Single Rotation
2. Double Rotation
1. Single Rotation
Three are two types of Single Rotations.
1. LL (Left Left) Rotation (having Right sub tree).
For example: 15

12

-2
LL 12
0

15

18

-1
18

0
LL Rotation is to be performed when the right sub
tree of the node is having.

12

11 tree).
2. RR (Right Right) Rotation (having Left sub
For example: - 2
0
11

10

10

12

1
RR

0
0
RR Rotation is to be performed when the Left sub
tree of the node is having.

2. Double Rotation:There are two types of Rotations.


1. LR (Left to Right) Rotation.
LR rotation is to be performed when a new node is
inserted to the left of right of the root node.
2 20
2
0
20

-1

10

LL

0
1

15

15

15

10

2. RL (Right to Left) Rotation.

RR 0
10

20

RL rotation is to be performed when a new node is


inserted to the right of left of the root node

-2 20

20

-2

0 22

-1

25

22

RR
0

22

20

25

LL
0

25

Exp : 12,6,9,11,10,4,7

1.)

12

12

2.)

6
12

0
12

3.)9

12

LR

-1

0
4.)11
9

-1

0 6

12 1

0 11
5.)10
9

-2

0 6

12 2

1 11
0

10
11

12

10

RL

0
1

6.)4

11

1 6
0

12

12

0 10

11

0
0 6
0 4

10

RR

7.)7
11

04

-1

1
12

1 10

1
0

Vous aimerez peut-être aussi