Sunteți pe pagina 1din 74

DATA STRUCTURE

Submitted by:-Ishaan dhiman


17BEC1150
4ECE-2, GROUP A

SUBMITTED TO:-HARPREET
MAM
EXPERIMENT-1
AIM: -WAP to transverse element an array
SOURCE CODE:-
#include<iostream.h>

using namespace std;

int main()

int a[40];

int i,n;

cout<<"Enter the size of array : ";

cin>>n;

cout<<"Enter elements of an array : ";

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

cin>>a[i];

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

cout<<a[i];

return 0;

}
OUTPUT:-

RESULT:-We have learnt how to transverse an


element in an array.
EXPERIMENT -2
AIM:- WAP to insert an element an array
SOURCE CODE:-

#include<iostream.h>

using namespace std;

int main()

int a[20],n,insert, i, p;

cout<<"Enter Array Size ";

cin>>n;

cout<<"Enter array elements ";

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

cin>>a[i];

cout<<"Enter element to be insert ";

cin>>insert;

cout<<"Enter the position ";

cin>>p;

for(i=n; i>p; i--)

a[i]=a[i-1];

a[p]=insert;

cout<<"Element inserted";

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

cout<<a[i]<<" ";
}

return 0;

0UTPUT:-

RESULT:-We have learnt how to insert elements in an


array.
EXPERIMENT-3
AIM:-WAP to delete an element at pth position in an array
SOURCE CODE:-
#include<iostream.h>

using namespace std;

int main()

int a[40];

int i,n,insert=70,p=3;

cout<<"Enter the size of array ";

cin>>n;

cout<<"Enter elements of an array ";

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

cin>>a[i];

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

cout<<a[i];

for(i=p;i<n;i++)

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

}
cout<<"new array is";

for(i=0;i<n-1;i++)

cout<<a[i]<<" ";

return 0;}

OUTPUT:-

Result:-I have learnt how to delete an element of


particular position in an array.
EXPERIMENT-4
AIM:- Wap to search an element at pth position in array
SOURCE CODE:-
#include<iostream.h>
using namespace std;
main()
{
int a[10];
int p;
cout<<"enter the elemnts of array";
for(inti=0;i<6;i++)
{
cin>>a[i];
}
cout<<"enter the element to be searched";
cin>>p;
for(inti=0;i<5;i++)
{
while(a[i]==p)
{
cout<<"number found";
break;
}
}
}
OUTPUT:-

Result:-I have learnt how to find an element of a particular


position.
EXPERIMENT-5
AIM:- Write a program to search an array element from an array using
linear search algorithm.

SOURCE CODE: -

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10], i, num, n, c=0, pos;
cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
getch();
}
OUTPUT:-

Result:-I have learnt how to find an element in array using linear


search means find an element in a list until whole search is done.
EXPERIMENT-6
AIM: Write a program to search an array element from an array using
Binary search algorithm.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
else if(arr[middle] == search)
{
cout<<search<<" found at location
"<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in
the list.";
}
getch();
}

OUTPUT:-

Result:-I learnt how to find a desired element in sorted array


means binary search.

EXPERIMENT-7
AIM: Program to sort an array of integers in ascending order using
bubble sort.

SOURCE CODE:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], j, temp;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" numbers :";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using bubble sort technique...\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"Elements sorted successfully..!!\n";
cout<<"Sorted list in ascending order :\n";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

OUTPUT: -
Result:-I have leatrnt how to arrange an element in ascending
order using bubble sort in which steps are repeated.

EXPERIMENT-8
AIM:- Write a program to sort of integers in ascending order using insertion sort.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort ... \n";
for(i=1; i<size; i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
cout<<"Array after sorting : \n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

Output:-
Result:-I have learnt to arrange element in ascending order using
insertion sort.Insertion sort means sort an array one item at a
time.

EXPERIMENT-9
AIM:- Write a program to sort an array of integers in ascending order using selection
Sort.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[100], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort...\n";
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Now the Array after sorting is :\n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

OUTPUT:-
Result:-I have learnt how to write a program using selection sort. In it the
smallest element is selected from the unsorted array and swapped with the
leftmost element.

EXPERIMENT-10
AIM:-Write a program to insert an element in a stack using array.
Source code:-
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

int push(int [], int &, int);


void display(int [], int);
const int SIZE = 30;

void main()
{
clrscr();
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{
cout<<"Overflow..!!..Aborting..Press a key
to exit..\n";
getch();
exit(1);
}
cout<<"Element inserted successfully..!!\n";
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter more ? (y/n).. ";
cin>>ch;
}
getch();
}

