Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code below is the data structure of a tree that our professor built in class

ID: 3818309 • Letter: T

Question

The code below is the data structure of a tree that our professor built in class.

#include<iostream>

typedef long int myint;
class Frac {
private:
   myint num;
   myint den;
public:
   Frac(const myint& = 0, const myint& = 1);
   Frac(const Frac&);
   void operator=(const Frac&);
   myint getNum() const;
   myint getDen() const;
   void setNum(const myint &);
   void setDen(const myint &);
   myint operator<(const Frac &) const;
   myint operator>(const Frac &) const;

};
Frac::Frac(const myint& _num, const myint &_den) {
   num = _num;
   den = _den;
   if (den == 0) { num = 0; den = 1; }
   if (den<0) { num *= -1; den *= -1; }
}
Frac::Frac(const Frac& _copyFrom) {
   num = _copyFrom.getNum();
   den = _copyFrom.getDen();
}
myint Frac::getNum() const { return num; }
myint Frac::getDen() const { return den; }
void Frac::setNum(const myint & _n) { num = _n; }
void Frac::setDen(const myint & _d) {
   den = _d;
   if (den == 0) { num = 0; den = 1; }
   if (den<0) { num *= -1; den *= -1; }
}
void Frac::operator=(const Frac & _copyFrom) {
   den = _copyFrom.getDen();
   num = _copyFrom.getNum();
}
myint Frac::operator<(const Frac & _cW) const {
   if (num * _cW.getDen() < den * _cW.getNum()) {
       return 1;
   }
   return 0;
}
myint Frac::operator>(const Frac &_cW) const {
   if (num * _cW.getDen() > den * _cW.getNum()) {
       return 1;
   }
   return 0;
}

class TNode {
public:
   Frac value;
   TNode * lChild;
   TNode * rChild;
   TNode();
};
TNode::TNode() {
   lChild = nullptr;
   rChild = nullptr;
}
void insert(TNode*& root, const Frac& fr) {
   if (root == nullptr) {
       root = new TNode;
       root->value = fr;
   }
   else {
       if (fr<root->value) { insert(root->lChild, fr); }
       if (fr>root->value) { insert(root->rChild, fr); }
   }
}
void insert(TNode*& root, const int& b, const int&c = 1) {
   Frac fr(b, c);
   insert(root, fr);
}
void deleteNode(TNode*& root) {
   if (root != nullptr) {
       if (root->lChild != nullptr) {
           TNode *helper = root->lChild;
           if (helper->rChild != nullptr) {
               TNode *h2 = helper->rChild;
               while (h2->rChild != nullptr) {
                   helper = helper->rChild;
                   h2 = h2->rChild;
               }
               root->value = h2->value;
               if (h2->lChild == nullptr) {
                   delete h2;
                   helper->rChild = nullptr;
               }
               else {
                   deleteNode(h2);
               }
           }
           else {
               root->value = helper->value;
               if (helper->lChild == nullptr) {

                   delete helper;
                   root->lChild = nullptr;
               }
               else {
                   deleteNode(helper);
               }
           }

       }
       else {
           if (root->rChild != nullptr) {
               TNode *helper = root->rChild;
               if (helper->lChild != nullptr) {
                   TNode *h2 = helper->lChild;
                   while (h2->lChild != nullptr) {
                       helper = helper->lChild;
                       h2 = h2->lChild;
                   }
                   root->value = h2->value;
                   if (h2->rChild == nullptr) {
                       delete h2;
                       helper->lChild = nullptr;
                   }
                   else {
                       deleteNode(h2);
                   }
               }
               else {
                   root->value = helper->value;
                   if (helper->rChild == nullptr) {

                       delete helper;
                       root->rChild = nullptr;
                   }
                   else {
                       deleteNode(helper);
                   }
               }


           }
           else {
               delete root;
               root = nullptr;
           }
       }
   }
}

myint deleteValue(TNode *&root, const Frac &fr) {
   if (root == nullptr) {
       return 0;
   }
   if (fr<root->value) {
       return deleteValue(root->lChild, fr);
   }
   if (fr>root->value) {
       return deleteValue(root->rChild, fr);
   }
   deleteNode(root);
   return 1;
}


void printAll(TNode * root) {
   if (root != nullptr) {
       printAll(root->lChild);
       std::cout << (root->value).getNum();
       std::cout << "/";
       std::cout << (root->value).getDen();
       std::cout << " ";
       printAll(root->rChild);
   }
}

