PLEASE INCLUDE A TESTER PROGRAM FOR THE OUTPUT. Please read the question careful
ID: 3860929 • Letter: P
Question
PLEASE INCLUDE A TESTER PROGRAM FOR THE OUTPUT. Please read the question carefully and using the correct format below, Thank you.
public class Node {
private String data;
private Node next;
public Node(String d, Node n ) {
data = d;
next = n;
}
// The usual get/set methods, plus toString()
public void setData(String d) { data = d; }
public void setNext(Node n) { next = n; }
public String getData() { return data; }
public Node getNext() { return next; }
public String toString() { return data + " --> "; }
}
public class LinkedList {
private Node front;
//private int count;
//private Node end;
public LinkedList(Node f ) {
front = f;
}
public LinkedList() {
front = null;
}
public void addToFront(String d) {
Node n = new Node(d, front);
front = n;
}
public boolean isEmpty() {
if(front == null)
return true;
else
return false;
}
public void clear() {
front = null;
}
public String getFrontData() {
return front.getData();
}
public Node getFrontNode() {
return front;
}
public String toString() {
String ts = "[";
Node cur = front;
while(cur != null) {
ts += cur;
cur = cur.getNext();
}
return ts + "]";
}
public int size() {
int count = 0;
Node cur = front;
while(cur != null) {
count++;
cur = cur.getNext();
}
return count;
}
public void removeFront() {
if(!isEmpty())
front = front.getNext();
// else
// System.out.println(ìNo front to remove!î);
}
public void addToEnd(String d) {
Node n = new Node(d, null);
if(isEmpty())
front = n;
else {
Node cur = front;
while(cur.getNext() != null)
cur = cur.getNext();
cur.setNext(n);
}
}
public void removeLast() {
if(!isEmpty()) {
if(front.getNext() == null)
front = null;
else {
Node cur = front;
while(cur.getNext().getNext() != null)
cur = cur.getNext();
cur.setNext(null);
}
//} else { System.out.println(ìNo end to remove!î);
}
}
public int contains(String d) {
Node cur = front;
boolean found = false;
int index = -1;
while(cur != null && !found) {
index++;
if(cur.getData().equals(d))
found = true;
cur = cur.getNext();
}
if(!found)
index = -1;
return index;
}
public void add(int index, String d) {
if(index >= 0 && index <= size() ) {
if(index == 0)
addToFront(d);
else {
Node cur = front;
for(int i=0; i<index-1; i++)
cur = cur.getNext();
Node n = new Node(d, cur.getNext());
cur.setNext(n);
}
}
//else { System.out.println(ìIndex out of bounds!î); }
}
public void remove(int index) {
if(index >= 0 && index <= size() ) {
if(index == 0)
removeFront();
else if(index == size()-1)
removeLast();
else {
Node cur = front;
for(int i=0; i<index-1; i++)
cur = cur.getNext();
cur.setNext(cur.getNext().getNext());
}
}
//else { System.out.println(ìIndex out of bounds!î); }
}
public Node getNode(int index) {
Node cur = null;
if(index >= 0 && index <= size() ) {
if(index == 0)
cur = front;
else {
cur = front;
for(int i=0; i<index; i++)
cur = cur.getNext();
}
} // else { System.out.println(ìIndex out of bounds!î); }
return cur;
}
public void addAll(LinkedList other) {
Node cur = other.getFrontNode();
while(cur != null) {
addToEnd(cur.getData());
cur = cur.getNext();
}
}
public static LinkedList merge(LinkedList first, LinkedList second) {
LinkedList result = new LinkedList();
Node cur = first.getFrontNode();
while(cur != null) {
result.addToEnd(cur.getData());
cur = cur.getNext();
}
cur = second.getFrontNode();
while(cur != null) {
result.addToEnd(cur.getData());
cur = cur.getNext();
}
return result;
}
public LinkedList subList(int start, int end) {
LinkedList result = new LinkedList();
if(!(start >= end || start < 0 || start > size() || end >= size() )) {
Node cur = getFrontNode();
for(int i=0; i<start; i++) {
cur = cur.getNext();
}
for(int i=start; i<=end; i++) {
addToEnd(cur.getData());
cur = cur.getNext();
}
}
return result;
}
public static LinkedList union(LinkedList first, LinkedList second) {
LinkedList result = new LinkedList();
Node cur = first.getFrontNode();
while(cur != null) {
result.addToEnd(cur.getData());
cur = cur.getNext();
}
cur = second.getFrontNode();
while(cur != null) {
if(result.contains(cur.getData()) == -1)
result.addToEnd(cur.getData());
cur = cur.getNext();
}
return result;
}
public static LinkedList intersection(LinkedList first, LinkedList second) {
LinkedList result = new LinkedList();
Node cur = first.getFrontNode();
while(cur != null) {
if(second.contains(cur.getData()) != -1)
result.addToEnd(cur.getData());
cur = cur.getNext();
}
return result;
}
}
Explanation / Answer
/*
* LinkedTree.java
*
* Software engineering E-119
*
* Alterations and increments by:
* name:
* username:
*/
import java.util.*;
/**
* LinkedTree - a class that speaks to a paired tree containing information
* things with number keys. On the off chance that the hubs are embedded utilizing the
* embed strategy, the outcome will be a twofold pursuit tree.
*/
open class LinkedTree {
/An inward class for the hubs in the tree
private class Hub {
private int key;/the key field
private Question information;/whatever is left of the information thing
private Hub left;/reference to one side tyke/subtree
private Hub right;/reference to the correct kid/subtree
private Hub parent;/reference to the parent
private Node(int key, Question information, Hub left, Hub right, Hub parent){
this.key = key;
this.data = information;
this.left = left;
this.right = right;
this.parent = parent;
}
private Node(int key, Protest information) {
this(key, information, invalid, invalid, invalid);
}
}
/the foundation of the tree in general
private Hub root;
open LinkedTree() {
root = invalid;
}
/**
* Prints the keys of the tree in the request given by a preorder traversal.
* Summons the recursive preorderPrintTree technique to take every necessary step.
*/
open void preorderPrint() {
in the event that (root != invalid)
preorderPrintTree(root);
}
/*
* Recursively plays out a preorder traversal of the tree/subtree
* whose root is determined, printing the keys of the went by hubs.
* Note that the parameter is *not* fundamentally the foundation of the
* whole tree.
*/
private static void preorderPrintTree(Node root) {
System.out.print(root.key + " ");
on the off chance that (root.left != invalid)
preorderPrintTree(root.left);
on the off chance that (root.right != invalid)
preorderPrintTree(root.right);
}
/**
* Prints the keys of the tree in the request given by a postorder traversal.
* Summons the recursive postorderPrintTree strategy to take the necessary steps.
*/
open void postorderPrint() {
on the off chance that (root != invalid)
postorderPrintTree(root);
}
/*
* Recursively plays out a postorder traversal of the tree/subtree
* whose root is determined, printing the keys of the went by hubs.
* Note that the parameter is *not* fundamentally the foundation of the
* whole tree.
*/
private static void postorderPrintTree(Node root) {
on the off chance that (root.left != invalid)
postorderPrintTree(root.left);
on the off chance that (root.right != invalid)
postorderPrintTree(root.right);
System.out.print(root.key + " ");
}
/**
* Prints the keys of the tree in the request given by an inorder traversal.
* Summons the recursive inorderPrintTree strategy to take every necessary step.
*/
open void inorderPrint() {
on the off chance that (root != invalid)
inorderPrintTree(root);
}
/*
* Recursively plays out an inorder traversal of the tree/subtree
* whose root is indicated, printing the keys of the went by hubs.
* Note that the parameter is *not* essentially the foundation of the
* whole tree.
*/
private static void inorderPrintTree(Node root) {
on the off chance that (root.left != invalid)
inorderPrintTree(root.left);
System.out.print(root.key + " ");
on the off chance that (root.right != invalid)
inorderPrintTree(root.right);
}
/*
* Inward class for incidentally partner a hub's profundity
* with the hub, so levelOrderPrint can print the levels
* of the tree on discrete lines.
*/
private class NodePlusDepth {
private hub;
private int profundity;
private NodePlusDepth(Node hub, int profundity) {
this.node = hub;
this.depth = profundity;
}
}
/**
* Prints the keys of the tree in the request given by a
* level-arrange traversal.
*/
open void levelOrderPrint() {
LLQueue<NodePlusDepth> q = new LLQueue<NodePlusDepth>();
/Embed the root into the line if the root is not invalid.
in the event that (root != invalid)
q.insert(new NodePlusDepth(root, 0));
/We proceed until the point that the line is unfilled. At each progression,
/we expel a component from the line, print its esteem,
/and embed its youngsters (assuming any) into the line.
/We additionally monitor the present level, and include a newline
/at whatever point we progress to another level.
int level = 0;
while (!q.isEmpty()) {
NodePlusDepth thing = q.remove();
on the off chance that (item.depth > level) {
System.out.println();
level++;
}
System.out.print(item.node.key + " ");
on the off chance that (item.node.left != invalid)
q.insert(new NodePlusDepth(item.node.left, item.depth + 1));
on the off chance that (item.node.right != invalid)
q.insert(new NodePlusDepth(item.node.right, item.depth + 1));
}
}
/**
* Scans for the predefined enter in the tree.
* Summons the recursive searchTree strategy to play out the genuine inquiry.
*/
open Question search(int key) {
Hub n = searchTree(root, key);
return (n == invalid ? invalid : n.data);
}
/*
* Recursively scans for the predetermined key in the tree/subtree
* whose root is determined. Note that the parameter is *not*
* fundamentally the base of the whole tree.
*/
private static Hub searchTree(Node root, int key) {
on the off chance that (root == invalid)
return invalid;
else if (key == root.key)
return root;
else if (key < root.key)
return searchTree(root.left, key);
else
return searchTree(root.right, key);
}
/**
* Additions the predetermined (key, information) match in the tree so that the
* tree remains a double hunt tree.
*/
open void insert(int key, Protest information) {
/Discover the parent of the new hub.
Hub parent = invalid;
Hub trav = root;
while (trav != invalid) {
parent = trav;
in the event that (key < trav.key)
trav = trav.left;
else
trav = trav.right;
}
/Embed the new hub.
Hub newNode = new Node(key, information);
on the off chance that (parent == invalid)/the tree was void
root = newNode;
else if (key < parent.key)
parent.left = newNode;
else
parent.right = newNode;
/refresh the parent on embed!
newNode.parent = parent;
}
/**
* Erases the hub containing the (key, information) match with the
* indicated key from the tree and restore the related information thing.
*/
open Question delete(int key) {
/Observe the hub to be erased and its parent.
Hub parent = invalid;
Hub trav = root;
while (trav != invalid && trav.key != key) {
parent = trav;
in the event that (key < trav.key)
trav = trav.left;
else
trav = trav.right;
}
/Erase the hub (assuming any) and restore the expelled information thing.
in the event that (trav == invalid)/no such key
return invalid;
else {
Question removedData = trav.data;
deleteNode(trav, parent);
return removedData;
}
}
/**
* Erases the hub determined by the parameter toDelete. parent
* determines the parent of the hub to be erased.
*/
private void deleteNode(Node toDelete, Hub parent) {
in the event that (toDelete.left != invalid && toDelete.right != invalid) {
/Case 3: toDelete has two kids.
/Discover a trade for the thing we're erasing - and in addition
/the substitution's parent.
/We utilize the littlest thing in toDelete's privilege subtree as
/the substitution.
Hub replaceParent = toDelete;
Hub supplant = toDelete.right;
while (replace.left != invalid) {
replaceParent = supplant;
supplant = replace.left;
}
/Supplant toDelete's critical and information with those of the
/substitution thing.
toDelete.key = replace.key;
toDelete.data = replace.data;
/Recursively erase the substitution thing's old hub.
/It has at most one youngster, so we don't need to
/stress over endless recursion.
deleteNode(replace, replaceParent);
} else {
/Cases 1 and 2: toDelete has 0 or 1 youngster
Hub toDeleteChild;
on the off chance that (toDelete.left != invalid)
toDeleteChild = toDelete.left;
else
toDeleteChild = toDelete.right;/invalid on the off chance that it has no kids
on the off chance that (toDelete == root)
{
root = toDeleteChild;
if(root != invalid)
root.parent = invalid;
}
else if (toDelete.key < parent.key)
{
parent.left = toDeleteChild;
if(parent.left != invalid)
parent.left.parent = parent;
}
else
{
parent.right = toDeleteChild;
if(parent.right != nul
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.