Priority Queues Interface Implementation Lab 11 Assignment

User Generated

WbaWbarf840

Programming

Description

Include word file containing all the required files with the code. Also include test files and their results. Follow the file attached to complete.

Unformatted Attachment Preview

//------------------------------------------------------------------------// RefSortedList.java by Dale/Joyce/Weems Chapter 6 // // Implements the ListInterface using a linked list. It is kept in increasing // order as defined by the compareTo method of the added elements. Only // Comparable elements may be added to a list. // // Null elements are not permitted on a list. // // One constructor is provided, one that creates an empty list. //---------------------------------------------------------------------------package ch06.lists; import support.LLNode; public class RefSortedList extends RefUnsortedList implements ListInterface { public RefSortedList() { super(); } public void add(T element) // Adds element to this list. { LLNode prevLoc; // trailing reference LLNode location; // traveling reference T listElement; // current list element being compared // Set up search for insertion point. location = list; prevLoc = null; // Find insertion point. while (location != null) { listElement = location.getInfo(); if (listElement.compareTo(element) < 0) // list element < add element { prevLoc = location; location = location.getLink(); } else break; } // Prepare node for insertion. LLNode newNode = new LLNode(element); // Insert node into list. if (prevLoc == null) { // Insert as first node. newNode.setLink(list); list = newNode; } else { // Insert elsewhere. newNode.setLink(location); prevLoc.setLink(newNode); } numElements++; } } //---------------------------------------------------------------------------// BinarySearchTree.java by Dale/Joyce/Weems Chapter 8 // // Defines all constructs for a reference-based BST //---------------------------------------------------------------------------package ch08.trees; import ch05.queues.*; import ch03.stacks.*; import support.BSTNode; public class BinarySearchTree implements BSTInterface { protected BSTNode root; // reference to the root of this BST boolean found; // used by remove // for traversals protected LinkedUnbndQueue inOrderQueue; // queue of info protected LinkedUnbndQueue preOrderQueue; // queue of info protected LinkedUnbndQueue postOrderQueue; // queue of info public BinarySearchTree() // Creates an empty BST object. { root = null; } public boolean isEmpty() // Returns true if this BST is empty; otherwise, returns false. { return (root == null); } private int recSize(BSTNode tree) // Returns the number of elements in tree. { if (tree == null) return 0; else return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1; } public int size() // Returns the number of elements in this BST. { return recSize(root); } public int size2() // Returns the number of elements in this BST. { int count = 0; if (root != null) { LinkedStack hold = new LinkedStack(); BSTNode currNode; hold.push(root); while (!hold.isEmpty()) { currNode = hold.top(); hold.pop(); count++; if (currNode.getLeft() != null) hold.push(currNode.getLeft()); if (currNode.getRight() != null) hold.push(currNode.getRight()); } } return count; } private boolean recContains(T element, BSTNode tree) // Returns true if tree contains an element e such that // e.compareTo(element) == 0; otherwise, returns false. { if (tree == null) return false; // element is not found else if (element.compareTo(tree.getInfo()) < 0) return recContains(element, tree.getLeft()); // Search left subtree else if (element.compareTo(tree.getInfo()) > 0) return recContains(element, tree.getRight()); // Search right subtree else return true; // element is found } public boolean contains (T element) // Returns true if this BST contains an element e such that // e.compareTo(element) == 0; otherwise, returns false. { return recContains(element, root); } private T recGet(T element, BSTNode tree) // Returns an element e from tree such that e.compareTo(element) == 0; // if no such element exists, returns null. { if (tree == null) return null; // element is not found else if (element.compareTo(tree.getInfo()) < 0) return recGet(element, tree.getLeft()); // get from left subtree else if (element.compareTo(tree.getInfo()) > 0) return recGet(element, tree.getRight()); // get from right subtree else return tree.getInfo(); // element is found } public T get(T element) // Returns an element e from this BST such that e.compareTo(element) == 0; // if no such element exists, returns null. { return recGet(element, root); } private BSTNode recAdd(T element, BSTNode tree) // Adds element to tree; tree retains its BST property. { if (tree == null) // Addition place found tree = new BSTNode(element); else if (element.compareTo(tree.getInfo())
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Here is the paper.

Table of contents
PriorityQueueInterface :.......................................................................................................................... 1
BinarySearchTree.java ............................................................................................................................. 1
RefSortedListAsPQ.java ........................................................................................................................... 7
Test files................................................................................................................................................... 8

0

PriorityQueueInterface :
package ch06.lists;
public interface PriorityQueueInterface {
public void enqueue(T item);
public T dequeue();
public boolean isEmpty();
public int size();
pu...


Anonymous
Just the thing I needed, saved me a lot of time.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags