2017-07-02 130 views
0

我試圖從二叉搜索樹中插入和刪除。到目前爲止,我的插入功能起作用,但是我的移除功能不適用。我正在瀏覽我的代碼,無法找到任何明顯的錯誤。我收到的錯誤消息是line 69: root.right_child = self._recurisve_delete(root.right_child, value) AttributeError: 'Binary_Search_Tree' object has no attribute '_recurisve_delete'"這裏實際發生了什麼?從二叉搜索樹(python)中刪除?

​​

下面是測試代碼我使用:

if __name__ == '__main__': 
    bst = Binary_Search_Tree() 
    values = [7, 2, 22, 5, 1, 8, 3, 6, 9, 8, 4, 11, 10, 12] 
    print('insert values', values) 
    for val in values: 
     bst.insert_element(val) #this all works well# 
    print('in-order: ', bst.in_order(), '\n') 
    bst.remove_element(22) 

輸出:

in-order: [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 22] 

的錯誤,當我嘗試刪除的元素來。任何有識之士將不勝感激!

回答

0

這只是一個錯字。

elif root.value < value and root.right_child is not None: 
     root.right_child = self._recurisve_delete(root.right_child, value) 

應該

elif root.value < value and root.right_child is not None: 
     root.right_child = self._recursive_delete(root.right_child, value) 
+0

哇,我覺得自己像個白癡!就是這樣。我想我一直在編寫太長的代碼......在這裏,我想我有一些巨大的邏輯錯誤! – runnergirl9