Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package BST; import Queue.MyQueue; public class BinarySearchTree implements BSTInterface { BSTNode root = null; public static void main ( String [ ] args ) {
package BST;
import Queue.MyQueue;
public class BinarySearchTree implements BSTInterface
BSTNode root null;
public static void mainString args
BinarySearchTree tree new BinarySearchTree;
Optional code for extra points
boolean Optional false;
Testing for inserting words into the tree
Prints inorder traversal of tree
tree.putdog;
tree.putcat;
tree.putalligator;
tree.putgiraffe;
tree.putelephant;
tree.puthippo;
tree.putzebra;
tree.printTreeStructure;
System.out.printlnContains dog: tree.containsdog;
System.out.printlnContains alligator: tree.containsalligator;
System.out.printlnContains horse: tree.containshorse;
System.out.println;
tree.deletedog;
tree.printTreeStructure;
System.out.println;
tree.deleteelephant;
tree.printTreeStructure;
System.out.println;
tree.deletecat;
tree.printTreeStructure;
tree.putpenguin;
tree.putaardvark;
tree.putbear;
tree.printTreeStructure;
Optional
if Optional
MyQueue inOrder tree.inOrder;
MyQueue preOrder tree.preOrder;
MyQueue postOrder tree.postOrder;
System.out.printlnIn order: inOrder;
System.out.printlnPreorder: preOrder;
System.out.printlnPostorder: postOrder;
Testing if clearing the tree works
System.out.println;
tree.makeEmpty;
System.out.printlntreeisEmpty;
public boolean isEmpty
return true;
returns true if the BST is empty, false otherwise
public void makeEmpty
Empties the BST
public boolean containsString s
return true;
Returns true if the BST contains the String, false otherwise
public void putString s
Adds a String to the BST If the String is already in the BST does nothing.
public void deleteString s
Removes a String from the BST If the String isn't in the BST does nothing.
public MyQueue inOrder
return new MyQueue;
OPTIONAL, Returns a queue with the elements in order
public MyQueue preOrder
return new MyQueue;
OPTIONAL, Returns a queue with the elements pre order
public MyQueue postOrder
return new MyQueue;
OPTIONAL, Returns a queue with the elements post order
TODO: Fill this in and call it from contains
protected boolean recursiveSearchBSTNode node, String s
return true;
TODO: Fill this in and call it from put
protected BSTNode recursiveInsertBSTNode node, String s
return new BSTNode;
TODO: Fill this in and call it from delete
protected BSTNode recursiveRemoveBSTNode node, String s
return new BSTNode;
TODO: Fill this in and call it from recursiveRemove
protected BSTNode deleteNodeBSTNode node
return new BSTNode;
TODO: Fill this in and call it from deleteNode
protected String getSmallestBSTNode node
return ;
Extra Credit
TODO: Fill this in and call it from inOrder
protected void inOrderRecursiveBSTNode node, MyQueue queue
TODO: Fill this in and call it from preOrder
protected void preOrderRecursiveBSTNode node, MyQueue queue
TODO: Fill this in and call it from postOrder
protected void postOrderRecursiveBSTNode node, MyQueue queue
Prints out the tree structure, using indenting to show the different levels
of the tree
public void printTreeStructure
printTree root;
Recursive helper for printTreeStructure
protected void printTreeint depth, BSTNode node
ifdepth System.out.println;
indentdepth;
if node null
System.out.printlnnodeitem;
printTreedepth node.left;
printTreedepth node.right;
else
System.out.printlnnull;
Indents with spaces
protected void indentint depth
for int i ; i depth; i
System.out.print; Indents two spaces for each unit of depth
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started