Trees and Binary Trees in Data Structures

UNIT-III
D
e
finition
 
o
f
 
T
r
ee
 
A
 
t
r
ee
 
is
 
a
 
f
i
n
i
t
e
 
s
e
t
 
of
 
one
 
or
 
m
o
r
e
 
nodes
such
 
t
h
a
t
:
 
The
r
e
 
is
 
a
 
spe
c
i
a
l
l
y
 
d
e
si
g
n
at
ed
 
node
 
c
a
l
led
t
he
 
r
oot.
 
T
h
e
 
r
e
m
a
i
n
i
ng
 
nodes
 
a
r
e
 
p
a
r
t
i
t
i
oned
 
i
n
t
o
n>=0
 
d
i
sjo
i
n
t
 
s
e
t
s
 
T1,
 
...
,
 
T
n
,
 
w
he
r
e
 
each
 
of
t
hese
 
s
e
t
s
 
is
 
a
 
t
r
e
e
.
 
W
e
 
c
a
l
l
 
T1,
 
...
,
 
T
n
 
t
he
 
su
b
t
r
e
e
s
 
of
 
t
he
 
r
oot.
139
R
ep
r
ese
n
t
a
t
i
on
T
0
of
T
r
ee
Le
v
el
1
T
1
2
T
2
T
3
T
4
T
T
3
5
6
F
ig
.
T
r
e
e
 
1
140
T
ermin
o
l
o
gy
A
D
B
C
E
F
G
H
F
ig
.
T
r
e
e
 
2
141
R
O
O
T
:
T
h
is
 
is
 
t
h
e
 
un
i
qu
e
 
n
o
d
e
 
in
 
t
h
e
 
t
r
ee
 
t
o
 
w
h
i
c
h
 
f
u
r
t
h
er
 
s
ub
t
r
e
e
s
 
a
r
e
a
tt
ac
h
e
d
.in
 
th
e
 
a
b
o
v
e
 
f
ig
 
n
o
d
e
 
A
 
is
 
a
 
r
oo
t
 
n
o
d
e.
Deg
r
ee
 
of
 
t
h
e
 
n
ode:
T
h
e
 
t
o
t
al
 
nu
m
b
er
 
o
f
 
s
u
b
-t
r
e
e
s
 
a
tt
ac
h
ed
 
t
o
th
e
n
o
d
e
is
c
all
e
d
th
e
d
eg
r
ee
 
o
f
N
o
d
e
A
E
th
e
n
o
d
e.
d
eg
r
ee
3
0
Le
a
v
es:
T
h
ese
 
a
r
e
 
t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
t
 
or
 
l
e
f
t
 
rig
h
t
 
c
h
il
d
r
en
 
of
 
a
n
y
p
ar
t
i
c
u
lar
 
n
o
d
e
is
f
a
s
t
b
e
c
a
u
se
o
f
th
e
r
a
nd
o
m
ac
c
ess.
157
 
 
 
D
i
sa
d
v
a
n
t
a
g
es
 
of 
s
eq
u
e
n
t
i
al
 r
ep
r
e
s
e
n
t
a
t
i
on
The
 
m
a
j
or
 
d
i
sad
v
a
n
t
a
g
e
 
wi
t
h
 
t
h
i
s
 
type
 
of
r
ep
r
es
e
n
t
a
t
ion
 
is
 
w
a
s
t
a
g
e
 
of
 
m
e
m
o
r
y
.
T
h
e
 
m
a
x
i
mum
 
de
p
t
h
 
of
 
t
he
 
t
r
ee
 
h
a
s
 
t
o
 
be
f
i
x
ed.
The
 
i
n
se
r
t
ions
 
and
 
del
e
t
ion
 
of
 
a
n
y
 
node
 
in
t
h
e
t
r
ee
 
wi
l
l
 
b
e
a
d
ju
s
t
ed
 
a
t
m
e
a
ni
ng
 
of
c
o
s
t
l
i
er
 
as
 
other
 
nodes
 
has
 
t
o
 
be
a
p
p
r
op
r
a
i
t
e
 
pos
i
t
i
ons
 
so
 
t
h
a
t
 
t
h
e
b
i
n
ar
y
 
t
r
ee
 
c
an
 
be
 
p
r
es
e
r
v
ed.
158
 
 
 
Lin
k
ed
s
t
r
uct
 
node
{
i
n
t
 
d
a
t
a;
R
ep
r
ese
n
t
a
tion
s
t
r
uct
};
node
*
l
e
ft
_
c
h
il
d,
*
r
i
g
h
t
_
c
h
il
d
;
d
a
t
a
rig
h
t
_c
h
ild
l
e
ft
_child
159
l
e
ft
_child
d
a
t
a
rig
h
t
_child
Lin
k
ed
R
ep
r
ese
n
t
a
tion
root
5
5
,4
4
,6
6
,3
3
,5
0
,22
160
X
    
 
2
2
      
 
X
3
3
    
 
X
X
      
 
5
0
   
 
X
X
     
 
6
6
    
 
X
44
55
Ad
v
a
n
t
a
g
es
 
