0% found this document useful (0 votes)
46 views78 pages

Nabia Noor Roll No. 063 Bsit 3 Semester Section A

The document contains 7 programs related to arrays and data structures in C++. Program 1 computes the sum of elements in a linear array. Program 2 prints even values in a linear array. Program 3 adds elements to the end of an array. Program 4 inserts a value into an array at a given position. Program 5 deletes a value from an array at a given position. Programs 6 and 7 demonstrate push/pop operations on a stack and insert/delete operations on a queue respectively.

Uploaded by

Syeda Mehar
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)
46 views78 pages

Nabia Noor Roll No. 063 Bsit 3 Semester Section A

The document contains 7 programs related to arrays and data structures in C++. Program 1 computes the sum of elements in a linear array. Program 2 prints even values in a linear array. Program 3 adds elements to the end of an array. Program 4 inserts a value into an array at a given position. Program 5 deletes a value from an array at a given position. Programs 6 and 7 demonstrate push/pop operations on a stack and insert/delete operations on a queue respectively.

Uploaded by

Syeda Mehar
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/ 78

Nabia Noor

Roll No. 063


BSIT
3rd Semester
Section A

PROGRAMS

PROGRAM 01:

Write a Program to compute the sum of values of elements of a linear array having 10
elements?

#include<iostream>

using namespace std;

class temp

private:

int abc[10],sum;

public:

temp()

sum=0;

input()

cout<<"Enter 10 values and press Enter after typing each value\n";


for(int i=0;i<=9;i++)

cin>>abc[i];

print()

for(int i=0;i<=9;i++)

sum=sum+abc[i];

cout<<"sum="<<sum;

};

main()

temp obj;

obj.input();

obj.print();

OUTPUT
PROGRAM 02:

Write a Program to print out even values stored in a linear array ABC having 10 elements?

#include<iostream>

using namespace std;

class temp

private:

int abc[10];

public:

input()

cout<<"Enter 10 values and Press Enter after typing each value\n";

for(int i=0;i<=9;i++)
cin>>abc[i];

print()

for(int i=0;i<=9;i++)

if (abc[i]%2==0);

cout<< "abc[i]"<<"\n";

};

main()

temp obj;

obj.input();

obj.print();

OUTPUT
PROGRAM 03:

Write a Program to add six values into elements at the end of array ‘Temp’ that has only 4
items stored in its first four elements?

#include<iostream>

using namespace std;

class temp

private:

int abc[10];

public:

assign()

abc[0]=66;

abc[1]=72;

abc[2]=36;
abc[3]=78;

input()

cout<<"Enter 6 values and Press Enter after typing each value\n";

for(int i=4;i<=9;i++)

cin>>abc[i];

print()

for(int i=0;i<=9;i++)

cout<<abc[i]<<endl;

}};

main()

temp obj;

obj.assign();

obj.input();

cout<<"values in Array "<<endl;

obj.print();

OUTPUT
PROGRAM 04:

Write a Program to insert a value M at location pos in array ABC having N elements?

#include<iostream>

using namespace std;

class temp

private:

int abc[5];

public:

assign(int p[])

{
for (int i=0;i<=3;i++)

abc[i]=p[i];

insert(int loc,int val)

for(int i=4;i>=loc;i--)

abc[i+1]=abc[i];

abc[loc]=val;

print(int n)

for(int i=0;i<=n;i++)

cout<<abc[i]<<endl;};

};

main()

temp obj;

int pos,n, a[4]={4,55,6,3};

obj.assign(a);

cout<<"values before insertion"<<endl;

obj.print(3);

cout<<"enter value to insert ?";

cin>>n;

cout<<"Enter position to insert?";


cin>>pos;

if(pos>=5)

cout<<"invalid location";

return 0;

obj.insert(pos,n);

cout<<"Values after insertion"<<endl;

obj.print(4);

OUTPUT:

PROGRAM 05:
Write a Program to delete the value at location K of an array “country” having N
elements?

#include<iostream>

