Provide details on what you need help with along with a budget and time limit. Questions are posted anonymously and can be made 100% private.
Match with a Tutor
Studypool matches you to the best tutor to help you with your question. Our tutors are highly qualified and vetted.
Get Quality Help
Your matched tutor provides personalized help according to your question details. Payment is made only after you have completed your 1-on-1 session and are satisfied with your session.
COSC 237 Community College of Baltimore County Binary Tree Node Java Coding Task
User Generated
unzmn61
Programming
COSC 237
community college of baltimore county
COSC
Description
Unformatted Attachment Preview
COSC 237 Final Exam Review
In this programming question, you are asked to implement a special type of binary search trees called
DupBinarySearchTree that can accept duplicate values. In principal, DupBinarySearchTree is mostly identical
to a binary search tree, except that it has either (A) a counter variable or (B) an extra pointer mLink (called
middle link pointer) or to keep track of the duplicate values.
In (A), a counter variable is a property of the BinaryTreeNode class which increases when a duplicate
value/info is inserted to the BST.
In (B), a middle link pointer is a property of the BinaryTreeNode class which will be created and inserted in the
middle when a duplicate value/info is inserted to the BST. For example, the following DupBinarySearchTree
corresponds to the input sequence 3 2 1 2 3 1 4 1 1 5 (with the first 3 as the root) if approach (A) was used.
Here, each node in the DupBinarySearchTree has its info, and three pointers: left (lLink), middle (mLink) and
right (rLink) links to point to nodes whose info are less than, exactly equal and greater than the current node.
For instance, the root with info 3 has three links corresponding to nodes with info 2, 3 and 4 as indicated in the
picture while the node with info 1 and its three duplicates has only one middle link. The definition of tree height
on a BST also applies on DupBinarySearchTree. All functions/methods in Lab 12 are of particular
interested for the final exam.
Sample output 01
Enter integers (999 to stop): 3 2 1 2 3 1 4 1 1 5 999
Printing sorted tree WITHOUT duplicates:
1 2 3 4 5
Printing sorted tree WITH duplicates:
1 1 1 1 2 2 3 3 4 5
Checking isDupBST(): true
Tree Height: 5
Enter value to search for:
3
3 was found in this tree with 1 copies
Enter value to be deleted from tree:
1
1 is found and deleted.
Printing sorted tree WITH duplicates:
1 1 1 2 2 3 3 4 5
ALL DONE
Sample output 02
Enter integers (999 to stop): 3 2 1 2 3 1 4 1 1 5 999
Printing sorted tree WITHOUT duplicates:
1 2 3 4 5
Printing sorted tree WITH duplicates:
1 1 1 1 2 2 3 3 4 5
Checking isDupBST(): true
Tree Height: 5
Enter value to search for:
3
3 was found in this tree with 1 copies
Enter value to be deleted from tree:
7 is NOT found and is NOT deleted.
Printing sorted tree WITH duplicates:
1 1 1 1 2 2 3 3 4 5
ALL DONE
7
//Class: BinarySearchTree extends
//Class: BinaryTree
public class BinarySearchTree extends BinaryTree {
//Default constructor
public BinarySearchTree() {
super();
}
public boolean search(T item) {
return recSearch(root, item);
}
//helper: recursive method called by search
public boolean recSearch(BinaryTreeNode tree, T item) {
if(tree == null)
return false;
else {
Comparable temp = (Comparable) tree.info;
if (temp.compareTo(item) == 0)
return true;
else if (temp.compareTo(item) > 0)
return recSearch(tree.lLink, item);
else
return recSearch(tree.rLink, item);
}
}
public void insert(T item) {
root = recInsert(root, item);
}
//helper: recursive method called by insert
public BinaryTreeNode recInsert(BinaryTreeNode tree, T item) {
if(tree == null) {
//create new node
tree = new BinaryTreeNode(item);
}
else {
Comparable temp = (Comparable) tree.info;
if (temp.compareTo(item) == 0) {
System.err.print("Already in - duplicates are not allowed.");
return null;
}
else if (temp.compareTo(item) > 0)
tree.lLink = recInsert(tree.lLink, item);
else
tree.rLink = recInsert(tree.rLink, item);
}
return tree;
}
public void delete(T item) {
root = recDelete(root, item);
}
//helper: recursive method called by delete
public BinaryTreeNode recDelete(BinaryTreeNode tree, T item) {
if(tree == null){ //empty tree
System.err.println("Cannot delete from an empty tree.");
return null;
}
else {
Comparable temp = (Comparable) tree.info;
if (temp.compareTo(item) > 0)
tree.lLink = recDelete(tree.lLink, item);
else if(temp.compareTo(item) < 0)
tree.rLink = recDelete(tree.rLink, item);
else if(tree.lLink != null && tree.rLink != null) {// 2 children
tree.info = findMin(tree.rLink).info;
//tree.info = findMax(tree.lLink).info;
tree.rLink = removeMin(tree.rLink);
}
else if(root.lLink != null) //1 left child
tree = tree.lLink;
else if(root.rLink != null) //1 right child
tree = tree.rLink;
return tree;
}
}
//helper: method called by recDelete
protected BinaryTreeNode findMin(BinaryTreeNode tree) {
if(tree != null)
while(tree.lLink != null)
tree = tree.lLink;
return tree;
}
//helper: method called by recDelete
protected BinaryTreeNode removeMin(BinaryTreeNode tree) {
if(tree == null){ //empty tree
System.err.println("Cannot delete from an empty tree.");
return null;
}
else if(tree.lLink != null) {
tree.lLink = removeMin(tree.lLink);
return tree;
}
else
return tree.rLink;
}
}
//Class: BinaryTree implements
//Interface: BinaryTreeADT
//Inner class: BinaryTreeNode
import java.util.NoSuchElementException;
public abstract class BinaryTree implements BinaryTreeADT {
//Definition of the BinaryTreeNode class
protected class BinaryTreeNode {
public T info;
public BinaryTreeNode lLink;
public BinaryTreeNode rLink;
//Default constructor
public BinaryTreeNode() {
info = null;
lLink = null;
rLink = null;
}
//Alternate constructor
public BinaryTreeNode(T item) {
info = item;
lLink = null;
rLink = null;
}
//Alternate constructor
public BinaryTreeNode(T item, BinaryTreeNode left, BinaryTreeNode right) {
info = item;
lLink = left;
rLink = right;
}
public Object clone() {
BinaryTreeNode copy = null;
try {
copy = (BinaryTreeNode) super.clone();
}
catch (CloneNotSupportedException e) {
return null;
}
return copy;
}
public String toString() {
return info.toString();
}
}
//Instance variable vor class BinaryTree
protected BinaryTreeNode root;
//Default constructor
public BinaryTree() {
root = null;
}
public Object clone() {
BinaryTree copy = null;
try {
copy = (BinaryTree) super.clone();
}
catch (CloneNotSupportedException e) {
return null;
}
if (root != null)
copy.root = copyTree(root);
return copy;
}
//helper method called by clone
private BinaryTreeNode copyTree(BinaryTreeNode otherTreeRoot) {
BinaryTreeNode temp;
if (otherTreeRoot == null)
temp = null;
else {
temp = (BinaryTreeNode) otherTreeRoot.clone();
temp.lLink = copyTree(otherTreeRoot.lLink);
temp.rLink = copyTree(otherTreeRoot.rLink);
}
return temp;
}
public boolean isEmpty() {
return (root == null);
}
public void inOrderTraversal() {
inOrder(root);
}
//helper method called by inOrderTraversal
private void inOrder(BinaryTreeNode t) {
if (t != null) {
inOrder(t.lLink);
System.out.print(t.info + " ");
inOrder(t.rLink);
}
}
public void preOrderTraversal() {
preOrder(root);
}
//helper method called by preOrderTraversal
private void preOrder(BinaryTreeNode t) {
if (t != null) {
System.out.print(t.info + " ");
preOrder(t.lLink);
preOrder(t.rLink);
}
}
public void postOrderTraversal() {
postOrder(root);
}
//helper method called by postOrderTraversal
private void postOrder(BinaryTreeNode t) {
if (t != null) {
postOrder(t.lLink);
postOrder(t.rLink);
System.out.print(t.info + " ");
}
}
public int treeHeight() {
return height(root);
}
//helper method called by treeHeight
private int height(BinaryTreeNode t) {
if (t == null)
return 0;
else if (t.lLink == null && t.rLink == null)
return 0;
else
return 1 + Math.max(height(t.lLink), height(t.rLink));
}
public int treeNodeCount() {
return nodeCount(root);
}
//helper method called by treeNodeCount
private int nodeCount(BinaryTreeNode t) {
System.out.println("To be done later.");
return 0;
}
public int treeLeavesCount() {
return leavesCount(root);
}
//helper method called by treeLeavesCount
private int leavesCount(BinaryTreeNode t) {
System.out.println("To be done later.");
return 0;
}
public void destroyTree() {
root = null;
}
//abstract methods
public abstract boolean search(T item);
public abstract void insert(T item);
public abstract void delete(T item);
}
//Interface: BinaryTreeADT
public interface BinaryTreeADT extends Cloneable {
public Object clone(); //Returns a clone of this BT.
public boolean isEmpty();//Method to determine whether the BT is empty.
public void inOrderTraversal();//Method to do an inorder traversal of a BT.
public void preOrderTraversal();//Method to do a preorder traversal of a BT.
public void postOrderTraversal();//Method to do a postorder traversal of a BT.
public int treeHeight(); //Method to determine the height of a BT.
public int treeNodeCount();//Method to find the number of nodes in a binary tree.
public int treeLeavesCount();//Method to determine the number of leaves in a BT.
public void destroyTree(); //Method to destroy a BT.
public boolean search(T item); //Method to determine whether item is in a BT.
public void insert(T item); //Method to insert item in a BT.
public void delete(T item);//Method to delete item from a BT.
}
//Input in this order: 70 83 39 27 45 94 55 80 85 105 40 95 102 52 50 125 110 999
import java.util.Scanner;
public class ClientBST {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BinarySearchTree bst = new BinarySearchTree();
Integer num;
System.out.print("Enter integers(999 to stop): ");
num = input.nextInt();
while (num != 999) {
bst.insert(num);
num = input.nextInt();
}
System.out.println("Tree Height: " + bst.treeHeight());
System.out.print("Enter value to search for: ");
num = input.nextInt();
if(bst.search(num))
System.out.println(num + " was found in this tree");
else
System.out.println(num + " was NOT found in this tree");
System.out.print("Inorder traversal: ");
bst.inOrderTraversal();
System.out.print("\nPreorder traversal: ");
bst.preOrderTraversal();
System.out.print("\nPostorder traversal: ");
bst.postOrderTraversal();
System.out.print("\nEnter value to be deleted from tree: ");
num = input.nextInt();
bst.delete(num);
System.out.print("\nInorder traversal after removing " + num + ": ");
bst.inOrderTraversal();
System.out.print("\nPreorder traversal after removing " + num + ": ");
bst.preOrderTraversal();
System.out.print("\nPostorder traversal after removing " + num + ": ");
bst.postOrderTraversal();
System.out.println();
}
}
Purchase answer to see full
attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Would you like to help your fellow students? Are you in need of an additional source of income? Apply to become a tutor on Studypool! Our best tutors earn over $7,500 each month!