Skip to main content

1.BST_operations

File: /home/saiesh/ADS/Assignment No 1/ads1.cpp
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node
{
int data;
node *left,*right;
}bstnode;
bstnode *root;
class BST
{
public:
BST()
{
root=NULL;
}
bstnode *getnode();
void create();
int maxdepth(bstnode *root);
void min(bstnode *T);
void change(bstnode *root);
bstnode *find(int x);
void inorder(bstnode *T);
};
bstnode *BST::getnode()
{
bstnode *temp;
temp=new bstnode;
cout<<"\nEnter the data:\t";
cin>>temp->data;
temp->left=temp->right=NULL;
return temp;
}
void BST::create()
{
char ans;
bstnode *temp,*ptr;
do
{
temp=getnode();
if(root==NULL)
root=temp;
else
{
ptr=root;
while(1)
{
if(temp->data<ptr->data)
{
if(ptr->left==NULL)
{
ptr->left=temp;
break;
}
else
ptr=ptr->left;
}
else
{
if(ptr->right==NULL)
{
ptr->right=temp;
Page 1 of 6File: /home/saiesh/ADS/Assignment No 1/ads1.cpp
Page 2 of 6
break;
}
else
ptr=ptr->right;
}
}
}
cout<<"Do you want to add more nodes? ";
cin>>ans;
} while(ans=='y'||ans=='Y');
}
int BST::maxdepth(bstnode *root)
{
if(root==NULL)
return 0;
else
{
int ldepth=maxdepth(root->left);
int rdepth=maxdepth(root->right);
if(ldepth>rdepth)
return(ldepth+1);
else
return(rdepth+1);
}
}
void BST::min(bstnode *T)
{
bstnode *ptr;
if(root==NULL)
cout<<"\nTree is empty!";
else
{
ptr=root;
while(1)
{
if(ptr->left==NULL)
{
cout<<"\nThe minimum node is:\t"<<ptr->data;
break;
}
else
ptr=ptr->left;
}
}
}
void BST::change(bstnode *root)
{
bstnode *s;
if(root==NULL)
return;
else
{
s=root->left;
root->left=root->right;
root->right=s;
change(root->left);
change(root->right);
}
}
bstnode *BST::find(int x)
{
bstnode *ptr;File: /home/saiesh/ADS/Assignment No 1/ads1.cpp
Page 3 of 6
ptr=root;
while(ptr!=NULL)
{
if(x==ptr->data)
return ptr;
else if(x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
return NULL;
}
void BST::inorder(bstnode *T)
{
if(T!=NULL)
{
inorder(T->left);
cout<<"
"<<T->data;
inorder(T->right);
}
}
int main()
{
bstnode *R,*S;
BST obj;
char ans;
int ch;
do
{
cout<<"\n\t\t\tBinary Search Tree";
cout<<"\n\t\t\t\tMENU";
cout<<"\n1.Insert new node";
cout<<"\n2.Find number of nodes in the longest path";
cout<<"\n3.Minimum data found in tree";
cout<<"\n4.Search a value";
cout<<"\n5.Change a tree so that the roles of the left and right
pointers are swapped at every node";
cout<<"\n6.Exit";
cout<<"\nEnter your choice:\t";
cin>>ch;
switch(ch)
{
case 1: obj.create();
cout<<"\nTree nodes are: ";
obj.inorder(root);
break;
case 2: int a;
a=obj.maxdepth(root);
cout<<"\nNumber of nodes in the longest path:
\t"<<a;
break;
case 3: obj.min(root);
break;
case 4: int a;
cout<<"\nEnter the node you want to search:\t";
cin>>a;
R=obj.find(a);
if(R==NULL)
cout<<"\nNumber not found!";
else
cout<<"\nNumber found!";File: /home/saiesh/ADS/Assignment No 1/ads1.cpp
Page 4 of 6
break;
case 5: obj.change(root);
cout<<"\nAfter roles of the left and right
pointers are swapped at every node: ";
obj.inorder(root);
break;
case 6: exit(0);
}
cout<<"\nDo you want to continue? ";
cin>>ans;
} while(ans=='y'||ans=='Y');
return 0;
}
/*
OUTPUT
saiesh@saiesh :~$ g++ ads1.cpp
saiesh@saiesh :~$ ./a.out
Binary Search Tree
MENU
1.Insert new node
2.Find number of nodes in the longest path
3.Minimum data found in tree
4.Search a value
5.Change a tree so that the roles of the left and right pointers are swapped at
every node
6.Exit
Enter your choice:
1
Enter the data: 7
Do you want to add more nodes? y
Enter the data: 2
Do you want to add more nodes? y
Enter the data: 9
Do you want to add more nodes? y
Enter the data: 0
Do you want to add more nodes? y
Enter the data: 5
Do you want to add more nodes? y
Enter the data: 6
Do you want to add more nodes? y
Enter the data: 8
Do you want to add more nodes? y
Enter the data: 1
Do you want to add more nodes? n
Tree nodes are:
0
1
2
Do you want to continue? y
5
6
7
8
9
Binary Search Tree
MENU
1.Insert new node
2.Find number of nodes in the longest path
3.Minimum data found in tree
4.Search a value
5.Change a tree so that the roles of the left and right pointers are swapped atFile: /home/saiesh/ADS/Assignment No 1/ads1.cpp
every node
6.Exit
Enter your choice:
Page 5 of 6
2
Number of nodes in the longest path:
Do you want to continue? y
4
Binary Search Tree
MENU
1.Insert new node
2.Find number of nodes in the longest path
3.Minimum data found in tree
4.Search a value
5.Change a tree so that the roles of the left and right pointers are swapped at
every node
6.Exit
Enter your choice:
3
The minimum node is:
0
Do you want to continue? y
Binary Search Tree
MENU
1.Insert new node
2.Find number of nodes in the longest path
3.Minimum data found in tree
4.Search a value
5.Change a tree so that the roles of the left and right pointers are swapped at
every node
6.Exit
Enter your choice:
4
Enter the node you want to search:
1
Number found!
Do you want to continue? y
Binary Search Tree
MENU
1.Insert new node
2.Find number of nodes in the longest path
3.Minimum data found in tree
4.Search a value
5.Change a tree so that the roles of the left and right pointers are swapped at
every node
6.Exit
Enter your choice:
4
Enter the node you want to search:
4
Number not found!
Do you want to continue? y
Binary Search Tree
MENU
1.Insert new node
2.Find number of nodes in the longest path
3.Minimum data found in tree
4.Search a value
5.Change a tree so that the roles of the left and right pointers are swapped at
every node
6.Exit
Enter your choice:
5
After roles of the left and right pointers are swapped at every node:
7
6
5
2
1
0
9
8File: /home/saiesh/ADS/Assignment No 1/ads1.cpp
Page 6 of 6
Do you want to continue? y
Binary Search Tree
MENU
1.Insert new node
2.Find number of nodes in the longest path
3.Minimum data found in tree
4.Search a value
5.Change a tree so that the roles of the left and right pointers are swapped at
every node
6.Exit
Enter your choice:
6
saiesh@saiesh :~$ */

Comments

  1. Best description ever..if you want to know about bit coin and getnode you can come in this link Getnode

    ReplyDelete
  2. Casinos Near Me - Mapyro
    The closest casinos to New York City offer gaming or live 당진 출장샵 dealer 충청남도 출장샵 games 부산광역 출장샵 to players is 경주 출장샵 Borgata Resort Casino, where the minimum limit is $50. 남원 출장마사지

    ReplyDelete

Post a Comment

Popular posts from this blog

PPL 1

NEW Unit III STRUCTURING OF PROGRAM MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of ‘Data Abstraction’? (a) Reduce Complexity (b) Binds together code and data (c) Parallelism (d) None of the mentioned Answer : a Explanation : An essential element of Object Oriented Programming is ‘Data Abstraction’ which means hiding things. Complexity is managed through abstraction. 2. Which of the following mechanisms is/are provided by Object Oriented Language to implement Object Oriented Model? (a) Encapsulation (b) Inheritance (c) Polymorphism (d) All of the mentioned Answer : d Explanation : None. 3. Which of these is the functionality of ‘Encapsulation’? (a) Binds together code and data (b) Using single interface for general class of actions. (c) Reduce Complexity (d) All of the mentioned Answer : a Explanation : ‘Encapsulation’ acts as protective wrapper that prevents code and data from being accessed by other code defined outside the

EEE

EEE Unit-1 DC Machines MCQ's MULTIPLE CHOICE QUESTIONS ON UNIT 1: DC MACHINES Edit 1.The sole purpose of a Commutator in a dc generator is to------- ((A))increase output voltage ((B)) reduce sparking at brushes ((C)) provide smoother output ((D)) convert the induced ac into dc Hide ! Answer : D 2.In a dc generator, the generated emf is directly proportional to the------ ((A)) field current ((B)) pole flux ((C)) number of armature parallel paths ((D)) number of dummy coils Hide ! Answer:B 3.An ideal dc generator has .......... voltage regulation. ((A))low ((B))zero ((C))positive ((D))negative Hide ! Answer:B 4.Which generator may have poorest voltage regulation? ((A))series ((B))shunt((C))compound ((D))high Hide ! Answer:A 5.Voltage equation of a dc motor is----- ((A)) V = Eb + IaRa ((B)) Eb = V + IaRa ((C))V = Eb /IaRa ((D))V = Eb + Ia2Ra Hide ! Answer: A 6.Which of the following motor has the constant speed? ((A))Series motor ((B))Shunt motor ((C))Cumulatively compound motor ((D)

13.interface

File: /home/student/saiesh/ADS/Assignment No 13/ads13_2A.java package pack; interface ip { public public public public } void void void void add(int sub(int mul(int div(int a, a, a, a, int int int int b); b); b); b); public class A implements ip { public void add(int a, int b) { System.out.println("Addition is\t\t: "+(a+b)); } public void sub(int a,int b) { System.out.println("Substraction is\t\t: "+(a-b)); } public void mul(int a, int b) { System.out.println("Multiplication is\t: "+(a*b)); } public void div(int a, int b) { System.out.println("Division is\t\t: "+(a/b)); } } Page 1 of 1 ....................... File: /home/student/saiesh/ADS/Assignment No 13/ads13_2B.java package mypack; import pack.*; import java.util.Scanner; public class B { public static void main(String arg[]) { A obj= new A(); Scanner sc=new Scanner(System.in); System.out.println("\nEnter first number\t: "); int a=sc.nextInt(); System.out.println("\nEnter