Anzeige
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
Anzeige
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
Nächste SlideShare
Please help write BinaryTree-java Thank you!   Create a class BinaryTr.pdfPlease help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
Wird geladen in ... 3
1 von 7
Anzeige

Más contenido relacionado

Similar a Create a class BinarySearchTree- A class that implements the ADT binar.pdf(20)

Más de shyamsunder1211(20)

Anzeige

Create a class BinarySearchTree- A class that implements the ADT binar.pdf

  1. Create a class BinarySearchTree. A class that implements the ADT binary search tree by extending BinaryTree. Recursive version. public class BinarySearchTree<T extends Comparable<? super T>> extends BinaryTree<T> implements SearchTreeInterface<T> { public BinarySearchTree () { super(); } // end default constructor 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<T> leftTree, BinaryTreeInterface<T> rightTree) { throw new UnsupportedOperationException(); } // end setTree //... public T remove (T entry) { ReturnObject oldEntry = new ReturnObject(null); BinaryNode<T> newRoot = removeEntry(getRootNode(), entry, oldEntry); setRootNode(newRoot); return oldEntry.get(); } // end remove //... private class ReturnObject { private T item; private ReturnObject(T entry)
  2. { 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<T> implements BinaryTreeInterface<T> { private BinaryNode<T> root; public BinaryTree() { root = null; } public BinaryTree(T rootData) { root = new BinaryNode<>(rootData); } public BinaryTree(T rootData, BinaryTree<T> leftTree, BinaryTree<T> rightTree) { privateSetTree(rootData, leftTree, rightTree); } private void privateSetTree(T rootData, BinaryTree<T> leftTree, BinaryTree<T> rightTree) { root = new BinaryNode<>(rootData); if (leftTree != null && !leftTree.isEmpty()) { root.setLeftChild(leftTree.root); }
  3. 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; }
  4. @Override public Iterator<T> getPreorderIterator() { return new PreorderIterator(); } @Override public Iterator<T> getInorderIterator() { return new InorderIterator(); } @Override public Iterator<T> getPostorderIterator() { return new PostorderIterator(); } private class PreorderIterator implements Iterator<T> { private StackInterface<BinaryNode<T>> nodeStack; public PreorderIterator() { nodeStack = new LinkedStack<>(); if (root != null) { nodeStack.push(root); } } public boolean hasNext() { return !nodeStack.isEmpty(); } public T next() { BinaryNode<T> nextNode; if (hasNext()) { nextNode = nodeStack.pop(); BinaryNode<T> leftChild = nextNode.getLeftChild(); BinaryNode<T> rightChild = nextNode.getRightChild(); if (rightChild != null) { nodeStack.push(rightChild); } if (leftChild != null) { nodeStack.push(leftChild); } } else {
  5. throw new NoSuchElementException(); } return nextNode.getData(); } public void remove() { throw new UnsupportedOperationException(); } } private class InorderIterator implements Iterator<T> { private StackInterface<BinaryNode<T>> nodeStack; private BinaryNode<T> currentNode; public InorderIterator() { nodeStack = new LinkedStack<>(); currentNode = root; } public boolean hasNext() { return !nodeStack.isEmpty() || (currentNode != null); } public T next() { BinaryNode<T> nextNode = null; 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(); } }
  6. private class PostorderIterator implements Iterator<T> { private StackInterface<BinaryNode<T>> nodeStack; private BinaryNode<T> currentNode; private BinaryNode<T> lastNodeVisited; public PostorderIterator() { nodeStack = new LinkedStack<>(); currentNode = root; lastNodeVisited = null; } public boolean hasNext() { return !nodeStack.isEmpty() || (currentNode != null); } public T next() { BinaryNode<T> leftChild, rightChild, nextNode = null; 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<T> parent = null; if (!nodeStack.isEmpty()) { parent = nodeStack.peek(); if (nextNode == parent.getLeftChild()) { currentNode = parent.getRightChild(); } else { currentNode = null; } } else { currentNode = null; } } else { throw new NoSuchElementException(); }
  7. return nextNode.getData(); } public void remove() { throw new UnsupportedOperationException(); }//end remove } @Override public Iterator<T> getLevelOrderIterator() { return null; } @Override public void setTree(T rootData) { } @Override public void setTree(T rootData, BinaryTreeInterface<T> leftTree, BinaryTreeInterface<T> rightTree) { } }//end BinaryTree
Anzeige