2015-04-01 38 views
0

所以我相当新的编程,我试图让我的二进制搜索树中的删除功能删除具有最高节点深度的一侧。然而,一旦我尝试运行它时,我总是收到一个错误,我知道这是一个简单的修复,但是在阅读了这里的一些类似问题之后,我无法弄清楚它。二进制搜索树'Int对象不可迭代'

这是我的错误,我得到:

C:\Python33\python.exe "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter7/Test.py" 
Traceback (most recent call last): 
    File "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter7/Test.py", line 10, in <module> 
    a.delete(tree, 9) 
    File "C:\Users\koopt_000\Desktop\College\Sophomore Semester 2\Computer Science 231\Chapter7\BinarySearchTree.py", line 111, in delete 
    ldepth == max(self.height(root.left)) 
TypeError: 'int' object is not iterable 

Process finished with exit code 1 

下面是我的树节点开始,到BST(主要功能)的代码我的以下部分,然后我的测试代码。

class TreeNode(object): 

    def __init__(self, data = None, left=None, right=None): 
     self.item = data 
     self.left = left 
     self.right = right 

    def __str__(self): 
     return str(self.item) 

from TreeNode import TreeNode 


class BST(object): 

    #------------------------------------------------------------ 

    def __init__(self): 

     """create empty binary search tree 
     post: empty tree created""" 

     self.root = None 
     self.size = 0 

    def delete(self, root, item, ldepth = 0, rdepth = 0): 

     """remove item from binary search tree 
     post: item is removed from the tree""" 


     if ldepth == 0: 
      ldepth == max(self.height(root.left)) 
     if rdepth == 0: 
      rdepth == max(self.height(root.right)) 

     if ldepth > rdepth: 
      depth = ldepth 
      print(depth) 
     elif ldepth < rdepth: 
      depth = rdepth 
      print(depth) 
     else: 
      depth = ldepth 
      print(depth) 

     self.root = self._subtreeDelete(root, item, depth) 

    #------------------------------------------------------------ 

    def _subtreeDelete(self, root, item, depth): 

     if root is None: # Empty tree, nothing to do 
      return None 
     if item < root.item:        # modify left 
      root.left = self._subtreeDelete(root.left, item) 
     elif item > root.item:       # modify right 
      root.right = self._subtreeDelete(root.right, item) 
     else:           # delete root 
      if root.left is None:      # promote right subtree 
       root = root.right 
      elif root.right is None:      # promote left subtree 
       root = root.left 
      else: 
       # root node can't be deleted, overwrite it with max of 
       # left subtree and delete max node from the subtree 
       root.item, root.left = self._subtreeDelMax(root.left) 
     return root 

    #------------------------------------------------------------ 

    def _subtreeDelMax(self, root): 

     if root.right is None:   # root is the max 
      return root.item, root.left # return max and promote left subtree 
     else: 
      # max is in right subtree, recursively find and delete it 
      maxVal, root.right = self._subtreeDelMax(root.right) 
      return maxVal, root 

    def height(self, root): 
     if root is None: 
      return 0 
     else: 
      return max(self.height(root.left), self.height(root.right)) + 1 

from BinarySearchTree import BST 
from TreeNode import TreeNode 

tree = TreeNode(4, TreeNode(2, TreeNode(1), TreeNode(3)), TreeNode (7, TreeNode(6),TreeNode(9))) 


a = BST() 
a._subtreeInsert(tree, 10) 
a._subtreeInsert(tree, 5) 
a.delete(tree, 9) 

print("PRE-ORDER TRANSVERSE:") 
print(a.preOrder(tree)) 
print("IN-ORDER TRANSVERSE:") 
print(a.inOrder(tree)) 
print("POST-ORDER TRANSVERSE:") 
print(a.postOrder(tree)) 

print("The max depth of the tree is,", a.height(tree),"nodes deep.") 
print("There are,", a.treeSize(tree),"nodes in this tree.") 

谁能告诉我什么是错了吗?我需要这个工作以使我的删除功能正常工作,

+1

你期望'max(self.height(root.left))'做什么? – user2357112 2015-04-01 00:41:16

+0

我认为它会给左侧的节点的深度... – Cooper 2015-04-01 00:42:51

+0

为什么'max'调用? – user2357112 2015-04-01 00:49:55

回答

0

python中的max()函数需要一个可迭代对象,就像一个列表一样,它可以迭代它以查找最大值。

self.height(root.left) 

是一个单一的int,实质上是一个不可迭代的值,它抛出了你的错误。

+0

但是当我这样做时,它返回'0'。作为最大。 – Cooper 2015-04-01 00:57:56

+0

你的代码具有'def height(self,root):'作为方法定义,当你调用'ldepth == self.height(root.left)'时,你只给了它一个参数,然后在你的高度函数定义你有'def高度(self,root): 如果root是None: return 0',所以它每次都会返回0。所以你需要添加根参数 - >'self.height(root.left,* theroot *)' – Danoram 2015-04-01 01:02:50

+0

抱歉混乱的回应..我讨厌格式化这个网站上的评论。 – Danoram 2015-04-01 01:06:37