Assignment 3
Assignment 3
Assignment no.1
A book consists of chapters, chapters consist of sections and sections consist of subsections.
Construct a binary tree and print the nodes. Find the time and space requirements of your
method.”
1] Binary Tree Recursive Program:
/* Binary Tree Recursive */
#include <iostream.h>
class node
{
int data;
node *left;
node *right;
public:
node(int val)
{
data=val;
left=NULL;
right=NULL;
}
friend class Tree;
};
class Tree
{
node *root;
public:
Tree()
{
root=NULL;
}
1
node *create();
void inorder(node*);
void preorder(node*);
void postorder(node*);
};
node* Tree::create()
{
int val;
cout<<"\n Enter a value for Root node: ";
cin>>val;
if(val==-1)
{
return NULL;
}
node*p=new node(val);
if(root=NULL)
{
root=p;
}
cout<<"\n Enter left child node for= "<<val;
p->left=create();
cout<<"\n Enter right child node for= "<<val;
p->right=create();
return p;
}
void Tree::inorder(node*t)
{
if(t!=NULL)
{
inorder(t->left);
2
cout<<t->data<<" ";
inorder(t->right);
}
}
void Tree::preorder(node*t)
{
if(t!=NULL)
{
cout<<t->data<<" ";
preorder(t->left);
preorder(t->right);
}
}
void Tree::postorder(node*t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
cout<<t->data<<" ";
}
}
void main()
{
node*p;
Tree T1;
p=T1.create();
cout<<"\nInorder traversal is= ";
T1.inorder(p);
cout<<"\nPreorder Traversal is= ";
3
T1.preorder(p);
cout<<"\nPostorder Traversal is= ";
T1.postorder(p);
}
Output:-
Enter a value for node: 5
4
Enter right child node for= 4
Enter a value for node: -1
5
Enter right child node for= 7
Enter a value for node: -1
6
1] Binary Tree Non-Recursive Program:
#include <iostream>
#include <stack>
using namespace std;
class node
{
int data;
node *left;
node *right;
public:
node(int val)
{
data = val;
left = NULL;
right = NULL;
}
friend class Tree;
};
class Tree
{
node *root;
public:
Tree()
{
root = NULL;
}
7
node *create();
void inorder();
void preorder();
void postorder();
};
node *Tree::create()
{
int val;
cout << "\n Enter a value for node= ";
cin >> val;
if (val == -1)
return NULL;
node *p = new node(val);
if (root == NULL)
root = p;
cout << "\n Enter left child node for= " << val;
p->left = create();
cout << "\n Enter right child node for= "<< val;
p->right = create();
return p;
}
void Tree::inorder()
{
if (root == NULL)
{
cout << "\n Ooops.. Tree is empty..!!";
return;
}
8
stack<node *> s;
node *t = root;
while (t != NULL || !s.empty())
{
while (t != NULL)
{
s.push(t);
t = t->left;
}
t = s.top();
s.pop();
cout << " " << t->data;
t = t->right;
}
}
void Tree::preorder()
{
if (root == NULL)
{
cout << "\n Ooops.. Tree is empty..!!";
return;
}
stack<node *> s;
node *t = root;
s.push(t);
while (!s.empty())
{
t = s.top();
s.pop();
9
cout << " " << t->data;
if (t->right)
s.push(t->right);
if (t->left)
s.push(t->left);
}
}
void Tree::postorder()
{
if (root == NULL)
{
cout << "\n Ooops.. Tree is empty..!!";
return;
}
stack<node *> s1, s2;
s1.push(root);
while (!s1.empty())
{
node *t = s1.top();
s1.pop();
s2.push(t);
if (t->left)
s1.push(t->left);
if (t->right)
s1.push(t->right);
}
while (!s2.empty())
{
node *t = s2.top();
10
Sanika Sachin Ekshette
Roll No: - SCC56
s2.pop();
cout << " " << t->data;
}
}
int main()
{
node *p;
Tree T1;
p = T1.create();
cout << "\nInorder traversal is: ";
T1.inorder();
cout << "\nPreorder traversal is: ";
T1.preorder();
cout << "\nPostorder traversal is: ";
T1.postorder();
return 0;
}
Output:-
Enter a value for node= 6
11
Enter left child node for= 3
Enter a value for node= 2
12
Enter left child node for=
8Enter a value for node=
9