कैसे एक द्विआधारी खोज पेड़ से नोड तत्वों के साथ एक सरणी, आरोही क्रम में भरने के लिए?

वोट
0

एक स्कूल असाइनमेंट में मैं एक विधि है कि 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);
}

मैं अगर मैं परीक्षण कोड के और अधिक पोस्ट चाहिए, यह काफी व्यापक है पता नहीं है, और यह एक छोटे से एक पद के लिए बहुत ज्यादा हो सकता है?

18/03/2011 को 13:02
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


4 जवाब

वोट
0

मुझे लगता है कि जहां भ्रमित कर रहे हैं कि अगर आप कैसे एक द्विआधारी खोज वृक्ष काम करता है की जाँच, कि यह हमेशा क्रमबद्ध किया जाता है है। आप अपने रूट नोड में शुरू, और फिर आप एक नया नोड सम्मिलित रूप में, यह यह उचित स्थिति (यानी बाएं या दाएं) में सम्मिलित करता है मूल्यों के आधार पर। तो अगर आप के साथ शुरू करने के लिए तरह कॉल करने के लिए नहीं होना चाहिए। इसलिए मैं वहाँ शुरू करते हैं, और बाइनरी खोज पेड़ पर पढ़ा होगा। उदाहरण के लिए विकिपीडिया एक सभ्य लेख है।

अपडेट: मेरी टिप्पणी पर ध्यान न दें आप ऐसा करने की आवश्यकता नहीं होगी। आप इसी क्रम में पेड़ में 8, 3, 7, 9, 12, 2, 10, 1 सम्मिलित कहो। यह इस तरह की तलाश में खत्म करना चाहिए:

      8
     / \
    3   9
   / \   \
  2   7   12
 /       /
1       10

आप इसे को देखें, तो उन्हें क्रम में, तो अगर यह बाईं ओर एक नोड बाईं ओर हो गया है, प्राप्त करने के लिए रूट पर शुरू करते हैं, नहीं तो अपने आप में लौटने के लिए, और अगर यह एक मूल्य है सही करने के लिए जाने का मतलब है कि। प्रत्येक नोड आप मुठभेड़ के लिए इस दोहरा।

18/03/2011 को 13:30
का स्रोत उपयोगकर्ता

वोट
1

मैं तुम्हें कोड है देखने

if (n.left != null) {
        current = n.left;
        sort(current, a);
  }

लेकिन मुझे लगता है, जिस पर आप वर्तमान नोड पर वर्तमान वापस सेट बात तो यह है कि तुम क्या जब खोजने के लिए नहीं कर पा रहे

a[i] = current.obj;

आप सही परिणाम मिलता है। यही कारण है कि शायद यही कारण है कि आप सभी परिणाम नहीं मिल रहे हैं। किसी भी मामले में मैं नहीं दिख रहा है (कोड के टुकड़े से कम से कम आप पोस्ट किया है) क्यों वर्तमान जरूरतों को एक वर्ग चर होने के लिए और बस प्रकार विधि में घोषित नहीं किया। सामान्य तौर पर आप वर्ग वैरिएबल का उपयोग करता है, तो क्या तुम सच में नहीं है उन्हें जरूरत नहीं होनी चाहिए।

संपादित करें: आप या तो वापस नोड के लिए वर्तमान सेट कर सकते हैं आप इस तरह छोड़ दिया बच्चे पर तरह बुला के बाद कार्रवाई कर रहे हैं

current = n;
a[i] = current.obj;
i++;

या बिल्कुल भी वर्तमान का उपयोग नहीं इस स्थिति में आप की तरह कुछ होगा

if (n.left != null)
    sort(n.left, a);
a[i] = n.obj;
i++;
if (n.right != null)
    sort(n.right, a);
18/03/2011 को 13:57
का स्रोत उपयोगकर्ता

वोट
0

http://cs.armstrong.edu/liang/intro8e/html/BinaryTree.html

आप के लिए क्या देख रहे सबसे आसान तरीका है inorder पेड़ पार और एक ArrayList में जोड़ने के लिए है। सरणी आप ArrayList की विधि .toArray () कॉल कर सकते हैं पाने के लिए।

आप एक ArrayList उपयोग नहीं कर सकते हैं, तो एक सूचकांक और inordertraversal और वेतन वृद्धि के बाहर एक सरणी की घोषणा, तुम्हें पता है कि कितने तत्वों पेड़ अपने सरणी घोषित करने के लिए कर रहे हैं की आवश्यकता होगी।

छद्म कोड:

variables:
arraysize = root.count()
E[] inOrderNodeArray = new E[arraysize]
int index = 0

inorder traversal:
void inorder(Node n) {
    if (n) {
        inorder(n.left)
        inOrderNodeArray[index] = n
        index++
        inorder(n.right)
    }
}
18/03/2011 को 14:01
का स्रोत उपयोगकर्ता

वोट
1

ठीक है, मुझे लगता है कि समस्या "वैश्विक" चर के आपके उपयोग है current। जिस तरह से यह सेट किया गया है, ज्यादा मतलब नहीं है। आप, वैसे भी करने की जरूरत नहीं है क्योंकि "वर्तमान" Nodeएक है कि मानकों में प्रदान की जाती है।

इसके अलावा, आप अपने कार्य का नाम बदलने पर विचार करना चाहिए। आप यहाँ कुछ भी छँटाई कर रहे हैं नहीं, बस पेड़ की सामग्री का संग्रह है, तो एक नाम इस तरह के रूप collectमें अधिक उपयुक्त होगा।

public E[] toArray(E[] a) {
  Node n = root;
  a = collect(n, a);
  return a;
}

public E[] collect(Node n, E[] a) {

  if (n.left != null) {
    // If there is a left (smaller) value, we go there first
    collect(n.left, a);
  }


  // Once we've got all left (smaller) values we can
  // collect the value of out current Node.
  a[i] = n.obj;
  i++;

  if (n.right != null) {
    // And if there is a right (larger) value we get it next
    collect(n.right, a);
  }

  return a;
}

(अस्वीकरण: मैं इस परीक्षण नहीं किया)


वैश्विक सूचकांक के बिना वैकल्पिक कार्यान्वयन:

public E[] toArray(E[] a) {
  Node n = root;
  collect(n, a, 0);
  return a;
}

public int collect(Node n, E[] a, int i) {

  if (n.left != null) {
    // If there is a left (smaller) value, we go there first
    i = collect(n.left, a, i);
  }


  // Once we've got all left (smaller) values we can
  // collect the value of out current Node.
  a[i] = n.obj;
  i++;

  if (n.right != null) {
    // And if there is a right (larger) value we get it next
    i = collect(n.right, a, i);
  }

  return i;
}
18/03/2011 को 14:07
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more