二进制搜索树“Int object not iterable”

2024-06-28 11:27:23 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我对编程还是个新手,我试着让我的二叉搜索树中的delete函数在节点深度最高的一侧删除。然而,我总是得到一个错误,一旦我尝试运行它,我知道这是一个简单的修复,但我无法找出它后,我看了一些类似的问题在这里。在

以下是我的错误:

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(main function),然后是我的测试代码。在

^{pr2}$
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.")

谁能告诉我怎么了?我需要这个来让我的删除功能正常工作


Tags: selfrighttreeifisrootitemdelete
1条回答
网友
1楼 · 发布于 2024-06-28 11:27:23

python中的max()函数接受一个iterable对象,比如一个list,它可以在其中迭代以找到max值。在

self.height(root.left)

是一个整数,本质上是一个不可迭代的值,这会引发错误。在

相关问题 更多 >