CS301 Assignment 1 Solution Fall 2018
#1
CS301 - Assignment 1 Solution

Semester: Fall 2018



Solution Code 
Code:
#include<iostream>
#include<conio.h>
#include<string>
#include<cstring>
using namespace std;
struct Node{
int empId;
char* empName;
double empSalary;
Node* left;
Node* right;
};
Node* buildBST(Node* root, int i,int id, char* name, double salary){
if(root== NULL){
Node* newNode = new Node();
newNode->empId = id;
newNode->empName = name;
newNode->empSalary = salary;
newNode->left = newNode->right = NULL; 
return newNode;
}
if(root->empId == id){
return root;
}
else if(id <= root->empId){
root->left = buildBST(root->left, i, id, name, salary);
}
else{
root->right = buildBST(root->right, i, id, name, salary);
}
}
void ascendingOrder(Node* root){
if(root==NULL){
return;
}
ascendingOrder(root->left);
cout<<endl<<root->empId<<"\t"<<root->empName<<"\t"<<root->empSalary<<endl;
ascendingOrder(root->right);
}
void postOrder(Node* root){
if(root==NULL){
return;
}
postOrder(root->left);
postOrder(root->right);
cout<<root->empId<<"\t"<<root->empName<<"\t"<<root->empSalary<<endl; 
}
main(){
int id[]={32,56,93,5,10};
char* name[]={"Raza","Sajjad","Rabia","Sehar","Ali"};
double salary[]={3000, 25000, 19230, 24000, 22200};
int c;
int x = (sizeof(id)/sizeof(*id)); 
Node* root = NULL;
char string[20];


for(int i=0; i<x; i++){
root = buildBST(root, i, id[i], name[i], salary[i]); 
}


s: cout<<endl<<"Press 1 to Enter Data \n"; 
cout<<"Press 2 to see the Records in Ascending Order \n";
cout<<"Press 3 to see the Records in Post Order \n";
cout<<"Press 4 to Exit\n ";
cin>>c;
switch(c){
case 1:{
r: cout<<endl<<"Enter Employee ID : ";
cin>>id[x];
cout<<endl<<"Enter Employee Salary : ";
cin>>salary[x];
cout<<endl<<"Enter Employee Name : ";
cin>>string;

for(int i=0;i<x;i++){
if(id[x] == id[i]){
cout<<endl<<"Record Already Exists...";
goto r;
}
}

name[x] = new char[strlen(string)+1];
strcpy(name[x], string);

for(int i=0; i<(x+1); i++){
root = buildBST(root, i, id[i], name[i], salary[i]); 
}
break;
}
case 2: 
cout<<"Ascending Order: \n";
ascendingOrder(root);
break;
case 3:
cout<<"Post Order: \n";
postOrder(root);
break;
case 4:
break;
default:
cout<<"\nWrong Input.";
break; 
}
char ch;
y: cout<<endl<<"DO YOU WANT TO DO SOMETHING ELSE...\nYES = y \t NO = n";
ch = getch();
if(ch=='y'){
goto s;
}
else if(ch=='n'){
cout<<"\nBye!";
}
else{
cout<<endl<<"\nWrong input.";
goto y;
}
}
#2
Thank you for sharing the solution.
  


Possibly Related Threads…
Thread Author Replies Views Last Post
  CS301 GDB Solution Spring 2019 Ayra Suleman 0 1,002 08-01-2019, 10:24 PM
Last Post: Ayra Suleman
  CS301 Assignment 3 Solution Spring 2019 Ayra Suleman 0 971 07-26-2019, 06:59 PM
Last Post: Ayra Suleman
  CS301 Assignment 2 Solution Spring 2019 Mishi Khan 0 507 05-28-2019, 01:57 AM
Last Post: Mishi Khan
  cs301 Assignment 2 solution Muhammad Yaseen 0 228 05-24-2019, 04:03 PM
Last Post: Muhammad Yaseen
  CS301 Assignment Solution Spring 2019 Mishi Khan 0 347 05-11-2019, 09:36 PM
Last Post: Mishi Khan
  cs301 Assignment Muhammad Yaseen 0 218 05-11-2019, 04:45 PM
Last Post: Muhammad Yaseen
  CS301 GDB Solution Mishi Khan 0 738 02-08-2019, 01:42 AM
Last Post: Mishi Khan
  CS301 Assignment 2019 Mishi Khan 0 665 01-30-2019, 01:06 AM
Last Post: Mishi Khan
  CS301 MidTerm Past Papers Mishi Khan 0 748 12-14-2018, 09:52 PM
Last Post: Mishi Khan
  need GDB CS301 solution raja kamran 3 2,434 08-10-2018, 10:35 AM
Last Post: Pakistani



Users browsing this thread:
1 Guest(s)