Why isn't my iterator working? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
For a class assignment I have to implement my own iterator from scratch. The iterator is iterating through a linked list of nodes. All of my test cases that use the iterator are failing and I can't tell what is wrong with it.
import java.util.Iterator;
import java.util.NoSuchElementException;
class LinkedNodeIterator<E> implements Iterator<E> {
LinkedNode<E> headNode;
LinkedNode<E> curr;
// Constructors
public LinkedNodeIterator(LinkedNode<E> head) {
headNode = head;
curr = headNode;
}
#Override
public boolean hasNext() {
if(headNode == null)
return false;
if(curr.getNext() == null)
return false;
return true;
}
#Override
public E next() {
if(curr.getNext() == null || curr == null)
throw new NoSuchElementException();
LinkedNode<E> save = curr;
curr = curr.getNext();
return save.getData();
}
#Override
public void remove() {
throw new UnsupportedOperationException();
}
}
Some test cases that fail (they all return count = 0):
public class PublicLinkedSetTest {
Set<String> set0;
Set<String> set1;
Set<String> set2;
Set<String> set3;
Set<String> set4;
#Before
public void before() {
set0 = new LinkedSet<String>();
set1 = new LinkedSet<String>();
for (String e : new String[]{"c", "a", "d", "b", "e"}) {
set1 = set1.adjoin(e);
}
set2 = new LinkedSet<String>();
for (String e : new String[]{"b", "d", "a", "e", "c"}) {
set2 = set2.adjoin(e);
}
set3 = new LinkedSet<String>();
for (String e : new String[]{"a", "d", "b"}) {
set3 = set3.adjoin(e);
}
set4 = new LinkedSet<String>();
for (String e : new String[]{"x", "y", "z", "a", "b", "d"}) {
set4 = set4.adjoin(e);
}
}
public void testIterator1() {
int count = 0;
for (String e : set1) {
count += 1;
}
assertEquals(5, count);
}
#Test
public void testIterator2() {
int count = 0;
for (String e : set2) {
count += 1;
}
assertEquals(5, count);
}
#Test
public void testIterator3() {
int count = 0;
for (String e : set3) {
count++;
}
assertEquals(3, count);
}
Here is the code for my LinkedSet
import java.util.Iterator;
public class LinkedSet<E> implements Set<E> {
private LinkedNode<E> head = null;
private LinkedNode<E> link;
// Constructors
public LinkedSet() {
}
public LinkedSet(E e) {
this.head = new LinkedNode<E>(e, null);
}
private LinkedSet(LinkedNode<E> header) {
header = head;
}
#Override
public int size() {
int count = 0;
for(E e : this){
count++;}
return count;
}
#Override
public boolean isEmpty() {
for(E e : this){
if(e != null)
return false;
}
return true;
}
#Override
public LinkedNodeIterator<E> iterator() {
return new LinkedNodeIterator<E>(this.head);
}
#Override
public boolean contains(Object o) {
for(E e : this){
if(e == o)
return true;
}
return false;
}
#Override
public boolean isSubset(Set<E> that) {
that = new LinkedSet<E>();
if(this.size()>that.size())
return false;
for(E e : this){
if(that.contains(e) == false)
return false;
}
return true;
}
#Override
public boolean isSuperset(Set<E> that) {
that = new LinkedSet<E>();
if(this.isSubset(that))
return true;
else
return false;
}
#Override
public Set<E> adjoin(E e) {
boolean alwaysEqual = true;
if(this.head == null)
return this;
for(E t : this){
if(t != e)
alwaysEqual = false;}
if(alwaysEqual == true)
return this;
LinkedNode<E> temp = this.head;
LinkedNode<E> newNode = new LinkedNode<E>(e, temp);
LinkedSet<E> newSet = new LinkedSet<E>(newNode);
Set<E> otherSet = newSet;
return otherSet;
}
#Override
public Set<E> union(Set<E> that) {
Set<E> thisSet = this;
for(E e : that){
if(!this.contains(e))
thisSet = thisSet.adjoin(e);
}
return thisSet;
}
#Override
public Set<E> intersect(Set<E> that) {
LinkedSet<E> newSet = null;
Set<E> otherNewSet = newSet;
for(E e : that){
if(this.contains(e)){
if(otherNewSet == null){
LinkedNode<E> newNode = new LinkedNode<E>(e, null);
otherNewSet = new LinkedSet<E>(newNode);
}
else{
otherNewSet = otherNewSet.adjoin(e);
}
}
}
return otherNewSet;
}
#Override
public Set<E> subtract(Set<E> that) {
LinkedSet<E> newSet = null;
Set<E> otherNewSet = newSet;
for(E e : that){
if(!this.contains(e)){
if(otherNewSet == null){
LinkedNode<E> newNode = new LinkedNode<E>(e, null);
otherNewSet = new LinkedSet<E>(newNode);
}
else{
otherNewSet = otherNewSet.adjoin(e);
}
}
}
return otherNewSet;
}
#Override
public Set<E> remove(E e) {
LinkedSet<E> newSet = null;
Set<E> otherNewSet = newSet;
if(!this.contains(e))
return this;
else{
for(E t : this){
if(t != e){
if(otherNewSet == null){
LinkedNode<E> newNode = new LinkedNode<E>(e, null);
otherNewSet = new LinkedSet<E>(newNode);
}
}
else{
otherNewSet = otherNewSet.adjoin(e);
}
}
}
return otherNewSet;
}
#Override
#SuppressWarnings("unchecked")
public boolean equals(Object o) {
if (! (o instanceof Set)) {
return false;
}
Set<E> that = (Set<E>)o;
return this.isSubset(that) && that.isSubset(this);
}
#Override
public int hashCode() {
int result = 0;
for (E e : this) {
result += e.hashCode();
}
return result;
}
}

The problem isn't your iterator, it's your adjoin method.
When you construct a new LinkedSet like this:
set1 = new LinkedSet<String>();
It means that you call this constructor:
// Constructors
public LinkedSet() {
}
But that constructor doesn't assign anything to head, so it has its default initial value:
private LinkedNode<E> head = null;
Since head starts off as null, and isn't ever assigned to, this method never does anything but return this:
#Override
public Set<E> adjoin(E e) {
boolean alwaysEqual = true;
if (this.head == null)
return this;
for (E t : this) {
if (t != e)
alwaysEqual = false;
}
if (alwaysEqual == true)
return this;
LinkedNode<E> temp = this.head;
LinkedNode<E> newNode = new LinkedNode<E>(e, temp);
LinkedSet<E> newSet = new LinkedSet<E>(newNode);
Set<E> otherSet = newSet;
return otherSet;
}
So your iterator isn't iterating over anything, because your set has nothing in it.

The code guarantees a NullPointerException if curr is null.
if(curr.getNext() == null || curr == null)
Execution is left to right. Test the reference for null before you attempt to dereference it.

Related

Time out issue in java

I have been running this code and it seems like it times out. I'm not sure how to fix it as it doesn't show me an actual error message or anything like that. The first block of code is the class that the methods in the demo are coming from, and it seems like all the methods that are being called for stringList are just not working. When I test the same thing with an int list it works fine.
public class DoublyLinkedList<E> {
private static class Node<E> {
// Node Fields
private E element; //data
private Node<E> prev; //previous Node
private Node<E> next; //next Node
// Node Constructor
public Node(E e, Node<E> p, Node<E> n) {
this.element = e;
this.prev = p;
this.next = n;
}
// Node Methods
public E getElement() {
return element;
}
public Node<E> getPrev() {
return this.prev;
}
public Node<E> getNext() {
return this.next;
}
public void setPrev(Node<E> p) {
this.prev = p;
}
public void setNext(Node<E> n) {
this.next = n;
}
}
// DLinkedList Fields
private Node<E> header;
private Node<E> trailer;
int size;
// DLinkedList Constructor
public DoublyLinkedList() {
this.header = new Node<>(null, null, null);
this.trailer = new Node<>(null, this.header, null);
this.header.setNext(this.trailer);
}
// DLinkedList Methods
public int size() {
return this.size;
}
public E first() {
if (isEmpty()) {
return null;
}
return this.header.next.getElement();
}
public E last () {
if (isEmpty()) {
return null;
}
return this.trailer.prev.getElement();
}
public boolean isEmpty() {
return size == 0;
}
public void addFirst (E e) {
addBetween(e, this.header, this.header.getNext());
}
public void addLast (E e) {
addBetween(e, this.trailer.getPrev(), this.trailer);
}
private void addBetween(E e, Node<E> predecessor, Node<E> successor) {
Node<E> newest = new Node<>(e, predecessor, successor);
predecessor.setNext(newest);
successor.setPrev(newest);
this.size++;
}
public E removeFirst() {
if (this.isEmpty()) {
return null;
}
return this.remove(header.getNext());
}
public E removeLast() {
if (this.isEmpty()) {
return null;
}
return this.remove(trailer.getPrev());
}
public E remove(Node<E> e) {
e.next.setPrev(e.prev);
e.prev.setNext(e.next);
this.size--;
return e.getElement();
}
public String toString() {
StringBuilder sb = new StringBuilder("(");
Node<E> walk = this.header.next;
while (walk != this.trailer) {
sb.append(walk.element);
if (walk.next != this.trailer)
sb.append(", ");
walk = walk.next;
}
sb.append(")");
return sb.toString();
}
//DONE
public void add(int index, E element) {
Node<E> pred = header;
Node<E> succ = pred.getNext();
int count = 0;
while(succ != null) {
if(count == index) addBetween(element, pred, succ);
count++;
pred = pred.getNext();
succ = succ.getNext();
}
}
//DONE
public void add(E e) {
add(size, e);
}
//DONE
public void clear() {
while(!isEmpty()) {
removeFirst();
}
}
public E get(int index) {
if (isEmpty()) return null;
Node<E> current = header;
int count = 0;
while(current != null) {
if(count == index) return current.getElement();
count++;
current = current.getNext();
}
return null;
}
public E set(int index, E element) {
if(isEmpty()) return null;
Node<E> current = header;
E returnVal = null;
int count = 0;
while(current != null) {
if(count == index) {
if(count == 0) {
returnVal = get(0);
removeFirst();
add(0, element);
}
else if(count == size) {
returnVal = get(size);
removeLast();
add(size, element);
}
else {
returnVal = get(index);
remove(current);
add(index, element);
}
}
}
return returnVal;
}
}
package labs;
public class DoublyLinkedListDemo {
public static void main(String[] args) {
//testing methods on a String DoublyList
DoublyLinkedList<String> stringList = new DoublyLinkedList<>();
stringList.addFirst("Strawberry");
stringList.addFirst("Banana");
stringList.addFirst("Apple");
stringList.set(0, stringList.get(1));
System.out.println(stringList);
stringList.add(1, "Pear");
System.out.println(stringList);
stringList.add("Blueberry");
System.out.println(stringList);
System.out.println(stringList.get(1));
stringList.clear();
System.out.println(stringList);
System.out.println(stringList.set(0, stringList.get(1)));
System.out.println(stringList.get(0));
}
}

Java BinaryTree balance a tree

I am trying to get a binary search tree to balance.
I balance the tree after the insert method.
I hope someone here can guide me through this.
The method is balanceTheTree(...).
I did a recursive method, I don't know if it's the best solution
public class BinaryTreeTable<E extends Comparable<E>, T> implements Table<E, T> {
private Node root;
public BinaryTreeTable() {
this.root = new Node(null, null, null);
}
#Override
public boolean insert(E key, T data) {
boolean ret;
if (this.root.key == null) {
Node toInsert = new Node(null, key, data);
this.root = toInsert;
ret = true;
} else {
Node father = this.seekFather(key);
if (father == null) {
ret = false;
} else {
Node toInsert = new Node(father, key, data);
if (key.compareTo(father.key) > 0) {
father.rSon = toInsert;
ret = true;
balanceTheTree(toInsert);
} else if (key.compareTo(father.key) < 0) {
father.lSon = toInsert;
ret = true;
balanceTheTree(toInsert);
} else {
ret = false;
}
}
}
return ret;
}
private void rightRotation(Node theN) {
Node k2 = theN.rSon;
theN.rSon = k2.lSon;
k2.lSon = theN;
}
private void leftRotation(Node theN) {
Node k1 = theN.lSon;
theN.lSon = k1.rSon;
k1.rSon = theN;
}
private void leftRightRotation(Node theN) {
leftRotation(theN.rSon);
rightRotation(theN);
}
private void rightLeftRotation(Node theN) {
rightRotation(theN.lSon);
leftRotation(theN);
}
private void balanceTheTree(Node theN) {
if (theN == null) {
} else {
if (Math.abs(Math.subtractExact(computeH(theN.rSon), computeH(theN.lSon))) > 1) {
System.out.println("A droite " + theN.getLabel());
if (computeH(theN.lSon) > computeH(theN.rSon)) {
leftRotation(theN);
} else {
leftRightRotation(theN);
}
} else if (Math.abs(Math.subtractExact(computeH(theN.lSon), computeH(theN.rSon))) > 1) {
System.out.println("A gauche");
} else {
balanceTheTree(theN.rSon);
balanceTheTree(theN.lSon);
}
}
}
private int computeH(Node theN) {
int ret = 1;
if (theN == null) {
ret = 0;
} else if ((theN.lSon != null) && (theN.rSon == null)) {
ret = computeH(theN.lSon) + 1;
} else if ((theN.lSon == null) && (theN.rSon != null)) {
ret = computeH(theN.rSon) + 1;
} else if ((theN.lSon != null) && (theN.rSon != null)) {
ret = Math.max(computeH(theN.lSon), computeH(theN.rSon)) + 1;
}
return ret;
}
public class Node {
// Attributs
private Node lSon ;
private Node rSon ;
private Node father ;
private T theValue ;
private E key ;
// Constructeur
public Node (Node father, E key, T theValue) {
this.father = father;
this.key = key;
this.theValue = theValue;
}
public String getLabel() {
return String.valueOf(key);
}
public Node getLeft() {
return lSon;
}
public Node getRight() {
return rSon;
}
public Node clone() {
return new Node(this.father, this.key, this.theValue);
}
}
}
The computeH() method allows me to know the size of a node
The insertion method works correctly.
The tree i want to balance
public static void main(String[] args) {
BinaryTreeTable binaryTreeTable = new BinaryTreeTable();
binaryTreeTable.insert(10, "Test");
binaryTreeTable.insert(5, "Test");
binaryTreeTable.insert(7, "Test");
binaryTreeTable.insert(3, "Test");
binaryTreeTable.insert(4, "Test");
binaryTreeTable.insert(15, "Test");
binaryTreeTable.insert(19, "Test");
binaryTreeTable.insert(16, "Test");
binaryTreeTable.insert(20, "Test");
binaryTreeTable.showTree();
}

LinkedQueue next value doesn't work as indeed

I want to create a Queue after the FIFO rule, every element is created at the end of the queue but my getNext() for the element don't work(the Value is null) and I have no clue.
Here is the UML:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyLinkedQueue<E> implements Iterable<E>, MyQueue<E> {
private Cell front;
private Cell rear;
private int count;
public Cell getFront() {
return front;
}
public void setFront(Cell front) {
this.front = front;
}
public Cell getRear() {
return rear;
}
public void setRear(Cell rear) {
this.rear = rear;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public class Cell {
private E value;
private Cell next;
Cell(E element) {
this.value = element;
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public Cell getNext() {
return next;
}
public void setNext(Cell next) {
this.next = next;
}
}
public class MyIterator implements Iterator<E> {
private Cell pointer;
private boolean begin;
private boolean removable;
public Cell getPointer() {
return pointer;
}
public void setPointer(Cell pointer) {
this.pointer = pointer;
}
public boolean isBegin() {
return begin;
}
public void setBegin(boolean begin) {
this.begin = begin;
}
public boolean isRemovable() {
return removable;
}
public void setRemovable(boolean removable) {
this.removable = removable;
}
public MyIterator() {
}
public boolean hasNext() {
return pointer != null ? true : false;
}
public E next() {
if (!hasNext())
throw new NoSuchElementException();
E e = pointer.value;
pointer = pointer.getNext();
return e;
}
public void remove() {
throw new NoSuchElementException();
}
}
public boolean isEmpty() {
return count == 0;
}
public E peek() throws NoSuchElementException {
E result = null;
if (isEmpty()) {
System.out.println("Queue is empty");
} else {
result = front.getValue();
}
return result;
}
public E peekLast() throws NoSuchElementException {
E result = null;
if (isEmpty()) {
System.out.println("Queue is empty");
} else {
result = rear.getValue();
}
return result;
}
public MyQueue<E> append(E element) {
if (front == null)
front = new Cell(element);
rear = new Cell(element);
// rear=rear.getNext(); getNext() PROBLEM
count++;
return this;
}
public E delete() throws NoSuchElementException {
E e;
e = front.getValue();
// front = front.getNext();getNext() PROBLEM
count--;
if (count == 0)
rear = null;
return e;
}
public int size() {
return count;
}
public String toString() {
StringBuilder s = new StringBuilder();
for (E e : this)
s.append(e + "; ");
return s.toString();
}
#Override
public Iterator<E> iterator() {
Iterator<E> iterator = new MyIterator();
return iterator;
}
}

How I have to return in "next" method of Iterator<E>?

in method "next" implemented in Iterator there is a mistake. I cannot return 'e' value, because Java says it is Object type. I don't understand why. Because in Elem class if I give for instance Point object, it must return it. But I don't know generics well, maybe someone could explain plz. Thnx)
package Tutorial3;
import java.util.Iterator;
public class MyLinkedList<E> implements Iterable<E> {
Elem<E> head;
Elem<E> tail;
public MyLinkedList() {
head = null;
tail = null;
}
public void add(E e) {
Elem<E> newElem = new Elem<E>(e);
if (head == null) {
tail.setNext(newElem);
head = newElem;
tail = newElem;
} else {
tail.setNext(newElem);
tail = newElem;
}
}
public void addToHead(E e) {
Elem<E> newElem = new Elem<E>(e);
newElem.setNext(head);
head = newElem;
}
public void addToTail(E e) {
add(e);
}
public void removeFirstValue() {
head = head.getNext();
}
public void removeLastValue() {
Elem<E> cursor;
cursor = head;
while (cursor.getNext() != tail) {
cursor = cursor.getNext();
}
tail = cursor;
tail.setNext(null);
}
public E get(int index) {
if ((index + 1) > size()) {
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + size());
} else {
Elem<E> cursor;
cursor = head;
int i = 0;
if (i == index) {
return cursor.getE();
} else {
while (i != index) {
cursor = cursor.getNext();
i++;
}
return cursor.getE();
}
}
}
public int size() {
Elem<E> cursor;
cursor = head;
int size = 0;
while (cursor != null) {
size++;
cursor = cursor.getNext();
}
return size;
}
#Override
public Iterator<E> iterator() {
return new MyLinkedListIterator<E>();
}
private class MyLinkedListIterator<T> implements Iterator<T> {
private Elem elem = head;
#Override
public boolean hasNext() {
return elem.getNext() != null;
}
#Override
public T next() {
return elem.getNext().getE(); //here is mistake: can't return e
//required : T, Found: Object
}
}
}
package Tutorial3;
public class Elem<E> {
private E e;
private Elem<E> next;
public Elem(E e) {
this.e = e;
}
public Elem(E e, Elem<E> next) {
this.e = e;
this.next = next;
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
public Elem<E> getNext() {
return next;
}
public void setNext(Elem<E> next) {
this.next = next;
}
}
It should be
private class MyLinkedListIterator implements Iterator<E> {
private Elem<E> elem = head;
#Override
public boolean hasNext() {
return elem.getNext() != null;
}
#Override
public E next() {
return elem.getNext().getE();
}
}
Your MyLinkedListIterator type should inherit E from the outer MyLinkedList<E> type, instead of declaring its own element type T, and then you compounded that by using a raw Elem type instead of Elem<E>.

inOrderIterator method cannot be applied to BinaryTreeNode<T>

Working on an InOrderIterator traversal method. I understand how to do this recursively but I keep getting this complier error.
inOrderIterator() in LinkedBinarySearchTree<T> cannot be applied to (BinaryTreeNode<T>)
Im not sure why I can't apply this method to that object. Any ideas?
Heres my method so far
public ArrayList<T> inOrderIterator()
{
ArrayList<T> myArr = new ArrayList<T>();
BinaryTreeNode<T> currentNode = this.root;
if(currentNode != null)
{
inOrderIterator(currentNode.getLeftChild());
myArr.add(currentNode.getElement());
inOrderIterator(currentNode.getRightChild());
}
return myArr;
}
LinkedBinarySearchTree.java
import jss2.exceptions.EmptyCollectionException;
import jss2.exceptions.ElementNotFoundException;
import java.util.ArrayList;
public class LinkedBinarySearchTree<T extends Comparable<T>>
{
private T elem;
BinaryTreeNode<T> root;
public LinkedBinarySearchTree (T element)
{
elem = element;
root = null;
}
public LinkedBinarySearchTree ()
{
root = null;
}
public void addToTree (T element)
{
//Check if root is null
if(root == null)
{
root.getElement().equals(element);
}
else
{
addToTreeHelper(root, element);
}
}
public void addToTreeHelper(BinaryTreeNode<T> node, T target)
{
BinaryTreeNode<T> child;
BinaryTreeNode<T> targetNode = new BinaryTreeNode<T>(target);
if(target.compareTo(node.getElement()) == -1)
{
child = node.getLeftChild();
if(child == null)
{
node.setLeftChild(targetNode);
}
else
{
addToTreeHelper(node.getLeftChild(), target);
}
}
else if(target.compareTo(node.getElement()) >= 0)
{
child = node.getRightChild();
if(child == null)
{
node.setRightChild(targetNode);
}
else
{
addToTreeHelper(node.getRightChild(), target);
}
}
}
//remove Element
public void removeElement(T target) throws Exception
{
BinaryTreeNode<T> node;
if(root.getElement() == null)
{
throw new EmptyCollectionException("tree is empty");
}
else if(target.compareTo(root.getElement()) == 0)
{
root = getReplacement(root);
}
else
{
node = removeElemHelper(root, target);
if(node == null)
{
throw new ElementNotFoundException ("not found "+target.toString());
}
}
}
//remove element helper
public BinaryTreeNode<T> removeElemHelper(BinaryTreeNode<T> node, T target)
{
BinaryTreeNode<T> result, child, replacement;
result = null;
if(node != null)
{
if(target.compareTo(node.getElement()) == -1)
{
child = node.getLeftChild();
if(child != null && target.compareTo(child.getElement()) == 0)
{
result = child;
replacement = getReplacement(child);
if(replacement == null)
{
node.setLeftChild(null);
}
else
{
node.setLeftChild(replacement);
}
}
else
{
result = removeElemHelper(child, target);
}
}
//
else if(target.compareTo(node.getElement()) == 1)
{
child = node.getRightChild();
if(child != null && target.compareTo(child.getElement()) == 0)
{
result = child;
replacement = getReplacement(child);
if(replacement == null)
{
node.setRightChild(null);
}
else
{
node.setRightChild(replacement);
}
}
else
{
result = removeElemHelper(child, target);
}
}
}
return result;
}
//replacement
public BinaryTreeNode<T> getReplacement(BinaryTreeNode<T> node)
{
BinaryTreeNode<T> result,leftChild, rightChild;
leftChild = node.getLeftChild();
rightChild = node.getRightChild();
if(node.getLeftChild() == null && node.getRightChild() == null)
{
result = null;
}
else if(node.getLeftChild() == null && node.getRightChild() != null)
{
result = node.getRightChild();
}
else if(node.getLeftChild() != null && node.getRightChild() == null)
{
result = node.getLeftChild();
}
else
{
result = findInorderSucessor(rightChild);
result.setLeftChild(leftChild);
result.setRightChild(rightChild);
}
return result;
}
//findInorderSucessor
private BinaryTreeNode<T> findInorderSucessor(BinaryTreeNode<T> node)
{
BinaryTreeNode<T> child = node.getLeftChild();
if(child == node)
{
return node;
}
else if(child.getLeftChild() == null)
{
child.setRightChild(node.getLeftChild());
}
return findInorderSucessor(child);
}
public ArrayList<T> inOrderIterator()
{
ArrayList<T> myArr = new ArrayList<T>();
BinaryTreeNode<T> currentNode = this.root;
if(currentNode != null)
{
inOrderIterator(currentNode.getLeftChild());
myArr.add(currentNode.getElement());
inOrderIterator(currentNode.getRightChild());
}
return myArr;
}
}
Look at your method declaration:
public ArrayList<T> inOrderIterator()
It doesn't have any parameters. But look how you're trying to invoke it:
inOrderIterator(currentNode.getRightChild());
... you're specifying an argument. There's no method which is applicable for that call.
I suspect you want to overload the method to have a private method accepting a node and a List<T> (the one you're building up), and then make your public method call that. For example:
public List<T> inOrderIterator() {
List<T> list = new ArrayList<T>();
inOrderIterator(list, this.root);
return list;
}
private void inOrderIterator(List<T> list, BinaryTreeNode<T> current) {
if (current == null) {
return;
}
inOrderIterator(current.getLeftChild());
list.add(current);
inOrderIterator(current.getRightChild());
}

Categories