Help with implementing the four function the first is an example thanks def inor
ID: 3940871 • Letter: H
Question
Help with implementing the four function the first is an example
thanks
def inorder(T,f):
if not is_bst(T):
return
if not T:
return
inorder(T[1],f)
f(T[0])
inorder(T[2],f)
# programming project: provide implementations for the functions below
# and add tests for these functions to the code in the block
# that starts "if __name__ == '__main__'"
def preorder(T,f):
pass
def postorder(T,f):
pass
def tree_height(T):
pass
def balance(T):
# returns the height of the left subtree of T
# minus the height of the right subtree of T
pass
Explanation / Answer
class Node:
def _init_(self,val):
self.value=val
self.leftChild=None
self.rightChild=None
def preorder(self):
if self:
print(str(self.value))
if self.leftChild:
self.leftChild. preorder()
if self.rightChild:
self. rightChild. preorder()
def postorder(self):
if self:
if self.leftChild:
self.leftChild. postorder ()
if self.rightChild:
self. rightChild. postorder ()
print(str(self.value))
def inorder(self):
if self:
if self.leftChild:
self.leftChild. inorder ()
print(str(self.value))
if self.rightChild:
self. rightChild. inorder ()
def height(self,root):
if root is None:
reture 0
else:
return max(self.height(root.left),self.height(root.right))+1
det balance(self,root):
if root is None:
return 0
else:
leftHeight= height(root.left)
rightHeight= height(root.right)
if leftHeight- rightHeight<=1 :
return 1
else:
return 0
class Tree:
def _init_(self):
self.root=None
def preorder(self):
print(“Preorder)”
self.root.preorder()
def postorder(self):
print(“Postorder)”
self.root. postorder ()
def inorder(self):
print(“Inorder)”
self.root. inorder ()
bst=Tree()
bst.preorder()
bst.postorder()
bst.inorder()
print("Height of tree is " (height (bst)))
if (balance(bst):
print (“Tree is balance”)
else
print (“Tree is not balance”)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.