of Lin
k
ed
 
r
ep
r
ese
n
t
a
tion
Th
i
s
 
r
ep
r
es
e
n
t
a
t
ion
 
is
 
su
p
e
r
ior
 
t
o
 
our
r
ep
r
es
e
n
t
a
t
ion
 
as
 
t
he
r
e
 
is
 
no
 
w
a
s
t
a
g
e
 
of
 
me
m
o
r
y
.
I
nse
r
t
i
ons
 
a
n
d
 
del
e
t
i
ons
 
wh
i
ch
 
a
r
e
 
t
he
 
mo
s
t
c
om
m
on
 
ope
r
a
t
i
ons
other
 
nodes.
c
an
be
done
w
i
t
hout
m
o
v
i
n
g
t
he
161
 
 
 
D
i
sad
v
a
n
t
a
g
e
s
 
of lin
k
e
d
 
r
e
p
r
e
s
e
n
t
a
tion
Th
i
s
 
r
ep
r
es
e
n
t
a
t
ion
 
does
 
not
 
p
r
o
v
i
de
 
d
i
r
e
c
t
acc
e
ss
 
t
o
 
a
 
node
 
and
 
spe
c
i
a
l
 
a
lg
or
i
t
hms
 
a
r
e
r
equ
i
r
ed.
Th
i
s
 
r
ep
r
es
e
n
t
a
t
ion
 
ne
e
ds
 
ad
d
i
t
ion
a
l
 
space
 
in
each
 
node
t
r
e
e
s.
f
or
s
t
o
r
i
n
g
t
he
l
e
f
t
and
r
i
g
h
t
su
b
-
162
 
 
 
Full
 
B
T
 
V
S
 
C
o
mpl
e
t
e
 
B
T
 
A
 
b
i
n
ar
y
 
t
r
ee
 
wi
t
h
 
n
 
nodes
 
a
n
d
 
de
p
t
h
 
k
 
i
s
c
o
m
p
l
e
t
e
 
if
f
 
i
t
s
 
nodes
 
c
o
r
r
espond
 
t
o
 
the
 
nodes
numbe
r
ed
 
f
r
om
 
1
 
t
o
 
n
 
in
 
t
h
e
 
f
u
l
l
 
b
i
na
r
y
 
t
r
ee
 
of
de
p
t
h
 
k
.
k
 
A
 
f
u
l
l
 
b
i
n
a
r
y
 
t
r
ee
of
 
de
p
t
h
 
k
 
is
 
a
 
b
i
na
r
y
t
r
ee
of
de
p
t
h
k
h
a
v
i
ng
A
2
-
1
nodes,
k
>
=
0
.
A
B
C
B
C
G
D
F
E
F
G
D
E
H
I
M
N
163
O
L
I
J
K
H
C
o
m
p
l
e
t
e
 
b
i
n
ary
 
t
r
e
e
Fu
l
l
 
b
i
n
ary
 
t
r
e
e
o
f
 
dep
th
 
4
Bin
a
r
y
 
T
r
ee
 
T
r
a
v
e
r
s
a
ls
The
 
p
r
o
ce
ss
 
of
 
g
oing thro
u
g
h
 
a
 
tr
e
e
 
in su
c
h a
 
w
a
y
 
th
a
t
 
eac
h
 
node
 
is
vis
t
e
d
 
on
c
e
 
is 
t
r
e
e tr
a
v
e
rs
a
l.sev
e
r
a
l
 
method
 
a
re used
for
 
tr
e
e
tr
a
v
e
r
s
a
l.
t
he tr
a
v
e
r
s
a
l 
i
n a
 
bin
a
r
y
ac
t
i
vi
t
ies
 
such
 
a
s:
V
isiti
n
g
 
t
h
e
 
r
oo
t
T
r
a
verse
 
left
 
s
u
b
t
r
ee
tr
e
e
involves
thr
e
e
kinds
of
b
a
sic
T
r
a
verse
rig
h
t
s
u
b
t
r
ee
164
W
e
 
w
i
l
l
 
u
se
 
so
m
e
 
no
t
a
t
ions
 
t
o
 
t
r
a
v
e
r
se
a
g
i
v
en
bin
ar
y
t
r
ee
as
 
f
ol
l
ow
s:
L
 
m
e
ans
 
m
o
v
e
 
t
o
 
the
 
L
e
f
t
 
chi
l
d.
R
 
m
eans
 
m
o
v
e
 
t
o
 
the
 
Ri
g
h
t
 
chi
l
d.
D
 
means
 
t
he
 
r
oot/pa
r
e
n
t
 
node.
The
only
 
d
i
f
f
e
r
en
c
e
 
a
m
ong
 
t
he
 