int push(int stack[], int &top, int elem)


{
if(top == SIZE-1)
{
return -1;
}
else
{
top++;
stack[top] = elem;
}
return 0;
}
void display(int stack[], int top)
{
cout<<stack[top]<<" <-- "<<"\n";
for(int i=top-1; i>=0; i--)
{
cout<<stack[i]<<"\n";
}
}

OUTPUT:-

Result:-I have learnt about stack.it is an abstract data type that


serves as a collection of data type. I have also learnt how to insert
an element in stack using array.
EXPERIMENT-11
AIM:- Write a program to delete an element in a stack using array.
Source code:-
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

int push(int [], int &, int);


void display(int [], int);
const int SIZE = 30;

void main()
{
clrscr();
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{
cout<<"Overflow..!!..Aborting..Press a key
to exit..\n";
getch();
exit(1);
}
cout<<"Element inserted successfully..!!\n";
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter more ? (y/n).. ";
cin>>ch;
}
getch();
}

int push(int stack[], int &top, int elem)


{
if(top == SIZE-1)
{
return -1;
}
else
{
top++;
stack[top] = elem;
}
return 0;
}
void display(int stack[], int top)
{
cout<<stack[top]<<" <-- "<<"\n";
for(int i=top-1; i>=0; i--)
{
cout<<stack[i]<<"\n";
}
}

OUTPUT:-

Result:-I have learnt how to delete an element in stack.

EXPERIMENT-12
AIM:- Write a program to convert infix to postfix expression.
Source code:-
cout<<"+num2 = "<<+num2<<endl;
cout<<"-num1 = "<<-num1<<"\t";
cout<<"-num2 = "<<-num2;

getch();
}#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

int num1, num2;


cout<<"Enter a positive number: ";
cin>>num1;
cout<<"Enter a negative number: ";
cin>>num2;

cout<<"\nIf:-\n\nnum1 = "<<num1<<"\tnum2 =
"<<num2<<endl<<endl;
cout<<"\nThen:-\n\nnum1 = "<<num1<<"\t";
cout<<"num2 = "<<num2<<endl;
cout<<"+num1 = "<<+num1<<"\t";

OUTPUT:-

Result:- I have learnt how to convert an infix expression to postfix expression.

EXPERIMENT-13
Aim: Write a program to find factorial of a no. using recursion.
Source Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, i, fact=1;
cout<<"Enter a number : ";
cin>>num;
for(i=num; i>0; i--)
{
fact=fact*i;
}
cout<<"Factorial of "<<num<<" is "<<fact;
getch();
}

OUTPUT:-

Result: - I have learnt how to find factorial of a number using recursion.


EXPERIMENT-14
Aim:- Write a program to insert an element in a linear queue.
Source Code:-
#include <iostream>
using namespace std;
int queue[9], n = 9, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
rear++;
queue[rear] = val;
}
}

int main() {
cout<<"## Insert elements to queue ##\n";
for (int i = 1; i <= 10; i++){
cout<<"Enter Element:";
Insert();
}

cout<<"Inserted elements in queue\n";


for (int i = front; i <= rear; i++){
cout<<queue[i]<<" ";

return 0;
}

Output:-

Result:-I have learnt how to insert an element using linear queue means
organized form.

EXPERIMENT-15
Aim:- Write a program to delete an element in a linear queue.
Source Code:-
#include <iostream>
using namespace std;
int queue[9], n = 9, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
rear++;
queue[rear] = val;
}
}

void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front]
<<endl;
front++;;
}
}

int main() {
cout<<"## Insert elements to queue ##\n";
for (int i = 1; i <= 10; i++){
cout<<"Enter Element:";
Insert();
}
cout<<"\nInserted elements in queue\n";
for (int i = front; i <= rear; i++){
cout<<queue[i]<<" ";
}

cout<<"\n\nDeleting Element from queue\n";


Delete();

cout<<"\n\nElements Left in queue\n";


for (int i = front; i <= rear; i++){
cout<<queue[i]<<" ";
}

return 0;
}

OUTPUT:-
Result:- I have learnt how to delete an element of linear queue means which
are present in sequential form.

EXPERIMENT-16
AIM:- WAP to insert element in circular queue.
SOURCE CODE:-
#include <iostream>

