Java Helper Classes and Functions
import java.util.*;
// Binary Tree Node class
public class BinNode<T> {
private T value;
private BinNode<T> left;
private BinNode<T> right;
public BinNode(T value) {
this.value = value;
this.left = null;
this.right = null;
}
public BinNode(BinNode<T> left, T value, BinNode<T> right) {
this.value = value;
this.left = left;
this.right = right;
}
public T getValue() {
return value;
}
public BinNode<T> getLeft() {
return left;
}
public BinNode<T> getRight() {
return right;
}
public void setValue(T value) {
this.value = value;
}
public void setLeft(BinNode<T> left) {
this.left = left;
}
public void setRight(BinNode<T> right) {
this.right = right;
}
public String toString() {
return "( " + left + " " + value + " " + right + " )";
}
}
// Linked List Node class
public class Node<T> {
private T value;
private Node<T> next;
public Node(T value) {
this.value = value;
this.next = null;
}
public Node(T value, Node<T> next) {
this.value = value;
this.next = next;
}
public T getValue() {
return this.value;
}
public Node<T> getNext() {
return this.next;
}
public void setValue(T value) {
this.value = value;
}
public void setNext(Node<T> next) {
this.next = next;
}
public String toString() {
return value.toString();
}
}
// Queue class implemented using linked Nodes
public class Queue<T> {
public Node<T> first;
public Node<T> last;
public Queue() {
this.first = null;
this.last = null;
}
public void insert(T x) {
Node<T> temp = new Node<T>(x);
if (first == null)
first = temp;
else
last.setNext(temp);
last = temp;
}
public T remove() {
T x = first.getValue();
first = first.getNext();
if (first == null)
last = null;
return x;
}
public T top() {
return first.getValue();
}
public boolean isEmpty() {
return first == null;
}
public String toString() {
StringBuilder sb = new StringBuilder();
Node<T> l = first;
while (l != null) {
sb.append(l.getValue()).append(" -> ");
l = l.getNext();
}
sb.append("null");
return sb.toString();
}
}