Foundation of Computing Systems

Download Report

Transcript Foundation of Computing Systems

Foundation of Computing Systems
Lecture 4
Trees: Part I
Tree: Example
jan
/
mar
feb
-
apr
may
jun
A
jul
nov
31.07.09
aug
dec
+
B
*
oct
C
sep
IT 60101: Lecture #4
E
D
2
Tree: Definition
• A tree is a finite set of one or
more nodes such that:
• (i) there is a specially
designated node called the
root
• (ii) remaining nodes are
partitioned into n (n > 0)
disjoint sets T1, T2, . . ., Tn,
where each Ti (i = 1, 2, . . .,
n) is a tree; T1, T2, . . ., Tn
are called sub trees of the
root.
31.07.09
T1
A
T2
B
D
T3
C
E
F
H
I
J
G
K
L
T = (A(B(E, F(K, L))), C(G), D(H, I, J))
IT 60101: Lecture #4
3
Binary Tree
• A binary tree is a special form of a
tree
A
• A binary tree T is a finite set of
nodes, such that
• (i) T is empty (called empty binary
tree), or
• (ii) T contains a specially designated
node called the root of T, and the
remaining nodes of T form two
disjoint binary trees T1 and T2 which
are called left sub-tree and the right
sub-trees, respectively.
31.07.09
IT 60101: Lecture #4
C
B
D
E
J
F
G
H
K
I
4
Full Binary Tree
1
2
3
5
4
8
9
10
6
11
12
7
13
14
15
Level
0
Nodes
1
1
2
2
4
3
8
A full binary tree of height 4
31.07.09
IT 60101: Lecture #4
5
Complete Binary Tree
Level Nodes
0
1
1
2
3
5
4
8
9
10
6
11
7
12
1
2
2
4
3
5
A complete binary tree of height 4
31.07.09
IT 60101: Lecture #4
6
Skewed Binary Tree
31.07.09
IT 60101: Lecture #4
7
Properties of Binary Trees
• Property 1
• In any binary tree, maximum number of nodes on level l is 2l,
where l ≥ 0.
• Property 2
• Maximum number of nodes possible in a binary tree of height
h is 2h – 1.
• Property 3
• Minimum number of nodes possible in a binary tree of height
h is h.
• Property 4
• For any non-empty binary tree, if n is the number of nodes
and e is the number of edges, then n = e + 1.
31.07.09
IT 60101: Lecture #4
8
Properties of Binary Trees
• Property 5
• For any non-empty binary tree T, if n0 is the number of leaf
nodes (degree = 0) and n2 is the number of internal node
(degree = 2), then n0 = n2 + 1.
• Property 6
• Height of a complete binary tree with n number of nodes is
log 2 (n  1)
• Property 7
• Total number of binary tree possible with n nodes is
1 2n
Cn
n 1
31.07.09
IT 60101: Lecture #4
9
Representation of Binary Trees
• Linear representation
– Using array
• Linked representation
– Using linked list structure
31.07.09
IT 60101: Lecture #4
10
Linear Representation of Binary Trees
1.
The root node is at location 1.
2.
For any node with index i, 1 < i
£ n, (for some n):
+
-
(a) PARENT(i) = i 2
*
A
B
/
C
For the node when i = 1, there is no
parent.
(a) A binary tree
(b) LCHILD(i) = 2 * i
If 2 * i > n, then i has no left child.
(c) RCHILD(i) = 2 * i + 1
1
+ -
2
3
*
If 2 * i + 1 > n, then i has no right
child.
31.07.09
E
D
IT 60101: Lecture #4
4
A
5
B
6
C
7
/
8
.
9
.
10
.
.
11
.
12 13
.
14
15 16
D
E
(b) Array representation of the binary tree
11
.
Linked Representation of Binary Trees
DATA
Address
RCHILD DATA LCHILD
LC
RC
Node content
A
50
75
+
40
B
C
/
40
B
75
62
46
D
+
46
C
62
*
58
E
66
(a) A binary tree
58
74
31.07.09
-
*
89
A
50
IT 60101: Lecture #4
D
66
/
74
E
12
Linked Representation of Binary
Trees
89
+
+
A
*
B
75
46
-
C
*
/
D
E
50
40
A
62
B
58
C
/
(a) A binary tree
66
74
D
E
(c) Logical view of the linked representation of a binary tree
31.07.09
IT 60101: Lecture #4
13
Binary Tree: Operations
• Major operations on a binary tree can be listed
as:
• Insertion. To include a node into an existing (may be empty)
binary tree.
• Deletion. To delete a node from a non-empty binary tree.
• Traversal. To visit all the nodes in a binary tree.
• Merge. To merge two binary trees into a larger one.
31.07.09
IT 60101: Lecture #4
14
Binary Tree: Insertion
A
B
D
C
E
F
G
31.07.09
IT 60101: Lecture #4
15
Binary Tree: Deletion
A
B
D
C
E
F
X
G
31.07.09
IT 60101: Lecture #4
16
Binary Tree: Traversals
• Depth-first search
R
• Breadth-first search
(level-by search)
Tl
Tr
• Recursive search
1. R Tl
2. Tl R
3. Tl Tr
31.07.09
Tr
Tr
R
4.
5.
6.
Tr Tl R
Tr R Tl
R Tr Tl
IT 60101: Lecture #4
17
Binary Tree: Traversals
+
-
A
*
B
C
/
E
FE/C*BA–+
+-AB*C/EF
31.07.09
F
A–B+C*E/F
F/E*C+B–A
AB–CEF/*+
+*/FEC–BA
IT 60101: Lecture #4
18
Binary Tree: Traversals
R
Tl
R Tl Tr
Tl R Tr
Tl Tr R
Tr
FE/C*BA–+
+-AB*C/EF
31.07.09
(Preorder)
(Inorder)
(Postorder)
A–B+C*E/F
F/E*C+B–A
AB–CEF/*+
+*/FEC–BA
IT 60101: Lecture #4
19
Binary Tree: Traversals
Preorder traversal
• Visit the root node R.
• Traverse the left sub-tree of R in preorder.
• Traverse the right sub-tree of R in preorder.
Inorder traversal
• Traverse the left sub-tree of the root node R is inorder.
• Visit the root node R.
• Traverse the right sub-tree of the root node R is inorder.
Postorder traversal
• Traverse the left sub-tree of the root R in postorder
• Traverse the right sub-tree of the root R in postorder
• Visit the root node R.
31.07.09
IT 60101: Lecture #4
20
Binary Tree: Merging
T1
u1
u1
u2
v1
u2
+
u3
u5
T1
u7
v2
u8
v1
=
u4
u6
T2
v4
v3
v5
v6
u3
v7
u5
u4
u6
u7
v2
u8
v4
v3
v5
v6
v7
T2
T
31.07.09
IT 60101: Lecture #4
21
Different Binary Trees
• There are several types of binary trees possible each with its own
properties.
• Expression tree
• Binary search tree
• Heap tree
• Threaded binary tree
• Huffman tree
• Height balanced tree (also known as AVL tree)
• Red black tree
• Splay tree
• Decision tree
31.07.09
IT 60101: Lecture #4
22