int main(){
myint a,b,c;
Frac tempFr;
SetOfFractions setF;
a=1;
while(a!=0){
std::cout<<"Choose one of: 0 (exit), 1 (add), 2 (check for element), ";
std::cout<<"or 3 (print all)"< std::cin>>a;
if(a==1){
std::cout<<"Insertion.";
std::cout<<" What are numerator and denominator? ";
std::cin>>b>>c;
tempFr.setNum(b);
tempFr.setDen(c);
setF.insertInS(tempFr);
}
if(a==2){
std::cout<<"Checking for element.";
std::cout<<" What are numerator and denominator? ";
std::cin>>b>>c;
tempFr.setNum(b);
tempFr.setDen(c);
std::cout< }
if(a==3){
std::cout<<"Printing all fractions."< setF.printAllFractions();
std::cout< }
}
return 0;
}

Homework Problem 1 Using the data structure of a tree that we built in class, create a class SetofFractions with the following declaration. class setof Fractions H private TNode root public setof Fractions, CO; setoff Fractions (const setofFractions & Setoff Fractions (SetofFractions &&) void operator (const Setof Fractions &D; void operator (SetofFractions &&); myint is Element Cconst Frac & Const; myint insertins (const Frac &); void printAllFractions O const; ~Setof Fractions C); Ca The default constructor SetofFractionsO should initialize an empty tree. The copy constructor SetofFractions(const Setof Fractions & copyFrom) should create the set of fractions with the same elements as the set copyFrom. Make sure that the elements are not shared so if later elements are changed in copyFrom the changes do not affect the objects created using copy constructor (b) Copy void operator the content of the set overthe existing elements. Make sure you properly deallocate the memory before copying to prevent memory leak.

Explanation / Answer


package com.chegg;
import java.util.Vector;

public class Scheduler extends Thread {
private Vector queue;   
private Vector queue1;
private Vector queue2;   
private int timeSlice;
private static final int DEFAULT_TIME_SLICE = 2000;
private static final int DEFAULT_MAX_THREADS = 20000;
private boolean[] tids;   
private int nextId = 0;

public TCB getMyTcb( ) {
Thread myThread = Thread.currentThread( );
  
synchronized( queue ) {
for ( int i = 0; i < queue.size( ); i++ ) {
TCB tcb = ( TCB )queue.elementAt( i );
Thread thread = tcb.getThread( );
if ( thread == myThread ) return tcb;   
}
}
synchronized( queue1 ) {
for ( int i = 0; i < queue1.size( ); i++ ) {
TCB tcb = ( TCB )queue1.elementAt( i );
Thread thread = tcb.getThread( );
if ( thread == myThread ) return tcb;
}
}
synchronized( queue2 ) {   
for ( int i = 0; i < queue2.size( ); i++ ) {
TCB tcb = ( TCB )queue2.elementAt( i );
Thread thread = tcb.getThread( );
if ( thread == myThread ) return tcb;
}
}
return null;
}
  
  
private void initTid( int maxThreads ) {
tids = new boolean[maxThreads];
for ( int i = 0; i < maxThreads; i++ ) {
tids[i] = false;
}
}

private int getNewTid( ) {
for ( int i = 0; i < tids.length; i++ ) {
int tentative = ( nextId + i ) % tids.length;
if ( tids[tentative] == false ) {
tids[tentative] = true;
nextId = ( tentative + 1 ) % tids.length;
return tentative;
}
}
return -1;
}
  

private boolean returnTid( int tid ) {
if ( tid >= 0 && tid < tids.length && tids[tid] == true ) {
tids[tid] = false;
return true;
}
return false;
}
  

public int getMaxThreads( ) {
return tids.length;
}

public Scheduler( ) {
timeSlice = DEFAULT_TIME_SLICE;
queue = new Vector( );
queue1 = new Vector( );
queue2 = new Vector( );
initTid( DEFAULT_MAX_THREADS );
}

  
public Scheduler(int quantum) {
timeSlice = quantum;
queue = new Vector( );
queue1 = new Vector( );
queue2 = new Vector( );
initTid( DEFAULT_MAX_THREADS );
}

public Scheduler( int quantum, int maxThreads ) {
timeSlice = quantum;
queue = new Vector( );
queue1 = new Vector( );
queue2 = new Vector( );
initTid( maxThreads );
}
private void schedulerSleep( ) {
try {
Thread.sleep( timeSlice );
} catch ( InterruptedException e ) {
}
}


public TCB addThread( Thread t ) {
TCB parentTcb = getMyTcb( );   
int pid = ( parentTcb != null ) ? parentTcb.getTid( ) : -1;
int tid = getNewTid( ); //Get a new TID
  
  
TCB tcb = new TCB( t, tid, pid );   
queue.add( tcb );   
return tcb;
}

public boolean deleteThread( ) {
TCB tcb = getMyTcb( );
if ( tcb!= null ) {
return tcb.setTerminated( );
}
}
public void sleepThread( int milliseconds ) {
try {
sleep( milliseconds );
} catch ( InterruptedException e ) { }
}
  

public void run( ) {
Thread current = null;
while ( true ) {
try {
if(allQueuesAreEmpty()) continue; //Back to top
  
if(queue_hasContent()){
if(processqueue(current)) continue; //Process queue
continue; //Back to top
}
if(queue_isEmpty() && queue1_hasContent()){
if(processQueue1(current)) continue; //Process queue1
continue; //Back to top
}
if(queue_isEmpty() && queue1_isEmpty() && queue2_hasContent()){
if(processQueue2(current)) continue; //Process queue2
continue; //Back to top
}
} catch ( NullPointerException e3 ) { };
}
}
  
  
private boolean processqueue(Thread current){
TCB currentTCB = (TCB)queue.firstElement( ); //grab queue's first TCB
if(threadIsDead(currentTCB, queue)) return true;
  
current = currentTCB.getThread( ); //grab thread object
getThreadGoing(current); //start/resume thread
sleepThread(timeSlice/2); //sleep the scheduler
  
//Move TCBs from queue to queue1
finishProcessingQueue(queue, queue1, current, currentTCB);
return false;
}
  
  
private boolean processQueue2(Thread current){
TCB currentTCB = (TCB)queue2.firstElement( ); //grab queue's first TCB
if(threadIsDead(currentTCB, queue2)) return true;
  
current = currentTCB.getThread( ); //grab thread object
getThreadGoing(current); //start/resume thread
  
sleepThread(timeSlice/2); //first timeSlice/2
if(queue_hasContent() || queue1_hasContent()) processNewTcb(current);
sleepThread(timeSlice/2); //second timeSlice/2
sleepThread(timeSlice); //last timeSlice
  
//Keep TCBs in queue2 so that their bursts can be completed.
finishProcessingQueue(queue2, queue2, current, currentTCB);
return false;
} private void processNewTcb(Thread current){
if (current != null && current.isAlive()){
current.suspend(); //put thread to sleep
Thread newProcess = null; //create new thread
processqueue(newProcess); //process new TCB
current.resume(); //resume the old TCB
}
}
  

private void finishProcessingQueue(Vector myQueue, Vector nextQueue,
Thread current, TCB currentTCB){
synchronized ( myQueue ) {
if ( current != null && current.isAlive( ) ) {
current.suspend(); //put thread to sleep
myQueue.remove( currentTCB ); //remove TCB from queue
nextQueue.add(currentTCB); //add TCB to queue1
}
}
}
  

private boolean threadIsDead(TCB currentTCB, Vector queue){
if ( currentTCB.getTerminated( ) == true ) { //if TCB is dead, run
queue.remove( currentTCB ); //remove TCB from queue
returnTid( currentTCB.getTid( ) ); //update the Tid array
return true; //return method call
}
return false;
}
  
private void getThreadGoing(Thread current){
if ( current != null ) { //No null threads!
if ( current.isAlive( ) ) { //if thread is suspened,
current.resume(); //resume the thread
} else { //Otherwise,
current.start( ); //'spin' it up
}
}
}
  

private boolean queue_hasContent(){ return (queue.size() > 0); }
private boolean queue1_hasContent(){ return (queue1.size() > 0); }
private boolean queue2_hasContent(){ return (queue2.size() > 0); }
  
private boolean queue_isEmpty(){ return (queue.size() == 0); }
private boolean queue1_isEmpty(){ return (queue1.size() == 0); }
private boolean queue2_isEmpty(){ return (queue2.size() == 0); }
  
private boolean allQueuesAreEmpty(){
return (queue_isEmpty() && queue1_isEmpty() && queue2_isEmpty());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote