Chapter 6 ALGO
Chapter 6 ALGO
Chapter Six
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
Depth of H 2
Height of a tree: depth of the deepest node. 3
Sub tree: a tree consisting of a node and its descendants.
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.
2
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
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 an array of memory
can be accessed sequentially.
Suppose a binary tree T of depth d. Then at most 2d – 1 node 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 tree of depth 3. Then SIZE = 23 – 1 = 7. Then the array A[7] is declared to hold the nodes.
3
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
The array representation of the binary tree is shown in Figure. To perform any operation often
we have to identify the father, the left child and right child of an arbitrary node.
1. The father of a node having index n can be obtained by (n – 1)/2. For example to find the
father of D, where array index n = 3. Then the father nodes index can be obtained
= (n – 1)/2
= 3 – 1/2
= 2/2
=1 i.e. A[1] is the father D, which is B.
2. The left child of a node having index n can be obtained by (2n+1). For example to find 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, if the 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 is not a
complete binary tree. Since there is no left child for node C, i.e., A[5] is vacant. Even though
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;
Inserting a Node
Algorithm
Newnode is a pointer variable to hold the address of the newly created node. Data is the
information to be pushed.
1. Input the Data to be pushed and Root node of the tree.
2. Newnode = Create a New Node.
3. If (Root== NULL)
a) Root= Newnode
4. Else If (Data < Root Info)
(a) Root = Root Lchild
(b) GoTo Step 4
5. Else If (Data > Root Info)
(a) Root = Root Rchild
(b) GoTo Step 4
6. If (Data < Root Info)
(a) ROOTLChild = Newnode
7. Else If (Data > Root Info)
(a) Root RChild = Newnode
8. Else
(a) Display (“DUPLICATE NODE”)
(b) EXIT
9. NewnodeInfo = Data
10. Newnode LChild = NULL
11. Newnode RChild = NULL
12. EXIT
Searching a Node
Algorithm
1. Input the Data to be searched and assign the address of the root node to ROOT.
2. If (Data == Root → Info)
(a) Display “The Data exist in the tree”
(b) GoTo Step 6
3. If (Root == NULL)
(a) Display “The DATA does not exist”
(b) GoTo Step 6
4. If(Data > Root →Info)
(a) Root = Root →RChild
(b) GoTo Step 2
5. If(Data < Root →Info)
(a) Root = Root →Lchild
6
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
ALGORITHM
Node is the current position of the tree, which is in under consideration. Loc is the 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 systematic manner. 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)
7
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
8
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
9
Complied By Mr. Abraham W