Chapter 4 Tree and Graph
Chapter 4 Tree and Graph
Introduction to Tree:
● Tree is a non-linear data structure, non-linear data structures are capable of
expressing more complex relationship than linear data structure
● A tree is a widely used Abstract Data Type(ADT) that simulates a hierarchical
tree structure.
● A tree is used for improving database search engine time, game programming,
3D graphics, data compression and in files system.
● Let us see some example,
Princip
al
Physic
CS HOD HOD
❖ Leaf Node: The node which does not have a child is called as
leaf node.
❖ Degree of a node: The total number of children of a node is
called as degree of a node.
❖ Degree of a tree: The highest degree of node among all the
nodes in a tree is called as degree of a tree.
❖ Level of Node: In a tree each step from top to bottom is called as
a level of node. level count starts from 0 and incremented by
one at each level.
❖ Height: The total number of edges from leaf node to a particular
node in the longest path is called as HEIGHT of that node.
❖ Depth: The total number edges from root node to a particular
node is called DEPTH of that node.
Tree Terminology
❖ Null tree: A tree with no nodes is a null tree.
❖ Node: Every individual element is called as Node.
❖ Root Node: a root node is special node which doesn’t have
parent node.
❖ Parent Node: The node which has a branch from it to any other
node is called a parent node.
❖ Child Node: The node which has a link from its parent node is
called as child node.
❖ Siblings: The nodes with same parent are called siblings.
BINARY TREE AND IT’ S TYPES
Binary Tree: It is a tree where every node can have at the most two
branches(children).
Strictly Binary Tree: A strictly binary tree is a binary tree where all
non-leaf node have two branches.
1. Complete Binary Tree: In a complete binary tree all level
accept the last are completely filled.
2. Full Binary Tree: in this tree all node have either zero or two
child nodes.
Perfect Binary Tree: in which all internal nodes have two children
and all leaves at same level.
Skewed Binary Tree: A tree in which every node have only left
subtree or right subtree is called as skewed binary tree.
Rooted Tree: In rooted tree every edge directly or indirectly
originate from the root.
Binary Search Tree: all nodes in the left subtree have values less
than the root and the node in the right sub tree have values greater
than the root.
Represenation of Binary Tree:
1. Sequential representation using array
2. Linked representation.
1.Sequential representation using array:
2. . Linked representation of Tree.
• All nodes should be allow dynamically.
• The fundamental component of binary tree is node.
• Each node consist of three field, Lchild, Data, Rchild.
Node
Data
Lchild Rchild
Node
• The Lchild field holds the address of its left node and Rchild field
holds the address of right node.
Node
struct Node {
int data;
Node *left, *right;
};
return root;
}
Binary Tree Traversal:
• When we want to display a binary tree, we need to follow
some order in which all the node of that binary tree must be
displayed.
• In binary tree displaying order of nodes depends on the
traversal method.
Methods of Traversing Tree:
1. Preorder traversal
2. Inorder traversal
3. Postorder traversal
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}
3.Postorder Traversal (LRD)
In this traversal left child node is visited fist, then its right child
and then its root node.
• Traverse the left subtree of D in preorder (Left-L)
• Traverse the Right subtree of D in preorder (Right-R).
• Process the root Data(D)
or :Attach the right subtree in place of the deleted node and then attach
the left subtree to the appropriate node of right subtree.
function to Delete a Node from BST // If root matches with the given key
Node* getSuccessor(Node* curr){ else {
curr = curr->right; // Cases when root has 0 children
while (curr != NULL && curr->left ! // or only right child
=NULL) if (root->left ==NULL) {
curr = curr->left; Node* temp = root->right;
return curr; delete root;
} return temp;
}
Node* delNode(Node* root, int x){ // When root has only left child
if (root->right ==NULL) {
// Base case Node* temp =root->left;
if (root == NULL)
delete root;
return root;
return temp;
// If key to be searched is in a subtree }
if (root->key > x) // When both children are present
root->left = delNode(root->left, x); Node* succ = getSuccessor(root);
else if (root->key < x) root->key = succ->key;
root->right = delNode(root->right, x); root->right = delNode(root->right, succ->key);
}
return root;
}
Searching in a BST
● To search a target key,we first compare it with the key at the
root of the tree. if it is same, then the search ends and if it is
less than key at root, search the target key in left subtree else
search in the right subtree.
The Non Recursive Function for search Key: Recursive Function for search Key:
node *search(node *root, int Key) node *rsearch(node *root, int Key)
{ {
node *temp=root; node *temp=root;
while(temp!=NULL) && (temp -> data != NULL) if (temp = = NULL) | | (temp -> data = = key)
{ return temp;
if(key==temp->data) else
{ if(key < temp -> data)
return temp; research(temp -> left, key);
} else
else if(key < temp -> data) research(temp -> right, key);
temp= temp -> left; }
else
temp= temp -> right;
}
return(temp);
}
GRAPH
Representation of Graph
1. Sequential representation(Adjacency matrix) using array
2. Linked representation(Adjacency list) using linked list.
1. Sequential representation(Adjacency matrix)
An adjacency matrix is a way of representing a graph as a matrix of boolean (0’s and 1’s).
Graph Traversal
●The process of visiting and exploring a graph for processing is called traversal.
1. Depth First Search(DFS)
2. Breadth First Search(BFS)