using namespace std;

class temp

private:

int abc[5];

public:

assign(int p[])

for (int i=0;i<=4;i++)

abc[i]=p[i];

del(int loc)

for(int i=loc;i<=4;i++)

abc[i]=abc[i+1];

abc[4]=0;

print()

for(int i=0;i<=4;i++)

cout<<abc[i]<<endl;};
};

main()

temp obj;

int pos,n, a[5]={4,8,55,6,3};

obj.assign(a);

cout<<"values before deletion"<<endl;

obj.print();

cout<<"enter value to delete ?";

cin>>n;

cout<<"Enter position to delete?";

cin>>pos;

if(pos>=5)

cout<<"invalid location";

return 0;

obj.del(pos-1);

cout<<"Values after deletion"<<endl;

obj.print();

OUTPUT:
PROGRAM 06:

Write a Program to push add an item ‘X’ into a stack ‘S’

Write a Program to pop the top element from stack?

#include <iostream>

#include <conio.h>

using namespace std;

class stack

private:

int top;

int S [10];

public:

stack()

{
top=-1;

push(int n)

if(top==0)

cout<<"stack overflow";

return 0;

top++;

S[top]=n;

int pop()

int data;

if(top==-1)

cout<<"stack is empty";

return NULL;

data=S[top];

top--;

return data;

print()
{

if(top==-1)

cout<<"stack is empty";

return NULL;

for(int i=top; i>=0; i--)

cout<<S[i]<<endl;

};

int main()

stack obj;

int opt, val;

while(opt!=3)

cout<<"1: push\n";

cout<<"2: pop\n";

cout<<"3: exit\n";

cout<<"enter the choice";

cin>>opt;

switch(opt)

case 1:

cout<<"enter value to insert";


cin>>val;

obj.push(val);

cout<<"stack after insertion";

obj.print();

break;

case 2:

cout<<"value"<<obj.pop()<<"is popped";

cout<<"stack after deletion";

obj.print();

break;

return 0;

OUTPUT:
PROGRAM 07:

Write a Program to add an item ‘Item’ in queue Q

Write a Program to delete an item ‘Item’ in queue Q?

#include <iostream>

using namespace std;

class que

private:

int F, R;

int QA[10];

public:
que()

F=-1; R=-1;

insert(int n)

if(R>=9)

cout<<"que is full";

return 0;

else

R++;

QA[R]=n;

if (F==-1) F=0;

int del()

int data;

if(F==-1)

cout<<"que is empty";

return NULL;
}

data=QA[F];

if(F == R) F = R = -1;

else F++;

return data;

print()

if(F==-1)

cout<<"que is empty";

return NULL;

for(int i=F; i<=R; i++)

cout<<QA[i]<<endl;

};

main()

que obj;

int opt, val;

while(opt!=3)

cout<<"1: insert\n";

cout<<"2: delete\n";
cout<<"3: exit\n";

cout<<"enter the choice";

cin>>opt;

switch(opt)

case 1:

cout<<"enter value to insert";

cin>>val;

obj.insert(val);

cout<<"que after insertion";

obj.print();

break;

case 2:

cout<<"value"<<obj.del()<<"is popped";

cout<<"que after deletion";

obj.print();

break;

OUTPUT
PROGRAM 09:

Write a Program to insert items into priority queue represented by a two-dimensional


array ABC with 3 rows and 10 columns

Write a Program to delete items into priority queue consisting of a two-dimensional array
ABC with 3 rows and 10 columns?

#include<iostream>

#include<conio.h>

using namespace std;

class que

private:

int F1,R1,F2,R2,F3,R3;

int QA[3][10];

public:

que()
{

F1=-1;R1=-1;

F2=-1;R2=-1;

F3=-1;R3=-1;

void insert(int,int);

void add(int,int&,int&,int);

del(int);

int remove(int&,int&,int);

void print(int);

void ppp(int&,int&,int);

};

main()

que obj;

int opt,val,pro;

while(opt!=3)

cout<<"insert item\n";

