Escolar Documentos
Profissional Documentos
Cultura Documentos
TREES
Table of Contents
1) Introduction to Trees
Tree Terminology
Trees Properties
2) Binary Trees
Complete Binary Trees
Extended Binary Trees
3) Memory Representation
Sequential
Linked
4) Binary Tree Traversal
Pre-order
In-order
Post-order
5) Traversal Algorithms using Stacks
President-CEO
Production
Manager
Personnel
Manager
Purchasing
Supervisor
Warehouse
Supervisor
Sales
Manager
Shipping
Supervisor
Definition of Tree
A tree is a finite set of one or more nodes
such that:
There is a specially designated node 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.
Tree Terminology
Terminology
The degree of a node is the number of subtrees
of the node
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 subtrees is 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
along the path from the root to the node.
Ancestors of u
Descendants of u
Level Number
Each node in a binary tree T is assigned a Level
Number.
The root R of the tree T is assigned a level number 0.
Every other node is assigned a level number which is
one more than the level number of its parent.
The nodes with the same level number are said to
belong to the same generation.
Level: 0
A
B
C
E
D
F
Level: 1
Level: 2
Level: 3
Exercise
Number of Nodes
Degree of a node(shown by green number)
Leaf node(terminal)
Non terminal nodes
Parent
Children
2
Sibling
Ancestor
2
0
Descendant
Level of a node
0
0
Height(depth) of a tree
Level
0
3
1
1
0
Exercise to do
A
Property
Number of nodes
Height
Root Node
Leaves
Interior nodes
Number of levels
Ancestors of H
Descendants of B
Siblings of E
Degree of node A
Value
Binary Trees
Binary Trees
A Binary Tree T is defined as a finite set of elements, called
nodes , such that:
a) T is empty (called null tree or empty tree) or
b) T contains a distinguished node R, called the root of T,
and the remaining nodes of T form an ordered pair of
disjoint binary tree T1(left subtree) and T2(right subtree).
[This definition of the binary tree is recursive. ]
Any node in Binary tree T has either 0,1 or 2 successors. i.e.
A special class of trees: max degree for each node is 2
Example
A
B
C
L
M
C
D
Skewed Tree
C
D
F
G
G
A
B
D
H
E
I
C
F
D
I
G
M
Sequential Representation
This representation uses only a single linear array TREE as
follows:
The root R of T is stored in TREE[1].
If a node N occupies TREE[K], then its left child is
stored in TREE[2*K] and its right child is stored in
TREE[2*K+1].
oNULL is used to indicate an empty sub tree.
oIn particular, TREE[1]=NULL indicates that the tree is
empty.
45
2 22
3 77
4 11
5 30
6
7 90
22
77
9 15
10 25
11
12
11
30
90
13
14 88
15
16
.
.
15
25
88
.
29
save memory
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
.
[16]
A
B
-C
----
B
D
-.
E
A
B
C
D
E
F
G
H
I
Linked Representation
Tree T will be maintained in memory by means of a linked
Representation which uses three parallel arrays, INFO,
LEFT and RIGHT, and a pointer variable ROOT.
left
right
Linked Representation
Pointer variable ROOT contains the location of
the root R of T.
If the tree T ids empty, then ROOT will contain
the null value.
If any subtree of the tree is empty, then the
corresponding pointer will contain the null
value.
left
data
right
Exercise
Construct a Binary tree using Linked Representation
Root
INFO
LEFT
RIGHT
14
Avail
10
17
18
13
19
10
11
12
13
12
14
15
15
16
16
11
17
18
19
20
20
Preorder
1. Process the root R.
2. Traverse the left subtree of R in preorder.
3. Traverse the right subtree of R in preorder.
Inorder
1. Traverse the left subtree of R in inorder.
2. Process the root R.
3. Traverse the right subtree of R in inorder.
Postorder
1. Traverse the left subtree of R in postorder.
2. Traverse the right subtree of R in postoder.
3. Process the root R.
Preorder
a
b
c
a b c
Preorder Example
a
b
c
e
d
g
a b d g h e i
c f
*
e
+
a
* + a b -
c d + e
Inorder Example
a
b
c
b a c
Inorder Example
a
b
c
e
d
g
g d h b e i
a f
*
e
+
a
d /
Postorder Example
a
b
c
b c a
Postorder Example
a
b
c
e
d
g
g h d i
e b j
c a
*
e
+
a
a b + c d -
* e f
5
4
9
10
6
11
12
6 10 12
3
27
11
11 10 12 14 20 27 22 30
12 14 15 20 22 27 30
20
22
14
30
preorder traversal
+**/ABCDE
prefix expression
inorder traversal
A/B*C*D+E
infix expression
postorder traversal
AB/C*D*E+
postfix expression
Some Examples
a
preorder = ab
b
inorder = ab
b
b
a
postorder = ab
b
b
a
level order = ab
a
a
a
b
D
K
fjc
fjc
Preorder = a b d g h e i c f j
b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a
b
gdh
fjc
ei
fjc
ei
preorder = a b d g h e i c f j
d is the next root; g is in the left subtree;
h is in the right subtree.
a
fjc
b
d
g
ei
h
inorder = g d h b e i a f j c
postorder = g h d i e b j f c a
Tree root is a; gdhbei are in left subtree; fjc are in
right subtree.
E
H
k
C
Tree T1
G
Tree T2
D
Tree T3
Tree T4
Exercise
Q1: The post order traversal of a binary tree is 452631. Find out the pre
order traversal
a. 126345
b. 142653
c. 124536
d. 124356
Q2: The inorder and preorder traversals of a binary tree are d h b e f c I g j
and a b d h e c f g I j respectively. The Post order traversal is
a) h d e b f i j g c a
b) d h e b i j g f c a
c) e h d b f i j g c a
D) h e d b f i j g a c