39
LAB MANUAL OF DATA STRUCTURES NAME: SYED UMER HUSSAIN ROLL NO: 2014-CS-294 TEACHER: MISS MEHWISH SIR SYED UNIVERSITY OF ENGINEERING AND TECHNOLOGY UNIVERSITY ROAD, KARACHI 75300

ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

LAB MANUAL

OF

DATA STRUCTURES

NAME: SYED UMER HUSSAIN

ROLL NO: 2014-CS-294TEACHER: MISS MEHWISH

SIR SYED UNIVERSITY OF ENGINEERING AND TECHNOLOGY

UNIVERSITY ROAD, KARACHI 75300

Page 2: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

INDEXLAB NO TASK NO TOPIC REMRAKS Sign1 1 Input from array

2 Array with dynamic memory

3 Display array address with dynamic memory

2 1 Input an array & calculate its product

2 Stack with push, pop and display functions

3 Bubble & selection sorting

4 Class sorting with selection, bubble, & insertion sorting

5 QuickSort

6 ShellSort

7 MergeSort

8 LinkLists

9 Binary Search Trees

Page 3: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

LAB # 1

TASK # 1

Q: Take an input from array and print this input on screen and also find their product:

CODE:#include <iostream>using namespace std;int main(){

int arr[10], n;int pro;cout << "Enter the size of array but maximum 10" << endl;cin >> n;cout << "Enter the values of array" << endl;for (int i=0;i<n;i++){

cin >> arr[i];}for (int i=0;i<n;i++)cout << "You have entered " << arr[i] << endl;for (int i=0;i<n;i++){pro = arr[i]*arr[i];cout << "Product is: " << pro << endl;}

cin.get();cin.ignore();

}

OUTPUT:

Page 4: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

TASK # 2

Q: Take an input from array and print this on screen with dynamic memory allocation:

CODE:#include <iostream>using namespace std;int main(){

int n;cout << "Enter the size of array" << endl;cin >> n;int *arr;arr = new int [n];if (arr == nullptr){

cout << "Error" << endl;}else{

cout << "Enter the values of array" << endl;for (int i=0;i<n;i++){cin >> arr[i];}cout << "You have entered " << endl;for (int i=0;i<n;i++){

cout << arr[i] << ", ";} delete[] arr;

}cin.get();cin.ignore();

}

OUTPUT:

Page 5: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

TASK # 3

Q: Take an input from array and print the address on screen with dynamic memory allocation:

CODE:

#include <iostream>using namespace std;int main(){

int n;cout << "Enter the size of array" << endl;cin >> n;int *arr;arr = new int [n];if (arr == nullptr){

cout << "Error" << endl;}else

{cout << "Enter the values of array" << endl;for (int i=0;i<n;i++){cin >> arr[i];}

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

cout << "Address of " << arr[i] << " is " << &arr[i] << endl;

} delete[] arr;

}cin.get();cin.ignore();

}

OUTPUT:

Page 6: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

LAB # 2

Q. Take an input an array and calculate its product.

CODE:#include <iostream>using namespace std;int main(){

int arr[10], n;int pro=1;cout << "Enter the size of array but maximum 10" << endl;cin >> n;cout << "Enter the values of array" << endl;for (int i=0;i<n;i++){

cin >> arr[i];}for (int i=0;i<n;i++)cout << "You have entered " << arr[i] << endl;for (int i=0;i<n;i++){pro=pro*arr[i];}cout << "Product is" << pro << endl;cin.get();cin.ignore();

}

OUTPUT:

Page 7: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

Q. Create a class stack and its three objects with push, pop and display functions.

CODE:

#include <iostream>using namespace std;

class stack{

int top;public:

int array[5];stack(){

top = -1;}void push(int d){

if (top>5){

cout << "Stack Overflow";}else{

top++;array[top] = d;

}}

int pop(){

int data;if (top<0){

cout << "Underflow << endl";return 0;

}else

data = array[top];top--;return data;

}

void display(){

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

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

}};

int main(){

stack a, b, Sum;

Page 8: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

cout << "Enter 5 Values for stack A: " << endl;for (int i = 0; i<5; i++){

cin >> a.array[i];}

cout << endl;

cout << "Enter 5 Values for stack B: " << endl;for (int i = 0; i<5; i++){

cin >> b.array[i];}cout << endl;

cout << "Sum of stack A & B: " << endl;for (int i = 0; i<5; i++){

Sum.array[i] = a.array[i] + b.array[i];cout << Sum.array[i] << endl;

}cout << endl;cin.get();cin.ignore();

}

OUTPUT:

Page 9: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

LAB # 3