cout<<"delete item\n";

cout<<"exit\n";

cout<<"enter the choice";

cin>>opt;

switch(opt)
{

case 1:

cout<<"enter value to insert:";

cin>>val;

cout<<"enter priority";

cin>>pro;

obj.insert(val,pro);

cout<<"queue after insertion\n";

obj.print(pro);

getch();

void que::insert(int n,int p)

switch(p)

case 1:

add(n,R1,F1,0);

break;

case 2:

add(n,R2,F2,1);

break;

case 3:
add(n,R2,F2,2);

break;

void que::add(int x,int& R,int& F,int i)

if(R>=9)

cout<<" Queue is full";

getch();

return;

else

R++;

QA[i][R]=x;

if(F==-1)

F=0;

que::del(int p)

switch(p)
{

case 1:

remove(R1,F1,0);

break;

case 2:

remove(R2,F2,1);

break;

case 3:

remove(R2,F2,2);

break;

int que::remove(int& R,int& F,int i)

int data;

if(F==-1)

cout<<"Queue is empty";

getch();

return NULL;

data=QA[i][F];

if(F==R)

F=R=-1;
else F++;

return data;

void que::print(int p)

switch(p)

case 1:

ppp(R1,F1,0);

break;

case 2:

ppp(R2,F2,1);

break;

case 3:

ppp(R2,F2,2);

break;

void que::ppp(int& R,int&F,int i)

if(F==-1)

cout<<"queue is empty";
return;

for(int c=F;c<=R;c++)

cout<<QA[i][c]<<"\t";

OUTPUT:

PROGRAM 10:

Write an algorithm to find a value ITEM from an array ABC consisting of N elements?
#include <iostream>

using namespace std;

class seq

private: int a[10];

public: void input(void);

void search(int);

};

int main()

{
seq obj;

int item;

obj.input();

cout<<"Enter required value to search ?";

cin>>item;

obj.search(item);

//Member funcion to input data into array

void seq::input(void)

cout<<"Enter 10 values "<<endl;

for (int i=0; i<=9; i++)

cin>> a[i];

//Member function to search data from array

void seq::search(int n)

int i, loc=-1;

i=0;

while(i<=9)

if(n==a[i])

loc=i+1;
cout<<"Data found in location = "<<loc;

break;

i++;

if(loc==-1)

cout<<"Data not found";

OUTPUT:

PROGRAM 11:

Write a program to sort an array A consisting of N elements in ascending order using


bubble sort?
#include <iostream>

using namespace std;


class bubble

private: int a[5];

public :

void input()

cout<<"Enter 5 values "<<endl;

for (int i=0; i<=4; i++)

cin>> a[i];

void sort()

int t;

for(int u=4;u>=1;u--)

for(int i=0;i<u;i++)

if(a[i]>a[i+1])

t=a[i];

a[i]=a[i+1];

a[i+1]=t;

void print()

cout<<"Sorted array "<<endl;


for(int i=0; i<=4; i++)

cout<<a[i]<<endl;

};

int main()

bubble obj;

obj.input();

obj.sort();

obj.print();

OUTPUT:
PROGRAM 12:

Write a program to sort an array A consisting of N elements in ascending order using


selection sort ?
#include <iostream>

using namespace std;

class selection

private: int a[5];

public :

void input()

cout<<"Enter 5 values "<<endl;

for (int i=0; i<=4; i++)

cin>>a[i];

void sort()

int min, t, loc;

for(int u=0;u<4;u++)

min=a[u];

loc=u;

for(int i=u;i<=4;i++)

if(min>a[i])
{

min=a[i];

loc=i;

t=a[loc];

a[loc]=a[u];

a[u]=t;

cout<<endl;

void print()

cout<<"Sorted array "<<endl;

for(int i=0; i<=4; i++)

cout<<a[i]<<endl;

};

int main()

selection obj;

obj.input();

obj.sort();

obj.print();
}

OUTPUT:

PROGRAM 13:

