एक स्कूल असाइनमेंट में मैं एक विधि है कि ascendig क्रम में नोड तत्वों की एक सरणी लौटना चाहिए पूरा करने के लिए माना जाता रहा हूँ। नोड्स एक द्विआधारी खोज वृक्ष में इकट्ठे होते हैं, इसलिए उन्हें सही सॉर्ट करने के लिए, मैं काम करने के लिए एक पुनरावर्ती विधि बनाने के लिए एक टिप मिली।
समस्या यह है कि यह और भी परीक्षण उत्पादन के अनुसार संग्रह में सभी तत्वों को उपज नहीं है (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 ( फ्राई)) == 0 की उम्मीद:। सच है, लेकिन था: inf1010.assignment पर झूठे inf1010.assignment.IfiCollectionTest.assertCompareToEqualsNoOrder (IfiCollectionTest.java:100) inf1010.assignment.IfiCollectionTest.toArray पर पर .IfiCollectionTest.assertCompareToEquals (IfiCollectionTest.java:74) inf1010.assignment.IfiCollectionTest.assertCompareToEquals (IfiCollectionTest.java:83) पर ( 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);
}
मैं अगर मैं परीक्षण कोड के और अधिक पोस्ट चाहिए, यह काफी व्यापक है पता नहीं है, और यह एक छोटे से एक पद के लिए बहुत ज्यादा हो सकता है?













