Depth of a Binary Search Tree in Python -
in binary search tree making depth function tell user depth of tree have insert. function crucial unique delete function deletes node largest depth-sided node. think know problem i'm not sure.
this error keep receiving.
c:\python33\python.exe "c:/users/koopt_000/desktop/college/sophomore semester 2/computer science 231/chapter7/test.py" traceback (most recent call last): pre-order transverse: file "c:/users/koopt_000/desktop/college/sophomore semester 2/computer science 231/chapter7/test.py", line 19, in <module> 4 print("the max depth of tree is,", a.height(tree),"nodes deep.") 2 file "c:\users\koopt_000\desktop\college\sophomore semester 2\computer science 231\chapter7\binarysearchtree.py", line 245, in height 1 3 7 6 5 10 none in-order transverse: 1 2 3 4 5 6 7 10 none post-order transverse: 1 return max(bst.height(root.left), bst.height(root.right)) + 1 3 typeerror: height() missing 1 required positional argument: 'root' 2 5 6 10 7 4 none process finished exit code 1 now think problem arises in section of code:
return max(bst.height(root.left), bst.height(root.right)) + 1 i believe statement causing due fact making me call bst function height function in 'work'. tried "height.(root.left)", did not work because said there no global variable height. isn't case don't believe.
here full list of code function, starting tree node, bst file (main), , 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, item): """remove item binary search tree post: item removed tree""" self.root = self._subtreedelete(self.root, item) #------------------------------------------------------------ def _subtreedelete(self, root, item): 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(bst.height(root.left), bst.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._subtreedelete(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.") does see reason why height function isn't working properly? thanks!
your function height instance method of class bst, need call via self , not class bst. in particular case is:
def height(self, root): if root none: return 0 else: return max(self.height(root.left), self.height(root.right)) + 1 however, function height not rely on data associated directly search tree. self needed continue recursion. thus, turn static method via staticmethod decorator:
@staticmethod def height(root): if root none: return 0 else: return max(bst.height(root.left), bst.height(root.right)) + 1 alternatively, move function out of bst class entirely , rid off bst.height , call via height.
from code posted, holds functions of bst class. don't see need it. may use treenode , top-level functions (without bst class) in python module modify , interact tree.
Comments
Post a Comment