2015-10-23 62 views
-2
void BinTree::arrayToBSTreeHelper(NodeData* toRead[], Node* current, int low, int high) // ****** 
{ 
    if (low >= high) 
    { 
     return; 
    } 

    int midPoint = (low + high)/2; 

    cout << "low, midPoint, and high at entry to helper: " << low << " " << midPoint << " " << high << endl; // ****** prints 0, 6 and 13 

    insert(toRead[midPoint]); // ****** insert takes a NodeData* 
    arrayToBSTreeHelper(toRead, current->left, low, midPoint); // ERROR 
    arrayToBSTreeHelper(toRead, current->right, midPoint + 1, high); 
} 

low,midPoint和high的打印輸出發生一次,然後在第一次遞歸調用中給出「不良訪問」消息。 「插入」已經過測試,似乎正常工作。這個遞歸例程爲什麼給出EXC_BAD_ACCESS(code = 1,address = 0x8)?

遞歸地傳遞NodeData *數組「toRead」似乎有問題,但我無法弄清楚它是什麼。

環境是Xcode 7.1。

+0

請發佈[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

'current'爲空。 – molbdnilo

回答

1

似乎有一些錯誤傳遞的 NODEDATA陣列「探路者」 *遞歸

這是不正確的。

當(因爲)current爲零時失敗。

如果您認爲您的設計在某個時刻可靠地達到了零,那麼您需要修復該設計錯誤。因爲我們可以看到它IS爲零。

如果設計的其他部分在這一點上不應保證current指向一個有效的對象,那麼你使用它的代碼應該在測試之前進行測試。

相關問題