python - Binary Search Tree 'Int object not iterable' -
so i'm new programming , trying make delete function in binary search tree delete on side has highest depth of nodes. however, keep getting error once try run , know simple fix cannot figure out after reading few similar questions on here.
here error get:
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 not iterable process finished exit code 1
here following parts of code starting treenodes, bst(main function), , test code.
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 binary search tree post: item removed 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 none: # empty tree, nothing 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 none: # promote right subtree root = root.right elif root.right none: # promote left subtree root = root.left else: # root node can't deleted, overwrite max of # left subtree , delete max node subtree root.item, root.left = self._subtreedelmax(root.left) return root #------------------------------------------------------------ def _subtreedelmax(self, root): if root.right none: # root max return root.item, root.left # return max , promote left subtree else: # max in right subtree, recursively find , delete maxval, root.right = self._subtreedelmax(root.right) return maxval, root def height(self, root): if root none: return 0 else: return max(self.height(root.left), self.height(root.right)) + 1
from binarysearchtree import bst treenode import treenode tree = treenode(4, treenode(2, treenode(1), treenode(3)), treenode (7, treenode(6),treenode(9))) = 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 tree is,", a.height(tree),"nodes deep.") print("there are,", a.treesize(tree),"nodes in tree.")
could tell me whats wrong? need work in order make delete function work properly,
the max() function in python takes iterable object, list, in can iterates on find max value.
self.height(root.left)
is single int, single value not iteratable, throwing error.
Comments
Post a Comment