Question: These classes are all we all, and they have missing definitions and problems when you put them together. Please do not just give me those

These classes are all we all, and they have missing definitions and problems when you put them together. Please do not just give me those two methods and also check if all the classes work well when they in one program! There do have problems and I have no idea how to correct them!

The goal of the homework is to implement a priority queue as a BST adapter. This requires: 1. The creation of a method: removeMin for the BST class 2. The modification of the usual insert method in the BST class to allow for insertion of duplicate data. The following program is missing its insert and removeMin methods. The main purpose of the project is to supply these methods. You should also alter the class E00000000 to match your ID number and make it print your name and ID number as the first line of output. The main program is designed to operate a PQsort function on random integer data. ********************************************************************/ import java.util.ArrayList; import java.util.Iterator; import java.util.Random; import class22.BST; import class13.BNode; import class17.PriorityQueue; class BSTPQ> extends BST implements PriorityQueue { public void insert(K x) throws Exception { // TODO add this code return; } public K removeMin() throws Exception { // TODO add this code return null; } } // ---- main program to test priority queue methods class E00000000 { public static Iterator PQsort(Iterator x) throws Exception { PriorityQueue q = new BSTPQ(); while (x.hasNext()) q.insert(x.next()); ArrayList temp = new ArrayList<>(); try { while (true) temp.add(q.removeMin()); } catch (Exception e) {} return temp.iterator(); } public static void main(String args[]) throws Exception { Random r = new Random(); ArrayList v = new ArrayList<>(); for (int i = 0; i < 20; i++) v.add(r.nextInt(30)); System.out.print("Unsorted: "); for (Integer x:v) System.out.print("" + x + " "); System.out.print(" Sorted: "); Iterator sorted = PQsort(v.iterator()); while (sorted.hasNext()) System.out.print("" + sorted.next() + " "); System.out.println(); } } Below are the package that has been imported above, dont change them just give me the code required for insert and removemin where it says TODO and dnt change anything please ------------------------------------------------------- package class22; import class13.BNode; import class13.BinaryTree; public class BST> extends BinaryTree implements BSTree { public BNode findNode(K x) { if (root() == null) return null; return findNode((BNode) root(), x); } public BNode findNode(BNode n, K x) { BNode nn = null; if (n.getData().compareTo(x) == 0) return n; if (n.getData().compareTo(x) > 0) nn = n.getLeft(); else nn = n.getRight(); if (nn == null) return n; return findNode(nn, x); } public void remove(K x) { BNode n = findNode(x); if (n == null || !n.getData().equals(x)) return; // x is not present removeNode(n); } public void insert(K x) throws Exception { BNode n = findNode(x); if (n == null) addRoot(x); else if ((n.getData()).compareTo(x) > 0) addLeft(n, x); else if ((n.getData()).compareTo(x) < 0) addRight(n, x); } public boolean find(K x) { BNode n = findNode(x); if (n == null || !n.getData().equals(x)) return false; return true; } } ----------------------------------------------------------- package class17; public interface PriorityQueue> { public void insert(K x) throws Exception; public K removeMin() throws Exception; } --------------------------------- package class13; import java.util.ArrayList; import java.util.Iterator; import class12.Node; public class BNode extends Node { BNode left, right; public BNode(T d, BNode p, BNode l, BNode r) { setData(d); setParent(p); left = l; right = r; } public void setLeft(BNode n) { left = n; } public void setRight(BNode n) { right = n; } public BNode getLeft() { return left; } public BNode getRight() { return right; } public Iterator> children() { ArrayList> v = new ArrayList<>(); if (left != null) v.add(left); if (right != null) v.add(right); return v.iterator(); } public void removeChild(BNode n) { if (getLeft() == n) setLeft(null); else setRight(null); } } .................................................................................................................................... package class12; import java.util.Iterator; public abstract class Node { protected Node parent; protected T data; public abstract Iterator> children(); public void setParent(Node n) { parent = n; } public void setData(T t) { data = t; } public Node getParent() { return parent; } public T getData() { return data; } } ----------------------------------------- package class13; import java.util.ArrayList; import class12.Tree; public class BinaryTree extends Tree { public BinaryTree() { super(); } public void addRoot(T x) throws Exception { if (root != null) throw new Exception("The tree is not empty"); root = new BNode(x, null, null, null); size++; } public void addLeft(BNode n, T x) throws Exception { if (n.getLeft() != null) throw new Exception("Already used"); n.setLeft(new BNode(x, n, null, null)); size++; } public void addRight(BNode n, T x) throws Exception { if (n.getRight() != null) throw new Exception("Already used"); n.setRight(new BNode(x, n, null, null)); size++; } // returns the parent of the removed node public BNode removeNode(BNode n) { if (isLeaf(n)) { // base case BNode p = (BNode) parent(n); if (p == null) root = null; else p.removeChild(n); size--; return p; } if (n.getLeft() != null) { BNode m = rightmostLeftDescendant(n); n.setData(m.getData()); return removeNode(m); // recursively remove rightmost left descendant } // otherwise n does have a right child BNode m = leftmostRightDescendant(n); n.setData(m.getData()); return removeNode(m); } public BNode leftmostRightDescendant(BNode n) { BNode m = n.getRight(); while (m.getLeft() != null) m = m.getLeft(); return m; } public BNode rightmostLeftDescendant(BNode n) { BNode m = n.getLeft(); while (m.getRight() != null) m = m.getRight(); return m; } public ArrayList> inOrder() { ArrayList> answer = new ArrayList>(); inOrder((BNode) root(), answer); return answer; } public void inOrder(BNode n, ArrayList> v) { if (n == null) return; inOrder(n.getLeft(), v); v.add(n); inOrder(n.getRight(), v); } public ArrayList> flatOrder() { return inOrder(); } } ------------------------------- package class22; public interface BSTree> { public void remove(K x); public void insert(K x) throws Exception; public boolean find(K x); } ------------------------------------------------------------------- package class12; import java.util.ArrayList; import java.util.Iterator; public class Tree { protected Node root; protected int size; public Tree() { root = null; size = 0; } public Node root() { return root; } public Node parent(Node v) { return v.getParent(); } public Iterator children(Node v) { return v.children(); } public boolean isRoot(Node v) { return v == root; } public boolean isInternal(Node v) { return children(v).hasNext(); } public boolean isLeaf(Node v) { return !isInternal(v); } public int size() { return size; } public boolean empty() { return size == 0; } public void replace(Node v, T t) { v.setData(t); } public int depth(Node v) { if (isRoot(v)) return 0; return 1 + depth(parent(v)); } public int height(Node v) { if (isLeaf(v)) return 0; int maxChild = 0; Iterator c = children(v); while (c.hasNext()) { int hc = height((Node) c.next()); if (hc > maxChild) maxChild = hc; } return maxChild + 1; } public int height() { if (root == null) return -1; return height(root); } public ArrayList> preOrder() { ArrayList> answer = new ArrayList<>(); preOrder(root(), answer); return answer; } public void preOrder(Node n, ArrayList> v) { if (n == null) return; v.add(n); Iterator x = children(n); while (x.hasNext()) { Node m = x.next(); preOrder(m, v); } } public ArrayList> postOrder() { ArrayList> answer = new ArrayList>(); postOrder(root(), answer); return answer; } public void postOrder(Node n, ArrayList> v) { if (n == null) return; Iterator x = children(n); while (x.hasNext()) { Node m = x.next(); postOrder(m, v); } v.add(n); } public ArrayList> levelOrder() { ArrayList> waiting = new ArrayList<>(); waiting.add(null); if (root() == null) return waiting; waiting.add(root()); int done = 0; while (done < waiting.size()) { Node oldNode = waiting.get(done++); if (oldNode == null) { if (done < waiting.size()) waiting.add(null); continue; } Iterator c = children(oldNode); while (c.hasNext()) waiting.add(c.next()); } return waiting; } public ArrayList flatOrder() { return preOrder(); } public String toString() { return treePrint(null); } public String treePrint(Node cursor) { String answer = " "; Iterator> lev = levelOrder().iterator(); Iterator flat = flatOrder().iterator(); lev.next(); // skip first new line while (lev.hasNext()) { Node o = lev.next(); if (o == null) { answer += " "; flat = flatOrder().iterator(); } else while (true) { boolean atCursor = false; Node n = flat.next(); if (n == cursor) atCursor = true; String s = n.getData().toString(); if (atCursor) s = "* " + s + " *"; if (n == o) { answer += s; break; } else for (int i = 0; i < s.length(); i++) answer += ' '; } } return answer; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!