0% found this document useful (0 votes)
3 views

Data Structure Unit-5-Tree-Data-Structure

This document provides an overview of tree data structures, explaining their hierarchical nature, key properties, and various types such as binary trees, full binary trees, and balanced binary trees. It discusses the implementation of trees using pointers and outlines their applications in organizing data, storing hierarchical information, and more. The document also includes programming examples for creating binary trees in C.

Uploaded by

aniruddh2573
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structure Unit-5-Tree-Data-Structure

This document provides an overview of tree data structures, explaining their hierarchical nature, key properties, and various types such as binary trees, full binary trees, and balanced binary trees. It discusses the implementation of trees using pointers and outlines their applications in organizing data, storing hierarchical information, and more. The document also includes programming examples for creating binary trees in C.

Uploaded by

aniruddh2573
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT-5

Tree Data Structure


We read the linear data structures like an array, linked list, stack and queue in which all
the elements are arranged in a sequential manner. The different data structures are used
for different kinds of data.
Some factors are considered for choosing the data structure:
o What type of data needs to be stored?: It might be a possibility that a certain
data structure can be the best fit for some kind of data.
o Cost of operations: If we want to minimize the cost for the operations for the most
frequently performed operations. For example, we have a simple list on which we
have to perform the search operation; then, we can create an array in which
elements are stored in sorted order to perform the binary search. The binary
search works very fast for the simple list as it divides the search space into half.
o Memory usage: Sometimes, we want a data structure that utilizes less memory.

A tree is also one of the data structures that represent hierarchical data. Suppose we want
to show the employees and their positions in the hierarchical form then it can be
represented as shown below:

The above tree shows the organization hierarchy of some company. In the above
structure, john is the CEO of the company, and John has two direct reports named
as Steve and Rohan. Steve has three direct reports named Lee, Bob, Ella where Steve is
a manager. Bob has two direct reports named Sal and Emma. Emma has two direct
reports named Tom and Raj. Tom has one direct report named Bill. This particular logical
structure is known as a Tree. Its structure is similar to the real tree, so it is named a Tree.
In this structure, the root is at the top, and its branches are moving in a downward
direction. Therefore, we can say that the Tree data structure is an efficient way of storing
the data in a hierarchical way.

Let's understand some key points of the Tree data structure.


o A tree data structure is defined as a collection of objects or entities known as nodes
that are linked together to represent or simulate hierarchy.
o 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.
o 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. In the above tree structure, the
node contains the name of the employee, so the type of data would be a string.
o Each node contains some data and the link or reference of other nodes that can
be called children.

In the above structure, each node is labeled with some number. Each arrow shown in the
above figure is known as a link between the two nodes.
o Root: The root node is the topmost node in the tree hierarchy. In other words, the
root node is the one that doesn't have any parent. In the above structure, node
numbered 1 is the root node of the tree. If a node is directly linked to some other
node, it would be called a parent-child relationship.
o Child node: If the node is a descendant of any node, then the node is known as a
child node.
o Parent: If the node contains any sub-node, then that node is said to be the parent
of that sub-node.
o Sibling: The nodes that have the same parent are known as siblings.
o Leaf Node:- The node of the tree, which doesn't have any child node, is called a
leaf node. A leaf node is the bottom-most node of the tree. There can be any
number of leaf nodes present in a general tree. Leaf nodes can also be called
external nodes.
o Internal nodes: A node has atleast one child node known as an internal
o Ancestor node:- An ancestor of a node is any predecessor node on a path from
the root to that node. The root node doesn't have any ancestors. In the tree shown
in the above image, nodes 1, 2, and 5 are the ancestors of node 10.
o Descendant: The immediate successor of the given node is known as a descendant
of a node. In the above figure, 10 is the descendant of node 5.

Properties of Tree data structure


o Recursive data structure: The tree is also known as a recursive data structure. A
tree can be defined as recursively because the distinguished node in a tree data
structure is known as a root node. The root node of the tree contains a link to all
the roots of its subtrees. The left subtree is shown in the yellow color in the below
figure, and the right subtree is shown in the red color. The left subtree can be
further split into subtrees shown in three different colors. Recursion means
reducing something in a self-similar manner. So, this recursive property of the tree
data structure is implemented in various applications.