m
e
t
hods
is
 
t
he
o
r
der
in
 
which
 
t
hese
 
t
h
r
ee
 
ope
r
a
t
ions
 
a
r
e
 
pe
r
f
o
r
m
e
d.
T
h
e
r
e
 
a
r
e
 
t
h
r
ee
 
s
t
a
n
d
a
r
d
 
w
a
y
s
e
m
p
t
y
 
b
i
n
ar
y
 
t
r
ee
 
n
a
m
e
l
y
 
:
P
r
eo
r
d
er
I
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r
e
e
_
p
o
i
n
t
e
r
n
o
d
e
)
i
n
t
t
o
p=
-
1;
/*
i
n
i
t
i
a
l
i
ze
s
t
a
c
k
*/
tree_pointer
stack[MAX_STACK_SIZE];
f
o
r
(
;
;)
{
f
o
r
(;
n
o
d
e
;
n
o
d
e
=
n
o
d
e->left_chil
d
)
add(&top,
node);/*
a
d
d
to
s
t
a
ck
*/
n
o
d
e
=
d
e
l
e
t
e
(
&
t
o
p
)
;
/*
d
e
l
e
te
f
r
om
s
t
a
c
k
*/
if
(!node)
break;
/*
e
m
p
t
y
s
t
a
c
k
*/
printf(“%D”,
nod
e
->data);
n
o
de
=
n
o
d
e
->right_child;
}
}
O
(n
)
174
T
r
ace
Ope
r
a
tions
o
f
Ino
r
der
T
r
a
v
e
r
s
a
l
175
C
a
ll
 
o
f
 
i
n
o
r
d
e
r   
 
Va
l
u
e
 
in
 
r
oo
t  
 
A
c
ti
o
n
C
a
l
l
 
o
f
 
i
n
o
rd
e
r  
 
Va
l
u
e
 
in
 
r
o
o
t  
 
A
c
ti
o
n
1
2
3
4
5
6
5
7
4
8
9
8
1
0
3
+
*
*
/
 
A
NU
L
L
A
NU
L
L
/
 
B
NU
L
L
B
NU
L
L
 
* 
                      
p
r
i
n
t
f 
p
r
i
n
t
f
p
r
i
n
t
f
p
r
i
n
t
f
1
1
1
2
1
1
1
3
2
1
4
1
5
1
4
1
6
1
1
7
1
8
1
7
1
9
C
NU
L
L
C
NU
L
L
*
 
D
NU
L
L
D
NU
L
L
+
 
E
NU
L
L
E
 
NU
L
L 
            
p
r
i
n
tf 
p
r
i
n
tf
p
r
i
n
tf 
p
r
i
n
tf
p
r
i
n
tf
 
 
 
 
L
e
v
el
 
O
r
der
 
T
r
a
v
e
r
s
a
l
(
u
si
n
g
 
qu
e
u
e
)
v
o
id
l
e
v
e
l
_
o
r
d
e
r
(
t
r
e
e
_
p
o
i
n
t
er
p
t
r)
*/
/*
{
l
e
v
e
l
o
r
d
e
r
t
r
ee
t
r
a
v
e
r
s
a
l
i
n
t
f
r
o
n
t
=
r
e
ar
=
0;
tree_pointer
queue[MAX_QUEUE_SIZE];
if
(
!
p
t
r)
r
e
t
u
r
n
;
/*
e
m
p
t
y
q
u
e
u
e
*/
addq(front,
&rear,
ptr);
f
o
r
(
;
;)
{
p
t
r
=
d
e
l
e
t
e
q
(
&
f
r
o
n
t
,
r
e
a
r
);
176
if
(ptr)
{
printf(“%d”,
pt
r
->data);
if
(
p
t
r
->left_child)
addq(front,
&rear,
ptr->left_child);
if
(
p
t
r
->right_child)
addq(front,
&rear,
ptr->right_child);
}
e
l
se
b
r
e
a
k;
}
}
177
+
 
*
 
E
 
*
 
D
 
/
 
C
 
A
 
B
C
o
p
y
i
ng
 
B
ina
r
y
 
