2013-09-26 89 views
-1

如何使用inorder遍歷將數組中的元素存儲在數組中? 我讀過一些人說店鋪父母指數在我和離開孩子2 * i + 1和右ar 2 * i + 2。但它不適合我? 需要幫助:)存儲樹的遍歷C

+0

請發表您獲得更多幫助的代碼。 – HAL

+0

如果根節點位於索引1,而不是0,那麼概述的技術就可以工作。最簡單的系統就是分配一個額外的條目,而不是使用0條目。因此,根在1,其兩個孩子在2和3; 2歲的孩子在4歲和5歲; 3的孩子在6和7,依此類推。這是不是你要找的是另一回事;順序遍歷是不同的。 –

+0

我知道了我的代碼現在工作正常:) 反正謝謝大家! – user2714823

回答

1

假設它是一個二叉樹,這裏是一個僞

tree_to_array(tree, array, index) 
    if tree != NULL then 
     // stores recursively all the elements on the left 
     index = tree_to_array(tree.left, array, index) 
     // the root of the (sub)tree 
     array[index] = tree 
     // stores recursively all the elements on the right 
     return tree_to_array(tree.right, array, index + 1) 
    return index