Q: Write a program to sort the values with both bubble and selection sorting.

CODE:

#include <iostream>using namespace std;void bubble( int [], int);void selection (int [],int );int main(){

int array[5];int n;

cout << "Enter the elements of array " << endl;for(int i=0;i<5;i++){

cin >> array[i];}cout <<"Choose one sorting method" << endl;cout << "1 for bubble sort" << endl;cout << "2 for selection sort" << endl;cin >> n;switch(n){case 1:

cout << "The sorted values are " << endl;

bubble(array,5);break;case 2:

cout << "The sorted values are " << endl;

selection(array,5);break;}for(int i=0;i<5;i++)cout << array[i] << endl;

system("pause");}void bubble(int array[],int size){

int temp;for(int i=0;i<size;i++){

for(int j=size-1;j>i;j--)if (array[j]<array[j-1]){

temp=array[j];array[j]=array[j-1];array[j-1]=temp;

}}

Page 10: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

}void selection (int array[],int size){

int min,minvalue;for(int i=0;i<(size-1);i++){

min=i;minvalue=array[i];

for(int j=i+1;j<size;j++){

if (array[j]<minvalue){

minvalue=array[j];min=j;

}array[min] = array[i];array[i] = minvalue;

}}

}

OUTPUT:

Page 11: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

LAB # 4Q: Create a class sorting and its three methods selection , insertion and bubble and further create a method in class to take an input from array and display values in main.

CODING:

#include <iostream>using namespace std;class sorting{public:

int array[5];void selection (int array[],int size)

{int min,minvalue;for(int i=0;i<(size-1);i++){

min=i;minvalue=array[i];

for(int j=i+1;j<size;j++){

if (array[j]<minvalue){

minvalue=array[j];min=j;

}array[min] = array[i];array[i] = minvalue;

}}} void bubble(int array[],int size)

{int temp;for(int i=0;i<size;i++){

for(int j=size-1;j>i;j--)if (array[j]<array[j-1]){

temp=array[j];array[j]=array[j-1];array[j-1]=temp;

}}

}void insertion (int array[],int size){

int j, temp;

for (int i =1; i < size; i++){

Page 12: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

j = i;

while (j > 0 && array[j] < array[j-1]){ temp = array[j]; array[j] = array[j-1]; array[j-1] = temp; j--; }

}}void arr (){

cout << "Enter the elements of array " << endl;for(int i=0;i<5;i++){

cin >> array[i];}

}};int main(){

sorting a;int n;int array;a.arr();

cout <<"Choose one sorting method" << endl;cout << "1 for bubble sort" << endl;cout << "2 for selection sort" << endl;cout << "3 for insertion sort" << endl;cin >> n;switch(n){ case 1:

cout << "The sorted values are " << endl;

a.bubble(a.array,5);break;case 2:

cout << "The sorted values are " << endl;

a.selection(a.array,5);break;case 3:

cout << "The sorted values are " << endl;a.insertion( a.array,5);break;

}for(int i=0;i<5;i++)cout <<a.array[i] << endl;

system("pause");}

Page 13: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

OUTPUT:

Page 14: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

LAB # 5

CODING:

#include <iostream>#include <conio.h>using namespace std;

void quickSort(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; // partition while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } } // recursion if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right);}int main(){

int a[10],count=0,n; cout<<"Ener 10 values in unsorted order : \n"; for (n=0;n<10;n++) { cin>>a[n]; count++; } n=0; quickSort(a,n,count-1);

cout<<"The Sorted order is : \n"; for (n=0;n<10;n++) { cout<<a[n]<<"\n";

Page 15: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

} getch();}

OUTPUT:

LAB # 6

Q .Sort thr array elements using shellsort.

CODING:

#include <iostream>using namespace std;void shellsort(int [],int );

int main(){

int array[5];

cout << "Enter the elements of array " << endl;for(int i=0;i<5;i++){

cin >> array[i];}

Page 16: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

cout << "The sorted values are " << endl;shellsort(array,5);

for(int i=0;i<5;i++)cout << array[i] << endl;

system("pause");}void shellsort(int arr[],int num)

{int tmp,i,j,k;

for(i=5/2; i>0; i=i/2)_ {_

for(j=i; j<5; j++)_ {_

for(k=j-i; k>=0; k=k-i)_ {_

if(arr[k+i]>=arr[k])_ break;_

else_ {_

tmp=arr[k];_ arr[k]=arr[k+i];_ arr[k+i]=tmp;_

}_ }_

}_}}

OUTPUT:

Page 17: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

LAB # 7

Q. Sort the array elements using mergesort.

CODING:

