Non-Broom Trees and Subtree Concepts
Non-Broom Trees and Subtree Concepts
Tree is a non-linear data structure which organizes data in hierarchical structure and this
is a recursive definition.
A tree data structure can also be defined as follows...
In a tree data structure, if we have N number of nodes then we can have a maximum of N-
1 number of links.
EXAMPLE:
A tree is a non-linear abstract data type with a hierarchy-based structure. It consists of nodes
(where the data is stored) that are connected via links. The tree data structure stems from a
single node called a root node and has subtrees connected to the root.
5.1.1 Explain the terminology related to Tree (Root, Edge, Parent,
Child, Siblings, Leaf, Internal nodes, Degree, Level, Height, Depth,
Path, Sub tree, Forest).
Terminology
In a tree data structure, we use the following terminology...
1. Root
In a tree data structure, the first node is called as Root Node. Every tree must have a root node.
We can say that the root node is the origin of the tree data structure. In any tree, there must be
only one root node. We never have multiple root nodes in a tree.
2. Edge
In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree
with 'N' number of nodes there will be a maximum of 'N-1' number of edges.
3. Parent
In a tree data structure, the node which is a predecessor of any node is called as PARENT NODE.
In simple words, the node which has a branch from it to any other node is called a parent node.
Parent node can also be defined as "The node which has child / children".
4. Child
In a tree data structure, the node which is descendant of any node is called as CHILD Node. In
simple words, the node which has a link from its parent node is called as child node. In a tree, any
parent node can have any number of child nodes. In a tree, all the nodes except root are child
nodes
5. Siblings
In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple
words, the nodes with the same parent are called Sibling nodes.
6. Leaf
In a tree data structure, the node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a
node with no child. In a tree, leaf node is also called as 'Terminal' node.
7. Internal Nodes
In a tree data structure, the node which has atleast one child is called as INTERNAL Node. In
simple words, an internal node is a node with atleast one child.
In a tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root node
is also said to be Internal Node if the tree has more than one node. Internal nodes are also called
as 'Non-Terminal' nodes.
8. Degree
In a tree data structure, the total number of children of a node is called as DEGREE of that Node.
In simple words, the Degree of a node is total number of children it has. The highest degree of a
node among all the nodes in a tree is called as 'Degree of Tree'
9. Level
In a tree data structure, the root node is said to be at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In simple
words, in a tree each step from top to bottom is called as a Level and the Level count starts with
'0' and incremented by one at each level (Step).
10. Height
In a tree data structure, the total number of edges from leaf node to a particular node in the longest
path is called as HEIGHT of that Node. In a tree, height of the root node is said to be height of
the tree. In a tree, height of all leaf nodes is '0'.
11. Depth
In a tree data structure, the total number of egdes from root node to a particular node is called
as DEPTH of that Node. In a tree, the total number of edges from root node to a leaf node in the
longest path is said to be Depth of the tree. In simple words, the highest depth of any leaf node
in a tree is said to be depth of that tree. In a tree, depth of the root node is '0'.
12. Path
In a tree data structure, the sequence of Nodes and Edges from one node to another node is called
as PATH between that two Nodes. Length of a Path is total number of nodes in that path. In below
example the path A - B - E - J has length 4.
13. Sub Tree
In a tree data structure, each child from a node forms a subtree recursively. Every child node will
form a subtree on its parent node.
13. Forest:
A collection of disjoint trees is called a forest.
Tree Representations
A tree data structure can be represented in two methods. Those methods are as follows...
1. List Representation
1. List Representation
In this representation, we use two types of nodes one for representing the node with data called
'data node' and another for representing only references called 'reference node'. We start with a
'data node' from the root node in the tree. Then it is linked to an internal node through a 'reference
node' which is further linked to any other node directly. This process repeats for all the nodes in
the tree.
The above example tree can be represented using List representation as follows...
2. Left Child - Right Sibling Representation
In this representation, we use a list with one type of node which consists of three fields namely
Data field, Left child reference field and Right sibling reference field. Data field stores the actual
value of a node, left reference field stores the address of the left child and right reference field
stores the address of the right sibling node. Graphical representation of that node is as follows...
In this representation, every node's data field stores the actual value of that node. If that node has
left a child, then left reference field stores the address of that left child node otherwise stores NULL.
If that node has the right sibling, then right reference field stores the address of right sibling node
otherwise stores NULL.
The above example tree can be represented using Left Child - Right Sibling representation as
follows...
5.2 Define Binary Tree
In a normal tree, every node can have any number of children. A binary tree is a special type of
tree data structure in which every node can have a maximum of 2 children. One is known as a
A tree in which every node can have a maximum of two children is called Binary Tree.
In a binary tree, every node can have either 0 children or 1 child or 2 children but not more than 2
children.
Example
In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every
node should have exactly two children or none. That means every internal node must have exactly
A binary tree in which every node has either two or zero number of children is called
Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree
Strictly binary tree data structure is used to represent mathematical expressions.
Example
In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every
node should have exactly two children or none and in complete binary tree all the nodes must have
exactly two children and at every level of complete binary tree there must be 2level number of nodes.
For example at level 2 there must be 22 = 4 nodes and at level 3 there must be 23 = 8 nodes.
A binary tree in which every internal node has exactly two children and all leaf nodes are
A binary tree can be converted into Full Binary tree by adding dummy nodes to existing nodes
wherever required.
The full binary tree obtained by adding dummy nodes to a binary tree is called as
In above figure, a normal binary tree is converted into full binary tree by adding dummy nodes (In
pink colour).
5.3 Write the differences between General Tree and Binary Tree
General tree Binary tree
Note: If the parent node has only the right child in the general tree then it
becomes the rightmost child node of the last node following the parent
node in the binary tree. In the above example, if node B has the right child
node L then in binary tree representation L would be the right child of
node D.
5.5 Explain the linear representation and linked list representation of
a Binary Tree
A binary tree data structure is represented using two methods. Those methods are as follows...
1. Array Representation
In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a
binary tree.
To represent a binary tree of depth 'n' using array representation, we need one dimensional array
follows...
In linked representation, Tree is maintained in memory by means of three parallel arrays, INFO,
LEFT, and RIGHT, and a pointer variable ROOT.
Each node N of T will correspond to a location K such that INFO[K] contains data at node
N. LEFT[K] contains the location of the left child of node N and RIGHT[K] contains the location
of right child of node N.
ROOT will contain the location of root R of Tree. If any subtree is empty, the corresponding
pointer will contain a null value. If the tree T itself is empty, then ROOT will contain a null value
5.6 Define Binary Search Tree
To enhance the performance of binary tree, we use a special type of binary tree known as Binary
Search Tree. Binary search tree mainly focuses on the search operation in a binary tree. Binary
search tree can be defined as follows...
Binary Search Tree is a binary tree in which every node contains only smaller values in its
left subtree and only larger values in its right subtree.
In a binary search tree, all the nodes in the left subtree of any node contains smaller values and
all the nodes in the right subtree of any node contains larger values as shown in the following
figure...
Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node contains nodes
with smaller values and right subtree of every node contains larger values.
Every binary search tree is a binary tree but every binary tree need not to be binary search
tree.
In the above figure, we can observe that the root node is 40, and all the nodes of the left
subtree are smaller than the root node, and all the nodes of the right subtree are greater
than the root node.
Similarly, we can see the left child of root node is greater than its left child and smaller
than its right child. So, it also satisfies the property of binary search tree. Therefore, we
can say that the tree in the above image is a binary search tree.
Suppose if we change the value of node 35 to 55 in the above tree, check whether the
tree will be binary search tree or not.
In the above tree, the value of root node is 40, which is greater than its left child 30 but
smaller than right child of 30, i.e., 55. So, the above tree does not satisfy the property of
Binary search tree. Therefore, the above tree is not a binary search tree.
1. Search
2. Insertion
3. Deletion
Example
Construct a Binary Search Tree by inserting the following sequence of numbers...
10,12,5,4,20,8,7,15 and 13
Above elements are inserted into a Binary Search Tree as follows...
O(n) as it only
depends on the O(n) as it only depends on the
Space number of nodes in number of nodes in the tree.
Complexity the tree.
When we wanted to display a binary tree, we need to follow some order in which all the nodes of
that binary tree must be displayed. In any binary tree, displaying order of nodes depends on the
traversal method.
Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal.
Tree Traversal refers to the process of visiting or accessing each node of the tree exactly once in a
certain order. Tree traversal algorithms help us to visit and process all the nodes of the tree. Since
tree is not a linear data structure, there are multiple nodes which we can visit after visiting a certain
node. There are multiple tree traversal techniques which decide the order in which the nodes of the
1. In - Order Traversal
Inorder Traversal:
Inorder traversal visits the node in the order: Left -> Root -> Right
I-D-J-B-F-A-G-K-C–H
Preorder Traversal:
Preorder traversal visits the node in the order: Root -> Left -> Right
Algorithm for Preorder Traversal:
Preorder(tree)
Visit the root.
Traverse the left subtree, i.e., call Preorder(left->subtree)
Traverse the right subtree, i.e., call Preorder(right->subtree)
In the above example of binary tree, first we visit root node 'A' then visit its left child 'B' which is a
root for D and F. So we visit B's left child 'D' and again D is a root for I and J. So we visit D's left
child 'I' which is the leftmost child. So next we go for visiting D's right child 'J'. With this we have
completed root, left and right parts of node D and root, left parts of node B. Next visit B's right
child 'F'. With this we have completed root and left parts of node A. So we go for A's right
child 'C' which is a root node for G and H. After visiting C, we go for its left child 'G' which is a root
for node K. So next we visit left of G, but it does not have left child so we go for G's right child 'K'.
With this, we have completed node C's root and left parts. Next visit C's right child 'H' which is the
rightmost child in the tree. So we stop the process.
That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-Order Traversal.
Pre-Order Traversal for above example binary tree is
A-B-D-I-J-F-C-G-K-H
Postorder Traversal:
Postorder traversal visits the node in the order: Left -> Right -> Root
I-J-D-F-B-K-G-H-C–A
1. Find the root node: Use the preorder traversal to find the root node.
2. Find the left and right subtrees: Use the inorder traversal to find the left and right
subtrees by finding the index of the root node of each subtree.
3. Recurse: Recurse down the left and right subtrees, repeating the process until you
find a single element in each subarray.
We recursively follow the above steps and get the following tree.
A
/ \
/ \
B C
/ \ /
/ \ /
D E F
5.10 Construct a Binary Tree using In-order and Post-order Traversals
1. Identify the root node: The last element in the postorder list is the root node.
2. Find the root node's position: Use the inorder list to find the position of the root node.
3. Partition the inorder list: Use the root node's position to split the inorder list into left
and right subtrees.
4. Recursively build the subtrees: Use the left and right partitions of the inorder and
postorder lists to recursively build the left and right subtrees.
5. Repeat: Repeat the process until all nodes are processed and the tree is
reconstructed
Examples:
Input:
in[] = {2, 1, 3}
post[] = {2, 3, 1}
Output: Root of below tree
1
/ \
2 3
Input:
in[] = {4, 8, 2, 5, 1, 6, 3, 7}
post[] = {8, 4, 5, 2, 6, 7, 3, 1}
Output: Root of below tree
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
5.11 Know the importance of Binary Search Trees over General Trees
A Binary search tree (BST) is a type of binary tree that’s more efficient than a general tree in several
ways:
Fast operations: BSTs are faster than arrays and linked lists for most operations including
searching, insertion , and deletion.
o First, we have to insert 45 into the tree as the root of the tree.
o Then, read the next element; if it is smaller than the root node, insert it as the root of
the left subtree, and move to the next element.
o Otherwise, if the element is larger than the root node, then insert it as the root of the
right subtree.
Step 1 - Insert 45.
As 15 is smaller than 45, so insert it as the root node of the left subtree.
As 79 is greater than 45, so insert it as the root node of the right subtree.
Step 4 - Insert 90.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of 79.
Step 7 - Insert 12.
12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right subtree
of 1
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree of 15.
50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left subtree of
55.
[Link]
[Link] the tree is empty,insert the first element as the root node of the [Link] following elements are
added as the leaf nodes.
[Link] an element is less than the root value,it is added into the left subtree as a leaf node.
[Link] an element is greater than the root value, it is added into the right subtree as a leaf node.
[Link] final leaf nodes of the tree point to NULL values as their child nodes.
[Link]
EXAMPLE:
The process of inserting a node into BST using an example.
The steps of searching a node in Binary Search tree are listed as follows -
1. First, compare the element to be searched with the root element of the tree.
2. If root is matched with the target element, then return the node's location.
3. If it is not matched, then check whether the item is less than the root element, if it is smaller
than the root element, then move to the left subtree.
4. If it is larger than the root element, then move to the right subtree.
5. Repeat the above procedure recursively until the match is found.
6. If the element is not found or not present in the tree, then return NULL.
Step1:
Step2:
Step3:
Algorithm to search an element in Binary search tree
1. Search (root, item)
2. Step 1 - if (item = root → data) or (root = NULL)
3. return root
4. else if (item < root → data)
5. return Search(root → left, item)
6. else
7. return Search(root → right, item)
8. END if
9. Step 2 - END
It is the simplest case to delete a node in BST. Here, we have to replace the leaf node
with NULL and simply free the allocated space.
We can see the process to delete a leaf node from BST in the below image. In below
image, suppose we have to delete node 90, as the node to be deleted is a leaf node, so
it will be replaced with NULL, and the allocated space will free.
In this case, we have to replace the target node with its child, and then delete the child
node. It means that after replacing the target node with its child node, the child node will
now contain the value to be deleted. So, we simply have to replace the child node with
NULL and free up the allocated space.
We can see the process of deleting a node with one child from BST in the below image.
In the below image, suppose we have to delete the node 79, as the node to be deleted
has only one child, so it will be replaced with its child 55.
So, the replaced node 79 will now be a leaf node that can be easily deleted.
This case of deleting a node in BST is a bit complex among other two cases. In such a
case, the steps to be followed are listed as follows -
The inorder successor is required when the right child of the node is not empty. We can
obtain the inorder successor by finding the minimum element in the right child of the node.
We can see the process of deleting a node with two children from BST in the below image.
In the below image, suppose we have to delete node 45 that is the root node, as the node
to be deleted has two children, so it will be replaced with its inorder successor. Now, node
45 will be at the leaf of the tree so that it can be deleted easily.
The complexity of the Binary Search tree
The time complexity for insertion, deletion, and searching operations in best case, average
case, and worst case.
1. Time Complexity
Operations Best case time Average case Worst case
complexity time time
complexity complexity
2. Space Complexity
Operations Space complexity
Insertion O(n)
Deletion O(n)
Search O(n)
o The space complexity of all operations of Binary search tree is O(n).
TREE TRAVERSAL
Traversal is a process to visit all the nodes of a tree and may print their
values too. Because, all nodes are connected via edges (links) we always
start from the root (head) node. That is, we cannot randomly access a node
in a tree. There are three ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and
later the right sub-tree. We should always remember that every node may
represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key
values in an ascending order.
D→B→E→A→F→C→G
Algorithm
Until all nodes are traversed −
Logic:
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree
and finally the right subtree.
We start from A, and following pre-order traversal, we first visit A itself and
then move to its left subtree B. B is also traversed pre-order. The process
goes on until all the nodes are visited. The output of pre-order traversal of
this tree will be −
A→B→D→E→C→F→G
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Example
Following are the implementations of this operation in various programming
languages –
Logic:
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the
left subtree, then the right subtree and finally the root node.
We start from A, and following pre-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. The output of post-
order traversal of this tree will be −
D→E→B→F→G→C→A
Algorithm
Until all nodes are traversed −
Example
Following are the implementations of this operation in various programming languages −
struct node {
int data;
struct node *right_child;
struct node *left_child;
};
return temp;
}
if (root == NULL)
return NULL;
if (x > root->data)
root->right_child = delete(root->right_child, x);
else if (x < root->data)
root->left_child = delete(root->left_child, x);
else {
if (root->left_child == NULL && root->right_child == NULL){
free(root);
return NULL;
}
else if (root->left_child == NULL || root->right_child == NULL){
struct node *temp;
if (root->left_child == NULL)
temp = root->right_child;
else
temp = root->left_child;
free(root);
return temp;
}
else {
struct node *temp = find_minimum(root->right_child);
root->data = temp->data;
root->right_child = delete(root->right_child, temp->data);
}
}
return root;
}
}
}
int main() {
struct node *root;
root = new_node(20);
clrscr();
insert(root, 5);
insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
return 0;
}
Output