o Number of edges: If there are n nodes, then there would n-1 edges. Each arrow
in the structure represents the link or path. Each node, except the root node, will
have atleast one incoming link known as an edge. There would be one link for the
parent-child relationship.
o Depth of node x: The depth of node x can be defined as the length of the path
from the root to the node x. One edge contributes one-unit length in the path. So,
the depth of node x can also be defined as the number of edges between the root
node and the node x. The root node has 0 depth.
o Height of node x: The height of node x can be defined as the longest path from
the node x to the leaf node.
Based on the properties of the Tree data structure, trees are classified into various
categories.
Implementation of Tree
The tree data structure can be created by creating the nodes dynamically with the help of
the pointers. The tree in the memory can be represented as shown below:

The above figure shows the representation of the tree data structure in the memory. In
the above structure, the node contains three fields. The second field stores the data; the
first field stores the address of the left child, and the third field stores the address of the
right child.

In programming, the structure of a node can be defined as:


struct node
{
int data;
struct node *left;
struct node *right;
}

The above structure can only be defined for the binary trees because the binary tree can
have utmost two children, and generic trees can have more than two children. The
structure of the node for generic trees would be different as compared to the binary tree.

Applications of trees
The following are the applications of trees:
o Storing naturally hierarchical data: Trees are used to store the data in the
hierarchical structure. For example, the file system. The file system stored on the
disc drive, the file and folder are in the form of the naturally hierarchical data and
stored in the form of trees.
o Organize data: It is used to organize data for efficient insertion, deletion and
searching. For example, a binary tree has a logN time for searching an element.
o Trie: It is a special kind of tree that is used to store the dictionary. It is a fast and
efficient way for dynamic spell checking.
o Heap: It is also a tree data structure implemented using arrays. It is used to
implement priority queues.
o B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures used to
implement indexing in databases.
o Routing table: The tree data structure is also used to store the data in routing
tables in the routers.

Types of Tree data structure


The following are the types of a tree data structure:
o General tree: The general tree is one of the types of tree data structure. In the
general tree, a node can have either 0 or maximum n number of nodes. There is
no restriction imposed on the degree of the node (the number of nodes that a
node can contain). The topmost node in a general tree is known as a root node.
The children of the parent node are known as subtrees.
o

o There can be n number of subtrees in a general tree. In the general tree, the
subtrees are unordered as the nodes in the subtree cannot be ordered.
Every non-empty tree has a downward edge, and these edges are connected to
the nodes known as child nodes. The root node is labeled with level 0. The nodes
that have the same parent are known as siblings.

o Binary tree: Here, binary name itself suggests two numbers, i.e., 0 and 1. In a binary
tree, each node in a tree can have utmost two child nodes. Here, utmost means
whether the node has 0 nodes, 1 node or 2 nodes.
Binary Search tree: Binary search tree is a non-linear data structure in which one
node is connected to n number of nodes. It is a node-based data structure. A node
can be represented in a binary search tree with three fields, i.e., data part, left-child,
and right-child. A node can be connected to the utmost two child nodes in a binary
search tree, so the node contains two pointers (left child and right child pointer).
Every node in the left subtree must contain a value less than the value of the root
node, and the value of each node in the right subtree must be bigger than the
value of the root node.

A node can be created with the help of a user-defined data type known as struct, as
shown below:

struct node
{
int data;
struct node *left;
struct node *right;
}
The above is the node structure with three fields: data field, the second field is the left
pointer of the node type, and the third field is the right pointer of the node type.

Binary Tree
The Binary tree means that the node can have maximum two children. Here, binary name
itself suggests that 'two'; therefore, each node can have either 0, 1 or 2 children.
Let's understand the binary tree through an example.
The above tree is a binary tree because each node contains the utmost two children. The
logical representation of the above tree is given below:

In the above tree, node 1 contains two pointers, i.e., left and a right pointer pointing to
the left and right node respectively. The node 2 contains both the nodes (left and right
node); therefore, it has two pointers (left and right). The nodes 3, 5 and 6 are the leaf
nodes, so all these nodes contain NULL pointer on both left and right parts.

Properties of Binary Tree