#include <iostream>#include <stdlib.h>using namespace std;void Merge(int *A,int *L,int leftcount,int *R,int rightcount){

int i,j,k;i=0; j=0;k=0;while (i<leftcount && j< rightcount){

if(L[i]<R[j]){

A[k] = L[i];k++;i++;

}else{

A[k] = R[j];k++; j++;

}}

while(i<leftcount){

A[k] = L[i];k++;i++;

}while(j< rightcount){

A[k] = R[j];k++; j++;

}}

void mergesort(int *A,int n){

int mid,i,*L,*R;if(n<2)

return;mid = n/2;L = (int*)malloc(mid*sizeof(int));R = (int*)malloc((n-mid)*sizeof(int));for(i=0;i<mid;i++)

L[i] = A[i];for (i=mid;i<n;i++)

R[i-mid] = A[i];

Page 18: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

mergesort(L,mid);mergesort(R,n-mid);Merge(A,L,mid,R,n-mid);

}int main(){

int *arr;int n;cout << "Enter the size of array" << endl;cin >> n;arr = new int [n];cout << "Enter the elements of array " << endl;for(int i=0;i<n;i++){

cin >> arr[i];}

cout << "The sorted values are " << endl;mergesort(arr,n);

for(int i=0;i<n;i++)cout << arr[i] << endl;delete[] arr;system("pause");

}

OUTPUT:

Page 19: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

LAB # 8

LINK LIST

CODING:

#include<iostream>#include<cstdio>#include<cstdlib>using namespace std;/* * Node Declaration */struct node{ int info; struct node *next;}*start; /* * Class Declaration */class single_llist{ public: node* create_node(int); void insert_begin(); void insert_pos(); void insert_last(); void delete_pos(); void sort(); void search(); void update(); void reverse(); void display(); single_llist() { start = NULL; }}; /* * Main :contains menu */int main(){ int choice, nodes, element, position, i; single_llist sl;

Page 20: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

