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

This problem will give you a chance to reverse engineer a switch statement from

ID: 3890007 • Letter: T

Question

This problem will give you a chance to reverse engineer a switch statement from machine code. In the following procedure, the body of the switch statement has been removed:

The disassembled machine code for the procedure is shown below. Recall parameter x is passed in register %rdi and parameter n is passed in register %rsi. The jump table resides in a different area of memory. We can see from the mov at address 0x040065e that the jump table begins at address 0x0400798. Using the gdb debugger, we can examine the ten 8-byte words of memory comprising the jump table with the command x/10x 0x400798. gdb prints the following:

Explanation / Answer

A. (c++) Write a program to store a tree as an array. In your test program, be sure to print the array values, and to print the values in tree¬¬¬ “format”.

B. (c++) Write a function to add an element to an existing tree.

C. (c++) Write a function to delete an element from an existing tree.

D. (c++) Write a function to perform an “in-order” traversal on an existing tree.

E. (c++) Write a function to perform an “pre-order” traversal on an existing tree.

F. (c++) Write a function to perform an “post-order” traversal on an existing tree.

G. (c++) Write a function to perform an “level-order” traversal on an existing tree.

H. (c++) Write a function to find the children of a specified node.

I. (c++) Write a function to find the parent of a specified node.

# include <iostream>

# include <cstdlib>

using namespace std;

// Node Declaration

struct node

{

int info;

struct node *left;

struct node *right;

}*root;

// Class Declaration

class BinarySearchTree

{

public:

void search(int, node **, node **);

void insert(int);

void del(int);

void case_p(node *,node *);

void case_q(node *,node *);

void case_r(node *,node *);

void preorder(node *);

void inorder(node *);

void postorder(node *);

void display(node *, int);

void insert(node *tree, node *newnode);

BinarySearchTree()

{

root = NULL;

}

};

int main()

{

int choice, num;

BinarySearchTree bst;

node *temp;

while (1)

{

cout<<"-----------------"<<endl;

cout<<"Operations on BinarySearchTree"<<endl;

cout<<"-----------------"<<endl;

cout<<"1.Insert Element "<<endl;

cout<<"2.Delete Element "<<endl;

cout<<"3.Inorder Traversal"<<endl;

cout<<"4.Preorder Traversal"<<endl;

cout<<"5.Postorder Traversal"<<endl;

cout<<"6.Display"<<endl;

cout<<"7.Quit"<<endl;

cout<<"Enter your choice : ";

cin>>choice;

switch(choice)

{

case 1:

temp = new node;

cout<<"Enter the number to be inserted : ";

cin>>temp->info;

bst.insert(root, temp);

case 2:

if (root == NULL)

{

cout<<"Tree is empty, nothing to delete"<<endl;

continue;

}

cout<<"Enter the number to be deleted : ";

cin>>num;

bst.del(num);

break;

case 3:

cout<<"Inorder Traversal of BinarySearchTree:"<<endl;

bst.inorder(root);

cout<<endl;

break;

case 4:

cout<<"Preorder Traversal of BinarySearchTree:"<<endl;

bst.preorder(root);

cout<<endl;

break;

case 5:

cout<<"Postorder Traversal of BinarySearchTree:"<<endl;

bst.postorder(root);

cout<<endl;

break;

case 6:

cout<<"Display BinarySearchTree:"<<endl;

bst.display(root,1);

cout<<endl;

break;

case 7:

exit(1);

default:

cout<<"Wrong choice"<<endl;

}

}

}

//Find Element in the Tree

void BinarySearchTree::search(int item, node **parent, node **location)

{

node *ptr, *ptrsave;

if (root == NULL)

{

*location = NULL;

*parent = NULL;

return;

}

if (item == root->info)

{

*location = root;

*parent = NULL;

return;

}

if (item < root->info)

ptr = root->left;

else

ptr = root->right;

ptrsave = root;

while (ptr != NULL)

{

if (item == ptr->info)

{

*location = ptr;

*parent = ptrsave;

return;

}

ptrsave = ptr;

if (item < ptr->info)

ptr = ptr->left;

else

ptr = ptr->right;

}

*location = NULL;

*parent = ptrsave;

}

