Given the three classes (CallStack, LookUpBST, and Node) fill in what is needed in the SimpleCompiler Class, and also the rest of what's needed in
Given the three classes (CallStack, LookUpBST, and Node) fill in what is needed in the SimpleCompiler Class, and also the rest of what's needed in the LookUpBST class
class LookUpBSTextends Comparable, V> {
//Root of tree
public Node root;
publicint size;
publicint size() { return size; }
publicstaticclass Node {
K key; // sorted by key
V val; // associated data
Node left, right; // left and right subtrees
public Node(K key, V val) {
this.key = key;
this.val = val;
}
public Node(K key, V val, Node l, Node r) {
this.key = key;
this.val = val;
this.left = l;
this.right = r;
}
}
// You can NOT add any instance/static variables in this class.
// You can add methods if needed but they must be PRIVATE.
import java.util. Iterator; class CallStack implements Iterable { // You'll want some instance variables here private Node top; private int size; 80 10 public CallStack() { //setup what you need top = null; size = 0; //create empty stack 11 12 13 14 150 public void push(T item) { //push an item onto the stack 1 / you may assume the item is not null 7/0(1) 17 18 //create a node with item as value Node node = new Node(item); //set current top as next node node.setNext(top); I/if top is not empty, set node as previous top if(top != null) { top.setPrev(node); //set node as new top and update the size top = node; size++; 340 35 36 public T pop() { //pop an item off the stack //if there are no items on the stack, return null 1/0(1) V/if the top null is empty if(top == null) { return null; 37 39 40 //gets data of top node, advance the top node, adjusts links //returns the removed value 43 45 46 47 T data = top.getValue(); top = top.getNext(); if(top != null) { top.setPrev(null); 49 //update size before returning removed value size--; //replace this dummy return statement return data; 53 55 56 57 public T peek() { //return the top of the stack (but don't remove it) //if there are no items on the stack, return null 1/0(1) 58 59 60 61 62 //if the top null is empty if(top == null) { return null; 63 64 //return data to top node T data = top.getValue(); 69 70 71 //replace this dummy return statement return data; 73 740 76 public String toString() { //Create a string of the stack where each item //is separated by a space. The top of the stack // should be shown to the right and the bottom of 1/the stack on the left. 77 79 80 //Hint: Reuse the provided code from another class //instead of writing this yourself! 81 83 1/0(n) 85 87 String data = ""; //calling listToStringBackwards method of Node class to get a string reverse order if(top != null) { data = Node.listToStringBackward(top); 89 //replace this dummy return statement return data; 93 940 95 96 97 98 public void clear() { //remove everything from the stack 1/0(1) top = null; size = 0; 99 100 101 102 103 public int size() { 1/return the number of items on the stack 1/0(1) 104 //replace this dummy return statement return size; 105 106 107 108 109 110 public boolean isEmpty() { //return whether or not the stack is empty 7/0(1) 111 112 113 114 115 116 1170 118 //replace this dummy return statement return size == 0; 119 @SuppressWarnings ("unchecked") public Object[] toArray() { //Return an array representation of the stack. //The top of the stack should be element o //in the array. 1/0(n) 120 121 122 123 124 125 126 127 128 129 Object[]array = new Object[size]; int index = 0; //loop through each node, add data to the array, then return it for (Nodenode = top; node != null; node = node.getNext() { array[index] = node.getValue(); index++; 130 131 //replace this dummy return statement 132 133 134 135 136 137 138 //return top and gets updated return array; 139 public Iterator iterator() { //Return an iterator that traverses from //the top of the stack to the bottom of //the stack. 140 141 142 143 144 145 146 147 148 149 150 151 152 153 0 154 //create iterator object return new Iterator() { //take reference to top Node node = top; //The iterator's hasNext() and next() methods //must both be 0(1) public boolean hasNext() { //a value left as long as node not null return node != null; 155 156 public T next() { /ext() should throw a NullPointerException //throws th exception if there are no more elements if(!hasNext()) { throw new NullPointerException(); 157 158 159 160 161 162 //take data at node, advance node and return data T data = node.getValue(); node = node.getNext(); return data; 163 164 165 public void remove() { 166 167 168 169 170 171 172 //if you try to use next when there are no //more items 173 174 175 //replace this dummy return statement 177 178 179 180 181 public static void main(String[] args) { CallStack s1 = new CallStack(); s1.push("a"); s1.push("b"); 182 183 184 185 CallStack s2 = new CallStack(); s2.push(1); s2.push(2); s2.push(3); 186 if(s1.toString().equals("a b") && s1.toArray ( ) [O].equals("b") && $1. toArray ( ) [1] .equals("a") && s1.toArray().length == 2) { System.out.println("Yay 1"); if(s1.peek().equals("b") && s2.peek().equals(3) && s1.size() == 2 && s2.size() == 3) { System.out.println("Yay 2"); 187 188 189 190 191 192 193 194 195 196 197 198 199 if(s1.pop().equals("b") && $2.pop().equals(3) && $1.size() == 1 && s2.size() == 2) { System.out.println("Yay 3"); 200 if(s1.toString().equals("a") && s1.peek().equals("a") && s2.peek().equals(2) && s1.pop().equals("a") && s2.pop().equals(2) && s1.size() == 0 && s2.size() == 1) { System.out.println("Yay 4"); 201 202 203 if(s1.toString().equals("") && $1.peek() == null && s2.peek().equals(1) && $1.pop() == null && s2.pop().equals(1) && s1.size() == 0 && s2.size() == 0) I System.out.println("Yay 5"); 204 205 206 207 208 209 210 s2.push(10); s2.push(20); s2.push(30); if(51. isEmpty() && $1. toArray().length == 0 && !s2.isEmpty()) { s2.clear(); if(s2.isEmpty()) { System.out.println("Yay 6"); 211 212 213 214 215 216 217 218 219 CallStack s3 = new CallStack(); s3.push(3); s3.push(2); s3.push(1); 220 221 222 223 224 int i = 1; for(Integer item: 53) { if(i == item) System.out.println("Yay " + (6+i)); else System.out.println(item); 225 226 227 228 229 i++; } ] 230 51 //Do not edit this class, just add JavaDocs //You may edit the main method for testing things if you want //but do not change the actual class structure. 3 5 class Node 1 private T value; private Node next; private Node prev; 10 public Node (T value) { this.value = value; DRE } 14 public T getValue() { return value; public void setValue(T value) { this.value = value; public Node getNext() { return this.next; NNNNNNNN public void setNext (Node next) { this.next = next; public Node getPrev() { return this.prev; public void setPrev (Node prev) { this.prev = prev; public static String listToString(Node> head) { StringBuilder ret = new StringBuilder(); Node> current = head; while(current != null) { ret.append(current.value); ret.append(" "); current = current.getNext(); return ret.toString().trim(); public static String listToStringBackward (Node> head) { Node> current = head; while(current.getNext() != null) { current = current.getNext(); StringBuilder ret = new StringBuilder(); while(current != null) { ret.append(current.value); ret.append(" "); current = current.getPrev(); return ret.toString().trim(); public static void main(String[] args) { //main method for testing, edit as much as you want //make nodes Node n1 = new Node ("A"); Node n2 = new Node ("B"); Node n3 = new Node ("C"); //connect forward references n1.setNext(n2); n2.setNext(n3); //connect backward references n3.setPrev(n2); n2.setPrev(nl); 1/print forward and backward System.out.println (Node.listToString(nl)); System.out.println(Node. listToStringBackward(nl)); import java.util. Iterator; class CallStack implements Iterable { // You'll want some instance variables here private Node top; private int size; 80 10 public CallStack() { //setup what you need top = null; size = 0; //create empty stack 11 12 13 14 150 public void push(T item) { //push an item onto the stack 1 / you may assume the item is not null 7/0(1) 17 18 //create a node with item as value Node node = new Node(item); //set current top as next node node.setNext(top); I/if top is not empty, set node as previous top if(top != null) { top.setPrev(node); //set node as new top and update the size top = node; size++; 340 35 36 public T pop() { //pop an item off the stack //if there are no items on the stack, return null 1/0(1) V/if the top null is empty if(top == null) { return null; 37 39 40 //gets data of top node, advance the top node, adjusts links //returns the removed value 43 45 46 47 T data = top.getValue(); top = top.getNext(); if(top != null) { top.setPrev(null); 49 //update size before returning removed value size--; //replace this dummy return statement return data; 53 55 56 57 public T peek() { //return the top of the stack (but don't remove it) //if there are no items on the stack, return null 1/0(1) 58 59 60 61 62 //if the top null is empty if(top == null) { return null; 63 64 //return data to top node T data = top.getValue(); 69 70 71 //replace this dummy return statement return data; 73 740 76 public String toString() { //Create a string of the stack where each item //is separated by a space. The top of the stack // should be shown to the right and the bottom of 1/the stack on the left. 77 79 80 //Hint: Reuse the provided code from another class //instead of writing this yourself! 81 83 1/0(n) 85 87 String data = ""; //calling listToStringBackwards method of Node class to get a string reverse order if(top != null) { data = Node.listToStringBackward(top); 89 //replace this dummy return statement return data; 93 940 95 96 97 98 public void clear() { //remove everything from the stack 1/0(1) top = null; size = 0; 99 100 101 102 103 public int size() { 1/return the number of items on the stack 1/0(1) 104 //replace this dummy return statement return size; 105 106 107 108 109 110 public boolean isEmpty() { //return whether or not the stack is empty 7/0(1) 111 112 113 114 115 116 1170 118 //replace this dummy return statement return size == 0; 119 @SuppressWarnings ("unchecked") public Object[] toArray() { //Return an array representation of the stack. //The top of the stack should be element o //in the array. 1/0(n) 120 121 122 123 124 125 126 127 128 129 Object[]array = new Object[size]; int index = 0; //loop through each node, add data to the array, then return it for (Nodenode = top; node != null; node = node.getNext() { array[index] = node.getValue(); index++; 130 131 //replace this dummy return statement 132 133 134 135 136 137 138 //return top and gets updated return array; 139 public Iterator iterator() { //Return an iterator that traverses from //the top of the stack to the bottom of //the stack. 140 141 142 143 144 145 146 147 148 149 150 151 152 153 0 154 //create iterator object return new Iterator() { //take reference to top Node node = top; //The iterator's hasNext() and next() methods //must both be 0(1) public boolean hasNext() { //a value left as long as node not null return node != null; 155 156 public T next() { /ext() should throw a NullPointerException //throws th exception if there are no more elements if(!hasNext()) { throw new NullPointerException(); 157 158 159 160 161 162 //take data at node, advance node and return data T data = node.getValue(); node = node.getNext(); return data; 163 164 165 public void remove() { 166 167 168 169 170 171 172 //if you try to use next when there are no //more items 173 174 175 //replace this dummy return statement 177 178 179 180 181 public static void main(String[] args) { CallStack s1 = new CallStack(); s1.push("a"); s1.push("b"); 182 183 184 185 CallStack s2 = new CallStack(); s2.push(1); s2.push(2); s2.push(3); 186 if(s1.toString().equals("a b") && s1.toArray ( ) [O].equals("b") && $1. toArray ( ) [1] .equals("a") && s1.toArray().length == 2) { System.out.println("Yay 1"); if(s1.peek().equals("b") && s2.peek().equals(3) && s1.size() == 2 && s2.size() == 3) { System.out.println("Yay 2"); 187 188 189 190 191 192 193 194 195 196 197 198 199 if(s1.pop().equals("b") && $2.pop().equals(3) && $1.size() == 1 && s2.size() == 2) { System.out.println("Yay 3"); 200 if(s1.toString().equals("a") && s1.peek().equals("a") && s2.peek().equals(2) && s1.pop().equals("a") && s2.pop().equals(2) && s1.size() == 0 && s2.size() == 1) { System.out.println("Yay 4"); 201 202 203 if(s1.toString().equals("") && $1.peek() == null && s2.peek().equals(1) && $1.pop() == null && s2.pop().equals(1) && s1.size() == 0 && s2.size() == 0) I System.out.println("Yay 5"); 204 205 206 207 208 209 210 s2.push(10); s2.push(20); s2.push(30); if(51. isEmpty() && $1. toArray().length == 0 && !s2.isEmpty()) { s2.clear(); if(s2.isEmpty()) { System.out.println("Yay 6"); 211 212 213 214 215 216 217 218 219 CallStack s3 = new CallStack(); s3.push(3); s3.push(2); s3.push(1); 220 221 222 223 224 int i = 1; for(Integer item: 53) { if(i == item) System.out.println("Yay " + (6+i)); else System.out.println(item); 225 226 227 228 229 i++; } ] 230 51 //Do not edit this class, just add JavaDocs //You may edit the main method for testing things if you want //but do not change the actual class structure. 3 5 class Node 1 private T value; private Node next; private Node prev; 10 public Node (T value) { this.value = value; DRE } 14 public T getValue() { return value; public void setValue(T value) { this.value = value; public Node getNext() { return this.next; NNNNNNNN public void setNext (Node next) { this.next = next; public Node getPrev() { return this.prev; public void setPrev (Node prev) { this.prev = prev; public static String listToString(Node> head) { StringBuilder ret = new StringBuilder(); Node> current = head; while(current != null) { ret.append(current.value); ret.append(" "); current = current.getNext(); return ret.toString().trim(); public static String listToStringBackward (Node> head) { Node> current = head; while(current.getNext() != null) { current = current.getNext(); StringBuilder ret = new StringBuilder(); while(current != null) { ret.append(current.value); ret.append(" "); current = current.getPrev(); return ret.toString().trim(); public static void main(String[] args) { //main method for testing, edit as much as you want //make nodes Node n1 = new Node ("A"); Node n2 = new Node ("B"); Node n3 = new Node ("C"); //connect forward references n1.setNext(n2); n2.setNext(n3); //connect backward references n3.setPrev(n2); n2.setPrev(nl); 1/print forward and backward System.out.println (Node.listToString(nl)); System.out.println(Node. listToStringBackward(nl))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance