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

Tree unit VI Notes

Tree imp notes

Uploaded by

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

Tree unit VI Notes

Tree imp notes

Uploaded by

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

Unit- V Tree Marks-16

Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)


Introduction to tree
Q.What is tree?
Q.Define the term tree?
Q.Define tree?
A tree is a collection of elements called “nodes”.
One node is known as root say r
The root have zero or more non-empty subtree T1,T2 ,…..T k
Each subtree of root is called as child of r and r is parent of each root.

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

Q.Explain any eight term of tree terminology


1)Root:-it is special node in a tree structure and the entire tree referenced through it. This node does not
have a parent. the Root is A.
2)Parent:-it is immediate predecessor of node. in fig shows that A is parent of B,C,D
3)Child:-all immediate successors of node are its children.
4)Siblings: Nodes with the same parent are called as siblings.H,I and J are siblings of same parent D.
Path:-it is number of successive edges from source node to destination 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)

Representation of binary tree using an Array


Q.Describe with an example sequential representation of tree in memory?
Q.Describe representation of binary tree?
 In order to represent tree in single
one-dimensional array.
 the nodes are numbered sequentially level by level left to right
 Even empty nodes are numbered.

Location number zero of array can be used to store the size of the tree terms of total number of
nodes.

All non-existing children are shown by “\0” in the array.


 Index of the left child of node i =2 * i
 Index of the right child of node i =2 * i+1
 Index of the parent of node i =i/2
 if i is a left child of its parent then Sibling of node i will be found at the locations i+1

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

Left and right are pointer type fields,


Left holds the address of left and right holds the address of right child.

Linked Representation of binary Tree (ADT)


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

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

Total number of nodes in a full binary tree of height


h=20 +21 + 22 ….. +2h
=2h+1 -1
The height of the tree shown in fig is 2 and number of nodes is given by
=22+1 -1
=8-1
=7
2)Complete Binary tree
Q.Describe complete binary tree along with an example?
Q.Define the term ‘complete binary tree’?
Complete binary tree is defined as a binary tree where
i)All leaf node are on level n or n-1.
ii)Levels are filled from left to right.

3)Skewed Binary tree


 Skewed binary tree could be skewed (ततततत) to the left or it could be skewed to right.
 In a left skewed binary tree ,most of nodes have left child without corresponding right child.
 In a Right skewed binary tree ,most of nodes have Right child without corresponding left child.

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.

5)Extended Binary tree (2-Tree)


Q.Describe extended binary tree along with an example ?
 In an extended tree ,each empty subtree is replaced by failure node.
 Failure node is represented by rectangle .

 Nodes with 2 children are called as internal node.


 Nodes with 0 children are called as external node.
 Any binary tree can be converted into extended binary tree by replacing each empty subtree by
failure node.

Binary tree Traversal


Q.Give recursive algorithms for preorder,inorder and postorder traversal of binary tree.
Q.Enlist operations on trees.
Q.Define tree traversal along with preorder,inorder and postorder algorithms.
Q.Explain inorder, preorder and postorder traversal.
Q.List operations on tree.
Q.Define tree traversal .list different types tree traversal.
Binary tree Traversal
Traversing a tree is process of visiting every node of the tree and exactly once.
Binary tree Traversal type
1.Preorder Traversal
2.Inorder Traversal
3.Postorder Traversal

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”,Tdata);visit the root node
preoreder(Tleft); Preorder Traversal on left subtree
preoreder(Tright);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(Tleft); inorder Traversal on left subtree
printf(“\n%d”,Tdata);visit the root node
inorder(Tright);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(Tleft); postorder Traversal on left subtree
postorder(Tright); postorder Traversal on right subtree
printf(“\n %d”,Tdata);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)

Binary search tree(BST)


Q.What is meant by binary search tree with example?
 All nodes of left subtree are less than the root node.
 All nodes of right subtree are more than the root node.

Example on creation of BST


Q.Construct binary search tree for the following data:
7,2,9,0,5,6,8,1

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

Q.Construct binary search tree for the following data:


1)10,5,8,9,7,6,2,15
2) 8 ,5,7,9,4,5,16
3)15,22,8,14,17,2,4
4)5,32,15,48,60,50
5)4,15,65,49,21,8
Operation on Binary search tree
Q.Enlist operation on trees?
1)Initialise 2)Find
3)Makeempty 4) Insert
5)Delete 6)Create
7) Findmin 8) Findmax

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(Rootright,x)) if x>root data
return(Find(Rootleft,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 ((rootdata= = x)
return(root);
if (x>root data)
return (find(rootright),x);
return (find(rootleft),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 (rootleft);
makeempty (rootright);
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));
Tdata=x;
Tleft=NULL;
Tright=NULL;
Return(T)
}
if(x>Tdata) insert in right subtree
{
Tright=insert( Tright,x);
return(T);
}
Tleft=insert( Tleft,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)

b)Node with one child

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 (Tleft!=NULL)
T=Tleft;
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 (Tright!=NULL)
T=Tright;
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)

Q.Draw the tree structure for the following expressions.


1) (a-3b) (2x-y)3
Rewriting the expression after specifying every operator

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

You might also like