Write a program to sort an array A consisting of N elements in ascending order using


insertion sort ?
#include <iostream>
using namespace std;

class insertion
{
private: int a[5];
public :
void input()
{
cout<<"Enter 5 values "<<endl;
for (int i=0; i<=4; i++)
cin>>a[i];
}
void sort()
{
int temp, i, c;
for(c=0;c<4;c++)
{
temp=a[c];
for(i=c; i>0 && temp< a[i-1];i--)
a[i]=a[i-1];
a[i]=temp;
}
}
void print()
{
cout<<"Sorted array "<<endl;
for(int i=0; i<=4; i++)
cout<<a[i]<<endl;
}
};

int main()
{
insertion obj;

obj.input();
obj.sort();
obj.print();
}
OUTPUT:
PROGRAM 14:
Write a program to sort an array AB consisting of N elements using merge sort?
#include <iostream>
using namespace std;

class merging
{
private: int ab[10], a[5], b[5], r, s;
public :
void input()
{
cout<<"Enter 10 values "<<endl;
for (int i=0; i<=9; i++)
cin>>ab[i];
}
void sort()
{
r=(9+0)/2;
s=r+1;
int i;
for(i=0;i<=r;i++)
a[i]=ab[i];
for(i=s;i<=9;i++)
b[i-s]=ab[i];
int temp, c;
for(c=0; c<=r; c++)
{
temp=a[c];
for(i=c; i>0 && temp<a[i-1]; i--)
a[i]=a[i-1];
a[i]=temp;
}
for(c=0; c<s; c++)
{
temp=b[c];
for(i=c; i>0 && temp<b[i-1]; i--)
b[i]=b[i-1];
b[i]=temp;
}
//merging
int L1, L2, L;
L1=0;
L2=0;
L=0;
while(L1<=r && L2<s)
{
if(a[L1] < b[L2])
{
ab[L]=a[L1];
L++;
L1++;
}
else
{
ab[L]=b[L2];
L++;
L2++;
}
}
if(L1 > r)
for(i=0; i<s-L2; i++)
ab[L+i]=b[L2+i];
else
for(i=0; i<r-L1; i++)
ab[L+i]=a[L1+i];
}
void print()
{
cout<<"Sorted array "<<endl;
for(int i=0; i<=9; i++)
cout<<ab[i]<<endl;
}
};

int main()
{
merging obj;

obj.input();
obj.sort();
obj.print();
}
OUTPUT:
PROGRAM 15:

Write a program to create a linked list consisting of ten nodes and enter values into the list
items?
#include <iostream>

using namespace std;

struct node

int data;

node *link;

};
class list

private: node *start, *cur, *temp;

public: list() {

start=NULL;

//Member Functions to create and add data into list

void append(int n)

if(start==NULL)

start=new node;

start-> data=n;

start-> link=NULL;

else

cur=start;

while(cur->link!=NULL)

cur=cur->link;

temp=new node;

temp->data=n;

temp->link=NULL;

cur->link=temp;

}
}

void print()

cout<<"\nData in link list\n\n";

cur=start;

while(cur->link!=NULL)

cout<<cur->data<<endl;

cur=cur->link;

cout<<cur->data<<endl;

};

int main()

list obj;

int val, p;

cout<<"Enter five values\n";

for(int i=1; i<=5; i++)

cin>>val;

obj.append(val);

obj.print();
}

OUTPUT:

PROGRAM 17:

Write a Program to count the total number of items or nodes in a linked list?

#include <iostream>

using namespace std;

struct node

int data;

node*link;

};

class list

private:
node*start,*cur,*temp;

public:

list()

start=NULL;

//member function to add items into list

add_item(int n)

//creat first node & assign data

if(start==NULL)

start=new node;

start->data=n;

start->link=NULL;

else

cur=start;

//go to end of list

while(cur->link!=NULL)

cur=cur->link;

//creat and add new nodes at end

temp=new node;

temp->data=n;
temp->link=NULL;

cur->link=temp;

//member fuction to count nodes

