I have a BinarySearchTree , which consists of nodes that are like a template of the studentType student class, where the student is a class with private name and class variables.
At the moment, I can print the tree, find the names and / or ratings in the tree, but I canβt remove the nodes from the tree.
I am trying to remove all students who had a class of 50 (this did not succeed).
After removing node, you must do one of the following:
- Left child is empty: replace node with its right child.
- The left child is not empty: replace node with the highest element on the left branch.
My understanding of this, if it was a tree:
1 / \ 2 3 / \ /\ 4 5 6 7
If 2 failed, i.e. fifty
As a result, you get
1 / \ 4 3 \ / \ 5 6 7
4 - the highest element in the left branch.
If it was a tree:
1 / \ 2 3 \ / \ 5 6 7
and 2 failed
you will have
1 / \ 5 3 / \ 6 7
If it was a tree:
1 / \ 2 3 / \ / \ 4 5 6 7
and 1 failed to execute
you will have
5 / \ 2 3 / / \ 4 6 7
I am having problems creating functions for this, at the moment I have:
void checkBranch() //called from main - used to pass the root node to checkBranch() { checkBranch(studentTree.getRoot()); } bool checkBranch(BTNode<Student>* currentNode) { if (currentNode != NULL) { if (checkBranch(currentNode -> getLeft())) { deleteNode(currentNode, true); } if (checkBranch(currentNode -> getRight())) { deleteNode(currentNode, false); } return (currentNode -> getData().getGrade() < 50.0); } else { return false; } }
Now I'm trying to add the deleteNode function, where I just got stuck on what to do / handle what should happen:
void deleteNode(BTNode<Student> *parentNode, bool left) { BTNode<Student>* nodeToDelete; if (left) { nodeToDelete = parentNode->getLeft(); } }
source share