0% found this document useful (0 votes)
1 views

JavaHelpersQueueNodeBinNode

The document contains Java helper classes for data structures, including a binary tree node class (BinNode), a linked list node class (Node), and a queue class implemented using linked nodes (Queue). Each class includes constructors, getter and setter methods, and a toString method for representation. These classes provide foundational building blocks for implementing various data structures in Java.

Uploaded by

noaam.farber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

JavaHelpersQueueNodeBinNode

The document contains Java helper classes for data structures, including a binary tree node class (BinNode), a linked list node class (Node), and a queue class implemented using linked nodes (Queue). Each class includes constructors, getter and setter methods, and a toString method for representation. These classes provide foundational building blocks for implementing various data structures in Java.

Uploaded by

noaam.farber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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();
}
}

You might also like