int count()

int c=0;

cur=start;

while(cur->link!=NULL)

c++;

cur=cur->link;

return c+1;

};

main()

list obj;

int val;

cout<<"enter five values \n";

for(int i=1; i<=5;i++)

cin>>val;
obj.add_item(val);

cout<<"total items="<<obj.count();

OUTPUT:

PROGRAM 18:

Write a Program to delete a specified node in a linked list?

#include<iostream>

using namespace std;

struct node

int data;

node*link;

};

class list

private:

node*start,*cur,*temp;
public:

list()

start=NULL;

add_item(int n)

node*temp;

//creat first node and assign data

if(start==NULL)

start=new node;

start->data=n;

start->link=NULL;

else

cur=start;

//go to end of list

while(cur->link!=NULL)

cur=cur->link;

//create and add new nodes at end

temp= new node;

temp->data=n;

temp->link=NULL;
cur->link=temp;

//member function to delete node

del(int n)

//go to specified node

cur=temp=start;

while(cur->link!=NULL)

if(cur->data==n)

cout<<"\n number found & deleted \n";

temp->link=cur->link;

delete cur;

break;

temp=cur;

cur=cur->link;

cout<<"\n data not found\n";

//member function to print values

print()

{
cout<<"data in link list\n";

cur=start;

while(cur->link!=NULL)

cout<<cur->data<<endl;

cur=cur->link;

//print last node value

cout<<cur->data<<endl;

};

main()

list obj;

int val;

cout<<"enter five values \n";

for(int i=1;i<=5;i++)

cin>>val;

obj.add_item(val);

cout<<"enter number to search & delete?";

cin>>val;

obj.del(val);

obj.print();
}

OUTPUT:

PROGRAM 20:

Write a Program to create a double- linked list consisting of 10 nodes?

#include<iostream>

using namespace std;

struct node

node*p;

int data;

node*n;

};
class list

private:

node*start,*cur,*temp;

public:

list()

start=NULL;

add_item(int x)

//create first node and assign data

if(start==NULL)

start=new node;

start->p=NULL;

start->data=x;

start->n=NULL;

else

node*prev;

cur=start;

//go to end of list

while(cur->n!=NULL)
cur=cur->n;

//create and add new nodes at end

prev=temp;

temp=new node;

temp->p=prev;

temp->data=x;

temp->n=NULL;

cur->n=temp;

print()

cout<<"data in link list\n";

cur=start;

while(cur->n!=NULL)

cout<<cur->data<<endl;

cur=cur->n;

//print last node value

cout<<cur->data<<endl;

};

main()

{
list obj;

int val;

cout<<"enter five values \n";

for(int i=1; i<=5; i++)

cin>>val;

obj.add_item(val);

obj.print();

OUTPUT:
PROGRAM 21:

Write a Program to insert a new node in a Binary Search Tree?

#include<iostream>

#include<conio.h>

using namespace std;

struct node{

int data;

node *left;

node *right;

};

class tree

private:

node *start,*cur,*temp;

public:

tree()

start=NULL;

void create(int x);

void search(int x);

};

