Skip to content

CS301 – Data Structures

2 Topics 35 Posts
  • 0 Votes
    2 Posts
    935 Views
    zareenZ
    #include<iostream> using namespace std; class TNode{ private: node* root; public: TNode(); int isEmpty(); void buildTree(int item); void insert(int item,node *,node *); void minNode(node*); void maxNode(node*); int countNodes(node*); int treeHeight(node*); void displayBinTree(); }; TNode::TNode(){ root = NULL; } int TNode::isEmpty() { return (root == NULL); } void TNode::buildTree(int item){ node *p = new node; node *parent; cout <<"Insert node in BST :" << item <<endl; insert(item,p,parent); } void TNode::insert(int item,node * p,node * parent){ p->data=item; p->left=NULL; p->right=NULL; parent=NULL; if(isEmpty()){ root = p; } else{ node *ptr; ptr = root; while(ptr != NULL){ parent = ptr; if(item > ptr->data){ ptr = ptr->right; } else{ ptr = ptr->left; } } if(item < parent->data){ parent->left = p; } else{ parent->right = p; } } } void TNode::minNode(node* p){ while (p->left != NULL){ p = p->left; } cout << "Minimum value : " << p->data <<endl; } void TNode::maxNode(node* p){ while (p->right != NULL){ p = p->right; } cout << "Maximum value : " << p->data <<endl; } int TNode::countNodes(node* p){ int node = 1; //Node itself should be counted if (p == NULL){ return 0; } else{ node += countNodes(p->left); node += countNodes(p->right); return node; } } int TNode::treeHeight(node* p){ if (p == NULL) { return 0; } else { int left_side = treeHeight(p->left); int right_side = treeHeight(p->right); if (left_side > right_side) { return(left_side + 1); } else { return(right_side + 1); } } } void TNode::displayBinTree(){ cout <<endl<<endl; minNode(root); maxNode(root); cout <<"Height of BST : "<<treeHeight(root); cout <<"\nTotal nodes : "<<countNodes(root); } int main(){ TNode b; int data[] = {7,2,9,1,5,14}; cout <<"Constructing Binary Search Tree by Inserting Nodes One by One "<<endl; cout <<"------------------------------------------------------------- "<<endl; int arrSize = sizeof(data)/sizeof(data[0]); for(int i = 0; i < arrSize; i++) { b.buildTree(data[i]); } }
  • CS301 Assignment 2 Solution and Discussion

    Solved cs301 assignment 2 solution discussion fall 2019
    2
    0 Votes
    2 Posts
    2k Views
    zareenZ
    100% Solved: #include <iostream> #include <string.h> using namespace std; const int arrLength = 10; struct Student{ string userVUID; string userDetails; }std1={"NULL","NULL"}; class ArrQueue{ private: //Data Members Student arr[arrLength]; int front; int rear; public: //Constructor ArrQueue(){ for(int i=0; i<arrLength; i++) arr[i] = std1; front = -1; rear = -1; } //Member Functions void enQue(Student std){ if(isEmpty()){ arr[0] = std; front++; rear++; } else if(isFull()){ cout << " Queue is full."; } else{ arr[rear+1] = std; rear++; } } void deQue(){ if(isEmpty()){ cout<< " No Student In the Queue.\n"; } else if(rear == 0){ arr[front] = std1; front = -1; rear = -1; } else{ int tempfront = front; arr[front] = std1; for(int i=1; i<=rear; i++ ){ arr[front] = arr[i]; front++; } rear--; front = tempfront; } } int queLength(){ return rear+1; } bool isEmpty(){ if(front==-1 || rear==-1) return true; else return false; } bool isFull(){ if(rear == arrLength-1) return true; else return false; } void showQue(){ cout << "\n |Sr. VU ID Details |"; cout << "\n --- -------------------------------\n"; for(int i=front; i<=rear; i++){ cout<<" "<< i+1<< ". "<< arr[i].userVUID << " " << arr[i].userDetails <<"\n"; } } }; int main(){ /*Code For Even Id's Student std[] = {{"BC12345684","Bilal (BSCS)"}, {"BC12345685","Bilal (BSCS)"},{"BC12345686","Bilal (BSCS)"},{"BC12345687","Bilal (BSCS)"}, {"BC12345688","Bilal (BSCS)"}};*/ /*Code For Odd Id's */ Student std[] = {{"BC12345683","Bilal (BSCS)"}, {"BC12345684","Bilal (BSCS)"},{"BC12345685","Bilal (BSCS)"},{"BC12345686","Bilal (BSCS)"}, {"BC12345687","Bilal (BSCS)"}}; ArrQueue arrQue; cout << "\n -----------------------------------"; cout << "\n | Queue (After Adding Students) |"; cout << "\n -----------------------------------"; for(int i=0; i<=4; i++){ arrQue.enQue(std[i]); } arrQue.showQue(); cout << "\n -----------------------------------"; cout << "\n | Queue (After Removing Students) |"; cout << "\n -----------------------------------"; /* Code For Even Id's for(int i=0; i<=1; i++){ arrQue.deQue(); }*/ /*Code For Odd Id's*/ for(int i=0; i<1; i++){ arrQue.deQue(); } arrQue.showQue(); }
How to Build a $1,000/Month PAK VS BAN Live Live Cricket Streaming
File Sharing

1

Online

3.0k

Users

2.8k

Topics

8.2k

Posts
| |