Menu

[solved]-Create New Method Checks Binary Tree Properly Structured Bst Discuss Time Complexity Imple Q39047013

Create a new method that checks if a binary tree is a properly structured BST. Discuss the time complexity after implementing

Create a new method that checks if a binary tree is a properly structured BST. Discuss the time complexity after implementing this method. class BinaryTree ( lf, val): def self.value val self.left = None self.right- None self.parent = None def se self,node) self.left.parent self def set right (self,node): self.right = node self.right.parent = self def preorder(self) left vals = self.left.preorder if self.left is not None else right_vals = self.right.preorder ) if self.right is not None else [] return [self.valuel left vals right vals def inorder (self) elf.left.inorder ) if self.left is ot None else [] right vals = self.right.inorder if self.right is not None else ] return left_vals [self.value] right_vals def postorder (self): left_vals = self.left.postorder if self.left is not None else right_vals = self.right.postorder) if self.right is not None else return left_vals right_vals + [self.value def checkbst (temp): you can use this stub to create a method to check to see if the binary tree is also a binary search tree return True #outside the class main ‘: if_name #this tree is a BST tree BinaryTree (10) left BinaryTree (5) right BinaryTree (15) lert.set left (BinaryTree (2)) right.set left (BinarvTree (13)) right.set right (BinaryTree (19)) tree.set left (left) tree.set_right (right print (tree.preorder ()) print (tree.inorder ) r)) if tree.checkbst (): print “Valid BST! “) else print “Invalid BST!”) #this tree is not a BST tree BinaryTree (4) left BinaryTree (3) right BinaryTree (7) left.set left (BinaryTree (1)) left.set_right (BinaryTree (20)) right.set left (BinaryTree (6)) right.set_right (BinaryTree (30)) t right (right) print (tree.preorder print (tree.inorder () print tree.postorder ) Show transcribed image text Create a new method that checks if a binary tree is a properly structured BST. Discuss the time complexity after implementing this method. class BinaryTree ( lf, val): def self.value val self.left = None self.right- None self.parent = None def se self,node) self.left.parent self def set right (self,node): self.right = node self.right.parent = self def preorder(self) left vals = self.left.preorder if self.left is not None else right_vals = self.right.preorder ) if self.right is not None else [] return [self.valuel left vals right vals def inorder (self) elf.left.inorder ) if self.left is ot None else [] right vals = self.right.inorder if self.right is not None else ] return left_vals [self.value] right_vals def postorder (self): left_vals = self.left.postorder if self.left is not None else right_vals = self.right.postorder) if self.right is not None else return left_vals right_vals + [self.value def checkbst (temp): you can use this stub to create a method to check to see if the binary tree is also a binary search tree return True #outside the class main ‘: if_name #this tree is a BST tree BinaryTree (10) left BinaryTree (5) right BinaryTree (15) lert.set left (BinaryTree (2)) right.set left (BinarvTree (13)) right.set right (BinaryTree (19)) tree.set left (left) tree.set_right (right print (tree.preorder ()) print (tree.inorder ) r)) if tree.checkbst (): print “Valid BST! “) else print “Invalid BST!”) #this tree is not a BST tree BinaryTree (4) left BinaryTree (3) right BinaryTree (7) left.set left (BinaryTree (1)) left.set_right (BinaryTree (20)) right.set left (BinaryTree (6)) right.set_right (BinaryTree (30)) t right (right) print (tree.preorder print (tree.inorder () print tree.postorder )

Expert Answer


Answer to Create a new method that checks if a binary tree is a properly structured BST. Discuss the time complexity after impleme… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *