Question: Create a class BinarySearchTree. A class that implements the ADT binary search tree by extending BinaryTree. Recursive version. public class BinarySearchTree
Create a class BinarySearchTree. A class that implements the ADT binary search tree by extending BinaryTree. Recursive version.
public class BinarySearchTree
public BinarySearchTree(T rootEntry) { super(); setRootNode(new BinaryNode<>(rootEntry)); } // end constructor
public void setTree(T rootData) // Disable setTree (see Segment 25.6) { throw new UnsupportedOperationException(); } // end setTree
public void setTree(T rootData, BinaryTreeInterface
//...
public T remove(T entry) { ReturnObject oldEntry = new ReturnObject(null); BinaryNode
return oldEntry.get(); } // end remove
//...
private class ReturnObject { private T item; private ReturnObject(T entry) { item = entry; } // end constructor public T get() { return item; } // end get
public void set(T entry) { item = entry; } // end set } // end ReturnObject } // end BinarySearchTree HERE IS BinaryTree.java
import java.util.Iterator; import java.util.NoSuchElementException;
public class BinaryTree
private BinaryNode
public BinaryTree() { root = null; }
public BinaryTree(T rootData) { root = new BinaryNode<>(rootData); }
public BinaryTree(T rootData, BinaryTree
private void privateSetTree(T rootData, BinaryTree
if (leftTree != null && !leftTree.isEmpty()) { root.setLeftChild(leftTree.root); }
if (rightTree != null && !rightTree.isEmpty()) { if (rightTree != leftTree) { root.setRightChild(rightTree.root); } else { root.setRightChild(rightTree.root.copy()); } }
if (leftTree != null && leftTree != this) { leftTree.clear(); }
if (rightTree != null && rightTree != this) { rightTree.clear(); } }
@Override public T getRootData() { if (isEmpty()) { throw new EmptyTreeException("The tree is empty."); } return root.getData(); }
@Override public int getHeight() { return root.getHeight(); }
@Override public int getNumberOfNodes() { return root.getNumberOfNodes(); }
@Override public boolean isEmpty() { return root == null; }
@Override public void clear() { root = null; }
@Override public Iterator
@Override public Iterator
@Override public Iterator
private class PreorderIterator implements Iterator
public PreorderIterator() { nodeStack = new LinkedStack<>(); if (root != null) { nodeStack.push(root); } }
public boolean hasNext() { return !nodeStack.isEmpty(); }
public T next() { BinaryNode
if (hasNext()) { nextNode = nodeStack.pop(); BinaryNode
if (rightChild != null) { nodeStack.push(rightChild); }
if (leftChild != null) { nodeStack.push(leftChild); } } else { throw new NoSuchElementException(); }
return nextNode.getData(); }
public void remove() { throw new UnsupportedOperationException(); } }
private class InorderIterator implements Iterator
public InorderIterator() { nodeStack = new LinkedStack<>(); currentNode = root; }
public boolean hasNext() { return !nodeStack.isEmpty() || (currentNode != null); }
public T next() { BinaryNode
while (currentNode !=null) { nodeStack.push(currentNode); currentNode = currentNode.getLeftChild(); } if (!nodeStack.isEmpty()) { nextNode = nodeStack.pop(); currentNode = nextNode.getRightChild(); } else { throw new NoSuchElementException(); }
return nextNode.getData(); }
public void remove() { throw new UnsupportedOperationException(); } }
private class PostorderIterator implements Iterator
public boolean hasNext() { return !nodeStack.isEmpty() || (currentNode != null); }
public T next() { BinaryNode
while (currentNode != null) { nodeStack.push(currentNode); leftChild = currentNode.getLeftChild(); if (leftChild == null) { currentNode = currentNode.getRightChild(); } else { currentNode = leftChild; } }
// stack is not empty, pop top node if (!nodeStack.isEmpty()) { nextNode = nodeStack.pop();
BinaryNode
return nextNode.getData(); }
public void remove() { throw new UnsupportedOperationException(); }//end remove }
@Override public Iterator
@Override public void setTree(T rootData) { }
@Override public void setTree(T rootData, BinaryTreeInterface
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