o At each level of i, the maximum number of nodes is 2i.
o The height of the tree is defined as the longest path from the root node to the leaf
node. The tree which is shown above has a height equal to 3. Therefore, the
maximum number of nodes at height 3 is equal to (1+2+4+8) = 15. In general, the
maximum number of nodes possible at height h is (20 + 21 + 22+….2h) = 2h+1 -1.
o The minimum number of nodes possible at height h is equal to h+1.
o If the number of nodes is minimum, then the height of the tree would be maximum.
Conversely, if the number of nodes is maximum, then the height of the tree would
be minimum.
If there are 'n' number of nodes in the binary tree.
The minimum height can be computed as:
As we know that,
n = 2h+1 -1
n+1 = 2h+1
Taking log on both the sides,
log2(n+1) = log2(2h+1)
log2(n+1) = h+1
h = log2(n+1) - 1

The maximum height can be computed as:


As we know that,
n = h+1
h= n-1

Types of Binary Tree


There are four types of Binary tree:
o Full/ proper/ strict Binary tree
o Complete Binary tree
o Perfect Binary tree
o Degenerate Binary tree
o Balanced Binary tree

1. Full/ proper/ strict Binary tree


The full binary tree is also known as a strict binary tree. The tree can only be considered
as the full binary tree if each node must contain either 0 or 2 children. The full binary tree
can also be defined as the tree in which each node must contain 2 children except the leaf
nodes.
Let's look at the simple example of the Full Binary tree.

In the above tree, we can observe that each node is either containing zero or two children;
therefore, it is a Full Binary tree.
Properties of Full Binary Tree
o The number of leaf nodes is equal to the number of internal nodes plus 1. In the
above example, the number of internal nodes is 5; therefore, the number of leaf
nodes is equal to 6.
o The maximum number of nodes is the same as the number of nodes in the binary
tree, i.e., 2h+1 -1.
o The minimum number of nodes in the full binary tree is 2*h-1.
o The minimum height of the full binary tree is log2(n+1) - 1.
o The maximum height of the full binary tree can be computed as:
n= 2*h - 1
n+1 = 2*h
h = n+1/2

Complete Binary Tree


The complete binary tree is a tree in which all the nodes are completely filled except the
last level. In the last level, all the nodes must be as left as possible. In a complete binary
tree, the nodes should be added from the left.
Let's create a complete binary tree.

The above tree is a complete binary tree because all the nodes are completely filled, and
all the nodes in the last level are added at the left first.

Properties of Complete Binary Tree


o The maximum number of nodes in complete binary tree is 2h+1 - 1.
o The minimum number of nodes in complete binary tree is 2h.
o The minimum height of a complete binary tree is log2(n+1) - 1.
o The maximum height of a complete binary tree is

Perfect Binary Tree


A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf
nodes are at the same level.
Let's look at a simple example of a perfect binary tree.
The below tree is not a perfect binary tree because all the leaf nodes are not at the same
level.

Note: All the perfect binary trees are the complete binary trees as well as the full binary tree, but vice
versa is not true, i.e., all complete binary trees and full binary trees are the perfect binary trees.

Degenerate Binary Tree


The degenerate binary tree is a tree in which all the internal nodes have only one children.
Let's understand the Degenerate binary tree through examples.
The above tree is a degenerate binary tree because all the nodes have only one child. It is
also known as a right-skewed tree as all the nodes have a right child only.

The above tree is also a degenerate binary tree because all the nodes have only one child.
It is also known as a left-skewed tree as all the nodes have a left child only.

Balanced Binary Tree


The balanced binary tree is a tree in which both the left and right trees differ by atmost 1.
For example, AVL and Red-Black trees are balanced binary tree.
Let's understand the balanced binary tree through examples.
The above tree is a balanced binary tree because the difference between the left subtree
and right subtree is zero.

The above tree is not a balanced binary tree because the difference between the left
subtree and the right subtree is greater than 1.

Binary Tree Implementation


A Binary tree is implemented with the help of pointers. The first node in the tree is
represented by the root pointer. Each node in the tree consists of three parts, i.e., data,
left pointer and right pointer. To create a binary tree, we first need to create the node. We
will create the node of user-defined as shown below:
struct node
{
int data,
struct node *left, *right;
}
In the above structure, data is the value, left pointer contains the address of the left
node, and right pointer contains the address of the right node.

