Tree 1
Tree 1
Data Structure
Vani M.
Trees: Outline
Introduction
Representation Of Trees
Binary Trees
Heaps
Examples:
Introduction (2/8)
Definition (recursively): A tree is a finite set of
one or more nodes such that
There is a specially designated node called root.
The remaining nodes are partitioned into n>=0 disjoint
set T1,…,Tn, where each of these sets is a tree. T1,…,Tn
are called the subtrees of the root.
H
3
D E F G
I
4
Introduction (6/8)
Representation of Trees
List Representation
we can write of Figure 5.2 as a list in which each of the subtrees is
also a list
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first,
followed by a list of sub-trees
Introduction (7/8)
Representation of
Trees (cont’d)
Left Child-
Right Sibling
Representation
Introduction (8/8)
Representation of Trees (cont’d)
Representation
as a Degree
Two Tree
Binary Trees (1/9)
Binary trees are characterized by the fact that any
node can have at most two branches
Definition (recursive):
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
B B
Any tree can be transformed into binary tree
by left child-right sibling representation
Binary Trees (2/9)
The abstract data type of binary tree
Binary Trees (3/9)
Two special kinds of binary trees:
(a) skewed tree, (b) complete binary tree
The all leaf nodes of these trees are on two adjacent levels
Binary Trees (4/9)
Properties of binary trees
Lemma 1 [Maximum number of nodes]:
1. The maximum number of nodes on level i of a binary tree
is 2i-1, i 1.
2. The maximum number of nodes in a binary tree of depth k
is 2k-1, k1.
3. Lemma 2 [Relation between number of leaf nodes and degree-
2 nodes]:
For any nonempty binary tree, T, if n0 is the number of leaf
nodes and n2 is the number of nodes of degree 2, then n0 =
n2 + 1.
Inorder
Postorder
Level order
Binary Tree Traversals
Arithmetic Expression using binary tree
inorder traversal (infix expression)
A/B*C*D+E
preorder traversal (prefix expression)
+**/ABCDE
postorder traversal
(postfix expression)
AB/C*D*E+
level order traversal
+*E*D/CAB
Preorder Traversal
void preOrder(treePointer ptr)
if (ptr != NULL)
{ visit(t);
preOrder(ptr->leftChild);
preOrder(ptr->rightChild);
}
Preorder Example (Visit = print)
a
b c
a b c
Preorder Example (Visit = print)
a
b c
f
d e
g h i j
a b d g h e i c f j
Preorder of Expression Tree
/
* +
e f
+ -
a b c d
/ * + a b - c d + e f
output + * * / A B C D E
:
V
L
R
Inorder Traversal
void inOrder(treePointer ptr)
{ if (ptr != NULL)
{
inOrder(ptr->leftChild);
visit(ptr);
inOrder(ptr->rightChild);
}
}
Inorder Example (Visit = print)
a
b c
b a c
Inorder Example (Visit = print)
a
b c
f
d e
g h i j
g d h b e i a f j c
Inorder By Projection
a
b c
f
d e
g h i j
g d h b e i a f j c
Inorder of Expression Tree
/
* +
e f
+ -
a b c d
a + b * c - d / e + f
output A / B * C * D + E
:
ptr
L
V
R
Binary Tree Traversals
Iterative inorder traversal
we use a stack to simulate recursion
5 4 11
8 3 14
2 17
1
A B
/ *C D
* E
+
L
V
b c
b c a
Postorder Example (Visit = print)
a
b c
f
d e
g h i j
g h d i e b j f c a
Postorder of Expression Tree
/
* +
e f
+ -
a b c d
a b + c d - * e f + /
output A B / C * D * E +
:
L
R
V
Traversal Applications
a
b c
f
d e
g h i j
• Make a clone.
• Determine height.
•Determine number of nodes.
Binary Tree Traversals
Level-order traversal
method:
We visit the root first, then the root’s left child, followed by the
root’s right child.
We continue in this manner, visiting the nodes at each new level
from the leftmost node to the rightmost nodes
This traversal requires a queue to implement
Level Order
Let ptr be a pointer to the tree root.
while (ptr != NULL)
{ visit node pointed at by ptr and put its children
on a FIFO queue;
if FIFO queue is empty, set ptr = NULL;
otherwise, delete a node from the FIFO queue
and call it ptr;
}
Level-Order Example (Visit = print)
a
b c
f
d e
g h i j
a b c d e f g h i j
Binary Tree Traversals
Level-order traversal (using queue)
output + * E * D / C A B
:
1 17 3 14 4 11 5
2 8
*+ E * D / C A B
FIFO
ptr
Binary Tree Construction
Suppose that the elements in a binary tree are distinct.
inorder = ab
b a
a b
postorder = ab
b b
a a
level order = ab
a a
b b
Binary Tree Construction
preorder = a b d g h e i c f j
gdhbei fjc
Inorder And Preorder
a
gdhbei 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 fjc
gd ei
h
Inorder And Preorder
a
b fjc
gd ei
preorder
h = abdgheicf j
d is the next root; g is in the left
subtree; h is inathe right subtree.
b fjc
d ei
g h
Inorder And Postorder
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.
Inorder And Level Order
inorder = g d h b e i a f j c
level order = a b c d e f g h i j
Tree root is a; gdhbei are in left subtree; fjc are in right
subtree.
Additional Binary Tree Operations (1/7)
Copying Binary Trees
we can modify the postorder traversal algorithm only
slightly to copy the binary tree
similar as
Program 5.3
Additional Binary Tree Operations
(2/7)
Testing Equality
Binary trees are equivalent if they have the same
topology and the information in corresponding nodes
is identical V L R
postorder traversal
data
value X3
X1 X3
X2 X1
Additional Binary Tree Operations (6/7)
node structure
For the purpose of our evaluation algorithm, we assume
each node has four fields:
TRUE
FALSE
FALSE
TRUE T
TRUE
F
FALSE FALSE
T TRUE
TRUE
F T
FALSE TRUE