0% found this document useful (0 votes)
6 views3 pages

Lab 8 Binary Tree 22122021 102727am

Uploaded by

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

Lab 8 Binary Tree 22122021 102727am

Uploaded by

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

#include<iostream>

#include<conio.h>
using namespace std;

class Node
{
public:
Node *left;
Node *right;
int data;
};
class binarytree
{
public:
binarytree();
bool isempty();
Node *createnewnode(int value);
void insert(int value);
void inorder();
void preorder();
void postorder();
private:
Node *root;
void insert(Node *t,int value); // function overloading
void inorder(Node *t);
void preorder(Node *t);
void postorder(Node *t);
};

binarytree:: binarytree()
{
root=NULL;
}

bool binarytree::isempty()
{
return (root==NULL); //if root is null true otherwise false
}

Node * binarytree::createnewnode(int value)


{
Node *n=new Node;
n->data=value;
n->left=NULL;
n->right=NULL;
return n;
}

void binarytree::insert(int value) //8 //3 //10 //1 //6 //4


{
if(isempty())
{
Node * n= createnewnode(value);
root=n; //root node inserting
}
else //function overloading and recursion.
//two or more function hvaing same name but different parameters.
insert(root, value); //node 8, 3 //8,10 //8,1 //8,6 //8,4
}

void binarytree::insert(Node *t, int value) //node8, 3 //8,10 //8,1 //3,1 //8,6 //3,6
//8,4
{
if(value<= t->data) // 3<=8 .. true //10<=8.. false //1<=8.. true
//1<=3..true //6<=8..true //6<=3.. false
{
if(t->left==NULL) //8 left is not null..false
{
Node * n= createnewnode(value); // create node3 //node1
t->left=n;
}
else
{
insert(t->left,value); //node3,1 // recusrsion //node3, 6 //3,4
}
}
else //10>8 //6>3 //4>3
{
if(t->right==NULL)
{
Node *n=createnewnode(value); // create node10 //node6
t->right=n; //node3->right=6
}
else
{
insert(t->right,value); //node 6,4
}
}
}

void binarytree::inorder()
{
inorder(root);
cout<<endl;
}
void binarytree::preorder()
{
preorder(root);
cout<<endl;
}
void binarytree::postorder()
{
postorder(root);
cout<<endl;
}

void binarytree::inorder(Node *t)


{
if(t!=NULL)
{
inorder(t->left);
cout<< t->data << " ";
inorder(t->right);
}
}
void binarytree::preorder(Node *t)
{
if(t!=NULL)
{
cout<< t->data << " ";
preorder(t->left);
preorder(t->right);
}
}

void binarytree::postorder(Node *t)


{
if(t!=NULL)
{

postorder(t->left);
postorder(t->right);
cout<< t->data << " ";
}
}
//int mystery(int);
int main()
{
binarytree t;
t.insert(8);
t.insert(3);
t.insert(10);
t.insert(1);
t.insert(6);
t.insert(4);
t.insert(7);
t.insert(14);
t.insert(13);
t.inorder(); // 1 3 4 6 7 8 10 13 14
t.preorder(); // 8 3 1 6 4 7 10 14 13
t.postorder(); // 1 4 7 6 3 13 14 10 8
// return 0;
_getch();

You might also like