Download - FCS 04 Trees Part01
-
7/25/2019 FCS 04 Trees Part01
1/22
Foundation of Computing Systems
Lecture 4
Trees: Part I
-
7/25/2019 FCS 04 Trees Part01
2/22
31.07.09 IT 60101: Lecture #4 2
Tree: Example
j a n
f e b m a r
a p rm a y j u n
j u l a u g
s e p
c t
! e cn "
#
$
% &
'
( )
* +
-
7/25/2019 FCS 04 Trees Part01
3/22
31.07.09 IT 60101: Lecture #4 3
Tree: Definition
, %tree-s a f-n-te set f ne rmre n!es suc tat:
, /- tere -s a spec-ally!es-gnate! n!ecalle! te
rt
, /-- rema-n-ng n!es arepart-t-ne! -nt n/n 0!-sj-nt sets T1 T2 . . . Tnere eac Ti/i 1 2 . . .
n-s a tree5 T1 T2 . . . Tnare calle! sub trees f tert.
T T T
%
&
*
+
)
8 I
: L
1 32
T /A/B/E F/K L C/G D/H I J
-
7/25/2019 FCS 04 Trees Part01
4/22
31.07.09 IT 60101: Lecture #4 4
Binary Tree
, % binary tree-s a spec-al frm f atree
, % b-nary tree T-s a f-n-te set fn!es suc tat
, /- T-s empty /calle! empty b-narytree r
, /-- Tcnta-ns a spec-ally !es-gnate!n!e calle! te rtf T an! terema-n-ng n!es f Tfrm t
!-sj-nt b-nary trees T1
an! T2
-care calle! left sub$tree an! te r-gtsub$trees respect-"ely.
%
& *
+ )
8
I
:
-
7/25/2019 FCS 04 Trees Part01
5/22
31.07.09 IT 60101: Lecture #4 ;
Full Binary Tree
% f u l l b - n a r y t r e e f e - g t 4
L e v e l N o d e s
1
2 3
4 5 6 7
8 9 1 0 1 1 1 2 1 3
1 4 1 5
0 1
1 2
2 4
3 8
-
7/25/2019 FCS 04 Trees Part01
6/22
31.07.09 IT 60101: Lecture #4 6
Complete Binary Tree
L e v e l N o d e s
1
2 3
4 5 6 7
8 9 1 0 1 1 1 2
0 1
1 2
2 4
3 5
% c m p l e t e b - n a r y t r e e f . e - g . t 4
-
7/25/2019 FCS 04 Trees Part01
7/22
31.07.09 IT 60101: Lecture #4 7
Skewed Binary Tree
-
7/25/2019 FCS 04 Trees Part01
8/22
31.07.09 IT 60101: Lecture #4 -mum number f n!es n le"el l-s 2l
ere l? 0.
, =rperty 2
, @a>-mum number f n!es pss-ble -n a b-nary tree f e-gth-s 2hA 1.
, =rperty 3, @-n-mum number f n!es pss-ble -n a b-nary tree f e-gt
h-s h.
, =rperty 4, r any nn$empty b-nary tree -f n-s te number f n!es
an! e-s te number f e!ges ten n e' 1.
-
7/25/2019 FCS 04 Trees Part01
9/22
31.07.09 IT 60101: Lecture #4 9
roperties of Binary Trees
, =rperty ;
, r any nn$empty b-nary tree T -f n0-s te number f leaf
n!es /!egree 0 an! n2-s te number f -nternal n!e
/!egree 2 ten n0 n2' 1.
, =rperty 6
, 8e-gt f a cmplete b-nary tree -t nnumber f n!es -s
, =rperty 7, Ttal number f b-nary tree pss-ble -t nn!es -s
)1(log2 +n
n
n
Cn
2
1
1
+
-
7/25/2019 FCS 04 Trees Part01
10/22
31.07.09 IT 60101: Lecture #4 10
!epresentation of Binary Trees
, L-near representat-n
A Bs-ng array
, L-nCe! representat-nA Bs-ng l-nCe! l-st structure
-
7/25/2019 FCS 04 Trees Part01
11/22
31.07.09 IT 60101: Lecture #4 11
Linear !epresentation of Binary Trees
1. Te rt n!e -s at lcat-n 1.
2. r any n!e -t -n!e> i 1 D i E n /frsme n:
/a =%F)GT/i
r te n!e en i 1 tere -s n parent.
/b L*8IL+/i 2 (i
If 2 (i n ten ias n left c-l!.
/c F*8IL+/i 2 ( i' 1
If 2 ( i' 1 n ten ias n r-gt c-l!.
2i
'
$ (
% & * #
/ a % b - n a r y t r e e
1 2 3 4 ; 6 7 < 9 1 0 1 1 1 2 1 3 1 4 1 ; 1 6
' $ ( % & * # . . . . . . + )
/ b % r r a y r e p r e s e n t a t - n f t e b - n a r y t r e e
.
+ )
-
7/25/2019 FCS 04 Trees Part01
12/22
31.07.09 IT 60101: Lecture #4 12
Linked !epresentation of Binary Trees
+ % T %
F * L *
'
$ (
% & * #
+ )
/ a % b - n a r y t r e e
% ! ! r e s s G ! e c n t e n t
; 0
7 ;
4 0
< 9
6 2
4 6
6 6
;