Unit Iii
Unit Iii
It is non-linear data structure which contains a set of nodes and edges that have
parent-child relationship. The starting node of a tree is called root node. Every other node
other than root node have parent node. Each nodes of a tree can have any number of
children.
E.g.: -
Tree Terminology
1. Root
In a tree data structure first node is known ad root node. Any tree must have a root
node and there must be only one root node foe a tree.
E.g.: - In the above tree node A is called root node.
2. Edge
The connecting link between any two nodes is called as edge. In a tree with n no. of
nodes there will be maximum of (n-1) edges.
E.g.: - Edge AB connects node A and B
3. Parent Node
The node which is predecessor of any node is called as parent node.
Eg:- B is parent of D and E and A is the parent of B and C.
4. Child Node
Node which is successor of any node is called child node. In a tree any parent node can
have any number of child nodes. The entire node except root node is child node.
Here B and C are children of A.
5. Leaf Node
Nodes which do not have a child are called as leaf nodes. They are also called as terminal
nodes.
E.g.: - Here H,I,J,F,G are leaf nodes.
12. Path
Path is the sequences of edges from one node to another node.
Eg: - The path between nodes A to D is sequences of edges AB and BD respectively.
BINARY TREE
It is a tree data structure where each node has almost two children called left child
and right child.
E.g:-
1. Left sub tree of a node have key values (Data part) less than its parent node.
2. Right sub tree of a node haver key values greater than its parent node.
E.g.: -
RECURSSION
The process in which the function calls itself directly or indirectly is called as
recursion and corresponding function is called as recursive function. Using recursive
function certain problems such as tree traversal can be solved easily. In a recursive function,
the solution to base case is provided and solution of bigger problem is expressed in terms of
smaller problems.
TREE TRAVERSAL
The process of visiting nodes of a tree is called as tree traversal. There are three different
methods of tree traversal.
i) In-order Traversal
In this type of tree traversal left child of a node is first visited then root node is visited
and finally right node is visited. The algorithm follows recursive method represented as:
Step 1: - Recursively traverse left sub tree.
Step 2: - Visit root node.
Step 3: - Recursively traverse right sub tree.
E.g.: -
Implement a binary search tree and write down the functions for tree
traversal
#include<iostream>
using namespace std;
class node
{
public:
int data;
node *left,*right;
};
class bst:public node
{
public:
node *root;
bst()
{
root=NULL;
}
void create();
void inorder(node*);
void preorder(node*);
void postorder(node*);
};
void bst::create()
{
node *temp;
temp=new node;
cout<<"Enter the Data"<<endl;
cin>>temp->data;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
root=temp;
else
{
node *p;
p=root;
while(1)
{
if(temp->data<p->data)
{
if(p->left==NULL)
{
p->left=temp;
break;
}
else if(p->left!=NULL)
p=p->left;
}
else if(temp->data>p->data)
{
if(p->right==NULL)
{
p->right=temp;
break;
}
else if(p->right!=NULL)
p=p->right;
}}}}
int main()
{
bst B;
int y=1,ch;
while(y==1)
{
cout<<"Enter the Choice"<<endl;
cout<<"Enter 1.Create 2.Inorder 3.Preorder 4.Postorder
5.Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1: B.create();
break;
case 2: B.inorder(B.root);
break;
case 3: B.preorder(B.root);
break;
case 4: B.postorder(B.root);
break;
case 5: y=0;
break;
default: cout<<"Invalid Option"<<endl;
}}
return 0;
}