二进制搜索树' Int对​​象不可迭代'

时间:2015-04-01 00:39:04

标签: python binary-search-tree iterable

所以我对编程很新,我试图在我的二叉搜索树中删除功能,以删除节点深度最高的一侧。但是,一旦我尝试运行它,我就会一直收到错误,我知道这是一个简单的修复,但在这里阅读了几个类似的问题之后我无法弄明白。

这是我的错误:

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

以下是从我的TreeNodes到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 个答案:

答案 0 :(得分:0)

python中的max()函数接受一个可迭代的对象,就像一个列表,它可以在迭代中找到最大值。

self.height(root.left)

是一个单独的int,基本上是一个不可迭代的单值,这会引发你的错误。