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

Assignment 3

The document discusses two methods for implementing a binary tree - recursive and non-recursive. It includes code snippets to create a binary tree, and perform inorder, preorder and postorder traversals. Sample input and output is also provided.

Uploaded by

Vaishnavi Kaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Assignment 3

The document discusses two methods for implementing a binary tree - recursive and non-recursive. It includes code snippets to create a binary tree, and perform inorder, preorder and postorder traversals. Sample input and output is also provided.

Uploaded by

Vaishnavi Kaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

“Group-B

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

Enter left child node for= 5


Enter a value for node: 4

Enter left child node for= 4


Enter a value for node: 3

Enter left child node for= 3


Enter a value for node: 2

Enter left child node for= 2


Enter a value for node: 1

Enter left child node for= 1


Enter a value for node: -1

Enter right child node for= 1


Enter a value for node: -1

Enter right child node for= 2


Enter a value for node: -1

Enter right child node for= 3


Enter a value for node: -1

4
Enter right child node for= 4
Enter a value for node: -1

Enter right child node for= 5


Enter a value for node: 6

Enter left child node for= 6


Enter a value for node: 7

Enter left child node for= 7


Enter a value for node: 8

Enter left child node for= 8


Enter a value for node: 9

Enter left child node for= 9


Enter a value for node: 10

Enter left child node for= 10


Enter a value for node: -1

Enter right child node for= 10


Enter a value for node: -1

Enter right child node for= 9


Enter a value for node: -1

Enter right child node for= 8


Enter a value for node: -1-

5
Enter right child node for= 7
Enter a value for node: -1

Enter right child node for= 6


Enter a value for node: -1

Inorder traversal is= 1 2 3 4 5 10 9 8 7 6


Preorder Traversal is= 5 4 3 2 1 6 7 8 9 10
Postorder T
raversal is= 1 2 3 4 10 9 8 7 6 5

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

Enter left child node for= 6


Enter a value for node= 5

Enter left child node for= 5


Enter a value for node= 4

Enter left child node for= 4


Enter a value for node= 3

11
Enter left child node for= 3
Enter a value for node= 2

Enter left child node for= 2


Enter a value for node= 1

Enter left child node for= 1


Enter a value for node= -1

Enter right child node for= 1


Enter a value for node= -1

Enter right child node for= 2


Enter a value for node= -1

Enter right child node for= 3


Enter a value for node= -1

Enter right child node for= 4


Enter a value for node= -1

Enter right child node for= 5


Enter a value for node= -1

Enter right child node for= 6


Enter a value for node= 7

Enter left child node for= 7


Enter a value for node= 8

12
Enter left child node for=
8Enter a value for node=
9

Enter left child node for=


9Enter a value for node=
10

Enter left child node for=


10Enter a value for node=
-1

Enter right child node for=


10Enter a value for node= -
1-

Enter right child node for=


9Enter a value for node= -
1

Enter right child node for=


8Enter a value for node= -
1

Enter right child node for=


7Enter a value for node= -
1

Inorder traversal is: 1 2 3 4 5 6 10 9 8 7


Preorder traversal is: 6 5 4 3 2 1 7 8 9 10
3
Postorder traversal is: 1 2 3 4 5 10 9 8 7 6

You might also like