T
r
ees
tree_poointer
{
copy(tree_pointer
original)
tree_pointer
temp;
if
(original)
{
temp=(tree_pointer)
malloc(sizeof(node));
if
(IS_FULL(temp))
{
fprintf(stderr,
exit(1);
“the
memory
is
ful
l
\n”);
}
temp->left_child=copy(origina
l
->left_child
)
;
temp->right_child=copy(origina
l
->right_chi
l
d
temp->data=origina
l
->data;
return
}
temp;
return
}
NULL;
178
p
o
s
t
o
r
d
er
P
o
s
t
-
o
r
de
r
-
e
v
al
 
f
unc
ti
on
v
oid
 
p
o
s
t_
o
r
d
er_
e
v
a
l
(
t
r
e
e
_
p
oi
n
t
er
 
n
o
d
e)
{
/*
mod
i
fi
e
d
p
o
s
t
 
o
r
d
er
 
t
r
a
v
e
r
s
a
l
 
t
o
 
e
v
al
u
at
e
 
a
 
p
r
o
p
o
s
iti
on
al
c
alc
u
l
u
s
 
t
r
ee
 
*/
if
 
(
n
o
d
e)
 
{
p
o
s
t
_
o
r
d
er_
e
v
a
l
(
n
o
de
-
>l
e
f
t
_c
h
il
d)
;
p
o
s
t
_
o
r
d
er_
e
v
a
l
(
n
o
d
e
-
>rig
h
t
_
c
h
il
d)
;
s
w
i
t
c
h
(
n
o
d
e
-
>
d
at
a)
 
{
c
a
s
e
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
 
f
f
 
A
 
f
f
 
--
 
f
 
 
 
 
 
 
N
e
x
t
 
Node
 
in
 
Th
r
eaded
 
B
T
threaded_pointer
tree)
{
insuc
c
(threaded_pointer
threaded_pointer
temp;
temp
=
tre
e
-
>right_child;
if
(!t
r
e
e
->
right_threa
d
)
wh
i
le
(!
t
em
p
->left_threa
d
)
te
m
p
=
te
m
p
->left_child;
ret
u
rn
tem
p
;
}
186
Ino
r
der
 
T
r
a
v
e
r
s
a
l
 
o
f
 
Th
r
eaded
 
B
T
void
{
tinorde
r
(threaded_poi
n
ter
tree)
/*
t
r
a
v
e
r
se
 
 
t
h
e
t
h
r
e
a
d
ed
b
i
n
a
ry
t
r
ee
inorder
*/
threaded_pointer
t
e
mp
=
t
r
e
e
;
f
o
r
(
;
;)
t
e
mp
{
=
insucc(temp);
if
(temp==tree)
break;
printf(“%3c”,
tem
p-
>data);
}
O(
n)
(
t
i
m
e
c
ompl
e
x
i
t
y
)
}
187
Insert
i
ng
 
Nodes
 
i
nt
o
 
Th
r
eaded
 
B
T
s
 
I
nse
r
t
 
chil
d
 
as
 
t
he
 
r
i
g
h
t
 
chi
l
d
 
of
 
node
 
parent
c
h
a
n
g
e
parent->right_thread
 
t
o
 
F
ALSE
s
e
t
 
child->left_thread
 
a
n
d
 
child->right_thread
t
o
 
T
R
UE
s
e
t
s
e
t
child->left_child
 
t
o
 
p
o
i
n
t
 
t
o
 
parent
child->right_child
 
t
o
 
paren
t
->right_child
ch
a
n
g
e
 
parent->right_child
 
t
o
 
p
o
i
n
t
 
t
o
 
child
188
E
x
amples
In
sert
r
oo
t
a
n
ode
D
as
a
rig
h
t
 
ch
ild
r
oo
t
o
f
B.
A
A
(
1)
p
a
r
e
n
t
p
a
r
e
n
t
B
B
(3)
c
h
il
d
c
h
il
d
C
C
D
D
(2)
e
m
p
ty
189
 
 
 
 
 
 
 
 
*Fi
g
u
r
e
 
5.2
4
:
 
I
nse
r
t
i
o
n
 
o
f
 
c
h
il
d
 
as
 
a r
i
g
h
t
 
c
h
il
d
 o
f 
p
a
r
e
n
t
 
i
n
 
a t
h
r
e
a
de
d
 
b
i
n
ary
 
t
r
e
e
 
(
p
.217)
(3)
(
1)
(2)
(
4)
n
o
n
e
m
pt
y
190
 
 
 
 
 
 
 
 
 
 
 
 
 
Ri
g
h
t
 
Insert
i
on
 
in
 
Th
r
eaded
 
B
T
s
void
insert_right
(
threaded_pointer
threaded_pointer
parent,
child)
{
threaded_pointer
temp;
(
1
)
 
chil
d
->right_child
=
paren
t
->right_child;
child->right_thread
=
paren
t
->right_threa
d
;
chil
d
->left_child
 
 
=
parent;
case
(a)
(
2
)
 
child->left_thread
 
 
=
TRUE;
child;
(
3
)
parent->right_child
=
parent->right_thread
 
 
=
FALSE;
if
(!chi
l
d->right_thread)
{
case
(b)
(
4
)
 
temp
 
 
=
 
 
insucc(child);
temp->left_child
=
child;
}
}
191
Heap
 
A
 
m
a
x
 
t
r
e
e
 
i
s
 
a
 
t
r
ee
 
i
n
 
wh
i
ch
 
t
he
 
k
e
y
 
v
a
l
ue
 
i
n
each
 
node
 
is
 
no
 
smal
l
er
 
t
han
 
t
he
 
k
e
y
 
v
a
l
ues
 
in
i
t
s
 
chi
l
d
r
en.
A
 
m
a
x
 
h
eap
 
is
 
a
 
c
o
m
p
l
e
t
e
 
b
i
na
r
y
t
r
ee
 
t
h
a
t
 
is
 
al
so
 
a
 
m
a
x
 
t
r
e
e
.
 
A
 
min
 
t
r
e
e
 
i
s
 
a
 
t
r
ee
 
i
n
 
wh
i
ch
 
t
he
 
k
e
y
 
v
a
l
ue
 
i
n
each
 
node
 
is
 
no
 
l
a
r
g
er
 
t
han
 
t
he
 
k
e
y
 
v
a
l
ues
 
in
i
t
s
 
chi
l
d
r
en.
A
 
min
 
h
eap
 
is
 
a
 
c
o
m
p
l
e
t
e
 
b
i
na
r
y
t
r
ee
 
t
h
a
t
 
is
 
al
so
 
a
 
min
 
t
r
e
e
.
 
Ope
r
a
t
ions
 
on
 
hea
p
s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
196
Insert
i
o
n
 
i
nt
o
 
a
 
M
a
x
 
Heap
void
{
insert_max_heap(element
item,
int
*n)
int
i;
if
(HEAP_FULL(*n))
{
fprintf(stderr,
exit(1);
“the
heap
i
s
ful
l
.\n”);
}
i
=
++(*n);
while
((i!=
1
)&&(item.key>heap[i/2].key))
{
heap[i]
=
heap[i/2];
2
k
-
1
=
n
 
==>
k
=
lo
g
2
(n+1
)
i
/=
2;
}
heap[i]=
O(lo
g
2
n)
item;
}
197
E
x
amp
l
e
of
D
e
l
e
tion
f
r
om
M
a
x
Heap
r
e
m
o
v
e
20
10
15
2
2
2
15
14
15
14
14
10
10
198
De
l
e
tion
 
f
r
om
 
a
 
M
a
x
 
Heap
element
{
delete_max_hea
p
(int
*n)
int
parent,
child;
element
item,
temp;
if
(HEAP_EMPTY(*n))
{
fprintf(stderr,
exit(1);
“The
heap
i
s
emp
t
y
\n”);
}
/*
save
value
of
the
element
with
the
highest
 
 
key
 
 
*/
item
=
heap[1];
/*
use
l
a
st
el
e
ment
in
heap
t
o
adj
u
st
heap
temp
=
heap[(*n
)
--];
parent
=
1;
child
=
2;
199
while
(child
<=
*n)
{
/*
 
 
find
 
 
the
larger
child
of
the
current
parent
 
 
*/
if
((child
 
 
<
*n)&&
(heap[child].key<heap[child+1].key))
child++;
if
 
 
(temp.key
>=
heap[child].key)
break;
/*
 
 
move
 
 
to
 
 
the
 
 
next
 
 
lower
 
 
level
 
 
*/
heap[parent]
 
 
=
 
 
heap[child];
child
 
 
*=
 
 
2;
}
heap[parent]
return
 
 
item;
=
temp;
}
200
G
r
a
p
hs
201
W
h
a
t
 
is
 
a
 
g
r
ap
h
?
A
 
d
a
t
a
 
s
tr
uc
t
u
r
e
 
t
h
a
t
 
c
onsi
s
ts
 
of
 
a
 
s
e
t
 
of
 
n
od
e
s
(
ver
t
i
c
e
s
)
 
a
n
d
 
a
 
s
e
t
 
of
 
e
d
g
es
 
t
h
a
t
 
r
e
l
at
e
 
t
h
e
 
n
odes
t
o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t
e
m
)
;
}
}
}
}
 