// Inserting Element into the Tree

void BinarySearchTree::insert(node *tree, node *newnode)

{

if (root == NULL)

{

root = new node;

root->info = newnode->info;

root->left = NULL;

root->right = NULL;

cout<<"Root Node is Added"<<endl;

return;

}

if (tree->info == newnode->info)

{

cout<<"Element already in the tree"<<endl;

return;

}

if (tree->info > newnode->info)

{

if (tree->left != NULL)

{

insert(tree->left, newnode);

}

else

{

tree->left = newnode;

(tree->left)->left = NULL;

(tree->left)->right = NULL;

cout<<"Node Added To Left"<<endl;

return;

}

}

else

{

if (tree->right != NULL)

{

insert(tree->right, newnode);

}

else

{

tree->right = newnode;

(tree->right)->left = NULL;

(tree->right)->right = NULL;

cout<<"Node Added To Right"<<endl;

return;

}

}

}

// Delete Element from the tree

void BinarySearchTree::del(int item)

{

node *parent, *location;

if (root == NULL)

{

cout<<"Tree is empty"<<endl;

return;

}

search(item, &parent, &location);

if (location == NULL)

{

cout<<"Item is not present in the tree"<<endl;

return;

}

if (location->left == NULL && location->right == NULL)

case_p(parent, location);

if (location->left != NULL && location->right == NULL)

case_q(parent, location);

if (location->left == NULL && location->right != NULL)

case_q(parent, location);

if (location->left != NULL && location->right != NULL)

case_r(parent, location);

free(location);

}

// Case p

void BinarySearchTree::case_p(node *par, node *loc )

{

if (par == NULL)

{

root = NULL;

}

else

{

if (loc == par->left)

par->left = NULL;

else

par->right = NULL;

}

}

// Case q

void BinarySearchTree::case_q(node *par, node *loc)

{

node *child;

if (loc->left != NULL)

child = loc->left;

else

child = loc->right;

if (par == NULL)

{

root = child;

}

else

{

if (loc == par->left)

par->left = child;

else

par->right = child;

}

}

// Case r

void BinarySearchTree::case_r(node *parent, node *location)

{

node *ptr, *ptrsave, *suc, *parsuc;

ptrsave = location;

ptr = location->right;

while (ptr->left != NULL)

{

ptrsave = ptr;

ptr = ptr->left;

}

suc = ptr;

parsuc = ptrsave;

if (suc->left == NULL && suc->right == NULL)

case_p(parsuc, suc);

else

case_q(parsuc, suc);

if (parent == NULL)

{

root = suc;

}

else

{

if (location == parent->left)

parent->left = suc;

else

parent->right = suc;

}

suc->left = location->left;

suc->right = location->right;

}

// Pre Order Traversal

void BinarySearchTree::preorder(node *ptr)

{

if (root == NULL)

{

cout<<"Tree is empty"<<endl;

return;

}

if (ptr != NULL)

{

cout<<ptr->info<<" ";

preorder(ptr->left);

preorder(ptr->right);

}

}

// In Order Traversal

void BinarySearchTree::inorder(node *ptr)

{

if (root == NULL)

{

cout<<"Tree is empty"<<endl;

return;

}

if (ptr != NULL)

{

inorder(ptr->left);

cout<<ptr->info<<" ";

inorder(ptr->right);

}

}

// Postorder Traversal

void BinarySearchTree::postorder(node *ptr)

{

if (root == NULL)

{

cout<<"Tree is empty"<<endl;

return;

}

if (ptr != NULL)

{

postorder(ptr->left);

postorder(ptr->right);

cout<<ptr->info<<" ";

}

}

// Display Tree Structure

void BinarySearchTree::display(node *ptr, int level)

{

int i;

if (ptr != NULL)

{

display(ptr->right, level+1);

cout<<endl;

if (ptr == root)

cout<<"Root->: ";

else

{

for (i = 0;i < level;i++)

cout<<" ";

}

cout<<ptr->info;

display(ptr->left, level+1);

}

}

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