start = NULL; while (1) { cout<<endl<<"---------------------------------"<<endl; cout<<endl<<"Operations on singly linked list"<<endl; cout<<endl<<"---------------------------------"<<endl; cout<<"1.Insert Node at beginning"<<endl; cout<<"2.Insert node at last"<<endl; cout<<"3.Insert node at position"<<endl; cout<<"4.Sort Link List"<<endl; cout<<"5.Delete a Particular Node"<<endl; cout<<"6.Update Node Value"<<endl; cout<<"7.Search Element"<<endl; cout<<"8.Display Linked List"<<endl; cout<<"9.Reverse Linked List "<<endl; cout<<"10.Exit "<<endl; cout<<"Enter your choice : "; cin>>choice; switch(choice) { case 1: cout<<"Inserting Node at Beginning: "<<endl; sl.insert_begin(); cout<<endl; break; case 2: cout<<"Inserting Node at Last: "<<endl; sl.insert_last(); cout<<endl; break; case 3: cout<<"Inserting Node at a given position:"<<endl; sl.insert_pos(); cout<<endl; break; case 4: cout<<"Sort Link List: "<<endl; sl.sort(); cout<<endl; break; case 5: cout<<"Delete a particular node: "<<endl; sl.delete_pos(); break; case 6: cout<<"Update Node Value:"<<endl; sl.update(); cout<<endl; break; case 7: cout<<"Search element in Link List: "<<endl; sl.search(); cout<<endl; break; case 8: cout<<"Display elements of link list"<<endl; sl.display(); cout<<endl;

Page 21: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

break; case 9: cout<<"Reverse elements of Link List"<<endl; sl.reverse(); cout<<endl; break; case 10: cout<<"Exiting..."<<endl; exit(1); break; default: cout<<"Wrong choice"<<endl; } }} /* * Creating Node */node *single_llist::create_node(int value){ struct node *temp, *s; temp = new(struct node); if (temp == NULL) { cout<<"Memory not allocated "<<endl; return 0; } else { temp->info = value; temp->next = NULL; return temp; }} /* * Inserting element in beginning */void single_llist::insert_begin(){ int value; cout<<"Enter the value to be inserted: "; cin>>value; struct node *temp, *p; temp = create_node(value); if (start == NULL) { start = temp; start->next = NULL; } else { p = start; start = temp; start->next = p; } cout<<"Element Inserted at beginning"<<endl;

Page 22: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

} /* * Inserting Node at last */void single_llist::insert_last(){ int value; cout<<"Enter the value to be inserted: "; cin>>value; struct node *temp, *s; temp = create_node(value); s = start; while (s->next != NULL) { s = s->next; } temp->next = NULL; s->next = temp; cout<<"Element Inserted at last"<<endl; } /* * Insertion of node at a given position */void single_llist::insert_pos(){ int value, pos, counter = 0; cout<<"Enter the value to be inserted: "; cin>>value; struct node *temp, *s, *ptr; temp = create_node(value); cout<<"Enter the postion at which node to be inserted: "; cin>>pos; int i; s = start; while (s != NULL) { s = s->next; counter++; } if (pos == 1) { if (start == NULL) { start = temp; start->next = NULL; } else { ptr = start; start = temp; start->next = ptr; } } else if (pos > 1 && pos <= counter) { s = start;

Page 23: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

for (i = 1; i < pos; i++) { ptr = s; s = s->next; } ptr->next = temp; temp->next = s; } else { cout<<"Positon out of range"<<endl; }} /* * Sorting Link List */void single_llist::sort(){ struct node *ptr, *s; int value; if (start == NULL) { cout<<"The List is empty"<<endl; return; } ptr = start; while (ptr != NULL) { for (s = ptr->next;s !=NULL;s = s->next) { if (ptr->info > s->info) { value = ptr->info; ptr->info = s->info; s->info = value; } } ptr = ptr->next; }} /* * Delete element at a given position */void single_llist::delete_pos(){ int pos, i, counter = 0; if (start == NULL) { cout<<"List is empty"<<endl; return; } cout<<"Enter the position of value to be deleted: "; cin>>pos; struct node *s, *ptr; s = start; if (pos == 1)

Page 24: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

{ start = s->next; } else { while (s != NULL) { s = s->next; counter++; } if (pos > 0 && pos <= counter) { s = start; for (i = 1;i < pos;i++) { ptr = s; s = s->next; } ptr->next = s->next; } else { cout<<"Position out of range"<<endl; } free(s); cout<<"Element Deleted"<<endl; }} /* * Update a given Node */void single_llist::update(){ int value, pos, i; if (start == NULL) { cout<<"List is empty"<<endl; return; } cout<<"Enter the node postion to be updated: "; cin>>pos; cout<<"Enter the new value: "; cin>>value; struct node *s, *ptr; s = start; if (pos == 1) { start->info = value; } else { for (i = 0;i < pos - 1;i++) { if (s == NULL) { cout<<"There are less than "<<pos<<" elements"; return;

Page 25: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

} s = s->next; } s->info = value; } cout<<"Node Updated"<<endl;} /* * Searching an element */void single_llist::search(){ int value, pos = 0; bool flag = false; if (start == NULL) { cout<<"List is empty"<<endl; return; } cout<<"Enter the value to be searched: "; cin>>value; struct node *s; s = start; while (s != NULL) { pos++; if (s->info == value) { flag = true; cout<<"Element "<<value<<" is found at position "<<pos<<endl; } s = s->next; } if (!flag) cout<<"Element "<<value<<" not found in the list"<<endl; } /* * Reverse Link List */void single_llist::reverse(){ struct node *ptr1, *ptr2, *ptr3; if (start == NULL) { cout<<"List is empty"<<endl; return; } if (start->next == NULL) { return; } ptr1 = start; ptr2 = ptr1->next; ptr3 = ptr2->next; ptr1->next = NULL; ptr2->next = ptr1;

Page 26: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

while (ptr3 != NULL) { ptr1 = ptr2; ptr2 = ptr3; ptr3 = ptr3->next; ptr2->next = ptr1; } start = ptr2;} /* * Display Elements of a link list */void single_llist::display(){ struct node *temp; if (start == NULL) { cout<<"The List is Empty"<<endl; return; } temp = start; cout<<"Elements of list are: "<<endl; while (temp != NULL) { cout<<temp->info<<"->"; temp = temp->next; } cout<<"NULL"<<endl;}

OUTPUT:

Page 27: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF
Page 28: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

LAB # 9

BINARY SEARCH TREE

CODING:

#include <iostream>#include <cstdlib>using namespace std;

class BinarySearchTree{ private: struct tree_node { tree_node* left; tree_node* right; int data; }; tree_node* root; public: BinarySearchTree() { root = NULL; } bool isEmpty() const { return root==NULL; } void print_inorder(); void inorder(tree_node*); void print_preorder(); void preorder(tree_node*); void print_postorder(); void postorder(tree_node*); void insert(int); void remove(int);};

// Smaller elements go left// larger elements go rightvoid BinarySearchTree::insert(int d){ tree_node* t = new tree_node; tree_node* parent; t->data = d; t->left = NULL; t->right = NULL; parent = NULL; // is this a new tree? if(isEmpty()) root = t; else { //Note: ALL insertions are as leaf nodes tree_node* curr;