w
h
i
l
e
 
(
!
q
u
e
u
e
.
I
s
E
m
p
t
y
(
)
 
&
&
!
f
o
u
n
d
)
;
i
f
(
!
f
o
u
n
d
)
c
o
u
t
 
<
<
 
"P
ath
 
n
o
t
 
f
o
u
n
d
"
 
<
<
}
e
n
d
l
;
230
Single
-
sou
r
ce
 
shor
t
e
s
t
-
p
a
th
 
p
r
oblem
The
r
e
 
a
r
e
 
mul
t
i
p
le
 
p
a
t
hs
 
f
r
om
 
a
 
sou
r
ce
v
e
rt
e
x
 
t
o
 
a
 
de
s
t
i
n
a
t
ion
 
v
e
rt
e
x
S
h
or
t
e
s
t
 
p
at
h
:
 
t
he
 
p
a
t
h
 
whose
 
t
o
t
al
 
w
e
i
g
h
t
(i
.
e.,
 
sum
 
of
 
ed
g
e
 
w
e
i
g
h
t
s)
 
i
s
 
min
i
mum
E
x
amp
l
es:
Au
s
ti
n
-
>Hou
s
t
o
n
-
>
A
tla
n
t
a
-
>
W
ashi
n
g
t
on:
m
iles
1
5
60
Au
s
ti
n
-
>D
a
lla
s
-
>D
e
n
v
er
-
>
A
tla
n
t
a-
>
W
a
shi
n
g
t
on:
2
9
80
 