int main()
{

tree obj;

int val,s,opt,c=1;

while(c)

cout<<"1: create new Binary tree \n";

cout<<"2: Search Value from binary tree \n";

cout<<"3:Exit \n\n";

cout<<"Select your Choice [1-3]?";

cin>>opt;

switch(opt)

case 1:

cout<<"Enter ten Values \n";

for(int i=1; i<=10; i++)

cin>>val;

obj.create(val);

break;

case 2:

cout<<"Enter Value to Search?";

cin>>s;

obj.search(s);

break;
case 3:

c=0;

break;

void tree::create(int x){

if(start==NULL)

start=new node;

start->data=x;

start->left=NULL;

start->right=NULL;

else{

node *parent;

cur=start;

while(cur!=NULL)

if(cur->data==x)

cout<<"data already exist";

return;

if(cur->data<x)
{parent=cur;cur=cur->right;

else{

parent=cur;cur=cur->left;

temp=new node;

temp->data=x;

temp->left=NULL;

temp->right=NULL;

if(parent==NULL)

parent=temp;

else if(parent==NULL)

parent->left=temp;

else

parent->right=temp;

void tree::search(int x)

if(start==NULL)

cout<<"tree is empty";

getch();

return;
}

else{

cur=start;

while(cur!=NULL)

if(cur->data==x)

cout<<"data found";

getch();

return;

if(cur->data<x)

cur=cur->right;

else

cur=cur->left;

if(cur==NULL)

cout<<"Value not found";

getch();

}
OUTPUT:

PROGRAM 22:

Write a Program to search a Binary Search Tree?

#include<iostream>

using namespace std;

struct node

int data;

node*left;

node*right;

};

class tree

private:

node *start, *cur, *temp;

public:

tree()

start = NULL;

}
void create(int x);

void search(int x);

};

main()

tree obj;

int val,s, opt, c=1;

while(c)

cout<<"1: Create new Binary Tree \n" ;

cout<<"2: Search Value from Binary Tree \n" ;

cout<<"3: Exit \n\n" ;

cout<<"Select your Choice [1-3]?" ; cin>>opt;

switch(opt)

case 1:

cout<<"Enter ten value \n" ;

for(int i=1; i<=10; i++)

cin>>val;

obj.create(val);

break;
case 2:

cout<<"Enter value to search ?";

cin>>s;

obj.search(s);

break;

case 3: c=0; break;

void tree::create(int x)

if(start==NULL)

start = new node;

start->data = x;

start->left = NULL;

start->right = NULL;

else

node*parent;

cur=start;

while(cur!=NULL)

if(cur->data == x)
{

cout<<"Data already exist";

return;

if(cur->data < x)

{parent=cur; cur=cur->right;}

else

{parent = cur; cur=cur->left;}

temp = new node;

temp->data = x;

temp->left = NULL;

temp->right = NULL;

if (parent ==NULL)

parent=temp;

else if(parent->data>x)

parent->left=temp;

else

parent->right = temp;

void tree::search(int x)

if(start == NULL)
{

cout<<"Tree is empty";

return;

else

cur=start;

while(cur!=NULL)

if(cur->data == x)

cout<<"data found";

if(cur->data < x)

cur=cur->right;

else

cur=cur->left;

if(cur == NULL)

cout<<"Value not found";

}
OUTPUT:

PROGRAM 23:

Write a Program for deleting a leaf node from a binary search tree?

#include<iostream>

#include<conio.h>

using namespace std;

struct node

int data;

node *left;

node *right;

};

class tree

private:

node *start, *cur, *temp;


int top;

public:

tree()

start = NULL;

void create(int);

void inorder(void);

void inord(node*s);

};

main()

tree obj;

int val,p;

cout<<"enter ten value \n";

for(int i=1; i<=10; i++)

cin>>val;

obj.create (val);

obj.inorder();

getch();

void tree::inorder()
{

inord(start);

void tree::inord(node*s)

if(s!=NULL)

inord(s->left);

cout<<s->data<<"\t";

inord(s->right);

void tree::create(int x)

if(start==NULL)

//create root node& assign data

start = new node;

start ->data = x;

start->left= NULL;

start->right= NULL;

else

{
node* parent;

cur = start;

while(cur!=NULL)

if(cur->data== x)

{cout<<"data already exist\n";return;}

if(x>cur->data)

{parent=cur; cur=cur->right;}

else

{parent= cur; cur=cur->left;}

//create and insert new node

temp = new node;

temp->data =x;

temp->left=NULL;

temp->right=NULL;

if(parent==NULL)

parent = temp;

else if (parent->data>x)

parent->left=temp;

else

parent->right=temp;

}
OUTPUT:

PROGRAM 24:

Write a Program for preorder transversal of binary search tree?

#include<iostream>

#include<conio.h>

using namespace std;

struct node

int data;

node *left;

node *right;

};

class tree

