Due: Sunday December 9, 2018 @ 11:59PM
Handout Code is available HERE
BTNode<E>
You MUST use binary trees as developed in class for this assignment. The approach differs from the one in the text, but refer to the text for examples and a related explanation. If there is a conflict, what we've done in class is right. As always, ask for help if you are confused.
For this assignment, you will write the insert and remove method for a program that builds a binary search tree of characters.
In both cases, there may be duplicate characters in the lines. This is okay. Store all copies of a character in separate nodes. If there are three copies of a character in the first line, and two are removed because they are in the second line, one copy remains in the tree.
You are tasked with implementing four methods in BinarySearchTree.java
/**
* insert item into tree rooted at node
* look ahead to insert before falling off end of subtree
*
* @param node
* root of current subtree
* @param item
* item to be inserted in tree
*/
private void insert(BTNode<E> root, E item);
/**
* remove target from tree rooted at root with provided parent
*
* @param node
* node at top of current sub-tree that may contain target
* @param data
* item to be removed if it exists in tree
*/
private BTNode<E> remove (BTNode<E> node, E data)
/**
* Find the rightmost node (largest value), disconnect it from the tree, and
* return its data.
*
* Preconditions: node and parent are not null.
*
* @param parent
* parent of this node so that it can be deleted
* @param node
* current node that potentially is rightmost
* @return the data from the rightmost node which has been removed
*/
private E removeLargest (BTNode<E> parent, BTNode<E> node)
/**
* Replaces old child with new child in parent's links. Figures out whether it
* was a left or right child.
*
* Precondition: parent is not null.
*
* @param parent
* node that has children with one needing replacement
* @param oldChild
* node that will be discarded
* @param newChild
* node that will be attached in oldChild's place
*/
private void replaceChild (BTNode<E> parent, BTNode<E> oldChild, BTNode<E> newChild)
insert
and remove
MUST be implemented recursively
In a binary search tree, the entries of the nodes can be compared with a total order semantics. These two rules are followed for every node n:
Enter a line of input to insert: marauder
After inserting: aademrru
-
u
-
r
-
r
-
m
-
e
-
d
-
a
-
a
-
Line of characters to remove: rummage
After removing: adr
-
r
-
d
-
a
-
insert
first. Think about the condition necessary for inserting into a BSTremove
. Think about all the various cases for removalreplaceChild
and removeLargest
fit into the logic of remove
replaceChild
(hint: it's not recursive)removeLargest
(hint: this can be recursive or it can use a while-loop)Submit this program as Lab 9: Binary Search Tree
NOTE: credit will ONLY be given if insert AND remove are implemented recursively