Using Java you are to implement a Binary Search Tree (BST) of integers (with no
ID: 3859418 • Letter: U
Question
Using Java you are to implement a Binary Search Tree (BST) of integers (with no duplicate) where each node contains: (1) an integer value; (2) reference to the left child; (3) reference to the right child; and (4) a reference to the node’s parent (you may consider that the parent of the root is equal to null). Recall that a BST has the property that for every node in the tree, the value of any node in its left sub-tree is less than the value of the node, and the value of any node in its right sub-tree is greater than the value of the node.
Add the following methods to your BST:
1-add(int element): this method adds a node with value element into the BST
2-int leaves(): this method returns the number of leaves in the tree
3-List<Integer> getPath(int value): this method returns the path from the specified value to the root. Your method should take time proportional to the height of the tree
4-int rangeCount(int leftValue, int rightValue): this method returns the number of elements in the BST that are between leftValue and rightValue. You may assume that both leftValue and rightValue exist in the tree
5-void toReverse(): this method modifies the BST into a reverse BST. A reverse BST has the property that for every node in the tree, the value of any node in its left sub-tree is greater than the value of the node, and the value of any node in its right sub-tree is less than the value of the node. Note, in all the other methods you can assume that the tree is a BST.
6-Write a main method that tests your implementation. Note, you can add any other helper methods to simply your implementation.
Explanation / Answer
public bool Remove(T data)
{
if (root == null)
return false;
BinaryTreeNode<T> current = root, parent = null;
int result = comparer.Compare(current.Value, data);
while (result != 0)
{
if (result > 0)
{
parent = current;
current = current.Left;
}
else if (result < 0)
{
parent = current;
current = current.Right;
}
if (current == null)
return false;
else
result = comparer.Compare(current.Value, data);
}
count--;
// CASE 1: If current has no right child, then current's left child becomes
// the node pointed to by the parent
if (current.Right == null)
{
if (parent == null)
root = current.Left;
else
{
result = comparer.Compare(parent.Value, current.Value);
if (result > 0)
parent.Left = current.Left;
else if (result < 0)
parent.Right = current.Left;
}
}
// CASE 2: If current's right child has no left child, then current's right child
// replaces current in the tree
else if (current.Right.Left == null)
{
current.Right.Left = current.Left;
if (parent == null)
root = current.Right;
else
{
result = comparer.Compare(parent.Value, current.Value);
if (result > 0)
parent.Left = current.Right;
else if (result < 0)
parent.Right = current.Right;
}
}
// CASE 3: If current's right child has a left child, replace current with current's
// right child's left-most descendent
else
{
BinaryTreeNode<T> leftmost = current.Right.Left, lmParent = current.Right;
while (leftmost.Left != null)
{
lmParent = leftmost;
leftmost = leftmost.Left;
}
lmParent.Left = leftmost.Right;
leftmost.Left = current.Left;
leftmost.Right = current.Right;
if (parent == null)
root = leftmost;
else
{
result = comparer.Compare(parent.Value, current.Value);
if (result > 0
parent.Left = leftmost;
else if (result < 0)
parent.Right = leftmost;
}
}
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.