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

Data Structure Programs

This file contains cpp codes of :- 1. Insertion, deletion and traversing in array. 2. PUSH and POP on Stacks 3. Converting from Infix to postfix 4. Insertion and Deletion in Queues 5. Circular Queue 6. Insertion and Deletion in Linked List 7. Traversing binary Tree 8. Binary Search 9. Linear Search 10. Merger Sorting 11. Bubble Sort 12. Quick Sort 13. Insertion Sort 14. Breadth First Search 15. Depth First Search 16. Traversing and Finding depth of BST and Display 17. Binary Tree Display

Uploaded by

Abhijeet Tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Data Structure Programs

This file contains cpp codes of :- 1. Insertion, deletion and traversing in array. 2. PUSH and POP on Stacks 3. Converting from Infix to postfix 4. Insertion and Deletion in Queues 5. Circular Queue 6. Insertion and Deletion in Linked List 7. Traversing binary Tree 8. Binary Search 9. Linear Search 10. Merger Sorting 11. Bubble Sort 12. Quick Sort 13. Insertion Sort 14. Breadth First Search 15. Depth First Search 16. Traversing and Finding depth of BST and Display 17. Binary Tree Display

Uploaded by

Abhijeet Tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 71

University Institute of Engineering and

Technology Panjab University, Chandigarh


Department of Computer Science

DATA STRUCTURES

Submitted To - Submitted By-


Mrs. Seema Rani Abhijeet Tyagi
CSE (ue173003)

1
S.No Name of Program Page No.

1. Insertion, deletion and traversing in array. 3-6

2. PUSH and POP on Stacks 7-11

3. Converting from Infix to postfix 12-14

4. Insertion and Deletion in Queues 15-17

5. Circular Queue 18-22

6. Insertion and Deletion in Linked List 23-32

7. Traversing binary Tree 33-39

8. Binary Search 40-41

9. Linear Search 42-43

10. Merger Sorting 44-46

11. Bubble Sort 47-48

12. Quick Sort 49-51

13. Insertion Sort 52-53

14. Breadth First Search 54-55

15. Depth First Search 56-58

16. Traversing and Finding depth of BST and Display 59-62

17. Binary Tree Display 63-71

2
1.//Insertion, deletion and traversing in array

#include<iostream>

#include<conio.h>

using namespace std;

int traversing (int*,int);

int searching (int*,int,int);

int insertion (int*,int,int,int);

int deletion (int*,int,int);

int main()

int array[30],choice,location,item,i,length,position;

cout<<"\nEnter length of array : ";

cin>>length;

cout<<"Enter element of array \n";

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

