23HCS4103 DS1 Merged
23HCS4103 DS1 Merged
DATA STRUCTURE
ABHINAV GUPTA
23HCS4103
SEMESTER: 3
Write a program to implement singly linked list (represented by head and tail) that supports the
following operations:
#include <iostream>
using namespace std;
class node{
private:
int elem;
node* next;
node(): next(NULL) {};
friend class sll;
};
class sll{
private:
node* head;
node*tail;
public:
sll(){
head=NULL;
tail = NULL;
}
~sll(){
node *p=NULL;
while(head!=NULL)
{
p=head;
head=head->next;
delete p;
}
}
bool empty(){
return head==tail;
}
int top(){
return head->elem;
}
int back(){
return tail->elem;
}
void addfrnt(int i){
node* ptr= new node;
ptr->elem=i;
if( head==NULL){
head=ptr;
tail = ptr;
}
else{
ptr->next=head;
head=ptr;
}
}
void display(){
node* ptr=head;
while(ptr!=NULL){
cout<<ptr->elem<<"->";
ptr=ptr->next;
}
cout<<endl;
}
void addtail(int i) {
node* temp = new node;
temp->elem = i;
temp->next = NULL;
if (tail != NULL) {
tail->next = temp;
tail = temp;
} else {
head = temp;
tail = temp;
}
}
void removefront(){
node*old= head;
head=old->next;
delete old;
}
void deletetail(){
node*ptr=head;
while(ptr->next!=tail){
ptr=ptr->next;
}
tail=ptr;
ptr=ptr->next;
delete ptr;
tail->next=NULL;
}
void insert( int pos, int i){
node* ptr=head;
int count=1;
while(count<pos-1){
ptr=ptr->next;
count++;
}
node*temp=new node;
temp->elem=i;
temp->next=ptr->next;
ptr->next=temp;
}
void deletepos(int pos){
node* ptr=head;
int count=1;
while(count<pos-1){
ptr=ptr->next;
count++;
}
node*temp = ptr->next;
ptr->next=temp->next;
delete temp;
}
void search(int i){
node*ptr=head;
int count=1;
while(ptr->elem!=i){
ptr=ptr->next;
count++;
}
cout<<count<<endl;
}
int count(){
int ct=0;
node* pt= head;
while(pt!=NULL){
ct++;
pt=pt->next;
}
return ct;
}
void middle(){
int ct=count();
node*ptr=head;
int count=1;
while(count<(ct/2+1)){
ptr=ptr->next;
count++;
}
cout<<ptr->elem<<endl;
}
void reverse(){
node* p,*p1,*p2;
p=head;
p1=p->next;
while(p1!=NULL){
p2=p1->next;
p1->next=p;
p=p1;
p1=p2;
}
head->next=NULL;
tail=head;
head=p;
return;
};
int main() {
string ans="Y";
cout<<"1. Add an element at front."<<endl;
cout<<"2. Add an element at last."<<endl;
cout<<"3. Add an element at a given index."<<endl;
cout<<"4. Delete an element from front."<<endl;
cout<<"5. Delete an element from last."<<endl;
cout<<"6. Delete an element at given index."<<endl;
cout<<"7. Search an element."<<endl;
cout<<"8. Reverse the list."<<endl;
cout<<"9. Return Middle element."<<endl;
cout<<"10. Display the list."<<endl;
sll A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add at first: ";cin>>n;
A.addfrnt(n);
break;
case 2:
n=0;
cout<<" Enter element to add at last: ";cin>>n;
A.addtail(n);
break;
case 3:
ind=0;
n=0;
cout<<" Enter index: ";cin>>ind;
cout<<" Enter element to add: ";cin>>n;
A.insert(ind,n);
break;
case 4:
A.removefront();
cout<<"First element deleted."<<endl;
break;
case 5:
A.deletetail();
cout<<"Last element deleted."<<endl;
break;
case 6:
ind=0;
cout<<" Enter index: ";cin>>ind;
A.deletepos(ind);
cout<<" Element "<<ind<<" deleted."<<endl;
break;
case 7:
n=0;
cout<<" Enter element to search: ";cin>>n;
A.search(n);
break;
case 8:
A.reverse();
cout<<"List Reversed."<<endl;
break;
case 9:
cout<<" Middle Element is:";
A.middle();
break;
case 10:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
OUTPUTS:
-- THANK YOU --
PRACTICAL 2
DATA STRUCTURE
ABHINAV GUPTA
23HCS4103
SEMESTER :3RD
QUESTION : Write a program to implement a Sorted Singly Linked List that supports the following
functions:
#include <iostream>
using namespace std;
class node{
private:
int elem;
node* next;
node(): next(NULL) {};
friend class sll;
};
class sll{
private:
node* head;
node*tail;
public:
sll(){
head=NULL;
tail = NULL;
}
~sll(){
node *p=NULL;
while(head!=NULL)
{
p=head;
head=head->next;
delete p;
}
}
bool empty(){
return head==tail;
}
int top(){
return head->elem;
}
int back(){
return tail->elem;
}
void add(int i) {
node* ptr = new node;
ptr->elem = i;
if (head == NULL) {
head = ptr;
tail = ptr;
} else if (i <= head->elem) {
ptr->next = head;
head = ptr;
} else {
node* temp = head;
while (temp->next != NULL && temp->next->elem < i) {
temp = temp->next;
}
ptr->next = temp->next;
temp->next = ptr;
if (ptr->next == NULL) {
tail = ptr;
}
}
}
void display(){
node* ptr=head;
while(ptr!=NULL){
cout<<ptr->elem<<"->";
ptr=ptr->next;
}
cout<<endl;
}
void removefront(){
node*old= head;
head=old->next;
delete old;
}
void deletetail(){
node*ptr=head;
while(ptr->next!=tail){
ptr=ptr->next;
}
tail=ptr;
ptr=ptr->next;
delete ptr;
tail->next=NULL;
}
void deleteElement(int i) {
if (empty()) {
cout << "List is empty, nothing to delete" << endl;
return;
}
else if (head->elem == i) {
removefront();
return;
}
else{
node* ptr = head;
node* prev = NULL;
while (ptr != NULL && ptr->elem != i) {
prev = ptr;
ptr = ptr->next;
}
if (ptr == NULL) {
cout << "Element " << i << " not found in the list" << endl;
return;
}
else{
prev->next = ptr->next;
if (ptr == tail) {
tail = prev;
}
delete ptr;
cout << "Element " << i << " deleted from the list" << endl;
}
} }
void search(int i){
node*ptr=head;
int count=1;
while(ptr->elem!=i){
ptr=ptr->next;
count++;
}
cout<<count<<endl;
}
int count(){
int ct=0;
node* pt= head;
while(pt!=NULL){
ct++;
pt=pt->next;
}
return ct;
}
};
int main() {
string ans="Y";
cout<<"1. Add an element"<<endl;
cout<<"2. Delete an element from front."<<endl;
cout<<"3. Delete an element from last."<<endl;
cout<<"4. Delete an element at given index."<<endl;
cout<<"5. Search an element."<<endl;
cout<<"6. Display the list."<<endl;
sll A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add: ";cin>>n;
A.add(n);
break;
case 2:
A.removefront();
cout<<"First element deleted."<<endl;
break;
case 3:
A.deletetail();
cout<<"Last element deleted."<<endl;
break;
case 4:
ind=0;
cout<<" Enter element: ";cin>>ind;
A.deleteElement(ind);
cout<<" Element "<<ind<<" deleted."<<endl;
break;
case 5:
n=0;
cout<<" Enter element to search: ";cin>>n;
A.search(n);
break;
case 6:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
1. Insert an element
2. Delete an element from the beginning
== THANK YOU ==
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science
Data structure
Assignment-III
Name : Abhinav Gupta
Roll no. : 23HCS4103
Ques: Write a program to implement doubly linked list as an ADT that
supports the following operations. List should be implemented using
two dummy nodes header and trailer.
class node{
int elem;
node* prev;
node* next;
void display(){
cout<<"forward display"<<endl;
node * ptr = header->next;
while(ptr!=trailer){
cout<<ptr->elem<<" ";
ptr=ptr->next;
}
cout<<endl;
}
void b_display(){
cout<<"backward display"<<endl;
node * ptr = trailer->prev;
while(ptr!=header){
cout<<ptr->elem<<" ";
ptr=ptr->prev;
}
cout<<endl;
}
void addback(int i){
node * ptr= new node;
ptr->elem=i;
ptr->next=trailer;
ptr->prev=trailer->prev;
trailer->prev->next=ptr;
trailer->prev=ptr;
}
void removefront(){
if (empty()) {
cout << "List is empty, cannot remove from beginning!" << endl;
return;
}
node * ptr= header->next;
header->next= ptr->next;
ptr->next->prev=header;
delete ptr;
}
void removeback(){
if (empty()) {
cout << "List is empty, cannot remove from end!" << endl;
return;
}
node * ptr=trailer->prev;
trailer->prev= ptr->prev;
ptr->prev->next=trailer;
delete ptr;
}
int countNodes(){
node * temp = header->next;
int count = 0;
while(temp != trailer){
count++;
temp = temp->next;
}
return count;
}
void reverseList() {
if (empty() || header->next == trailer->prev) {
return;
}
node* current = header->next;
node* temp = NULL;
while (current != trailer) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
temp = header->next;
header->next = trailer->prev;
trailer->prev = temp;
header->next->prev = header;
trailer->prev->next = trailer;
}
};
int main() {
string ans="Y";
cout<<"1. Add an element at front."<<endl;
cout<<"2. Add an element at last."<<endl;
cout<<"3. Delete an element from front."<<endl;
cout<<"4. Delete an element from last."<<endl;
cout<<"5. Reverse the list."<<endl;
cout<<"6. Count the number of node"<<endl;
cout<<"7. Display the list."<<endl;
cout<<"8. Display the Reverse of list."<<endl;
dll A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
int p;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add at front: ";cin>>n;
A.add(n);
break;
case 2:
n=0;
cout<<" Enter element to add at last: ";cin>>n;
A.addback(n);
break;
case 3:
A.removefront();
cout<<"Front element deleted."<<endl;
break;
case 4:
A.removeback();
cout<<"Last element deleted."<<endl;
break;
case 5:
A.reverseList();
cout<<"List Reversed."<<endl;
break;
case 6:
p = A.countNodes();
cout<<endl<<"the number of nodes are "<<p<<endl;
break;
case 7:
A.display();
break;
case 8:
A.b_display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Part ii. Insert an element x at the end of the doubly linked list
Part iii. Remove an element from the beginning of the doubly linked list
Part iv. Remove an element from the end of the doubly linked list
Part v. reverse the list
Thank you
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science
Data structure
Assignment-IV
Name : Abhinav Gupta
Roll no. : 23HCS4103
Ques: Write a program to implement circular linked list as an ADT
which supports the following operations
#include <iostream>
}
void advance(){
cursor=cursor->next;
}
void removeaftercur(){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
cursor->next=ptr->next;
delete ptr;
}
void remove(int x){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
while(ptr->next->elem!=x){
ptr=ptr->next;
}
node *temp=ptr->next;
ptr->next=temp->next;
delete temp;
}
void removecursor(){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
while(ptr->next!=cursor){
ptr=ptr->next;
}
cursor=ptr;
ptr=cursor->next;
cursor->next=ptr->next;
delete ptr;
}
void removebeforecur(){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
while(ptr->next->next!=cursor){
ptr=ptr->next;
}
node* temp= ptr->next;
ptr->next=cursor;
delete temp;
}
node* search(int x){
node * ptr=cursor->next;
while(ptr!=cursor){
if(ptr->elem==x){
return ptr;
}
ptr=ptr->next;
}
if(cursor->elem==x){
return cursor;
}
return NULL;
}
};
int main() {
string ans="Y";
cout<<"1. Add an element after cursor."<<endl;
cout<<"2. Add an element before cursor."<<endl;
cout<<"3. Delete a element X "<<endl;
cout<<"4. Search an element X."<<endl;
cout<<"5. Delete an element at cursor."<<endl;
cout<<"6. Display the list."<<endl;
cout<<"7. Remove element after cursor ."<<endl;
cout<<"8. Remove element before cursor."<<endl;
cll A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
node *p;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add after cursor: ";cin>>n;
A.addaftercursor(n);
break;
case 2:
n=0;
cout<<" Enter element to add before cursor: ";cin>>n;
A.addbeforecursor(n);
break;
case 3:
cout<<"enter an elemnt to delete.";cin>>n;
A.remove(n);
break;
case 4:
cout<<"enter an elemnt to search.";cin>>n;
p=A.search(n);
if (p == NULL) {
cout << "Element not found." << std::endl;
} else {
cout << "Element found at node with address: " << p <<
std::endl;
}
break;
case 5:
A.removecursor();
break;
case 6:
A.display();
break;
case 7:
A.removeaftercur();
break;
case 8:
A.removebeforecur();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
iv deleting an element at the cursor. Cursor should point to the next element.
Data structure
Assignment-V
Name : Abhinav Gupta
Roll no. : 23HCS4103
Implement Stack using arrays:
#include <iostream>
using namespace std;
class arrayStack{
private:
int *arr;
int top;
int capacity;
public:
arrayStack(int size){
capacity = size;
arr = new int[size];
top = -1;
}
~arrayStack(){
delete[] arr;
}
bool isEmpty(){
return top == -1;
}
int getSize(){
return capacity;
}
bool isFull(){
return top == capacity - 1;
}
int pop(){
if(isEmpty()){
std::cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}
int peek() {
if(isEmpty()){
cout << "Stack is Empty\n";
return -1;
}
return arr[top];
}
void display() {
if(isEmpty()){
cout << "Stack is empty.\n";
return;
}
for(int i = top; i >= 0; i--){
cout << arr[i] << " ";
}
cout << endl;
}
};
int main(){
int size;
cout << "Enter size of your Stack: "; cin >> size;
arrayStack s(size);
string ans="Y";
cout << "\nMenu: \n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "4. Display Stack\n";
cout << "5. Get Stack Size\n";
while(ans!="n" and ans!="N"){
int choice, value;
cout<<"Enter Your Choice: "; cin>>choice;
switch (choice) {
case 1:
cout << "Enter the value to Push: ";
cin >> value;
s.push(value);
break;
case 2:
value = s.pop();
if (value != -1)
cout << "Popped element: " << value << endl;
break;
case 3:
value = s.peek();
if (value != -1)
cout << "Front element: " << value << endl;
break;
case 4:
cout << "Stack contents: ";
s.display();
break;
case 5:
cout << "Current Stack size: " << s.getSize() << endl;
break;
default:
cout << "Invalid choice. Please try again.\n";
}
cout<<"Do you want to continue?(y/n): ";
cin>>ans;
cout<<endl;
}
arrayStack A(5);
A.push(4);
A.push(5);
A.push(12);
A.push(15);
A.push(3);
cout << "Top element is: " << A.peek();
cout << endl;
A.display();
A.push(90);
A.pop();
A.display();
cout << "Top element is: " << A.peek();
return 0;
}
Output:
Implement Stack using linked list. Use it to convert an infix
expression to postfix and evaluate the postfix expression
#include <iostream>
#include <cmath>
#include <cctype>
#include <string>
using namespace std;
class Node {
int data;
Node* next;
friend class Stack;
};
class Stack {
Node* top;
public:
Stack():top(nullptr){}
~Stack(){
while(top != nullptr){
Node* temp = top;
top = top->next;
delete temp;
}
}
int pop(){
if(isEmpty()){
cout << "Stack underflow!" << endl;
exit(1);
}
int value = top->data;
Node* temp = top;
top = top->next;
delete temp;
return value;
}
int peek(){
if(isEmpty()){
cout << "Stack is empty!" << endl;
exit(1);
}
return top->data;
}
bool isEmpty(){
return top == nullptr;
}
};
int main() {
string infix;
cout << "Enter an infix expression: ";
cin >> infix;
return 0;
}
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science
Data structure
Assignment-VI
Name : Abhinav Gupta
Roll no. : 23HCS4103
1. Implement Queue using circular arrays
#include <iostream>
using namespace std;
class queue{
int *array;
int n,f,r;
int capacity;
public:
queue(int size){
array= new int[size];
capacity=size;
n=0;f=0;r=0;
}
void enqueue(int i){
if(n==capacity){
cout<<"list is full"<<endl;
return;
}
array[r]=i;
r=(r+1)%capacity;
n++;
}
bool empty(){
return (n==0);
}
int size(){
return n;
}
int front(){
if(empty()){
cout<<"empty"<<endl;
return 0;
}
return array[f];
}
int back(){
return array[r-1];
}
void display(){
int counter=0;
while(counter<(n-1)){
cout<<array[(f+counter)%capacity] <<" ";
counter++;
}
cout<<array[(f+counter)%capacity]<< " ";
}
void dequeue(){
if(empty()){
cout<<"Queue is empty"<<endl;
return ;
}
f=(f+1)%capacity;
n--;
}
};
int main() {
string ans="Y";
cout<<"1. Push an element."<<endl;
cout<<"2. Pop an element."<<endl;
cout<<"3. print front element."<<endl;
cout<<"4. print back element."<<endl;
cout<<"5. Size of Queue ."<<endl;
cout<<"6. Display Queue ."<<endl;
int p;
cout<<"Enter the capacity of Queue . "; cin>>p;
queue A(p);
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element : ";cin>>n;
A.enqueue(n);
break;
case 2:
A.dequeue();
break;
case 3:
n=A.front();
cout<<n<<endl;
break;
case 4:
n=A.back();
cout<<n<<endl;
break;
case 5:
n=A.size();
cout<<n<<endl;
break;
case 6:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
}
Output:
2. implement Queues using linked list
#include <iostream>
using namespace std;
class node{
int elem;
node* next;
void rmcur(){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
cursor->next=ptr->next;
delete ptr;
}
friend class CLLQueue;
};
class CLLQueue:private cll{
int n=0;
public:
void enqueue(int e){
add(e);
advance();
n++;
}
void dequeue(){
if(n==0){
cout<<"list is empty";
return;
}
rmcur();
n--;
}
int front(){
return cll::front();
}
int back(){
return cll::back();
}
int size(){
return n;
}
bool empty(){
return (n==0);
}
void display(){
cll::display();
}
};
int main() {
string ans="Y";
cout<<"1. Push an element."<<endl;
cout<<"2. Pop an element."<<endl;
cout<<"3. print front element."<<endl;
cout<<"4. print back element."<<endl;
cout<<"5. Size of Queue ."<<endl;
cout<<"6. Display Queue ."<<endl;
CLLQueue A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element : ";cin>>n;
A.enqueue(n);
break;
case 2:
A.dequeue();
break;
case 3:
n=A.front();
cout<<n<<endl;
break;
case 4:
n=A.back();
cout<<n<<endl;
break;
case 5:
n=A.size();
cout<<n<<endl;
break;
case 6:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Thank You Mam
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science
Data structure
Assignment-VII
Name : Abhinav Gupta
Roll no. : 23HCS4103
#include <iostream>
using namespace std;
class node{
int elem;
node* prev;
node* next;
void display(){
cout<<"forward display"<<endl;
node * ptr = header->next;
while(ptr!=trailer){
cout<<ptr->elem<<" ";
ptr=ptr->next;
}
cout<<endl;
}
void addback(int i){
node * ptr= new node;
ptr->elem=i;
ptr->next=trailer;
ptr->prev=trailer->prev;
trailer->prev->next=ptr;
trailer->prev=ptr;
}
void removefront(){
if (empty()) {
cout << "List is empty, cannot remove from beginning!" << endl;
return;
}
node * ptr= header->next;
header->next= ptr->next;
ptr->next->prev=header;
delete ptr;
}
void removeback(){
if (empty()) {
cout << "List is empty, cannot remove from end!" << endl;
return;
}
node * ptr=trailer->prev;
trailer->prev= ptr->prev;
ptr->prev->next=trailer;
delete ptr;
}
friend class dequeue;
};
class dequeue{
private :
int n=0;
dll l1;
public:
void addfront(int i){
l1.add(i);
n++;
}
void addback(int i){
l1.addback(i);
n++;
}
void rmfr(){
if(l1.empty()){
cout<<"list is empty"<<endl;
return;
}
l1.removefront();
n--;
}
void rmbk(){
if(l1.empty()){
cout<<"list is empty"<<endl;
return;
}
l1.removeback();
n--;
}
void display(){
l1.display();
}
int front(){
return l1.toop();
}
int back(){
return l1.back();
}
int size(){
return n;
}
};
int main() {
string ans="Y";
cout<<"1. Push an element at beggining."<<endl;
cout<<"2. Pop an element at beggining."<<endl;
cout<<"3. Push an element at end."<<endl;
cout<<"4. Pop an element at end."<<endl;
cout<<"5. print front element."<<endl;
cout<<"6. print back element."<<endl;
cout<<"7. Size of Queue ."<<endl;
cout<<"8. Display Queue ."<<endl;
dequeue A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element : ";cin>>n;
A.addfront(n);
break;
case 2:
A.rmfr();
break;
case 3:
n=0;
cout<<" Enter element : ";cin>>n;
A.addback(n);
break;
case 4:
A.rmbk();
break;
case 5:
n=A.front();
cout<<n<<endl;
break;
case 6:
n=A.back();
cout<<n<<endl;
break;
case 7:
n=A.size();
cout<<n<<endl;
break;
case 8:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Output
:
Thank you Mam
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science
Data structure
Assignment-VIII
Name : Abhinav Gupta
Roll no. : 23HCS4103
#include <iostream>
#include <queue>
using namespace std;
Node(T d){
this->data=d;
this->left=NULL;
this->right=NULL;
}
};
private:
void Insert(Node<T>* & root,T d){
if(root==NULL){ root=new Node<T>(d); return;}
else if(d<root->data)Insert(root->left,d);
else Insert(root->right,d);
}
Node<T>* temp=findMin(root->right);
root->data=temp->data;
root->right=DeleteValue(root->right,temp->data);
}
return root;
}
public:
Node<T>* root;
BST(){
root=NULL;
}
void displayPreOrder(){
preOrder(root); cout<<endl;
}
void displayInOrder(){
inOrder(root); cout<<endl;
}
void displayPostOrder(){
postOrder(root); cout<<endl;
}
void displayLevelByLevel(){
if(root==NULL)return;
queue<Node<T>*> q;
q.push(root);
while (!q.empty())
{
Node<T>* temp=q.front();
q.pop();
cout<<temp->data<<" ";
if(temp->left)q.push(temp->left);
if(temp->right)q.push(temp->right);
}
cout<<endl;
}
int countTotalNode(){
return Count(root);
}
int countLeafNode(){
return CountLeafNode(root);
}
int countNonLeafNode(){
return CountNonLeafNode(root);
}
int height(){
return Height(root);
}
bool isBST(){
return IsBST(root,NULL,NULL);
}
};
int main(){
BST<int> bst1;
bst1.insert(15);
bst1.insert(13);
bst1.insert(17);
bst1.insert(12);
bst1.insert(14);
bst1.insert(16);
bst1.insert(18);
bst1.deleteValue(17);
cout<<"InOrder Traversal after deleting 70: ";
bst1.displayInOrder();
BST<int> bst2;
bst2.insert(15);
bst2.insert(13);
bst2.insert(17);
bst2.insert(12);
bst2.insert(14);
bst2.insert(16);
bst2.insert(18);
if(bst1.checkEqual(bst2)){
cout<<"The two BSTs are equal.\n";
}
else{
cout<<"The two BSTs are not equal\n";
}
return 0;
}
Output:
THANK YOU
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science
Data structure
Assignment-IX
Name : Abhinav Gupta
Roll no. : 23HCS4103
Ques : Write a program to implement insert and search operation in
AVL trees.
Code:
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
int height;
Node(int d){
this->data=d;
this->left=NULL;
this->right=NULL;
this->height=0;
}
};
class AVL_tree{
Node * root;
y->left=x;
x->right=temp;
x->height=1+max(Height(x->left),Height(x->right));
y->height=1+max(Height(y->left),Height(y->right));
return y;
}
y->right=x;
x->left=temp;
x->height=1+max(Height(x->left),Height(x->right));
y->height=1+max(Height(y->left),Height(y->right));
return y;
}
root->height=1+max(Height(root->left),Height(root->right));
int balance=getBalance(root);
public:
AVL_tree(){
this->root=NULL;
}
void displayInOrder(){
inOrder(this->root); cout<<endl;
}
};
int main(){
string ans="Y";
cout<<"1. Add an element after cursor."<<endl;
cout<<"2. search an elemnet."<<endl;
cout<<"3. Display . "<<endl; ;
AVL_tree t1;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
Node *result;
switch(choice){
case 1:
n=0;
cout<<" Enter element to inert: ";cin>>n;
t1.insert(n);
break;
case 2:
n=0;
cout<<" Enter element to search: ";cin>>n;
result = t1.search(n);
if (result != NULL) {
cout << "Found " << n << " in the tree." << endl;
} else {
cout << n << " not found in the tree." << endl;
}
break;
case 3:
cout<<"Inorder display."<<endl;
t1.displayInOrder();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Output:
THANK YOU