Module 7 8 - Trees
Module 7 8 - Trees
7.1 INTRODUCTION
Trees are one of the most important data structure in Computer Science. They come in
many forms. They provide natural representation for many kinds of data that occur in applications,
and they are useful for solving a wide variety of problem.
In many applications, objects exhibit a nonlinear order in which a member may have
multiple successors. For example, in describing a family tree, a parent may have many descendants
(children). See Figure 7.1
“A tree is a nonlinear structure which consists of a finite set of elements, called node,
and a finite set of directed lines, called branches that connect the nodes.” [GILB98]
grandfather
Ancestor – all nodes along the path from the to that node
Subtree – any connected structure below the root. In Figure 7.2 we have 5 subtrees
(B,C,D,E,H)
Level 0
d Root
A
f Level 1
B C D
E F G H I J Level 2
K L M Level 3
The array representation can use a fixed-size or non-fixed array. For fixed-sized
array, the size of data structure is fixed during compilation and it does not change its size
during execution. For non fixed-sized array, it allows the array to grow or shrink as needed
during execution of program.
Figure 7.3 is an example of complete binary tree, it has ten nodes which contains a
single character. The ten characters that the tree contains can be stored in an array as shown
in the figure. We can consecutively place each node on the array based on their depth.
‘A’
‘B’ ‘C’
‘A’ ‘B’ ‘C’ ‘D’ ‘E’ ‘F’ ‘G’ ‘H’ ‘I’ ‘J’
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
1. The data from the root always appear in the [0] component of the array.
2. Suppose that the data for a non root node appears in component [i] of the array. Then
the data for its parent is always at location [(i - 1) / 2] (using integer division). For
example, using Figure 7.3, if i=6 then [(6-1)/2] = 2 where location [6] contains “G”. So
using the formula, the node parent of “G” is at location [2].
3. Suppose that the data for a node appears in component [i] of the array. Then its children
(if they exist) always have their data at the locations
Left child at component [2i + 1]
Right child at component [2i + 2]
For example, if i=2 , where location [2] contains “C”, then
Left child location [2(2) + 1] = 5, where location [5] contains “F”
Right child location [2(2) + 2] = 6 where location [6] contains “G”
These formulas make it easy to implement algorithm that traverse the tree, moving from
one node to another in various ways, processing data along the way.
7.4 Binary Trees
A binary tree is a tree in which no node can have more than two subtrees. In other words,
a node can have zero, one, or two subtrees [GILB98]. Figure 7.4 is an example of binary tree.
A binary tree with zero node is called a null tree.
B C
D E F G
a. Full Binary Tree – every leaf has the same depth and every non leaf has two
children. See Figure 7.4, it is an example of full binary tree.
b. Complete Binary Tree – every depth except the deepest must contain as many
nodes as possible and at the deepest level all the nodes are as far as left as
possible. See Figure 7.5, it is an example of complete binary tree.
B C
D E F G
H I J
Figure 7.5 Complete Binary Tree
Node
Left subtree <pointer to node>
Data <data type>
Right subtree <pointer to node>
End Node
A binary tree traversal requires that each node of the tree be processed once and only once
in a predetermined sequence.
1 A
2 C
B 5
3 D E F G
4 6 7
Preorder (root)
{
If (root is not null)
Process (root)
Preorder (root -> left subtree)
Preorder (root -> right subtree)
}
Algorithm 7.1 Preorder Traversal of a Binary Tree
The inorder traversal processes the left subtree first, then the root, and finally the
right subtree. The meaning of prefix in is that the root is processed in between the subtrees.
4
B C
2 6
D E F G
1 3 5 7
Inorder (root)
{
If (root is not null)
Inorder (root -> left subtree
Process (root)
Inorder (root -> right subtree)
}
7
A
6
B 3 C
D E F G
1 2 4 5
Postorder (root)
{
If (root is not null)
Postorder (root -> left subtree)
Postorder (root -> right subtree)
Process (root)
}
+ -
a b c d
Infix (tree)
* Print the infix expression for an expression tree
{
If (tree not empty)
If (tree-> token is an operand)
Print (tree-> token)
Else
Print (open parenthesis)
Infix (tree-> left)
Print (tree->token)
Infix(tree-> right)
Print ( close parenthesis)
}
The above algorithm traverses the tree and prints the expression. To print an infix
expression, we must add an opening parenthesis at the beginning of each expression and a
closing parenthesis at the end of each expression. Because the root of the tree and each of its
subtrees represents a subexpression; we print the opening parenthesis whenever we start a tree
or subtree and the closing parenthesis when we have processed all of its children.
( * )
4
( ) ( )
+ -
2 6
a b c d
E
1 3 5 7
( (a+b) * (c–d) )
Quick Review
➢ Binary tree is a tree in which no node can have more than two subtrees.
➢ There are three types of binary tree traversals namely, preorder, inorder, postorder.