Posts

4. Write a function to get the number of vertices in an undirected graph and its edges. You may assume thatno edge is input twice. i.Use adjacency list representation of the graph and find runtime of the functionii.Use adjacency matrix representation of the graph and find runtime of the function

#include<iostream> using namespace std; class node { public: char data; node *start = NULL; node *next; void list(char ch) { node *ptr = new node; node *temp = new node; ptr->data = ch; if(start == NULL) { start = ptr; start->next = NULL; } else { temp = start; while(temp->next != NULL) { temp = temp->next; } ptr->next = NULL; temp->next = ptr; } } void displaylist() { node *temp = new node; if(start == NULL) { cout<<"LIST IS EMPTY"<<endl; } else { temp = start; while(temp != NULL) { cout<<temp->data<<"->"; temp = temp->next; } cout<<endl; } } }; class graph { int a[10][10]; char arr[5] = {'A','B','C','D','E'}; int vertices; node al[5]; public: graph() { cout<<"ENTER THE NUMBER OF VERTICES IN THE GRAPH \t::\t"; cin>>ve...

3. For given expression eg. a-b*c-d/e+f construct inorder sequence and traverse it using postorder traversal(non recursive).

#include<iostream> #include<string> #include<stack> using namespace std; int getweight(char c) { switch(c) { case '^' : return 3; break; case '*' : case '/' : return 2; break; case '+' : case '-' : return 1; break; default : return 0; } } void infix2postfix(char infix[], char postfix[], int size ,int sizepos) { stack <char> s; int i = 0; int j = 0; int weight = 0; char ch; while( i < size) { ch = infix[i]; //cout<<i<<endl; // cout<<ch<<endl; if ( ch == '(' ) { s.push(ch); i++; continue; } else if( ch == ')') { if(s.top() == '(') { s.pop(); } else { while(s.top() != '(') { postfix[j++] = s.top(); s.pop(); } s.pop(); i++; continue; } } else { weight = getweight(ch); if(weight == 0) { postfix[j++] = ch; } else i...

2. Beginning with an empty binary search tree, Construct binary search tree by inserting the values in the order given. After constructing a binary tree -i.Insert new nodeii.Find number of nodes in longest path iii.Minimum data value found inthe tree iv.Change a tree so that the roles of the left and right pointers are swapped at every node v.Search a value

#include<iostream> #include<math.h> using namespace std; struct Bstnode { int data; Bstnode *left = NULL; Bstnode *right = NULL; }; class Btree { int n; int x; int flag; public: Bstnode * root; Btree() { root = NULL; } Bstnode *GetNewNode(int in_data) { Bstnode * ptr = new Bstnode(); ptr->data = in_data; ptr->left = NULL; ptr->right = NULL; return ptr; } Bstnode *insert( Bstnode *temp , int in_data) { if( temp == NULL ) { temp = GetNewNode(in_data); } else if( temp->data > in_data) { temp->left = insert(temp->left , in_data); } else { temp->right = insert( temp->right , in_data); } return temp; } void input() { cout<<"ENTER NUMBER OF ELEMENTS IN THE BST : "; cin>>n; for(int i = 0 ; i < n ; i++) { cout<<"NUMBER = "; cin>>x; root = insert(root , x); } } int search(Bstnode *temp ,int in_data) { if( te...

1. A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method.

#include<iostream> using namespace std; struct node { string name; node *B[5]; }; class book { int c,s,sub; public: node *temp = new node; void Getnewnode() { for(int i = 0 ; i<5 ; i++) { temp->B[i] = new node; temp->B[i]->name = "empty"; for(int j=0; j<5; j++) { temp->B[i]->B[j] = new node; temp->B[i]->B[j]->name = "empty"; for(int k = 0 ; k<5; k ++) { temp->B[i]->B[j]->B[k] = new node; temp->B[i]->B[j]->B[k]->name = "empty"; } } } } void add_title() { cout<<": ENTER THE TITLE OF THE BOOK = "; cin>>temp->name; Getnewnode(); } void add_chapter() { string cname; int cnum; cout<<": ENTER NUMBER OF CHAPTERS IN THE BOOK = "; cin>>cnum; c = cnum; for(int i = 0 ; i < cnum ; i++) { cout<<endl<<"Chapter "<...