Binary Tree program in C


#include<stdio.h>
struct node
{
int data;
struct node *left, *right;
}
void main()
{
struct node *root;
root = create();
}
struct node *create()
{
struct node *temp;
int data;
temp = (struct node *)malloc(sizeof(struct node));
printf("Press 0 to exit");
printf("\nPress 1 for new node");
printf("Enter your choice : ");
scanf("%d", &choice);
if(choice==0)
{
return 0;
}
else
{
printf("Enter the data:");
scanf("%d", &data);
temp->data = data;
printf("Enter the left child of %d", data);
temp->left = create();
printf("Enter the right child of %d", data);
temp->right = create();
return temp;
}
}
The above code is calling the create() function recursively and creating new node on each
recursive call. When all the nodes are created, then it forms a binary tree structure. The
process of visiting the nodes is known as tree traversal. There are three types traversals
used to visit a node:
o Inorder traversal
o Preorder traversal
o Postorder traversal

Implementation of Binary search tree


Now, let's see the program to implement the operations of Binary Search tree.
Program: Write a program to perform operations of Binary Search tree in C++.
In this program, we will see the implementation of the operations of binary search tree.
Here, we will see the creation, inorder traversal, insertion, and deletion operations of tree.
Here, we will see the inorder traversal of the tree to check whether the nodes of the tree
are in their proper location or not. We know that the inorder traversal always gives us the
data in ascending order. So, after performing the insertion and deletion operations, we
perform the inorder traversal, and after traversing, if we get data in ascending order, then
it is clear that the nodes are in their proper location.

#include <iostream>
using namespace std;
struct Node {
int data;
Node *left;
Node *right;
};
Node* create(int item)
{
Node* node = new Node;
node->data = item;
node->left = node->right = NULL;
return node;
}
/*Inorder traversal of the tree formed*/
void inorder(Node *root)
{
if (root == NULL)
return;
inorder(root->left); //traverse left subtree
cout<< root->data << " "; //traverse root node
inorder(root->right); //traverse right subtree
}
Node* findMinimum(Node* cur) /*To find the inorder successor*/
{
while(cur->left != NULL) {
cur = cur->left;
}
return cur;
}
Node* insertion(Node* root, int item) /*Insert a node*/
{
if (root == NULL)
return create(item); /*return new node if tree is empty*/
if (item < root->data)
root->left = insertion(root->left, item);
else
root->right = insertion(root->right, item);
return root;
}
void search(Node* &cur, int item, Node* &parent)
{
while (cur != NULL && cur->data != item)
{
parent = cur;
if (item < cur->data)
cur = cur->left;
else
cur = cur->right;
}
}
void deletion(Node*& root, int item) /*function to delete a node*/
{
Node* parent = NULL;
Node* cur = root;
search(cur, item, parent); /*find the node to be deleted*/
if (cur == NULL)
return;
if (cur->left == NULL && cur->right == NULL) /*When node has no children*/
{
if (cur != root)
{
if (parent->left == cur)
parent->left = NULL;
else
parent->right = NULL;
}
else
root = NULL;
free(cur);
}
else if (cur->left && cur->right)
{
Node* succ = findMinimum(cur->right);
int val = succ->data;
deletion(root, succ->data);
cur->data = val;
}
else
{
Node* child = (cur->left)? cur->left: cur->right;
if (cur != root)
{
if (cur == parent->left)
parent->left = child;
else
parent->right = child;
}
else
root = child;
free(cur);
}
}
int main()
{
Node* root = NULL;
root = insertion(root, 45);
root = insertion(root, 30);
root = insertion(root, 50);
root = insertion(root, 25);
root = insertion(root, 35);
root = insertion(root, 45);
root = insertion(root, 60);
root = insertion(root, 4);
printf("The inorder traversal of the given binary tree is - \n");
inorder(root);
deletion(root, 25);
printf("\nAfter deleting node 25, the inorder traversal of the given binary tree is - \n
");
inorder(root);
insertion(root, 2);
printf("\nAfter inserting node 2, the inorder traversal of the given binary tree is - \n"
);
inorder(root);
return 0;
}

Output
After the execution of the above code, the output will be -

You might also like