학교 과제에서 나는 노드 요소의 배열을 오름차순 순서로 반환해야하는 메소드를 완료해야합니다. 노드는 바이너리 검색 트리에 모아져 있으므로 올바른 정렬을 위해 재귀 적 메소드를 만들어 팁을 얻습니다.바이너리 검색 트리의 노드 요소로 배열을 오름차순으로 채우는 방법?
문제 이것도 테스트 출력에 따라 집합에서 모든 요소를 생성하지 않는다는 것이다 (에 java.lang.AssertionError :. toArray()는 컬렉션 내의 모든 요소를 리턴하지 않음)
배열을 다루는 다른 방법을 생각해 낼 수 없었습니다. 재귀가 작동하는지는 확실하지 않습니다. 어떤 도움이라도 대단히 감사합니다.
public class BinarySearchTree<E extends Comparable<E>> implements
IfiCollection<E> {
Node root;
Node current;
int size = 0;
int i = 0;
public class Node {
E obj;
Node left, right;
public Node(E e) {
obj = e;
}
} // END class Node
[...]
public E[] toArray(E[] a) {
Node n = root;
a = sort(n, a);
return a;
}
public E[] sort(Node n, E[] a) { //, int idx, E[] a) {
if (n.left != null) {
current = n.left;
sort(current, a);
}
a[i] = current.obj;
i++;
if (n.right != null) {
current = n.right;
sort(current, a);
}
return a;
} // END public Node sort
[...]
} // END class BinarySearchTree
시험 출력 :
에 java.lang.AssertionError :. toArray()이 컬렉션 : TestPerson ("벤더")의 모든 요소를 반환하지 않습니다은 compareTo (TestPerson 다음은 내 코드입니다 inf1010.assignment.IfiCollectionTest.assertCompareToEquals에서 거짓 inf1010.assignment.IfiCollectionTest.assertCompareToEquals에서 (IfiCollectionTest.java:74) (IfiCollectionTest.java:83) 에서 : ("튀김")) == 0 예상 : 사실 만했다 inf1010.assignment.IfiCollectionTest.assertCompareToEqualsNoOrder (IfiCollectionTest.java:100) at inf1010.assignment. IfiCollectionTest.toArray (IfiCollectionTest.java:202)
protected void assertCompareToEquals(TestPerson actual,
TestPerson expected, String msg) {
assertTrue(actual.compareTo(expected) == 0, String.format(// l:74
"%s: %s.compareTo(%s) == 0", msg, actual, expected));
}
[...]
protected void assertCompareToEquals(TestPerson[] actual,
TestPerson[] expected, String msg) {
for (int i = 0; i < actual.length; i++) {
TestPerson a = actual[i];
TestPerson e = expected[i];
assertCompareToEquals(a, e, msg); // l:83
}
}
[...]
protected void assertCompareToEqualsNoOrder(TestPerson[] actual,
TestPerson[] expected, String msg) {
assertEquals(actual.length, expected.length, msg);
TestPerson[] actualElements = new TestPerson[actual.length];
System.arraycopy(actual, 0, actualElements, 0, actual.length);
TestPerson[] expectedElements = new TestPerson[expected.length];
System.arraycopy(expected, 0, expectedElements, 0, expected.length);
Arrays.sort(expectedElements);
Arrays.sort(actualElements);
assertCompareToEquals(actualElements, expectedElements, msg); // l:100
}
[...]
@Test(dependsOnGroups = { "collection-core" },
description="Tests if method toArray yields all the elements inserted in the collection in sorted order with smallest item first.")
public void toArray() {
TestPerson[] actualElements = c.toArray(new TestPerson[c.size()]);
for (int i = 0; i < actualElements.length; i++) {
assertNotNull(actualElements[i],
"toArray() - array element at index " + i + " is null");
}
TestPerson[] expectedElements = allElementsAsArray();
assertCompareToEqualsNoOrder(actualElements, expectedElements, // l:202
"toArray() does not return all the elements in the collection.");
Arrays.sort(expectedElements);
assertCompareToEquals(actualElements, expectedElements,
"toArray() does not return the elements in sorted order with "
+ "the smallest elements first.");
TestPerson[] inArr = new TestPerson[NAMES.length + 1];
inArr[NAMES.length] = new TestPerson("TEMP");
actualElements = c.toArray(inArr);
assertNull(actualElements[NAMES.length],
"The the element in the array immediately following the "
+ "end of the list is not set to null");
}
나는 꽤 광범위의 테스트 코드의 자세한 내용을 게시하는 경우는 나도 몰라, 그것은 하나 개의 게시물에 대한 조금 너무 많이 될 수 있을까요?
귀하의 소스는 완전하지 않습니다. '현재'와'나는'선언 된 곳은 어디입니까? – RoToRa
테스트 한 입력 내용과 출력 내용을 게시하십시오. –