Tree unit VI Notes
Tree unit VI Notes
Basic Term
Q.Define the term related to binary tree:
Level,Depth,Leaf node,Root Node.
Q.Describe in brief the term related to binary tree:
Level,Depth,Leaf node,Root Node, Siblings
Q.Explain the term with help of diagram
(i)Siblings (ii)Leaf node
Unit-V Page 1
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
The path length from node D to Node N in fig ,node D is connected to node N through three edges D-
H,H-M,M-N
Degree of node:- the degree of node is a number of children of a node.
In above fig.
A and D are nodes of degree 3.
B,F and M are nodes of degree 2.
C and H are nodes of degree 1.
E,K,L,N,O,I,J are nodes of degree 0
Leaf Node:-
Node of degree 0 is also called as leaf node.
i e it has no children ,it is also called as terminal node. In above fig E,K,L,N,O,I,J are nodes leaf nodes.
Level:-
The level of node in binary tree is defined as
1.The root of the tree has level 0.
Depth:-
The depth of binary tree is the maximum level of any node in the tree.
This is equal to longest path from the root to any leaf node.
In above diagram depth is 4.
Degree of node:-
The degree of tree is the maximum degree of node in the tree
In above fig.
A and D are nodes have maximum degree of node in the tree is 3.
Ancestor of node:
All nodes on the path leading to root node are called as node.
Ancestor of node N are M,H,D,A
Binary Tree
Q.Explain the binary tree with suitable example and diagram.
Q.Write an algorithm for binary tree along with an example?
Q.Explain Binary tree with an example?
Q.What is binary tree?
Q.Describe the concept of binary tree and its application
Binary Tree
Tree is binary if each node of the tree can have maximum of two children.
Children of node of binary tree are ordered.
One child is called the left child and other child is called as right child.
Node A has two children B and C . similarly
Nodes B and C ,each have one child namely G and D respectively.
Unit-V Page 2
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Location number zero of array can be used to store the size of the tree terms of total number of
nodes.
Unit-V Page 3
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Similarly if i is a right child of its parent then its sibling will be found at i-1
Unit-V Page 4
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Linked Representation of binary Tree
Q.Describe with an example linked representation of trees in memory.
Q.Describe representation of binary tree.
Node of a binary tree consists of three fields
Data
Address of the left child
Address of the right child
General tree
Q.Define the term general tree?
Q.Describe general tree?
In general tree ,number of children per node is not limited to two. it might not be feasible to make
the children direct links in the node.
Unit-V Page 5
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Forest:
Forest is defined as set of trees.
Forest can be represented by binary tree.
Types of Tree
1.Full Binary Tree
2.Complete Binary tree
3.Skewed Binary tree
4.Strictly Binary tree
5.Extended Binary tree(2-tree)
1)Full Binary tree
Unit-V Page 6
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Q.Define the term full binary tree? 2m
Binary tree is said to be full binary tree if each of its node has either two children or no child at
all.
Number of node at any level i in full binary tree is given by 2 i
Unit-V Page 7
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
4)Strictly Binary tree
If every non-terminal node in binary tree consists of non-empty left subtree and right subtree
,then such tree is called as strictly binary tree.
Node will have either two children or no child at all.
Unit-V Page 8
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
1.Preorder Traversal
Q.State the principles of pre-order traversal of binary tree?
Q.Write an algorithm to traverse the tree in preorder with example.
Q.Define tree traversal along with preorder algorithms?
Q.With suitable example describe preorder traversal of tree .also write algorithms for preorder traversal ?
Q.Describe the process of pre-order traversal of binary tree ,give one example?
Q.Write algorithms for preorder traversal of binary tree.
C function for Preorder Traversal(Algorithm)
void preorder(node *T) ;address of root node is passed in T
{
if (T!=NULL)
{
printf(“\n%d”,Tdata);visit the root node
preoreder(Tleft); Preorder Traversal on left subtree
preoreder(Tright);Preorder Traversal on right subtree
}
}
Shortcut method to get preorder traversal
2.Inoreder Traversal
Q.State the principles of inorder traversal of binary tree?
Q.With the help of example ,describe the process of inorder traversal of binary tree.?
Q.Define tree traversal along with inorder algorithms?
Q.Write an algorithms for inorder traversal of binary tree ?
Q.Write a ‘C’ program to traverse the tree in inorder.
Q.Write algorithms for in-order traversal for binary tree ,demonstrate with suitable example.
2.Inorder Traversal(algorithm)
The following of inorder traversal of a non-empty binary tree is as follows:
1)Firstly ,traverse the left subtree in inorder
2)Next ,visit the root node.
3)At last ,traverse the right-subtree in inorder.
Left root right
Unit-V Page 9
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
C function for Inorder Traversal(Algorithm)
void inorder(node*T) ;address of root node is passed in T
{
if (T!=NULL)
{
inorder(Tleft); inorder Traversal on left subtree
printf(“\n%d”,Tdata);visit the root node
inorder(Tright);inorder Traversal on right subtree
}
}
3) Postorder Traversal
Q.Write an algorithms to traverse the tree in postorder with example?
Q.Define tree traversal along with postorder algorithms
Q.Write an algorithms for postorder traversal in a tree?
Postorder Traversal(Algorithm)
The following of postorder traversal of a non-empty binary tree is as follows:
1)Firstly ,traverse the left subtree in postorder.
2)Next ,traverse the right-subtree in postorder.
3)At last ,visit the root node.
left right root
C function for Postorder Traversal
void postorder(node*T) ;address of root node is passed in T
{
if (T!=NULL)
{
postorder(Tleft); postorder Traversal on left subtree
postorder(Tright); postorder Traversal on right subtree
printf(“\n %d”,Tdata);visit the root node
}
}
Solved example:
Q.Perform inorder, preorder, and postorder traversal of the following binary tree 6 M
Question
Unit-V Page 10
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Preorder: ABDEFCGH
Inorder: BEDFAGCH
Postorder: EFDBGHCA
Q.Perform inorder ,preorder, and postorder traversal of the following binary tree 6 M Question
Preorder: ABCDEFGHIJ
Inorder: DCEBGAFIHJ
Postorder: DECGFBIJHA
Q.Perform inorder ,preorder, and postorder traversal of the following binary tree 6 M Question
Preorder : ABDGKHLMCE
Inorder : KGDLHMBAEC
Postorder : KGLMHDBECA
Q.Perform inorder ,preorder, and postorder traversal of the following binary tree 6 M Question
preorder :ABDEFCGHJLK
Inorder :DBFFAGCLJHK
Postorder :DFEBGLJKHCA
Q.Perform inorder ,preorder, and postorder traversal of the following binary tree 6 M Question
Unit-V Page 11
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
preorder ABDHEICFJGKL
Inorder:- DHBIEAFJCGLK
Postorder:- HDIEBJFLKGCA
Q.Perform inorder ,preorder, and postorder traversal of the following binary tree 6 M Question
Unit-V Page 12
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Unit-V Page 13
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Unit-V Page 14
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Q.Construct binary search tree for the following data:
50,33,44,22,77,35,60,40
Unit-V Page 15
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Q.Construct binary search tree for the following data:
12,25.14,8,3,5
Unit-V Page 16
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Structure of node of binary search tree (ADT)
Typedef struct BSTnode
{
int data;
struct BSTnode *left,*right;
}BSTnode;
1)Initialize Operation
Initially the tree is empty and hence the referencing pointer root should be set to NULL.
BSTnode*initialize()
{
return(NULL);
}
2)Find operation
Q.Explain searching value in binary search tree with example?
Algorithm for Find
where x is key which we are finding.
return condition
Find (root,x) NULL if root== NULL
root if root data == x
return(Find(Rootright,x)) if x>root data
return(Find(Rootleft,x)) if x<root data
‘C Function for find ()
Q.Write a program for binary search in ‘C’ language
BSTnode *find(BSTnode *root,int x)
{
if ((root= = NULL)
return(NULL);
if ((rootdata= = x)
return(root);
if (x>root data)
return (find(rootright),x);
return (find(rootleft),x);
}
3)Makeempty() Operation
This function deletes every node of the tree .it also release the memory of node.
‘C’ Function to release memory
BSTnode *makeempty (BSTnode *root)
{
if (root!= NULL)
{
makeempty (rootleft);
makeempty (rootright);
free(root);
}
Return (NULL);
}
Unit-V Page 17
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
4)Insert Operation
Q.Explain insertion of node on binary search tree?
Q.Describe following binary search tree operations: insertion of node.
‘C’ Function for insert()
BSTnode * insert(BSTnode *T,int x)
{
if(T= = NULL)
{
T=(BST*)malloc( sizeof(BSTnode));
Tdata=x;
Tleft=NULL;
Tright=NULL;
Return(T)
}
if(x>Tdata) insert in right subtree
{
Tright=insert( Tright,x);
return(T);
}
Tleft=insert( Tleft,x); insert in left subtree
return(T);
}
5)Deletion Operation
Q.Give stepwise procedure for deletion of a node in binary tree?
Q.Explain deletion of node on binary search tree ?
Q.Give the algorithm for deletion of node in binary search tree ?
Q.Describe following binary tree search tree operations?
In order to delete node ,we must find the node to be deleted.
The node to be deleted may be:
a)Leaf node
b)Node with one child
c)Node with two children
a)Leaf node
If the node is leaf node ,it can be deleted immediately by setting parent pointer to NULL.
Assume that the node 25 is to be deleted ,node 25 is right child of its parent child 20,so right child of node
20 is set to NULL.
Unit-V Page 18
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Assume that node 9 is to be deleted ,since node 9 is right child of its parent node 4.the only child subtree
of node 9 with node 7 will become the right subtree of node 4 after deletion.
c)Node with two children
Unit-V Page 19
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Assume that node 4 is to be deleted, node 7 is smallest node in the right subtree of node 4, value 7 is
copied in the node 4.
6)Create
Binary search tree can be created by making repeated calls to insert operations.
7)FindMin
This function return to address of the node with smallest value in the tree.
‘C’ function for finding the smallest value in BST
BSTnode * findmin (BSTnode *T)
{
while (Tleft!=NULL)
T=Tleft;
return (T);
}
8) FindMax
This function return to address of the node with largest value in the tree.
‘C’ function for finding the largest value in BST
BSTnode * findmax (BSTnode *T)
{
while (Tright!=NULL)
T=Tright;
return (T);
}
Application of Trees
1.Expression Tree
2.Heap sort
1.Expression Tree
Q.Describe expression tree with example?
when an expression is represented through tree,it is known as an expression tree. the leaves of
expression tree are operands.
Q.Draw the tree structure for the following expressions.
1) (2a+5b)3 (x-7y)4
Rewriting the expression after specifying every operator
Unit-V Page 20
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Unit-V Page 21
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Unit-V Page 22
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Unit-V Page 23
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Unit-V Page 24
Unit- V Tree Marks-16
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Unit-V Page 25