C++ PROGRAMMING Topic: Binary Search Trees Explain the c++ code below.: SEE ATTACHED PHOTO FOR THE PROBLEM INSTRUCTIONS It doesn't have to be long, as long as you explain what the important parts of...





C++ PROGRAMMING

Topic:Binary Search Trees



Explain the c++ code below.:SEE ATTACHED PHOTO FOR THE PROBLEM INSTRUCTIONS




It doesn't have to be long, as long as you explain what the important parts of the code do. (The code is already implemented and correct, only the explanation needed)






node* findNewRoot(node* curr) {

        if(curr->left == NULL)

        {

            return curr;

        }

        return findNewRoot(curr->left);

    }





bool remove(int num) {

bool isPresent = search(num);

if(isPresent){

bool rem = false;

int numOfChild;

node* realRoot = search_node(root,num);

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

{

numOfChild = 0;

}

else if((realRoot->left != NULL && realRoot->right == NULL) || (realRoot->left == NULL && realRoot->right != NULL) )

{

numOfChild = 1;

}

else if(realRoot->left != NULL && realRoot->right != NULL)

{

numOfChild = 2;

}

if(numOfChild == 0)

{

bool leadRoot = false;

if(realRoot == root)

{

leadRoot = true;

}

if(leadRoot)

{

free(realRoot);

size--;

BSTree();

rem = true;

return rem;

}

if(realRoot->right == NULL && realRoot->left == NULL) {

bool toRight;

if(realRoot->element > realRoot->parent->element)

{

toRight = true;

}

else

{

toRight = false;

}



if(toRight)

{

realRoot->parent->right = NULL;

}

else

{

realRoot->parent->left = NULL;

}

rem = true;

free(realRoot);

size--;

return rem;

}

}

if(numOfChild == 1) {

bool leadRoot = false;

if(realRoot == root)

{

leadRoot = true;

}

if(realRoot->right != NULL && realRoot->left == NULL) {

bool toRight;

if(leadRoot)

{

root = realRoot->right;

root->parent = NULL;

rem = true;

free(realRoot);

 return rem;

}

if(realRoot->element > realRoot->parent->element)

{

toRight = true;

}

else

{

toRight = false;

}



if(toRight)

{

realRoot->parent->right = realRoot->right;

realRoot->right->parent = realRoot->parent;

}

else

{

realRoot->parent->left = realRoot->right;

realRoot->right->parent = realRoot->parent;

}

rem = true;

free(realRoot);

size--;

return rem;

}

if(realRoot->left != NULL && realRoot->right == NULL) {

bool toRight;

if(leadRoot)

{

root = realRoot->left;

root->parent = NULL;

rem = true;

free(realRoot);

return rem;

}

if(realRoot->element > realRoot->parent->element)

{

toRight = true;

}

else

{

toRight = false;

 }



if(toRight)

{

realRoot->parent->right = realRoot->left;

realRoot->left->parent = realRoot->parent;

}

else

{

 realRoot->parent->left = realRoot->left;

realRoot->left->parent = realRoot->parent;

}

rem = true;

free(realRoot);

size--;

return rem;

}

}

if(numOfChild == 2) {

node* temp;

temp = findNewRoot(realRoot->right);

if(realRoot->right->element == temp->element)

{

bool uniqueRight = false;

if(temp->right != NULL)

{

uniqueRight = true;

}

if(uniqueRight)

{

realRoot->right = temp->right;

temp->right->parent = temp->parent;

}

else

{

realRoot->right = NULL;

}

rem = true;

realRoot->element = temp->element;

free(temp);

size--;

return rem;

}

bool hasRight = false;

if(temp->right != NULL)

{

hasRight = true;

}

if(hasRight)

{

temp->parent->left = temp->right;

temp->right->parent = temp->parent;

}

else

{

temp->parent->left = NULL;

}

rem = true;

realRoot->element = temp->element;

free(temp);

size--;

}

return rem;

}

return isPresent;

}




bool isEmpty() {

        // TODO isEmpty

        return size == 0;

    }




void print_preorder(node* curr) {

        // TODO preorder traversal

        cout < curr->element <>

        if(curr->left != NULL)

        {

            print_preorder(curr->left);

        }

        if(curr->right != NULL)

        {

            print_preorder(curr->right);

        }

    }




void print_postorder(node* curr) {

        // TODO postorder traversal

        if(curr->left != NULL)

        {

            print_postorder(curr->left);

        }

        if(curr->right != NULL)

        {

            print_postorder(curr->right);

        }

        cout < curr->element <>

    }








You are to continue finishing up the last primary methods of the Binary Search Tree: the<br>remove method and the isEmpty which is self-explanatory.<br>As discussed in the lecture, you have to take note of three considerations in deleting a node:<br>(1) if the node has no children, just easily remove (or free up) the node and there will be no<br>consequences. Also make sure that its parent's child pointer no longer points to that node<br>that you have deleted, (2) if the node has exactly one child, delete the node and promote<br>the child to replace the deleted node (i.e. if the node deleted was once a right child, make<br>the child the new right child), (3) if the node has two children, find the least element in the<br>right subtree and replace the node's value with that least element and delete that node<br>instead. If you have successfully deleted a node, return true.<br>Of course, if the specified number does not exist in the tree, you are not to remove anything<br>and only return false.<br>In addition, you are also to implement the pre-order and the post-order traversal of the tree<br>much like how we did the in-order traversal in our implementation. The

Extracted text: You are to continue finishing up the last primary methods of the Binary Search Tree: the remove method and the isEmpty which is self-explanatory. As discussed in the lecture, you have to take note of three considerations in deleting a node: (1) if the node has no children, just easily remove (or free up) the node and there will be no consequences. Also make sure that its parent's child pointer no longer points to that node that you have deleted, (2) if the node has exactly one child, delete the node and promote the child to replace the deleted node (i.e. if the node deleted was once a right child, make the child the new right child), (3) if the node has two children, find the least element in the right subtree and replace the node's value with that least element and delete that node instead. If you have successfully deleted a node, return true. Of course, if the specified number does not exist in the tree, you are not to remove anything and only return false. In addition, you are also to implement the pre-order and the post-order traversal of the tree much like how we did the in-order traversal in our implementation. The "visit" action remains to be to print the element of the visited node.
Jun 09, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here