m
iles
231
 
Single-sou
r
c
e
 
s
h
o
r
t
e
s
t
-p
a
th
 
p
r
oblem
(
c
o
n
t.)
Com
m
on
 
a
l
g
o
r
i
t
hms:
 
D
i
j
k
s
t
ra's
 
a
l
g
o
r
i
t
hm,
Be
l
l
m
a
n
-
F
ord
 
a
l
g
o
r
i
t
hm
B
F
S
 
c
an
 
b
e
 
used
 
t
o
 
s
o
l
v
e
 
t
he
 
sho
r
t
e
s
t
 
g
r
aph
p
r
oblem
 
when
 
t
he
 
g
r
aph
 
is
 
w
e
ig
h
t
less
t
he
 
w
e
i
g
h
t
s
 
a
r
e
 
t
he
 
same
(
mark
 
v
er
t
i
c
es
 
b
e
f
o
r
e
 
E
n
qu
e
u
e
)
or
a
l
l
232
Slide Note
Embed
Share

A tree in data structures is a finite set of nodes with a designated root and subtrees, including internal nodes and leaf nodes. Terminology like root, parent nodes, leaves, and levels are explained, along with concepts of height and degree of a tree. Additionally, binary trees are introduced as a specific type of tree with distinct left and right subtrees. The transformation of any tree into a binary tree using left child-right sibling representation is discussed.

  • Trees
  • Data Structures
  • Binary Trees
  • Terminology
  • Parent Nodes

