Trees:
Introduction
Basic terminology,
General trees,
Representation of a general tree,
Types of Trees,
Binary Tree,
Properties,
Binary Tree Abstract Data Type,
Realization of a Binary Tree,
Insertion of a Node in Binary Tree,
Binary Tree Traversal (recursive traversals),
Formation of binary tree from its traversals,
Binary Search Tree:
Inserting a node,
Searching for a key,
Deleting a node,
Binary Tree and Binary Search Tree,
Applications of Binary Trees:
The data structure is a specialized method to organize and store data in the
computer to be used more effectively.
There are various types of data structures, such as stack, linked list, and
queue, arranged in sequential order.
For example, a linked data structure consists of a set of nodes that are linked
together by links or points.
Similarly, the tree data structure is a kind of hierarchal data arranged in a
tree-like structure.
It consists of a central node, structural nodes, and sub nodes, which are
connected via edges.
We can also say that tree data structure has roots, branches, and leaves
connected with one another.
A tree is non-linear and a hierarchical data structure consisting of a collection
of nodes such that each node of the tree stores a value, a list of references
to nodes (the “children”).
Recursive Definition: : A tree consists of a root, and zero or more subtrees
T1, T2, … , Tk such that there is an edge from the root of the tree to the root of
each subtree.
Basic Terminology In Tree Data Structure:
Parent Node: The node which is a predecessor of a node is called the
parent node of that node. {2} is the parent node of {6, 7}.
Child Node: The node which is the immediate successor of a node is
called the child node of that node. Examples: {6, 7} are the child nodes of
{2}.
Root Node: The topmost node of a tree or the node which does not have
any parent node is called the root node. {1} is the root node of the tree.
A non-empty tree must contain exactly one root node and exactly one
path from the root to all other nodes of the tree.
Degree of a Node: The total count of subtrees attached to that node is
called the degree of the node. The degree of a leaf node must be 0.
The degree of a tree is the maximum degree of a node among all the
nodes in the tree.
Leaf Node or External Node: The nodes which do not have any child
nodes are called leaf nodes. {6, 14, 8, 9, 15, 16, 4, 11, 12, 17, 18, 19} are
the leaf nodes of the tree.
Ancestor of a Node: Any predecessor nodes on the path of the root to
that node are called Ancestors of that node. {1, 2} are the parent nodes of
the node {7}
Descendant: Any successor node on the path from the leaf node to that
node. {7, 14} are the descendants of the node. {2}.
Sibling: Children of the same parent node are called siblings. {8, 9,
10} are called siblings.
Depth of a node: The count of edges from the root to the node. Depth of
node {14} is 3.
Height of a node: The number of edges on the longest path from that
node to a leaf. Height of node {3} is 2.
Height of a tree: The height of a tree is the height of the root node i.e the
count of edges from the root to the deepest node. The height of the above
tree is 3.
Level of a node: The count of edges on the path from the root node to
that node. The root node has level 0.
Internal node: A node with at least one child is called Internal Node.
Neighbour of a Node: Parent or child nodes of that node are called
neighbors of that node.
Subtree: Any node of the tree along with its descendant
.
Let’s consider another example of tree data structure.
Here,
Node A is the root node
B is the parent of D and E
D and E are the siblings
D, E, and F are the leaf nodes
A and B are the ancestors of E
A tree data structure is a non-linear data structure because it does not store in a
sequential manner.
It is a hierarchical structure as elements in a Tree are arranged in multiple levels.
In the Tree data structure, the topmost node is known as a root node.
Each node contains some data, and data can be of any type.
Tree Terminology
A tree is a hierarchical data structure defined as a collection of nodes.
Nodes represent value and nodes are connected by edges. A tree has
the following properties:
1. The tree has one node called root. The tree originates from this, and
hence it does not have any parent.
2. Each node has one parent only but can have multiple children.
3. Each node is connected to its children via edge.
Types of Trees
Types of trees depend on the number of children a node has. There are
two major tree types:
General Tree: A tree in which there is no restriction on the
number of children a node has, is called a General tree.
Examples are Family tree, Folder Structure.
Binary Tree: In a Binary tree, every node can have at most 2
children, left and right. In diagram below, B & D are left
children and C, E & F are right children.
Binary trees are further divided into many types based on its
application.
Full Binary Tree: If every node in a tree has either 0 or 2
children, then the tree is called a full tree. The tree in the
above diagram is not a full binary tree as node C has only the
right child.
Perfect Binary tree: It is a binary tree in which all interior nodes
have two children and all leaves have the same depth or same
level.
Balanced Tree: If the height of the left and right subtree at any
node differs at most by 1, then the tree is called a balanced
tree.
Binary Search Tree: It is a binary tree with binary search
property. Binary search property states that the value or key of
the left node is less than its parent and value or key of right
node is greater than its parent. And this is true for all nodes
Binary search trees are used in various searching and sorting
algorithms. There are many variants of binary search trees like AVL
tree, B-Tree, Red-black tree, etc.
General Tree
In the data structure, General tree is a tree in which each node can have
either zero or many child nodes.
It can not be empty. In general tree, there is no limitation on the degree of a
node.
The topmost node of a general tree is called the root node. There are many
subtrees in a general tree.
The subtree of a general tree is unordered because the nodes of the general
tree can not be ordered according to specific criteria.
In a general tree, each node has in-degree(number of parent nodes) one and
maximum out-degree(number of child nodes) n.
Example of General Tree
Binary Tree:
A binary tree is the specialized version of the General tree. A binary tree is a
tree in which each node can have at most two nodes.
In a binary tree, there is a limitation on the degree of a node because the
nodes in a binary tree can’t have more than two child node(or degree two).
The topmost node of a binary tree is called root node and there are mainly
two subtrees one is left-subtree and another is right-subtree.
Unlike the general tree, the binary tree can be empty.
Unlike the general tree, the subtree of a binary tree is ordered because the
nodes of a binary tree can be ordered according to specific criteria.
General tree Binary tree
General tree is a tree in
which each node can have Whereas in binary tree, each node can have
many children or nodes. at most two nodes.
The subtree of a general tree
do not hold the ordered While the subtree of binary tree hold the
property. ordered property.
In data structure, a general
tree can not be empty. While it can be empty.
In general tree, a node can
have at most n(number of While in binary tree, a node can have at
child nodes) nodes. most 2(number of child nodes) nodes.
While in binary tree, there is limitation on the
In general tree, there is no degree of a node because the nodes in a
limitation on the degree of a binary tree can’t have more than two child
node. node.
In general tree, there is
either zero subtree or many While in binary tree, there are mainly two
subtree. subtree: Left-subtree and Right-subtree.
Types of Binary Tree
Strictly binary tree
Strictly binary tree is defined as a binary tree where all the nodes will have either
zero or two children. It does not include one child in any node.
Skew tree
A skew tree is defined as a binary tree in which every node except the leaf has only
one child node.
There are two types of skew tree, i.e. left skewed binary tree and right skewed binary
tree.
Left skewed binary tree
A left skew tree has node associated with only the left child. It is a binary tree
contains only left subtrees.
Right skewed binary tree
A right skew tree has node associated with only the right child. It is a binary tree
contains only right subtrees.
Full binary tree or proper binary tree
A binary tree is defined as a full binary tree if all leaves are at the same level and
every non leaf node has exactly two children and it should consist of highest possible
number of nodes in all levels.
A full binary tree of height h has maximum 2h+1 – 1 nodes.
Complete binary tree
Every non leaf node has exactly two children but all leaves are not necessary to
belong at the same level.
A complete binary tree is defined as one where all levels have the highest number of
nodes except the last level.
The last level elements should be filled from left to right direction.
Almost complete binary tree
An almost complete binary tree is defined as a tree in which each node that has a
right child also has a left child.
Having a left child does not need a node to have a right child
Binary Tree Abstract Data Type,
Realization of a Binary Tree,
Definition of Binary Tree and few properties of Binary trees
Three standard ways to traverse a binary tree T with
root R are preorder, inorder, postorder, are as follows:
Preorder:
1. Process root R.
2. Travel in the left subtree of R in preorder.
3. Travel in the right subtree of R in preorder.
Inorder:
1. Travel in left subtree of R in inorder.
2. Process root R.
3. Travel in right subtree of R in inorder.
Postorder:
1. Travel in left subtree of R in postorder.
2. Travel in right subtree of R in postorder.
3. Process root R.
The three algorithm are sometimes called , respectively , the node-left-
right(NLR) traversal ,the left-node-right(LNR) traversal and the left-right-
root(LRN) traversal.
#include<iostream>
#include<stdlib.h>
using namespace std;
class Node{
public:
int data;
Node *left;
Node *right;
};
Node *root=NULL;
Node *temp;
void insertElements();
void preorder(Node *);
void inorder(Node *);
void postorder(Node *);
int main()
int ch;
while(1)
cout<<"\n 1.insert Elements \n 2.preorder \n 3.inorder \n 4.postorder \
n5.exit";
cout<<"\n enter ur choice ";
cin>>ch;
switch(ch)
case 1:insertElements();break;
case 2:preorder(root);break;
case 3:inorder(root);break;
case 4:postorder(root);break;
case 5:exit(1);break;
default:cout<<"invalid operation";
}
}
void insertElements()
Node *nc;
Node *pNode;
Node *temp;
int v;
cout<<"\n enter the value ";
cin>>v;
temp=new Node;
temp->data=v;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
root=temp;
else
{
nc=root;
while(nc!=NULL)
pNode=nc;
if(v<nc->data)
nc=nc->left;
else
nc=nc->right;
if(v<pNode->data)
pNode->left=temp;
else
pNode->right=temp;
}
void preorder(Node *temp)
if(temp!=NULL)
cout<<" "<<temp->data;
preorder(temp->left);
preorder(temp->right);
void inorder(Node *temp)
if(temp!=NULL)
inorder(temp->left);
cout<<" "<<temp->data;
inorder(temp->right);
}
void postorder(Node *temp)
if(temp!=NULL)
postorder(temp->left);
postorder(temp->right);
cout<<" "<<temp->data;
Output:
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 1
enter the value 5
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 1
enter the value 1
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 1
enter the value 3
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 1
enter the value 2
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 1
enter the value 4
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 2
5 1 3 2 4
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 3
1 2 3 4 5
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 4
2 4 3 1 5
1.insert Elements
2.preorder
3.inorder
4.postorder
5.exit
enter your choice 5
Binary Search Tree
Binary search tree is a data structure that quickly allows us to maintain a
sorted list of numbers.
It is called a binary tree because each tree node has a maximum of two
children.
It is called a search tree because it can be used to search for the presence
of a number in O(log(n)) time.
The properties that separate a binary search tree from a regular binary
tree is
1. All nodes of left subtree are less than the root node
2. All nodes of right subtree are more than the root node
3. Both subtrees of each node are also BSTs i.e. they have the above two
properties
Search Operation
The algorithm depends on the property of BST that if each left subtree has
values below root and each right subtree has values above the root.
If the value is below the root, we can say for sure that the value is not in the
right subtree; we need to only search in the left subtree and if the value is
above the root, we can say for sure that the value is not in the left subtree;
we need to only search in the right subtree.
Algorithm:
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
Let us try to visualize this with a diagram.
4 is found
If the value is found, we return the value so that it gets propagated in each
recursion step
If you might have noticed, we have called return search(struct node*) four
times. When we return either the new node or NULL, the value gets
returned again and again until search(root) returns the final result.
If the value is found in any of the subtrees, it is propagated up so that in the end it is returned,
otherwise null is returned
If the value is not found, we eventually reach the left or right child of a leaf
node which is NULL and it gets propagated and returned.
Insert Operation
Inserting a value in the correct position is similar to searching because we
try to maintain the rule that the left subtree is lesser than root and the right
subtree is larger than root.
We keep going to either right subtree or left subtree depending on the
value and when we reach a point left or right subtree is null, we put the new
node there.
Algorithm:
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
The algorithm isn't as simple as it looks. Let's try to visualize how we add a
number to an existing BST.
4<8 so, transverse through the left child of 8
4>3 so, transverse through the right child of 3
4<6 so, transverse through the left child of 6
Deletion Operation
There are three cases for deleting a node from a binary search tree.
Case I
In the first case, the node to be deleted is the leaf node. In such a case,
simply delete the node from the tree.
4 is to be deleted
Delete the node
Case II
In the second case, the node to be deleted lies has a single child node. In
such a case follow the steps below:
1. Replace that node with its child node.
2. Remove the child node from its original position.
6 is to be deleted
copy the value of its child to the node and delete the child
Final tree
Case III
In the third case, the node to be deleted has two children. In such a case
follow the steps below:
1. Get the inorder successor of that node.
2. Replace the node with the inorder successor.
3. Remove the inorder successor from its original position.
3 is to be deleted
Copy the value of the inorder successor (4) to the node
Delete the inorder successor
// Binary Search Tree operations in C++
#include <iostream>
using namespace std;
class Node {
public:
int key;
Node *left;
Node *right;
};
// Create a node
Node *newNode(int item) {
Node *temp;
temp->key = item;
temp->left = NULL;
temp->right = NULL;
return temp;
void inorder(Node *temp)
if(temp!=NULL)
inorder(temp->left);
cout<<" "<<temp->key;
inorder(temp->right);
// Insert a node
Node *insert(Node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);
// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
// Find the inorder successor
Node *minValueNode(Node *node) {
Node *current = node;
// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;
return current;
// Deleting a node
Node *deleteNode(Node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
// Find the node to be deleted
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// If the node is with only one child or no child
if (root->left == NULL) {
Node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
Node *temp = root->left;
free(root);
return temp;
// If the node has two children
Node *temp = minValueNode(root->right);
// Place the inorder successor in position of the node to be deleted
root->key = temp->key;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
return root;
}
// Driver code
int main() {
Node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
cout << "Inorder traversal: ";
inorder(root);
cout << "\nAfter deleting 10\n";
root = deleteNode(root, 10);
cout << "Inorder traversal: ";
inorder(root);