Page 29: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

curr = root; // Find the Node's parent while(curr) { parent = curr; if(t->data > curr->data) curr = curr->right; else curr = curr->left; }

if(t->data < parent->data) parent->left = t; else parent->right = t; }}

void BinarySearchTree::remove(int d){ //Locate the element bool found = false; if(isEmpty()) { cout<<" This Tree is empty! "<<endl; return; } tree_node* curr; tree_node* parent; curr = root; while(curr != NULL) { if(curr->data == d) { found = true; break; } else { parent = curr; if(d>curr->data) curr = curr->right; else curr = curr->left; } } if(!found)

{ cout<<" Data not found! "<<endl; return; }

// 3 cases : // 1. We're removing a leaf node // 2. We're removing a node with a single child // 3. we're removing a node with 2 children

// Node with single child if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL

Page 30: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

&& curr->right == NULL)) { if(curr->left == NULL && curr->right != NULL) { if(parent->left == curr) { parent->left = curr->right; delete curr; } else { parent->right = curr->right; delete curr; } } else // left child present, no right child { if(parent->left == curr) { parent->left = curr->left; delete curr; } else { parent->right = curr->left; delete curr; } } return; }

//We're looking at a leaf node if( curr->left == NULL && curr->right == NULL)

{ if(parent->left == curr) parent->left = NULL; else parent->right = NULL;

delete curr; return;

}

//Node with 2 children // replace node with smallest value in right subtree if (curr->left != NULL && curr->right != NULL) { tree_node* chkr; chkr = curr->right; if((chkr->left == NULL) && (chkr->right == NULL)) { curr = chkr; delete chkr; curr->right = NULL; } else // right child has children { //if the node's right child has a left child // Move all the way down left to locate smallest element

Page 31: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

if((curr->right)->left != NULL) { tree_node* lcurr; tree_node* lcurrp; lcurrp = curr->right; lcurr = (curr->right)->left; while(lcurr->left != NULL) { lcurrp = lcurr; lcurr = lcurr->left; }

curr->data = lcurr->data; delete lcurr; lcurrp->left = NULL; } else { tree_node* tmp; tmp = curr->right; curr->data = tmp->data;

curr->right = tmp->right; delete tmp; }

} return;

}

}

void BinarySearchTree::print_inorder(){ inorder(root);}

void BinarySearchTree::inorder(tree_node* p){ if(p != NULL) { if(p->left) inorder(p->left); cout<<" "<<p->data<<" "; if(p->right) inorder(p->right); } else return;}

void BinarySearchTree::print_preorder(){ preorder(root);}

void BinarySearchTree::preorder(tree_node* p){ if(p != NULL) { cout<<" "<<p->data<<" "; if(p->left) preorder(p->left); if(p->right) preorder(p->right);

Page 32: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

} else return;}

void BinarySearchTree::print_postorder(){ postorder(root);}

void BinarySearchTree::postorder(tree_node* p){ if(p != NULL) { if(p->left) postorder(p->left); if(p->right) postorder(p->right); cout<<" "<<p->data<<" "; } else return;}

int main(){ BinarySearchTree b; int ch,tmp,tmp1; while(1) { cout<<endl<<endl; cout<<" Binary Search Tree Operations "<<endl; cout<<" ----------------------------- "<<endl; cout<<" 1. Insertion/Creation "<<endl; cout<<" 2. In-Order Traversal "<<endl; cout<<" 3. Pre-Order Traversal "<<endl; cout<<" 4. Post-Order Traversal "<<endl; cout<<" 5. Removal "<<endl; cout<<" 6. Exit "<<endl; cout<<" Enter your choice : "; cin>>ch; switch(ch) { case 1 : cout<<" Enter Number to be inserted : "; cin>>tmp; b.insert(tmp); break; case 2 : cout<<endl; cout<<" In-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_inorder(); break; case 3 : cout<<endl; cout<<" Pre-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_preorder(); break; case 4 : cout<<endl; cout<<" Post-Order Traversal "<<endl; cout<<" --------------------"<<endl; b.print_postorder(); break;

Page 33: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF

case 5 : cout<<" Enter data to be deleted : "; cin>>tmp1; b.remove(tmp1); break; case 6 : return 0; } }}

OUTPUT:

Page 34: ssuetcssectione.files.wordpress.com …  · Web viewLAB MANUAL. OF. DATA STRUCTURES. NAME: SYED UMER HUSSAIN. ROLL NO: 2014-CS-294. TEACHER: MISS MEHWISH. SIR SYED UNIVERSITY OF