Objective: Binary Search Tree traversal (2 points)
Use traversal.pptx as guidance to write a program to build a binary search tree Dictionary. Input
records from inventory.txt. Both key and Element of BST have the same data from each input
record.
public interface BinNode {
/** Get and set the element value */
public E element();
public void setElement(E v);
/** @return The left child */
public BinNode left();
/** @return The right child */
public BinNode right();
/** @return True if a leaf node, false otherwise */
public boolean isLeaf();
}
import java.lang.Comparable;
/** Binary Search Tree implementation for Dictionary ADT */
class BST, E>
implements Dictionary {
private BSTNode root; // Root of the BST
int nodecount; // Number of nodes in the BST
/** Constructor */
BST() { root = null; nodecount = 0; }
/** Reinitialize tree */
public void clear() { root = null; nodecount = 0; }
/** Insert a record into the tree.
@param k Key value of the record.
@param e The record to insert. */
public void insert(Key k, E e) {
root = inserthelp(root, k, e);
nodecount++;
}
// Return root
public BSTNode getRoot()
{
return root;
}
/** Remove a record from the tree.
@param k Key value of record to remove.
@return The record removed, null if there is none. */
public E remove(Key k) {
E temp = findhelp(root, k); // First find it
if (temp != null) {
root = removehelp(root, k); // Now remove it
nodecount--;
}
return temp;
}
/** Remove and return the root node from the dictionary.
@return The record removed, null if tree is empty. */
public E removeAny() {
if (root == null) return null;
E temp = root.element();
root = removehelp(root, root.key());
nodecount--;
return temp;
}
/** @return Record with key value k, null if none exist.
@param k The key value to find. */
public E find(Key k) { return findhelp(root, k); }
/** @return The number of records in the dictionary. */
public int size() { return nodecount; }
private E findhelp(BSTNode rt, Key k) {
if (rt == null) return null;
if (rt.key().compareTo(k) > 0)
return findhelp(rt.left(), k);
else if (rt.key().compareTo(k) == 0) return rt.element();
else return findhelp(rt.right(), k);
}
/** @return The current subtree, modified to contain
the new item */
private BSTNode inserthelp(BSTNode rt,
Key k, E e) {
if (rt == null) return new BSTNode(k, e);
if (rt.key().compareTo(k) > 0)
rt.setLeft(inserthelp(rt.left(), k, e));
else
rt.setRight(inserthelp(rt.right(), k, e));
return rt;
}
/** Remove a node with key value k
@return The tree with the node removed */
private BSTNode removehelp(BSTNode rt,Key k) {
if (rt == null) return null;
if (rt.key().compareTo(k) > 0)
rt.setLeft(removehelp(rt.left(), k));
else if (rt.key().compareTo(k) < 0)
rt.setRight(removehelp(rt.right(), k));
else { // Found it
if (rt.left() == null) return rt.right();
else if (rt.right() == null) return rt.left();
else { // Two children
BSTNode temp = getmin(rt.right());
rt.setElement(temp.element());
rt.setKey(temp.key());
rt.setRight(deletemin(rt.right(.
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
1. Objective: Binary Search Tree traversal (2 points)
Use traversal.pptx as guidance to write a program to build a binary search tree Dictionary. Input
records from inventory.txt. Both key and Element of BST have the same data from each input
record.
public interface BinNode {
/** Get and set the element value */
public E element();
public void setElement(E v);
/** @return The left child */
public BinNode left();
/** @return The right child */
public BinNode right();
/** @return True if a leaf node, false otherwise */
public boolean isLeaf();
}
import java.lang.Comparable;
/** Binary Search Tree implementation for Dictionary ADT */
class BST, E>
implements Dictionary {
private BSTNode root; // Root of the BST
int nodecount; // Number of nodes in the BST
/** Constructor */
BST() { root = null; nodecount = 0; }
/** Reinitialize tree */
public void clear() { root = null; nodecount = 0; }
/** Insert a record into the tree.
@param k Key value of the record.
@param e The record to insert. */
public void insert(Key k, E e) {
root = inserthelp(root, k, e);
nodecount++;
}
// Return root
public BSTNode getRoot()
{
2. return root;
}
/** Remove a record from the tree.
@param k Key value of record to remove.
@return The record removed, null if there is none. */
public E remove(Key k) {
E temp = findhelp(root, k); // First find it
if (temp != null) {
root = removehelp(root, k); // Now remove it
nodecount--;
}
return temp;
}
/** Remove and return the root node from the dictionary.
@return The record removed, null if tree is empty. */
public E removeAny() {
if (root == null) return null;
E temp = root.element();
root = removehelp(root, root.key());
nodecount--;
return temp;
}
/** @return Record with key value k, null if none exist.
@param k The key value to find. */
public E find(Key k) { return findhelp(root, k); }
/** @return The number of records in the dictionary. */
public int size() { return nodecount; }
private E findhelp(BSTNode rt, Key k) {
if (rt == null) return null;
if (rt.key().compareTo(k) > 0)
return findhelp(rt.left(), k);
else if (rt.key().compareTo(k) == 0) return rt.element();
else return findhelp(rt.right(), k);
}
3. /** @return The current subtree, modified to contain
the new item */
private BSTNode inserthelp(BSTNode rt,
Key k, E e) {
if (rt == null) return new BSTNode(k, e);
if (rt.key().compareTo(k) > 0)
rt.setLeft(inserthelp(rt.left(), k, e));
else
rt.setRight(inserthelp(rt.right(), k, e));
return rt;
}
/** Remove a node with key value k
@return The tree with the node removed */
private BSTNode removehelp(BSTNode rt,Key k) {
if (rt == null) return null;
if (rt.key().compareTo(k) > 0)
rt.setLeft(removehelp(rt.left(), k));
else if (rt.key().compareTo(k) < 0)
rt.setRight(removehelp(rt.right(), k));
else { // Found it
if (rt.left() == null) return rt.right();
else if (rt.right() == null) return rt.left();
else { // Two children
BSTNode temp = getmin(rt.right());
rt.setElement(temp.element());
rt.setKey(temp.key());
rt.setRight(deletemin(rt.right()));
}
}
return rt;
}
private BSTNode getmin(BSTNode rt) {
if (rt.left() == null) return rt;
return getmin(rt.left());
}
4. private BSTNode deletemin(BSTNode rt) {
if (rt.left() == null) return rt.right();
rt.setLeft(deletemin(rt.left()));
return rt;
}
private void printhelp(BSTNode rt) {
if (rt == null) return;
printhelp(rt.left());
printVisit(rt.element());
printhelp(rt.right());
}
private StringBuffer out;
public String toString() {
out = new StringBuffer(400);
printhelp(root);
return out.toString();
}
private void printVisit(E it) {
out.append(it + " ");
}
}
class BSTNode implements BinNode {
private Key key; // Key for this node
private E element; // Element for this node
private BSTNode left; // Pointer to left child
private BSTNode right; // Pointer to right child
/** Constructors */
public BSTNode() {left = right = null; }
public BSTNode(Key k, E val)
{ left = right = null; key = k; element = val; }
public BSTNode(Key k, E val,
BSTNode l, BSTNode r)
{ left = l; right = r; key = k; element = val; }
/** Get and set the key value */
public Key key() { return key; }
public void setKey(Key k) { key = k; }
5. /** Get and set the element value */
public E element() { return element; }
public void setElement(E v) { element = v; }
/** Get and set the left child */
public BSTNode left() { return left; }
public void setLeft(BSTNode p) { left = p; }
/** Get and set the right child */
public BSTNode right() { return right; }
public void setRight(BSTNode p) { right = p; }
/** @return True if a leaf node, false otherwise */
public boolean isLeaf()
{ return (left == null) && (right == null); }
}
public interface Dictionary {
/** Reinitialize dictionary */
public void clear();
/** Insert a record
@param k The key for the record being inserted.
@param e The record being inserted. */
public void insert(Key k, E e);
/** Remove and return a record.
@param k The key of the record to be removed.
@return A maching record. If multiple records match
"k", remove an arbitrary one. Return null if no record
with key "k" exists. */
public E remove(Key k);
/** Remove and return an arbitrary record from dictionary.
@return the record removed, or null if none exists. */
public E removeAny();
/** @return A record matching "k" (null if none exists).
If multiple records match, return an arbitrary one.
@param k The key of the record to find */
public E find(Key k);
/** @return The number of records in the dictionary. */
public int size();
};
7. // parametrized constructor
public BST(BSTNode root) {
this.root = root;
}
// arranging nodes when cases like deleting node
public BSTNode MinAttach(BSTNode right, BSTNode left) {
if(right.getLeft() == null) {
right.setLeft(left);
return right;
}
else {
right.setLeft(MinAttach(right.getLeft(), left));
return right;
}
}
// deleting entry ..passing key
public void delete(K key) {
this.root = deleteNodeRecursively(root, key);
}
// deleting nodes
public BSTNode DeleteDoubNode(BSTNode node) {
if(node.getLeft() == null) {
//at the bottom of the nodes.
return node.getRight();
}
else {
//set the left node as the right of the one we found at the bottom.
node.setLeft(DeleteDoubNode(node.getLeft()));
}
return node;
}
// recursively deleting node
public BSTNode deleteNodeRecursively(BSTNode node, K key) {
// check right node to delete
if(key.compareTo(node.getKey()) == 0) {
// check for leaf
8. if((node.getLeft() == null) && (node.getRight() == null)) {
return null;
}
//check node with right child
else if((node.getLeft() == null) && (node.getRight() != null)) {
return node.getRight();
}
//check node with left child
else if((node.getLeft() != null) && (node.getRight() == null)) {
return node.getLeft();
}
// check node with two children
else if((node.getLeft() != null) && (node.getRight() != null)) {
BSTNode replacementNode = findMin(node.getRight());
BSTNode backupLeft = node.getLeft();
replacementNode.setRight(DeleteDoubNode(node.getRight()));
replacementNode.setLeft(backupLeft);
return replacementNode;
}
else {
return node;
}
}
//check key is less than
else if(key.compareTo(node.getKey()) < 0) {
if(node.getLeft() != null) {
node.left = deleteNodeRecursively(node.getLeft(), key);
return node;
}
}
//check key is greater than
else {
if(node.getRight() != null) {
node.right = deleteNodeRecursively(node.getRight(), key);
return node;
}
9. }
return node;
}
// returns depth of tree
public int depth() {
return depthPostOrder(root, 0);
}
// find minimum value of BST
public BSTNode findMin(BSTNode node) {
while(node.getLeft() != null) {
node = node.getLeft();
}
return node;
}
// BST inorder
public void inorder(BSTNode node) {
if(node != null) {
inorder(node.getLeft()); //get first left keys
System.out.println("key: " + node.getKey().toString() + " element: " +
node.getElement().toString());
inorder(node.getRight());
}
}
// inserting new element
public void insert(K key, E element) {
if(root == null) {
root = new BSTNode(key, element, null, null);
}
else {
insertBelow(root, key, element);
}
}
// recursive preorder depth
int depthPostOrder(BSTNode node, int current_depth) {
if(node != null) {return Math.max(depthPostOrder(node.getLeft(), current_depth+1),
10. depthPostOrder(node.getRight(), current_depth+1));
}
else return current_depth;
}
// printing whole tree
public void printTree() {
//printing recursively
System.out.println(" Outputting BSTree...");
inorder(root);
}
// serach method
public E search(K key) {
BSTNode nodeFound;
nodeFound = searchingNode(key);
if(nodeFound == null) {
return null; //not found
}
else return searchingNode(key).getElement(); //call another helper method.
}
// searching node
public BSTNode searchingNode(K key) {
if(key == null) {
return null;
}
if(root == null) {
return null; //if empty tree
}
//if both keys are equal
if(key.compareTo(root.getKey()) == 0) {
return root;
}
//less than root key
else if(key.compareTo(root.getKey()) < 0) {
return searchBelow(root.getLeft(), key); //call the recursive search method.
}