Uploaded on Sep 10, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. UNIT-III Definition of Tree A tree is a finite set of one or more nodes such that: There is a specially designatednode called the root. The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn the subtrees of the root. 139

  2. Representation of Tree Level 1 T0 T1 2 T2 T3 T4 T T 3 5 6 Fig.Tree 1 140

  3. Terminology A D B C E F G H Fig.Tree 2 141

  4. ROOT: This is the unique node in the tree to which further subtrees are attached.in the above fig node A is a root node. Degree of the node: The total number of sub-trees attached to the node is called the degree of Node A E 0 Leaves: These are terminal nodesof the tree.The nodes with degree 0 are always the leaf nodes.In above given tree E,F,G,C and H are the leaf nodes. Internal nodes: The nodes other than the root node and the leaves are called the internal nodes.Here B and D are internal nodes. the node. degree 3 142

  5. Parent nodes: The node which is having further sub-trees(branches)is called the parent node of those sub-trees. In the given example node B is parent node of E,F and G nodes. Predecessor: While displaying the tree ,if some particular node occurs previous to some other node then that node is called the predecessor of the other node.In above figure E is a predecessor of the node B. successor: The node which occurs next to some other node is a successor above figure B is successor of F and G. Level of the tree: The root node is always considered at level 0,then its adjacent children are supposed to be at level 1 and so on.In above figure the node A is at level 0,the nodes B,C,D are at level 1,the nodes E,F,G,H are at level 2. node.In 143

  6. Height of the tree: The maximum level is the height of the tree.Here height of the tree is 3.The height of the tree is also called depth of the tree. Degree of tree: The maximum degree of the node is called the degree of the tree. The degree of a node is the number of subtrees of The degree of A is 3; the degree of C is 1. The node with degree 0 is a leaf or terminal node. A node that has subtreesis the parent of the roots of the subtrees. The roots of these subtrees are the children of the node. Children of the same parent are siblings. The ancestors of a node are all the nodes alongthe path from the root to the node. the node 144

  7. Binary Trees A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree. Any tree can be transformedinto binary tree. by left child-right sibling representation The left subtree and the right subtree are distinguished. 145

  8. Types Of Binary Trees There are three types of binary trees Left skewed binary tree Right skewed binary tree Complete binary tree 146

  9. Left skewed binary tree If the right subtree is missing in every node of a tree we cal it as left skewed tree. A B C 147

  10. Right skewed binary tree If the left subtree is missing in every node of a tree we call it as right subtree. A B C 148

  11. Completebinary tree The tree in which degree of each node is at the most two is called a complete binary tree.In a complete binary tree there is exactly one node at level 0,twonodes at level 1 and four nodes at level 2 and so on.so we can say that a complete binary tree of depth d will contains exactly level l,where l is from 0 to d. nodes at each 2l A C B D E F G 149

  12. Abstract Data Type Binary_Tree structure Binary_Tree(abbreviatedBinTree)is objects: a finite set of nodes either empty or consisting of a root node, left Binary_Tree, and right Binary_Tree. functions: for all bt, bt1, bt2 BinTree,item Bintree Create()::= createsan empty binary tree BooleanIsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE element 150

  13. BinTree MakeBT(bt1, item, bt2)::= return a binary tree whose left subtree is bt1, whose right subtree bt2, and whose root Bintree Lchild(bt)::= else element Data(bt)::= else of bt Bintree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right subtree of bt is node contains the data item if (IsEmpty(bt)) return error return the left subtree of bt if (IsEmpty(bt)) return error return the data in the root node 151

  14. Maximum Number of Nodes in BT The maximum number of binary tree is The maximum nubmer of of depth k is 2k-1, k>=1. nodes on level i of a 2i-1, i>=1. nodes in a binary tree Prove by induction. k2i 1 i 1 1 2k 152

  15. Binary Tree Representation Sequential(Arrays) representation Linked representation 153

  16. Array Representation of Binary Tree This representation uses only a single linear array tree as follows: i)The root of the tree is stored in tree[0]. ii)if a node occupies tree[i],then its left child is stored in tree[2*i+1],its right tree[2*i+2],and the parent is 1)/2]. child is stored in stored in tree[(i- 154

  17. Sequential Representation A [1] [2] [3] [4] [5] [6] [7] [8] [9] A B C D E F G H I . . . . B C E G D F . . . . H I 155

  18. Sequential Representation [0] [1] [2] [3] [4] [5] [6] [7] [8] 55 44 66 33 50 55 44 66 50 33 22 22 156

  19. Advantages of sequential representation The only advantage with this type of representation is that the direct access to any node can be possible and findingthe parent or left right children of any particular node is fast because of the random access. 157

  20. Disadvantages of sequential representation The major disadvantage with this type of representation is wastage of memory. The maximumdepth of the tree has to be fixed. The insertions and deletion of any node in tree will be adjusted at meaning of binary tree can be preserved. the costlier as other nodes has to be appropraite positions so that the 158

  21. Linked Representation struct node { int data; struct }; node * left_child, *right_child; data right_child left_child data right_child left_child 159

  22. Linked Representation root 55 44 66 X X 50 X X 33 X 55,44,66,33,50,22 X 22 X 160

  23. Advantages of Linked representation This representation is superior to our representation as there is no wastage of memory. Insertions and deletions which are the most common operations other nodes. can be done without moving the 161

  24. Disadvantages of linked representation This representation does not provide direct access to a node and special algorithms are required. This representationneeds additional space in each node trees. for storing the left and right sub- 162

  25. Full BT VS Complete BT A binary tree with n nodes and depth k is completeiff its nodes correspondto the nodes numberedfrom 1 to n in the full binary tree of depth k. A full binary tree of depth k is a binary tree of depth k having A k 2 -1 nodes, k>=0. A B C B C G D F E F G E D H I N O M L J I K H 163 Complete binary tree Full binary tree of depth 4

  26. Binary Tree Traversals The process of going through a tree in such a way that each node is visted once is tree traversal.several method are used for tree traversal.the traversal in a binary activities such as: Visiting the root Traverse left subtree Traverse right subtree tree involves three kinds of basic 164

  27. We will use some notations to traverse a given binary tree as follows: L means move to the Left child. R means move to the Right child. D means the root/parentnode. The only difference among the methods is the order in which these three operations are performed. There are three standard ways empty binary tree namely : Preorder Inorder Postorder of traversing a non 165

  28. Preorder(also known as depth-first order) 1.Visit the 2.Traverse 3.Traverse root(D) the the left subtree in preorder(L) right subtree in preorder(R) Print 1st A Print 2nd B Print 4th D Print 3rd E C Print at the last A-B-C-D-E is the preorder traversal of the above figure. 166

  29. Inorder(also known as symmetric order) 1.Traverse 2.Visit the 3.Traverse the left subtree in Inorder(L) root(D) the right subtree in Inorder(R) A Print 3rd Print 2nd B Print 4th D Print 1st E C Print at the last C-B-A-D-E figure. is the Inorder traversal of the above 167

  30. Postorder 1.Traverse 2.Traverse 3.Visit the the left subtree in postorder(L) the right root(D) subtree in postorder(R) Print at the last A Print 3rd B Print 4th D Print 1st E C Print 2nd C-D-B-E-A is the postorder traversal of the above figure. 168

  31. Binary tree traversals A A B C B C G D F E G D F E J H I K H I J FIG(a) FIG(b) Preorder:ABDHIECFJKG Inorder:HDIBEAJFKCG Postorder:HIDEBJKFGCA preorder inorder: postorder:HIDJEBFGCA :ABDHIEJCFG HDIBJEAFCG 169

  32. Arithmetic Expression Using BT inorder traversal A / B * C * D + E infix expression preorder traversal + * * / A B C D E prefix expression postorder traversal A B / C * D * E + postfix expression level order traversal + * E * D / C A B + * E * D C / B A 170

  33. Inorder Traversal (recursive version) void inorder(tree_pointer ptr) /* { if (ptr) { inorder(ptr->left_child); printf( %d , ptr->data); indorder(ptr->right_child); } } inorder tree traversal */ A / B * C * D + E 171

  34. Preorder Traversal (recursive void preorder(tree_pointerptr) version) /* preorder tree { if (ptr) { printf( %d , ptr->data); preorder(ptr->left_child); predorder(ptr->right_child); } } traversal */ + * * / A B C D E 172

  35. Postorder Traversal (recursive version) void postorder(tree_pointerptr) /* postorder tree traversal */ { A B / C * D * E + if (ptr) { postorder(ptr->left_child); postdorder(ptr->right_child); printf( %d ,ptr->data); } 173 }

  36. Iterative Inorder Traversal (using stack) iter_inorder(tree_pointer node) void { int top= -1; /* initialize stack */ tree_pointer stack[MAX_STACK_SIZE]; for (;;) { for (; node; node=node->left_child) add(&top, node);/* add to stack */ node= delete(&top); /* delete from stack */ if (!node) break; /* empty stack */ printf( %D , node->data); node = node->right_child; } } O(n) 174

  37. Trace Operations of Inorder Traversal Call of inorder Value in root Action 1 2 3 4 5 6 5 7 4 8 9 8 10 3 Call of inorder Value in root Action 11 12 11 13 2 14 15 14 16 1 17 18 17 19 + * * / A NULL A NULL / B NULL B NULL * C NULL C NULL * D NULL D NULL + E NULL E NULL printf printf printf printf printf printf printf printf printf 175

  38. Level Order Traversal (using queue) void level_order(tree_pointer ptr) /* { int front = rear = 0; tree_pointer queue[MAX_QUEUE_SIZE]; if (!ptr) return; /* empty queue */ addq(front, &rear, ptr); for (;;) { ptr = deleteq(&front, rear); level order tree traversal */ 176

  39. if (ptr) { printf( %d , ptr->data); if (ptr->left_child) addq(front, &rear, ptr->left_child); if (ptr->right_child) addq(front, &rear, ptr->right_child); } else break; } + * E * D / C A B } 177

  40. Copying Binary Trees tree_poointer { tree_pointer temp; if (original) { temp=(tree_pointer) malloc(sizeof(node)); if (IS_FULL(temp)) { fprintf(stderr, exit(1); } temp->left_child=copy(original->left_child); temp->right_child=copy(original->right_child temp->data=original->data; return } return } copy(tree_pointer original) the memory is full\n ); temp; postorder NULL; 178

  41. Post-order-evalfunction void post_order_eval(tree_pointer node) { /* modified post order traversal to evaluate a propositional calculus tree */ if (node) { post_order_eval(node->left_child); post_order_eval(node->right_child); switch(node->data) { case not: node->value = !node->right_child->value; break; 179

  42. case and: node->value = node->right_child->value && node->left_child->value; break; case or: node->value = node->right_child->value | | node->left_child->value; break; case true: break; case false: } node->value = TRUE; node->value = FALSE; } } 180

  43. Threaded Binary Trees Two many null pointers in current of binary trees n: number of nodes number of non-null links: n-1 total links: 2n null links: 2n-(n-1)=n+1 representatio Replace these threads . null pointers with some useful 181

  44. Threaded Binary Trees (Continued) If ptr->left_child is replace it with a pointer visited before ptr in an null, to the node that would inorder traversal be If ptr->right_childis null, replace it with a pointer to the node that visited after ptr in an inorder traversal would be 182

  45. A Threaded Binary Tree root A dangling C B dangling G F E D inorder traversal: H, D, I, B, E, A, F, C, G I H 183

  46. Data Structures for Threaded BT left_thread left_child data right_child right_thread TRUE FALSE FALSE: child TRUE: thread typedef struct threaded_tree *threaded_pointer; typedef struct threaded_tree { short int left_thread; threaded_pointer left_child; char data; threaded_pointer right_child; short int right_thread; }; 184

  47. Memory Representation of A Threaded BT root -- f f A f f C B f f f f G F E D t t t t f t f I H t t t t 185

  48. Next Node in Threaded BT threaded_pointer tree) { threaded_pointer temp; temp = tree->right_child; if (!tree->right_thread) while (!temp->left_thread) temp = temp->left_child; return temp; } insucc(threaded_pointer 186

  49. Inorder Traversal of Threaded BT void { /* traverse the threaded binary tree inorder */ threaded_pointer temp = tree; for (;;) temp = insucc(temp); if (temp==tree) break; printf( %3c , temp->data); } O(n)(timecomplexity) } tinorder(threaded_pointer tree) { 187

  50. Inserting Nodes into Threaded BTs Insert child as the right child of node parent set child->left_threadand child->right_thread to TRUE change parent->right_childto point to child change parent->right_thread to FALSE child->left_childto point to parent set set child->right_childto parent->right_child 188

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#