Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this Java Data Structures Assignment. Thanks Driver Interface Methods import java.util.Vector; public interface DriverInterface { public Vector > getVectorOfTreeItems(); public BinarySearchTree

I need help with this Java Data Structures Assignment. Thanks

image text in transcribedimage text in transcribedimage text in transcribed

image text in transcribedimage text in transcribedimage text in transcribed

Driver Interface Methods

image text in transcribedimage text in transcribedimage text in transcribed

import java.util.Vector;

public interface DriverInterface {

public Vector> getVectorOfTreeItems(); public BinarySearchTree createAndPopulateBST(Vector> treeItems);

}

TreeItem Class

public class TreeItem , V> {

private K key; private V value; public TreeItem(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; }

public V getValue() { return value; }

public void setValue(V value) { this.value = value; } }

TreeNode Class

public class TreeNode, V> {

private TreeItem treeItem; private TreeNode leftChild; private TreeNode rightChild; private TreeNode parent; public TreeNode(TreeItem treeItem) { this.treeItem = treeItem; this.leftChild = null; this.rightChild = null; this.parent = null; }

public TreeNode getLeftChild() { return leftChild; }

public void setLeftChild(TreeNode leftChild) { this.leftChild = leftChild; }

public TreeNode getRightChild() { return rightChild; }

public void setRightChild(TreeNode rightChild) { this.rightChild = rightChild; }

public TreeNode getParent() { return parent; }

public void setParent(TreeNode parent) { this.parent = parent; }

public TreeItem getTreeItem() { return treeItem; } }

TreeException Class

public class TreeException extends RuntimeException {

public TreeException(String s) { super(s); } }

TreeIterator Class

import java.util.Vector;

public class TreeIterator,V> implements java.util.Iterator> {

private BinarySearchTree binarySearchTree; private TreeNode currentNode; private Vector> treeNodes;

public TreeIterator(BinarySearchTree binarySearchTree) { this.binarySearchTree = binarySearchTree; currentNode = null; // empty vector indicates no traversal type currently // selected or end of current traversal has been reached treeNodes = new Vector>(); } // end constructor

public int size() { return treeNodes.size(); }

public boolean hasNext() { return !treeNodes.isEmpty(); } // end hasNext

public TreeItem next() throws java.util.NoSuchElementException { currentNode = treeNodes.elementAt(0); treeNodes.remove(0); return currentNode.getTreeItem(); } // end next

public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } // end remove

public void setPreorder() { treeNodes.clear(); preorder(binarySearchTree.getRoot()); } // setPreOrder

private void preorder(TreeNode treeNode) { if (treeNode != null) { treeNodes.add(treeNode); preorder(treeNode.getLeftChild()); preorder(treeNode.getRightChild()); } // end if } // end preorder

public void setInorder() { treeNodes.clear(); inorder(binarySearchTree.getRoot()); } // end setInorder

private void inorder(TreeNode treeNode) { if (treeNode != null) { inorder(treeNode.getLeftChild()); treeNodes.add(treeNode); inorder(treeNode.getRightChild()); } // end if } // end inorder

public void setPostorder() { treeNodes.clear(); postorder(binarySearchTree.getRoot()); } // end setPostorder

private void postorder(TreeNode treeNode) { if (treeNode != null) { postorder(treeNode.getLeftChild()); postorder(treeNode.getRightChild()); treeNodes.add(treeNode); } // end if } // end postorder } // end TreeIterator

BinarySearchTree Class

public class BinarySearchTree , V> {

private TreeNode root;

public BinarySearchTree() { this.root = null; }

public BinarySearchTree(TreeNode root) { this.root = root; }

public TreeNode getRoot() { return root; }

public void setRoot(TreeNode root) { this.root = root; } public TreeItem getRootItem() throws TreeException { if (this.root == null) { throw new TreeException("TreeException: Tree Is Empty, No Root Item"); } else { return this.root.getTreeItem(); } } public boolean isEmpty() { return (root == null); } public void makeEmpty() { this.root = null; }

public TreeItem find(K key) { return findItem(this.root, key); }

private TreeItem findItem(TreeNode node, K key) { if (node == null) { return null; } else if (node.getTreeItem().getKey().compareTo(key) == 0) { return node.getTreeItem(); } else if (node.getTreeItem().getKey().compareTo(key) > 0) { return findItem(node.getLeftChild(), key); } else { return findItem(node.getRightChild(), key); } } public void insert(TreeItem treeItem) { this.root = insertItem(this.root, null, treeItem); }

private TreeNode insertItem(TreeNode node, TreeNode parent, TreeItem treeItem) { if (node == null) { node = new TreeNode (treeItem); node.setParent(parent); } else if (node.getTreeItem().getKey().compareTo(treeItem.getKey()) > 0) { node.setLeftChild(this.insertItem(node.getLeftChild(), node, treeItem)); } else { node.setRightChild(this.insertItem(node.getRightChild(), node, treeItem)); } return node; } public void delete(K key) throws TreeException { setRoot(deleteItem(getRoot(), key)); } // end delete

private TreeNode deleteItem(TreeNode node, K key) {

if (node == null) { throw new TreeException("TreeException: Item not found" ) ; } else { TreeItem treeItem = node.getTreeItem(); if (key.compareTo(treeItem.getKey()) == 0) { // item is in this node, which is the root of a subtree node = deleteNode(node) ; // delete the item } else if (key.compareTo(treeItem.getKey())

private TreeNode deleteNode( TreeNode node) { if ((node.getLeftChild() == null) && (node.getRightChild() == null) ) { // node is a leaf return null; } else if (node.getLeftChild() == null) { // no left child return node.getRightChild(); } else if (node.getRightChild() == null) { // no right child return node.getLeftChild(); } else { // there are two children: TreeNode successorNode; successorNode = findLeftmost(node.getRightChild()) ; node.setTreeItem(successorNode.getTreeItem()); node.setRightChild(deleteLeftmost(node.getRightChild())); return node; } // end if } // end deleteNode

private TreeNode findLeftmost( TreeNode node) { if ( node.getLeftChild() == null) { return node; } else { return findLeftmost(node.getLeftChild()) ; } // end if } // end findLeftmost

private TreeNode deleteLeftmost( TreeNode node) { if (node.getLeftChild() == null) { return node.getRightChild(); } else { node.setLeftChild(deleteLeftmost(node.getLeftChild())) ; return node; } // end if } // end deleteLeftmost }

Details 1. Treeltem Class You will be using the TreeItem class that we implemented in class. The class may be downloaded from Treeltemiava 2. TreeNode Class You will be using the Node class that we implemented in class. The class may be downloaded from TreeNode.iava Tree 3. TreeException Class You will be using the TreeException class that we implemented in class. The class may be downloaded from TreeException.iava 4. Tre elterator Class You will be using the TreeIterator class that we implemented in class. The class may be downloaded from Treelteratoriava Details 1. Treeltem Class You will be using the TreeItem class that we implemented in class. The class may be downloaded from Treeltemiava 2. TreeNode Class You will be using the Node class that we implemented in class. The class may be downloaded from TreeNode.iava Tree 3. TreeException Class You will be using the TreeException class that we implemented in class. The class may be downloaded from TreeException.iava 4. Tre elterator Class You will be using the TreeIterator class that we implemented in class. The class may be downloaded from Treelteratoriava

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions

Question

What tools might be helpful?

Answered: 1 week ago