This document defines basic terminology related to trees, including definitions of a tree, node, degree, leaf nodes, children, siblings, level, height, and binary trees. It then discusses binary tree traversal methods including preorder, inorder, and postorder traversal. Finally, it covers implementation of binary trees and the different cases and approaches for deleting a node from a binary tree.
result management system report for college project
Basic Tree Terminology and Binary Tree Traversal
1. For More Visit: Https://www.ThesisScientist.com
Unit 5
Trees
Basic Terminology about Trees
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 and remaining nodes are partitioned into n>0 disjoint sets S1....Sn where each of these sets is
a tree. S1...Sn are called the subtrees of the root. If we look at Fig 1.1 we see that the root of the tree is Alan
. Tree has three subtrees whose roots are Joe, John and Johnson.
Node: A node stands for the item of information plus the branches to other items. Consider the Tree of
Fig 1.2 it has 13 nodes.
Degree : The number of subtrees of a node is called its degree. In Fig 1.2 the degree of node 1
is 3.
Leaf or Terminal Nodes : Nodes that have degree zero is called leaf or Terminal nodes. In Fig 1.2,
6,7,9,10,11,12,13 are 'Leaf' nodes, other nodes of Tree are called 'NonLeaf' nodes.
Children : The roots of the subtrees of a node I are called the children of node I. I is the 'parent' of its
children.
AL A N
l l l
JO E
l l l
JO H N JO H N S O N
l
PET E RM A R Y C H R ISAL E C
AL A N
l l l
JO E
l l l
JO H N JO H N S O N
l
PET E RM A R Y C H R ISAL E C
2. For More Visit: Https://www.ThesisScientist.com
Siblings : Children of the same parent are called 'Siblings' 8,9,10, are Siblings so as 11
and12.
Level : The 'level' of a node is defined by initially letting the root be at level 1. If a node is at level l, then
its children are at level l+1.
Height or Depth : The height or depth of a Tree is defined as the maximum level of any node in the Tree.
1
3
7
2
5
11 12
6
4
10
8 9
13
LEVEL
1
2
3
4
11
33
77
22
55
1111 1212
66
44
10
88 99
1313
LEVEL
1
2
3
4
Figure 1.2: An Example Tree
Binary Trees
A Binary Tree is a finite set of elements that is either empty or is portitioned into three disjoint subsets. The
first subset contains a single element called the Root of the tree. The other two subsets are themselves
Binary Trees, called the left and right subtrees of the original tree. A left or right subtree can be empty Fig
1.3 shows a typical Binary Tree. A node of a Binary Tree can have at most two Branches.
If A is the root of a Binary Tree and B,C are the roots of its left and right subtrees respectively then A is
said to be the father of B, C and B are called Left and Right Sons respectively. If every Non Leaf node in a
Binary Tree has Non Empty Left and Right subtrees the tree is termed as Strictly Binary Tree. The Binary
Tree of Fig 1.4 is Strictly Binary Tree. A Strictly Binary Tree with n leaves always contains 2n-1 nodes.
A Complete Binary Tree is a Strictly Binary Tree of depth 'd' whose all leaves are at level d. Fig 1.5
represents complete Binary tree.
1
2
4
8
5
3
7
6
109
11
22
44
88
55
33
7
66
101099
Figure 1.3 : A Binary Tree
3. For More Visit: Https://www.ThesisScientist.com
1
2 3
54
6 7
11
22 33
544
66 77
Figure 1.4 : Strictly Binary Tree
1
2
4
8
5
3
76
9 10 11 12 13 14 15
11
22
44
88
55
33
766
99 10 11 12 13 14 15
Figure 1.5 : The Complete Binary Tree
Binary Tree Traversal
The traversal of a Binary Tree is to visit each node in the tree exactly once. A full traversal produces a
linear order for the information in a tree. When traversing a binary tree we want to treat each node and its
subtrees in the same fashion. If we let L,D,R stand for moving left, printing the data, and moving right
when at a node then there are six possible combinations of traversal: LDR, LRD, DLR,DRL,RDL andRDL.
If convention is adopted then we traverse left before right then only three traversals remain i.e.
LDR,LRD,DLR. To these, the names INORDER, POSTORDER and PREORDER are assigned because
there is a natural correspondence between these traversals and producing the INFIX, POSTFIX and
PREFIX forms of an expression.
Preorder
To traverse a non empty tree in preorder we perform the following three operations:
i) Visit the root
4. For More Visit: Https://www.ThesisScientist.com
ii) Traverse the left Subtree in Preorder
iii) Traverse the right Subtree in Preorder.
Inorder
i) Traverse the left Subtree in Inorder
ii) Visit the root
iii) Traverse the right Subtree in Inorder.
Postorder
i) Traverse the Left Subtree in Postorder.
ii) Traverse the right Subtree in Postorder.
iii) Visit the root.
Example 1
Consider the Tree of Fig 1.6.
l In Inorder Traversal the order will be
A/B**C*D+E
which is Infix form of the expression.
l In Postorder Traversal the order will be:
ABC**/D*E+
which is postfix form of the Traversal
+
E*
DI
**A
CB
+
EE*
DDI
**A
CB
Figure 1.6: Binary Tree
l In preorder Traversal the order will be :
+*/A**BCDE
5. For More Visit: Https://www.ThesisScientist.com
which is the prefix form of the expression.
Implementation of Binary Trees
A node (As in Fig. 1.7) of a binary tree can be represented as:
Left Data Right
;
Figure 1.7:
Deletion from a Binary Tree
One of the important operations associated with any Data Structure is deletion of the specified data item.
Assuming that we will pass the specified data item, we wish to delete from the Binary Tree to the delete
function.
There are four possible cases:
a) No node contains the specified data
b) The node containing the data has no children
c) The node c ontaining the data has exactly one child
d) The node containing the data has two children.
In case (a) we will only need to print the message that the data item is not present in the tree.
Fig 1.9 represents the case (b). We have to delete the node containing data 4. The node is leaf node and it
has no children, it can be deleted by making its parent pointing to NULL.Which of the two links(Left or
Right) of its parent node is set to NULL depends upon whether the node being deleted is a left child or a
right child of its parent.
8
96
74
a) Before Deletion
*
8
96
7
b) After Deletion
NULL
8
96
74
a) Before Deletion
*
8
96
7
b) After Deletion
NULL
6. For More Visit: Https://www.ThesisScientist.com
Figure 1.9
Fig 1.10 represents case (c) in which node to be deleted has one child; we have to adjust the pointer of the
parent of the node to be deleted such that after deletion it points to the child of the node being deleted. In
Fig 1.10(a) node 1 with data 1 is to be deleted. After deletion left pointers of node 6 is made to point to
child of node 1 i.e. 4 and now structure would be as shown in
Fig 1.10(b).
8
96
7
1
a) Before Deletion
*
8
96
74
b) After Deletion
4
8
96
77
1
a) Before Deletion
*
8
96
74
b) After Deletion
4
Figure 1.10
In case (d) the value is replaced by the smallest value in the right subtree or the largest key value in the left
subtree; Subsequently empty node is deleted recursively, Fig 16.11 represents this case. If the node with
data value 6 is to be deleted then first its value is replaced by smallest value in its right subtree. Adjustment
is done for the parent of this node (with smallest value) according to case (b) or (c), then the node is
deleted.