using namespace std;


int cqueue[5];

int front = -1, rear = -1, n=5;

void insertCQ(int val) {

if ((front == 0 && rear == n-1) || (front == rear+1)) {

cout<<"Queue Overflow \n";

return;

if (front == -1) {

front = 0;

rear = 0;

} else {

if (rear == n - 1)

rear = 0;

else

rear = rear + 1;

cqueue[rear] = val ;

void displayCQ() {

int f = front, r = rear;

if (front == -1) {

cout<<"Queue is empty"<<endl;

return;

cout<<"Queue elements are :\n";

if (f <= r) {

while (f <= r){

cout<<cqueue[f]<<" ";
f++;

} else {

while (f <= n - 1) {

cout<<cqueue[f]<<" ";

f++;

f = 0;

while (f <= r) {

cout<<cqueue[f]<<" ";

f++;

cout<<endl;

int main() {

int ch, val;

cout<<"1)Insert\n";

cout<<"2)Display\n";

cout<<"3)Exit\n";

do {

cout<<"Enter choice : "<<endl;

cin>>ch;

switch(ch) {

case 1:

cout<<"Input for insertion: "<<endl;

cin>>val;

insertCQ(val);
break;

case 2:

displayCQ();

break;

case 3:

cout<<"Exit\n";

break;

default: cout<<"Incorrect!\n";

} while(ch != 3);

return 0;

OUTPUT:-
Result:-I have learnt how to insert an element in circular queue
means in which operation is performed on the basis of first in first
out.

EXPERIMENT-17
AIM:- WAP to implement quick sort of array.
SOURCE CODE:-

#include<iostream>

#include<cstdlib>

using namespace std;

void swap(int *a, int *b)

int temp;

temp = *a;

*a = *b;

*b = temp;

}
int Partition(int a[], int low, int high)

int pivot, index, i;

index = low;

pivot = high;

for(i=low; i < high; i++)

if(a[i] < a[pivot])

swap(&a[i], &a[index]);

index++;

swap(&a[pivot], &a[index]);

return index;

int RandomPivotPartition(int a[], int low, int high)

int pvt, n, temp;

n = rand();

pvt = low + n%(high-low+1);

swap(&a[high], &a[pvt]);

return Partition(a, low, high);

}
int QuickSort(int a[], int low, int high)

int pindex;

if(low < high)

pindex = RandomPivotPartition(a, low, high);

QuickSort(a, low, pindex-1);

QuickSort(a, pindex+1, high);

return 0;

int main()

int n, i;

cout<<"\nEnter the number of data element to be sorted: ";

cin>>n;

int arr[n];

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

cout<<"Enter element "<<i+1<<": ";

cin>>arr[i];

QuickSort(arr, 0, n-1);

cout<<"\nSorted Data ";


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

cout<<"->"<<arr[i];

return 0;

OUTPUT:-
Result:-I have learnt to implement a program using quick sort. It
gives result on n logn complexity.

EXPERIMENT-18
AIM:- WAP to transverse through linked list.
SOURCE CODE:-
#include <iostream>
using namespace std;

typedef struct node {


int data;
node *next;
};

int printList(node *traverse) {


if (traverse->next == NULL) {
return -1;
}
traverse=traverse->next;
printList(traverse);
cout << traverse->data << endl;
return 0;
}

int main() {
node *head = NULL;
for (int i = 0; i < 10; i++) {
node *newEntry = new node;
newEntry->data = i;
newEntry->next = head;
head = newEntry;
}
printList(head);
return 0;
}

OUTPUT:-

Result:- I have learnt about transverse linear structure. It means


moving sequential using node by node.

EXPERIMENT-19
AIM:- WAP to insert an element in linked list
SOURCE CODE:-
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
struct Node* next;
};

int size = 0;

Node* getNode(int data)


{
Node* newNode = new Node();

newNode->data = data;
newNode->next = NULL;
return newNode;
}

void insertPos(Node** current, int pos, int data)


{
if (pos < 1 || pos > size + 1)
cout << "Invalid postion!" << endl;
else {

while (pos--) {

if (pos == 0) {
Node* temp = getNode(data);

temp->next = *current;

*current = temp;
}
else
current = &(*current)->next;
}
size++;
}
}

void printList(struct Node* head)


{
while (head != NULL) {
cout << " " << head->data;
head = head->next;
}
cout << endl;
}

int main()
{
Node* head = NULL;
head = getNode(3);
head->next = getNode(5);
head->next->next = getNode(8);
head->next->next->next = getNode(10);
size = 4;

cout << "Linked list before insertion: ";


printList(head);

int data = 12, pos = 3;


insertPos(&head, pos, data);
cout << "Linked list after insertion of 12 at position 3: ";
printList(head);

data = 1, pos = 1;
insertPos(&head, pos, data);
cout << "Linked list after insertion of 1 at position 1: ";
printList(head);

data = 15, pos = 7;


insertPos(&head, pos, data);
cout << "Linked list after insertion of 15 at position 7: ";
printList(head);

return 0;
}

OUTPUT:-
Result :-I have learnt about linked list and how to insert an
element in linked list.

EXPERIMENT-20
AIM:- WAP to search an element in linked list
SOURCE CODE:-
#include<iostream>
#include<conio.h>
#include<malloc.h>

struct node

int data;

struct node *next;

first, *nw;

int search(int);

int main()

int no,i,item,pos;

first.next=NULL;

nw=&first;

printf("Enter The No of nodes, you want in linked list: ");


scanf("%d",&no);

printf("\n");

for(i=0;i< no;i++)

nw->next=(struct node *)malloc(sizeof(struct node));

printf("Enter element in node %d: ",i+1);

scanf("%d",&nw->data);

nw=nw->next;

nw->next=NULL;

printf("Linked list is:\n");

nw=&first;

while(nw->next!=NULL)

printf("%d\t",nw->data);
nw=nw->next;

printf("\n");

printf("Enter item to be searched : ");

scanf("%d",&item);

pos=search(item);

if(pos<=no)

printf("Your item is at node=%d",pos);

else

printf("Sorry! your item is not in linked list.");

getch();
return 0;

int search(int item)


{

int count=1;

nw=&first;

while(nw->next!=NULL)

if(nw->data==item)

break;

else

count++;

nw=nw->next;

return count;

OUTPUT:-
Result:-I have learnt about how to search an element which
are in linked list.

EXPERIMENT-21
AIM:- WAP to delete element in linked list
SOURCE CODE:-
#include<stdio.h>

#include<stdlib.h>

#include<assert.h>

struct Node

int data;

struct Node* next;

};

void deleteList(struct Node** head_ref)

struct Node* current = *head_ref;


struct Node* next;

while (current != NULL)

next = current->next;

free(current);

current = next;

*head_ref = NULL;

void push(struct Node** head_ref, int new_data)

struct Node* new_node =

(struct Node*) malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

int main()

struct Node* head = NULL;

push(&head, 2);

push(&head, 4);
push(&head, 1);

push(&head, 42);

push(&head, 5);

printf("\n Deleting linked list");

deleteList(&head);

printf("\n Linked list deleted");

OUTPUT:-

Result:-I have learnt how to delete an element of linked list.


In linked list each node contains a data field and reference.
EXPERIMENT-22
AIM:- WAP to implement beadth first search graph
SOURCE CODE:-
#include<iostream.H>
#include <list>

using namespace std;

class Graph
{
int V;

list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);

void BFS(int s);


};

Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)


{
adj[v].push_back(w);
}

void Graph::BFS(int s)
{
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;

list<int> queue;

visited[s] = true;
queue.push_back(s);

list<int>::iterator i;

while(!queue.empty())
{
s = queue.front();
cout << s << " ";
queue.pop_front();

for (i = adj[s].begin(); i != adj[s].end(); ++i)


{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
}

int main()
{
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

cout << "Following is Breadth First Traversal "


<< "(starting from vertex 2) \n";
g.BFS(2);
return 0;
}

OUTPUT

Result:-I have learnt about breadth search in searching of tree is


done.It uses queue.i have also learnt to implement breadth search.

EXPERIMENT-23
AIM:- WAP to implement deapth the first search on graph
SOURCE CODE:-
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];

main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}

cout <<"enter initial vertex";


cin >>v;
cout <<"ORDER OF VISITED VERTICES";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
return 0;
}

OUTPUT:-
Result:- I have learnt about depth search. It is based on
stack. I have also learnt about implement program in depth
seach.

EXPERIMENT-24
AIM- WAP to insert element in binary search tree.
SOURCE CODE
#include <iostream>
#include <queue>
using namespace std;

struct Node {
int key;
struct Node* left, *right;
};

struct Node* newNode(int key)


{
struct Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
};

void inorder(struct Node* temp)


{
if (!temp)
return;

inorder(temp->left);
cout << temp->key << " ";
inorder(temp->right);
}

void insert(struct Node* temp, int key)


{
queue<struct Node*> q;
q.push(temp);

while (!q.empty()) {
struct Node* temp = q.front();
q.pop();

if (!temp->left) {
temp->left = newNode(key);
break;
} else
q.push(temp->left);

if (!temp->right) {
temp->right = newNode(key);
break;
} else
q.push(temp->right);
}
}

int main()
{
struct Node* root = newNode(10);
root->left = newNode(11);
root->left->left = newNode(7);
root->right = newNode(9);
root->right->left = newNode(15);
root->right->right = newNode(8);

cout << " inserted values are:";


inorder(root);

int key = 12;


insert(root, key);

cout << endl;


cout << " inserted values are:";
inorder(root);

return 0;
}

Output-

Result-I have learnt about how to insert elements in binary search tree.

EXPERIMENT-25
AIM- WAP to delete an element in binary search tree.
SOURCE CODE
#include<bits/stdc++.h>
#include<iostream>
using namespace std;

class node
{
public:
int data;
node* left;
node* right;

node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};

void deleteTree(node* node)


{
if (node == NULL) return;

deleteTree(node->left);
deleteTree(node->right);

cout << "\n Deleting node: " << node->data;


free(node);
}

int main()
{
node *root = new node(1);
root->left = new node(2);
root->right = new node(3);
root->left->left = new node(4);
root->left->right = new node(5);

deleteTree(root);
root = NULL;

cout << "\n Tree deleted ";

return 0;
}

Output:-
Result:- I have learnt how to delete elements from Binary search tree.

EXPERIMENT-26
AIM- WAP to implement in order transversing in binary search tree.
SOURCE CODE

#include<iostream>
using namespace std;
struct tree_node
{
tree_node *left;
tree_node *right;
int data;
};
class bst
{
tree_node *root;
public:
bst()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(int item);
void inordertrav();
void inorder(tree_node *);
};
void bst::insert(int item)
{
tree_node *p=new tree_node;
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(item>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item<parent->data)
parent->left=p;
else
parent->right=p;
}
}
void bst::inordertrav()
{
inorder(root);
}
void bst::inorder(tree_node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
cout<<" "<<ptr->data<<" ";
inorder(ptr->right);
}
}
int main()
{
bst b;
b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20); cout<<"inorder"<<endl;
b.inordertrav();
return 0;
}

Output:-

Result:-I have learnt how to implement in order transversing in a binary


search.
EXPERIMENT-27

AIM- WAP to implement pre-order transversing in binary search tree.


SOURCE CODE

#include<iostream>
using namespace std;
struct tree_node
{
tree_node *left;
tree_node *right;
int data;
};
class bst
{
tree_node *root;
public:
bst()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(int item);
void preordertrav();
void preorder(tree_node *);
};
void bst::insert(int item)
{
tree_node *p=new tree_node;
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(item>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item<parent->data)
parent->left=p;
else
parent->right=p;
}
}
void bst::preordertrav()
{
preorder(root);
}
void bst::preorder(tree_node *ptr)
{
if(ptr!=NULL)
{
cout<<" "<<ptr->data<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
int main()
{
bst b;
b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20);
cout<<endl<<"preorder"<<endl;
b.preordertrav();
return 0;
}

Output:-

Result:-I have learnt how to implement pre order transversing in a binary


search tree
EXPERIMENT-28

AIM- WAP to implement post order transversing in binary search tree.


SOURCE CODE

#include<iostream>
using namespace std;
struct tree_node
{
tree_node *left;
tree_node *right;
int data;
};
class bst
{
tree_node *root;
public:
bst()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(int item);
void postordertrav();
void postorder(tree_node *);
};
void bst::insert(int item)
{
tree_node *p=new tree_node;
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(item>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item<parent->data)
parent->left=p;
else
parent->right=p;
}
}
void bst::postordertrav()
{
postorder(root);
}
void bst::postorder(tree_node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<" "<<ptr->data<<" ";
}
}
int main()
{
bst b;
b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20);
cout<<endl<<"postorder"<<endl;
b.postordertrav();
return 0;
}

Ouutput:-

Result:-In this experiment I have learnt how to implement post


order transversing in a binary search tree.

S-ar putea să vă placă și