private:

node *start, *cur, *temp;

int top;
public:

tree()

start = NULL;

void create(int x);

void preorder(void);

};

int main()

tree obj;

int val,p;

cout<<"enter ten value \n";

for(int i=1; i<=10; i++)

cin>>val;

obj.create (val);

obj.preorder();

getch();

void tree::preorder()

int top = 0;
cout<<"\nPrint data in preorder\n\n";

node *stack[10];

stack[top]=start;

while(top>=0)

cur = stack[top];

top--;

while(cur!=NULL)

cout<<cur->data<<endl;

if(cur->right!=NULL)

top++;

stack[top]= cur->right;

cur = cur ->left;

void tree::create(int x)

if(start==NULL)

start = new node;

start ->data = x;
start->left= NULL;

start->right= NULL;

else

node* parent;

cur = start;

while(cur!=NULL)

if(cur->data== x)

cout<<"data already exist\n";return;}

if(x>cur->data)

parent=cur; cur=cur->right;}

else

parent= cur; cur=cur->left;}

//create and insert new node

temp = new node;

temp->data =x;

temp->left=NULL;

temp->right=NULL;
if(parent==NULL)

parent = temp;

else if (parent->data>x)

parent->left=temp;

else

parent->right=temp;

OUTPUT:

PROGRAM 25:

Write a Program for in order transversal of binary search tree?


#include<iostream>

#include<conio.h>

using namespace std;

struct node

int data;

node *left;

node *right;

};

class tree

private:

node *start, *cur, *temp;

int top;

public:

tree()

start = NULL;

void create(int);

void inorder(void);

void inord(node*s);

};

main()

{
tree obj;

int val,p;

cout<<"enter ten value \n";

for(int i=1; i<=10; i++)

cin>>val;

obj.create (val);

obj.inorder();

getch();

void tree::inorder()

inord(start);

void tree::inord(node*s)

if(s!=NULL)

inord(s->left);

cout<<s->data<<"\t";

inord(s->right);

}
void tree::create(int x)

if(start==NULL)

//create root node& assign data

start = new node;

start ->data = x;

start->left= NULL;

start->right= NULL;

else

node* parent;

cur = start;

while(cur!=NULL)

if(cur->data== x)

{cout<<"data already exist\n";return;}

if(x>cur->data)

{parent=cur; cur=cur->right;}

else

{parent= cur; cur=cur->left;}

//create and insert new node


temp = new node;

temp->data =x;

temp->left=NULL;

temp->right=NULL;

if(parent==NULL)

parent = temp;

else if (parent->data>x)

parent->left=temp;

else

parent->right=temp;

OUTPUT:

PROGRAM 25:

Write a Program for postorder transversal of binary search tree?

#include<iostream>

#include<conio.h>
using namespace std;

struct node

int data;

node *left;

node *right;

};

class tree

private:

node *start, *cur, *temp;

int top;

public:

tree()

start = NULL;

void create(int);

void postorder(void);

void post(node*s);

};

int main()

tree obj;

int val,p;
cout<<"enter ten value \n";

for(int i=1; i<=10; i++)

cin>>val;

obj.create (val);

obj.postorder();

getch();

void tree::postorder()

post(start);

void tree::post(node*s)

if(s!=NULL)

post(s->left);

post(s->right);

cout<<s->data<<"\t";

void tree::create(int x)

{
if(start==NULL)

//create root node& assign data

start = new node;

start ->data = x;

start->left= NULL;

start->right= NULL;

else

node* parent;

cur = start;

while (cur!=NULL)

if(cur->data== x)

{cout<<"data already exist\n";return;}

if(x>cur->data)

{parent=cur; cur=cur->right;}

else

{parent= cur; cur=cur->left;}

//create and insert new node

temp = new node;

temp->data =x;
temp->left=NULL;

temp->right=NULL;

if(parent==NULL)

parent = temp;

else if (parent->data>x)

parent->left=temp;

else

parent->right=temp;

OUTPUT:

You might also like