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;
}
}
Reply
#2

Thank you for sharing the solution.
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by juandkeim
10-15-2021, 11:58 PM
Last Post by Mourldorp
10-11-2021, 01:34 PM
Last Post by Mourldorp
10-11-2021, 06:26 AM



Users browsing this thread: 1 Guest(s)