Chapter 5
Chapter 5
TREES
Many real life problems can be represented and solved using trees.
Trees are very flexible, versatile and powerful non-liner data structure that can be used to
represent data items possessing hierarchical relationship between the grand father and his
children and grand children as so on.
A tree is a set of nodes and edges that connect pairs of nodes. It is an abstract model of a
hierarchical structure. Rooted tree has the following structure:
One node distinguished as root.
Every node C except the root is connected from exactly other node P. P is C's parent, and
C is one of C's children.
There is a unique path from the root to the each node.
The number of edges in a path is the length of the path.
Tree Terminologies
Root is a specially designed node (or data items) in a tree. It is the first node in the hierarchical
arrangement of the data items. ‘A‘ is a root node in the Figure below. Each data item in a tree is
called a node. It specifies the data information and links (branches) to other data items.
Consider the following tree
The tree is structured in different levels. The entire tree is leveled in such a way that the root node is
always level 0. Then, its immediate children are at level 1 and their immediate children are at level 2
and soon up to the terminal nodes. That is if a node is at level n, then its children will be at level n+1.
Full or Strictly binary tree: a binary tree where each node has either 0 or 2 children.
The tree is said to be strictly binary tree, if every non-leaf made in a binary tree has non-empty left and
right sub trees. A strictly binary tree with n leaves always contains 2n–1 nodes. That is every node in the
strictly binary tree can have either no children or two children. They are also called 2-tree or extended
binary tree.
Balanced binary tree: a binary tree where each node except the leaf nodes has left and right children
and all the leaves are at the same level.
Complete binary tree: a binary tree in which the length from the root to any leaf node is either h or h-
1 where h is the height of the tree. The deepest level should also be filled from left to right.
Binary search tree (ordered binary tree): a binary tree that may be empty, but if it is not empty it
satisfies the following.
Every node has a key and no two elements have the same key.
The keys in the right sub tree are larger than the keys in the root.
The keys in the left sub tree are smaller than the keys in the root.
The left and the right sub trees are also binary search trees.
Array Representation
An array can be used to store the nodes of a binary tree. The nodes stored in anarray of memory
can be accessed sequentially.
Suppose a binary tree T of depth d. Then at most 2d – 1node can be there in T. (i.e.
SIZE =2d–1) so the array of size “SIZE” to represent the binary tree. Consider the following
binary treeof depth 3. Then SIZE = 23 – 1= 7. Then the array A[7] is declared to hold thenodes.
2. The left child of a node having index n can be obtained by (2n+1). For example tofind the left
child of C, where array index n = 2. Then it can be obtained by
= (2n +1)
= 2*2 + 1
=4+1
=5 i.e., A[5] is the left child of C, which is NULL. So no left child for C
3. The right child of a node having array index n can be obtained by the formula (2n+ 2). For
example to find the right child of B, where the array index n = 1. Then
= (2n + 2)
= 2*1 + 2
=4 i.e.A[4] is the right child of B, which is E.
4. If the left child is at array index n, then its right brother is at (n + 1). Similarly, ifthe right child
is at index n, then its left brother is at (n – 1).
The array representation is more ideal for the complete binary tree. The above figure isnot a
complete binary tree. Since there is no left child for node C, i.e., A[5] is vacant. Eventhough
memory is allocated for A[5] it is not used, so wasted unnecessarily.
B C
D F
E
If a node has left or/and right node, corresponding LChild or RChild is assigned to Null. The
node structure can be logically represented in C/C++ as:
struct Node{
Data type info;
struct Node*LChild;
struct Node*RChild;
}; struct Node*NODE;
ALGORITHM
Node is the current position of the tree, which is in under consideration. Loc isthe place where
node is to be replaced. Data is the information of node to be deleted.
1. Find the location Node of the Data to be deleted.
2. If (Node = NULL)
(a) Display “Data is not in tree”
(b) Exit
3. If(Node→ Lchild = NULL)
(a) Loc = Node
(b) Node = Node → RChild
4. If(Node → RChild= =NULL)
(a) Loc = Node
(b) Node = Node → LChild
5. If((Node → Lchild not equal to NULL) && (Node → Rchild not equal to NULL))
(a) Loc = Node → RChild
6. While(Loc → Lchild not equal to NULL)
(a) Loc = Loc→ Lchild
7. Loc → Lchild = Node→ Lchild
8. Loc → RChild= Node → RChild
9. Exit
Traversing Binary Trees Recursively
Tree traversal is one of the most common operations performed on tree data structures.
It is a way in which each node in the tree is visited exactly once in a systematicmanner. There are
three standard ways of traversing a binary tree. They are:
1. Pre Order Traversal (Node-left-right)
2. In order Traversal (Left-node-right)
3. Post Order Traversal (Left-right-node)
Pre Orders Traversal Recursively
To traverse a non-empty binary tree in pre order following steps one to be processed
1. Visit the root node
2. Traverse the left sub tree in preorder
3. Traverse the right sub tree in preorder
That is, in preorder traversal, the root node is visited (or processed) first, beforetraveling through
left and right sub trees recursively. It can be implemented in C/C++ functionas below:
void preorder (Node * Root)
{
If (Root != NULL)
{
cout<<Root → Info;
preorder(Root → L child);
preorder(Root → R child);
}
}