{ cin>>array[i];

again:

cout<<"Travers= 1\nSearch = 2\nInsert = 3\nDelete = 4\nExit = 5 \n";

cout<<"Enter choice: ";

cin>>choice;

switch(choice)

{ case 1: traversing(array,length);

goto again;

case 2: searching(array,item,length);

3
goto again;

case 3: cout<<"Enter Item and Position to add \n";

cin>>item>>position;

insertion(array,length,item,position);

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

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

goto again;

case 4: cout<<"Enter position to delete \n";

cin>>position;

length =deletion(array,length,position);

cout<<"New array after delete \n";

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

{cout<<array[i]<<"\n";}

goto again;

case 5: break;

default: cout<<"wrong value\n";

goto again;

getch();

int traversing(int array[],int length)

{ for(int i=0;i<=length-1;i++)

{cout<<array[i]<<" ";}

4
int searching(int array[],int item,int length)

{ int i;

cout<<"\nEnter item to search \n";

cin>>item;

int location =-1;

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

{ if ( array[i]==item)

{ location=i;

if (location==-1)

{cout<<"\nItem not found ";

else{cout<<" \nItem is present at (index value): "<<location;

int insertion(int array[],int length,int item,int position)

{ int j=length-1;

while(j>=position)

{ array[j+1]=array[j];

j=j-1;

array[position]=item;

length=length+1;

5
int deletion(int array[],int length,int position)

while(position<length)

{ array[position]=array[position+1];

position=position+1;

length=length-1;

return length;

2.//PUSH and POP on stacks

6
#include<stdio.h>

#define MAX 5

int a[MAX],top=-1;

void push();

void pop();

void display();

int peek();

int main()

int ch;

printf("1. PUSH or INSERT\n");

printf("2. POP or DELETE\n");

printf("3. DISPLAY\n");

printf("4. END\n");

printf("5. PEEK");

while(1)

printf("\n\nEnter Choice:");

scanf("%d",&ch);

switch(ch)

case 1:

push();

7
break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("Exit from stack");

return 0;

case 5:

peek();

break;

8
void push()

int data;

if(top==MAX-1)

printf("\nstack is full\n");

else

printf("enter element\n");

scanf("%d",&data);

printf("%d is pushed into stack\n", data);

top++;

a[top]=data;

void pop()

if(top==-1)

printf("stack is empty\n");

else

printf("popped element : %d",a[top]);

9
top--;

void display()

int i;

if (top>=0)

printf("Elements of the stack:");

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

printf("\n%d",a[i]);

else

printf("The stack is empty");

int peek()

if(top<0)

printf("Stack is empty\n");

else

10
{

printf("%d is the peek element",a[top]);

return a[top];

11
3.//Converting from Infix to postfix

#include<iostream>

#include<bits/stdc++.h>

using namespace std;

int prec(char c)

if(c=='^')

return 3;

if(c=='*'&&c=='/')

return 2;

if (c=='+'&&c=='-')

return 1;

else

return -1;

void infixToPostfix(string s)

stack<char> st;

st.push('N');

int l = s.length();

string ns;

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

if((s[i] >= 'a' && s[i] <= 'z')||(s[i] >= 'A' && s[i] <= 'Z'))

ns+=s[i];

else if(s[i] == '(')

12
st.push('(');

else if(s[i] == ')')

while(st.top() != 'N' && st.top() != '(')

char c = st.top();

st.pop();

ns += c;

if(st.top() == '(')

char c = st.top();

st.pop();

} else{

while(st.top() != 'N' && prec(s[i]) <= prec(st.top()))

char c = st.top();

st.pop();

ns += c;

st.push(s[i]);

while(st.top() != 'N')

char c = st.top();

13
st.pop();

ns += c;

cout << ns << endl;

int main()

string exp = "a+b*(c^d-e)^(f+g*h)-i";

infixToPostfix(exp);

return 0;

};

14
4.//Insertion and Deletion in Queues

#include <iostream>

#include<conio.h>

#define MAX_SIZE 100

using namespace std;

class Queue {

private:

int item, i;

int arr_queue[MAX_SIZE];

int rear;

int front;

public:

Queue() {

rear = 0;

front = 0;

void insert() {

if (rear == MAX_SIZE)

cout << "\n## Queue Reached Max!";

else {

cout << "\nEnter The Value to be Insert : ";

cin>>item;

cout << "\n## Position : " << rear + 1 << " , Insert Value : " << item;

arr_queue[rear++] = item;

void deleteData() {

15
if (front == rear)

cout << "\n## Queue is Empty!";

else {

cout << "\n## Position : " << front << " , Remove Value :" << arr_queue[front];

front++;

}}

void display() {

cout << "\nQueue Size : " << (rear - front);

for (i = front; i < rear; i++)

cout << "\nPosition : " << i << " , Value : " << arr_queue[i];

}};

int main() {

int choice, exit_p = 1;

Queue obj;

do { cout << "\n\nQueue Main Menu";

cout << "\n1.Insert \n2.Delete \n3.Display \nOthers to exit";

cout << "\nEnter Your Choice : ";

cin>>choice;

switch (choice) {

case 1:

obj.insert();

break;

case 2:

obj.deleteData();

break;

case 3:

obj.display();

16
break;

default:

exit_p = 0;

break; }

} while (exit_p);

return 0; }

5.//Circular Queue

17
#include<iostream>

#define SIZE 100

using namespace std;

class node

public:

node()

next = NULL;

int data;

node *next;

}*front=NULL,*rear=NULL,*n,*temp,*temp1;

class cqueue

public:

void insertion();

void deletion();

void display();

};

int main()

cqueue cqobj;

int ch;

do

18
cout<<"\nMain Menu";

cout<<"\n1. Insert\t2. Delete\t3. Display\t4. Exit\nEnter Your Choice: ";

cin>>ch;

switch(ch)

case 1:

cqobj.insertion();

cqobj.display();

break;

case 2:

cqobj.deletion();

break;

case 3:

cqobj.display();

break;

case 4:

break;

default:

cout<<"\n\nWrong Choice!!! Try Again.";

}while(ch!=4);

return 0;

void cqueue::insertion()

n=new node[sizeof(node)];

cout<<"Enter the Element: ";

19
cin>>n->data;

if(front==NULL)

front=n;

else

rear->next=n;

rear=n;

rear->next=front;

void cqueue::deletion()

int x;

temp=front;

if(front==NULL)

cout<<"\nCircular Queue Empty!!!";

else

if(front==rear)

x=front->data;

delete(temp);

front=NULL;

20
rear=NULL;

else

x=temp->data;

front=front->next;

rear->next=front;

delete(temp);

cout<<"\nElement "<<x<<" is Deleted";

display();

void cqueue::display()

temp=front;

temp1=NULL;

if(front==NULL)

cout<<"Circular Queue Empty!!!";

else

cout<<"Circular Queue Elements are:\t";

while(temp!=temp1)

cout<<temp->data<<" ";

21
temp=temp->next;

temp1=front;

22
6..//Insertion and Deletion in Linked List

#include<iostream>

#include<cstdio>

#include<cstdlib>

using namespace std;

struct node

int info;

struct node *next;

}*start;

class single_llist

public:

node* create_node(int);

void insert_begin();

void insert_pos();

void insert_last();

void delete_pos();

void display();

single_llist()

start = NULL;

};

int main()

23
{

int choice, nodes, element, position, i;

single_llist sl;

start = NULL;

while (1)

cout<<"1.Insert Node at beg"<<endl;

cout<<"2.Insert node at last"<<endl;

cout<<"3.Insert node at pos"<<endl;

cout<<"4.Delete a Node"<<endl;

cout<<"5.Display LL"<<endl;

cout<<"6.Exit "<<endl;

cout<<"Enter your choice : ";

cin>>choice;

switch(choice)

{case 1:

cout<<"Inserting Node at Beg: "<<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 pos:"<<endl;

24
sl.insert_pos();

cout<<endl;

break;

case 4:

cout<<"Delete a node: "<<endl;

sl.delete_pos();

break;

case 5:

cout<<"Display LL"<<endl;

sl.display();

cout<<endl;

break;

case 6:

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)

25
{

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;

26
start = temp;

start->next = p;

cout<<"Element Inserted at beginning"<<endl;

//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()

27
{

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;

28
}

else if (pos > 1 && pos <= counter)

s = start;

for (i = 1; i < pos; i++)

ptr = s;

s = s->next;

ptr->next = temp;

temp->next = s;

else

cout<<"Positon out of range"<<endl;

//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;

29
cout<<"Enter the position of value to be deleted: ";

cin>>pos;

struct node *s, *ptr;

s = start;

if (pos == 1)

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

30
cout<<"Position out of range"<<endl;

free(s);

cout<<"Element Deleted"<<endl;

//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;

31
32
7.//Traversing binary tree

#include <iostream>

using namespace std;

struct node{

int value;

node *left;

node *right;

};

class btree{

public:

btree();

~btree();

void insert(int key);

node *search(int key);

void destroy_tree();

void inorder_print();

void postorder_print();

void preorder_print();

private:

void destroy_tree(node *leaf);

void insert(int key, node *leaf);

node *search(int key, node *leaf);

void inorder_print(node *leaf);

33
void postorder_print(node *leaf);

void preorder_print(node *leaf);

node *root;

};

btree::btree(){

root = NULL;

btree::~btree(){

destroy_tree();

void btree::destroy_tree(node *leaf){

if(leaf != NULL){

destroy_tree(leaf->left);

destroy_tree(leaf->right);

delete leaf;

void btree::insert(int key, node *leaf){

if(key < leaf->value){

if(leaf->left != NULL){

34
insert(key, leaf->left);

}else{

leaf->left = new node;

leaf->left->value = key;

leaf->left->left = NULL;

leaf->left->right = NULL;

}else if(key >= leaf->value){

if(leaf->right != NULL){

insert(key, leaf->right);

}else{

leaf->right = new node;

leaf->right->value = key;

leaf->right->right = NULL;

leaf->right->left = NULL;

void btree::insert(int key){

if(root != NULL){

insert(key, root);

}else{

root = new node;

root->value = key;

root->left = NULL;

35
root->right = NULL;

node *btree::search(int key, node *leaf){

if(leaf != NULL){

if(key == leaf->value){

return leaf;

if(key < leaf->value){

return search(key, leaf->left);

}else{

return search(key, leaf->right);

}else{

return NULL;

node *btree::search(int key){

return search(key, root);

void btree::destroy_tree(){

destroy_tree(root);

36
void btree::inorder_print(){

inorder_print(root);

cout << "\n";

void btree::inorder_print(node *leaf){

if(leaf != NULL){

inorder_print(leaf->left);

cout << leaf->value << ",";

inorder_print(leaf->right);

void btree::postorder_print(){

postorder_print(root);

cout << "\n";

void btree::postorder_print(node *leaf){

if(leaf != NULL){

inorder_print(leaf->left);

inorder_print(leaf->right);

cout << leaf->value << ",";

void btree::preorder_print(){

37
preorder_print(root);

cout << "\n";

void btree::preorder_print(node *leaf){

if(leaf != NULL){

cout << leaf->value << ",";

inorder_print(leaf->left);

inorder_print(leaf->right);

int main(){

//btree tree;

btree *tree = new btree();

tree->insert(10);

tree->insert(6);

tree->insert(14);

tree->insert(5);

tree->insert(8);

tree->insert(11);

tree->insert(18);

cout<<"Preorder Traversal"<<"\n";

tree->preorder_print();

38
cout<<"\n"<<"Inorder Traversal"<<"\n";

tree->inorder_print();

cout<<"\n"<<"Postorder Traversal"<<"\n";

tree->postorder_print();

delete tree;

39
8.//Binary search

#include<iostream>

#include<conio.h>

using namespace std;

int main()

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)

40
{

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();

41
9.//Linear Search

#include<iostream>

#include<conio.h>

using namespace std;

int main()

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;

42
}

if(c==0)

cout<<"Number not found..!!";

else

cout<<num<<" found at position "<<pos;

getch();

43
10.// Merge sorting

#include <iostream>

using namespace std;

void Merge(int *a, int low, int high, int mid)

int i, j, k, temp[high-low+1];

i = low;

k = 0;

j = mid + 1;

while (i <= mid && j <= high)

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

temp[k] = a[i];

k++;

i++;

else

temp[k] = a[j];

k++;

j++;

while (i <= mid)

44
temp[k] = a[i];

k++;

i++;

while (j <= high)

temp[k] = a[j];

k++;

j++;

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

a[i] = temp[i-low];

void MergeSort(int *a, int low, int high)

int mid;

if (low < high)

mid=(low+high)/2;

MergeSort(a, low, mid);

MergeSort(a, mid+1, high);

Merge(a, low, high, mid);

45
}

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];

MergeSort(arr, 0, n-1); cout<<"\nSorted Data ";

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

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

return 0;}

46
11.//Bubble sort

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int n, i, arr[50], j, temp;

cout<<"Enter total numberof 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;

47
}

cout<<"Elements sorted successfully....!!\n";

cout<<"Sorted list in ascending order :\n";

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

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

getch();

48
12.//Quick sort

#include<iostream>

using namespace std;

// Display the Array

void displayArray(int arr[],int size) {

int i;

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

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

// swap elements in the array

void swap(int arr[],int idx1,int idx2) {

int temp;

temp = arr[idx1];

arr[idx1] = arr[idx2];

arr[idx2] = temp;

// partition the array into halves s.t that pivot element

// gets stored at it's correct position in the sorted subarray

int partition(int arr[],int start_idx,int end_idx) {

int k = arr[end_idx];

49
int i = start_idx - 1;

int j;

// iterate over the subrray

for (j=start_idx;j<end_idx;j++) {

if (arr[j] <= k) {

i++;

swap(arr,i,j);

swap(arr,i+1,end_idx);

// return the position of pivot element

return i+1;

// quicksort the array recursively

void quickSort(int arr[], int start_idx, int end_idx) {

int pivot; // pivot element

if (start_idx < end_idx) {

pivot = partition(arr, start_idx, end_idx);

// recursively sort both the partitions

quickSort(arr, start_idx, pivot - 1);

quickSort(arr, pivot + 1, end_idx);

50
int main() {

int arr[] = {7,4,5,2,3,19,11,6,9,12,8};

int size = sizeof(arr)/sizeof(arr[0]);

cout<<"\nOriginal Array :: ";

displayArray(arr,size);

quickSort(arr,0,size-1);

cout<<"\nSorted Array :: ";

displayArray(arr,size);

cout<<endl;

return 0;

51
13.//Insertion sort

#include<iostream>

#include<conio.h>

using namespace std;

int main(){

int a[20], size, i, j, temp;

cout<<"\n\tEnter Array Size:";

cin>>size;

cout<<"\n\t Enter array Elements:";

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

cin>>a[i];

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

temp = a[i];

j = i-1;

while(j>=0 && a[j]>temp)

a[j+1] = a[j];

j--;

52
}

a[j+1] = temp;

cout<<"\n\tArray after insertion sort:";

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

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

return 0;

53
14.//Breadth First Search

#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;

int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];

int main()

int m;

//clrscr();

cout <<"Enter no of vertices:";

cin >> n;

cout <<"Enter 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 to traverse from:";

cin >>v;

cout <<"Visitied vertices:";

cout <<v<<" ";

visited[v]=1;

k=1;

while(k<n)

54
{

for(j=1; j<=n; j++)

if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)

visit[j]=1;

qu[rare++]=j;

v=qu[front++];

cout<<v <<" ";

k++;

visit[v]=0;

visited[v]=1;

getch();

return 0;

55
15.//Depth Breadth Search

#include<iostream>

using namespace std;

struct node{

struct node *left;

struct node *right;

int info;

} *root=NULL;

node *getnode(){

node *root;

root=new node;

return root;

node *insert(node *root,int digit)

if(root==NULL)

root= getnode();

root->left=root->right=NULL;

root->info=digit;

else

56
if(root->info>=digit){

root->left=insert(root->left,digit);

else

root->right=insert(root->right,digit);

return(root);

}}

void dfs(struct node* root){

if(root==NULL)

return;

cout<<root->info<<" ";

dfs(root->left);

dfs(root->right);

void display(node *root,int space){

if(root==NULL){

return;}

space +=3;

display(root->right,space);

cout<<"\n";

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

cout<<" ";

57
cout<<root->info;

cout << endl;

display(root->left,space);

int main(){

root=insert(root,78);

insert(root,52);

insert(root,123);

insert(root,137);

insert(root,60);

insert(root,63);

insert(root,56);

cout<<"dfs traversal is: ";

dfs(root);

display(root,0);}

58
16.// Traversing and finding depth of Binary Search Tree and display

#include<iostream>

using namespace std;

struct node{

struct node *left;

struct node *right;

int info;

} *root=NULL;

node *getnode(){

node *root;

root=new node;

return root;

node *insert(node *root,int digit)

if(root==NULL)

root= getnode();

root->left=root->right=NULL;

root->info=digit;

else

59
{

if(root->info>=digit){

root->left=insert(root->left,digit);

else

root->right=insert(root->right,digit);

return(root);

}}

void preorder(struct node* root){

if(root==NULL)

return;

cout<<root->info<<" ";

preorder(root->left);

preorder(root->right);

void inorder(struct node* root){

if(root==NULL)

return;

inorder(root->left);

cout<<root->info<<" ";

inorder(root->right);

60
void postorder(struct node* root){

if(root==NULL)

return;

postorder(root->left);

postorder(root->right);

cout<<root->info<<" ";

void display(node *root,int space){

if(root==NULL){

return;

space +=3;

display(root->right,space);

cout<<"\n";

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

cout<<" ";

cout<<root->info;

cout << endl;

display(root->left,space);}

int depth(node* root){

if(root==NULL)

return 0;

else{

int ldepth=depth(root->left);

61
int rdepth=depth(root->right);

return max(ldepth, rdepth)+1;}

int main(){

root=insert(root,78);

insert(root,52);

insert(root,123);

insert(root,137);

insert(root,60);

insert(root,63);

insert(root,56);

cout<<"preorder traversal is: ";

preorder(root);

cout<<"\ninorder traversal is: ";

inorder(root);

cout<<"\npostorder traversal is: ";

postorder(root);

display(root,0);

cout<<"depth of tree is: "<<depth(root);

return 0;

62
17.//Binary Tree Display

#include<iostream>

using namespace std;

#define COUNT 10

struct Node{

int data;

Node* left, *right;

};

Node* newNode(int data)

Node* node = new Node;

node->data = data;

node->left = node->right = NULL;

return node;

// It will do reverse inorder traversal

void print2DUtil(Node *root, int space)

// Base case

if (root == NULL)

return;

// Increase distance between levels

space += COUNT;

63
// Process right child first

print2DUtil(root->right, space);

// Print current node after space

// count

cout<<"\n";

for (int i = COUNT; i < space; i++)

cout<<" ";

cout<<root->data<<"\n";

// Process left child

print2DUtil(root->left, space);

// Wrapper over print2DUtil()

void print2D(Node *root)

// Pass initial space count as 0

print2DUtil(root, 0);

// Driver program to test above functions

int main()

Node *root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

64
root->left->left = newNode(4);

root->left->right = newNode(5);

root->right->left = newNode(6);

root->right->right = newNode(7);

//root->left->left->left = newNode(8);

root->left->left->right = newNode(9);

root->left->right->left = newNode(10);

root->left->right->right = newNode(11);

root->right->left->left = newNode(12);

root->right->left->right = newNode(13);

root->right->right->left = newNode(14);

root->right->right->right = newNode(15);

print2D(root);

return 0;}

65
18.//BST insertion and deletion

#include<iostream>

using namespace std;

bool c=false;

struct node

int data;

node* left;

node* right;

};

struct node* getNode(int data)

{ node* newNode=new node();

newNode->data=data;

newNode->left=NULL;

newNode->right=NULL;

return newNode;

void inorder(struct node* root)

if (root != NULL)

inorder(root->left);

cout<<root->data<<" ";

inorder(root->right);

66
}

node* findMin(node*root)

while(root->left!=NULL)

root=root->left;

return root;

struct node* Insert(struct node* root, int data)

if (root == NULL)

return getNode(data);

if (data < root->data)

root->left = Insert(root->left, data);

else if (data > root->data)

root->right = Insert(root->right, data);

return root;

bool Search(node*root,int value)

{ if(root==NULL)

return false;

else if(root->data == value)

return true;

else if(value < root-> data)

67
Search(root->left,value);

else if(value > root->data)

Search(root->right,value);

node* Delete( node* root,int value)

c=Search(root,value);

if(root==NULL)

return root;

else if(value< root->data)

root->left= Delete(root->left,value);

else if(value> root->data)

root->right= Delete(root->right,value);

// Node deletion

else

//case 1: Leaf Node

if(root->left==NULL&&root->right==NULL)

delete root;

root=NULL;

68
return root;

//case 2: one child

else if(root->left==NULL)

struct node* temp=root;

root=root->right;

delete temp;

return root;

else if(root->right==NULL)

struct node* temp=root;

root=root->left;

delete temp;

return root;

//case 3: 2 child

else

{ struct node*temp=findMin(root->right);

root->data=temp->data;

root->right=Delete(root->right,temp->data);

}}

return root;}

int main()

node* root=NULL;

69
root=Insert(root,20);

Insert(root,15);

Insert(root,25);

Insert(root,18);

Insert(root,10);

Insert(root,16);

Insert(root,19);

// Insert(root,17);

//Node insertion

root=Insert(root,17);

cout<<"\nNode Inserted"<<endl;

cout<<"\nAfter Insertion: ";

cout<<"\nInorder: ";

inorder(root);

cout<<endl<<endl;

//Deletion of node

cout<<"Before Deletion: "<<endl;

cout<<"Inorder: ";

inorder(root);

cout<<endl<<endl;

Delete(root,15);

if(c)

70
cout<<"Node Deleted"<<endl;

cout<<"\nAfter Deletion: "<<endl;

cout<<"Inorder: ";

inorder(root);

cout<<endl;

else

cout<<"Node Not Found"<<endl;

return 0;

71

You might also like