Question: Can you help me complete this code in python. We will begin by implementing binary search tree data structure in python. Please read the descriptions
Can you help me complete this code in python.







We will begin by implementing binary search tree data structure in python. Please read the descriptions of functions carefully and complete them according to description. You should be familiar with objects in python. There are many tutorials online that you can use for this : https://www.tutorialspoint.com/python/python_classes_objects.htm \# be the parent if we were to insert the key subsequently. def search(self, key): if self.key == key: return (True, self) \# your code here \#TODO: Complete the insert algorithm below \# To insert first search for it and find out \# the parent whose child the currently inserted key will be. \# Create a new node with that key and insert. \# return None if key already exists in the tree. \# return the new node corresponding to the inserted key otherwise. def insert(self, key): \# your code here \# TODO: Complete algorithm to compute height of the tree \# height of a node whose children are both None is defined \# to be 1 . \# height of any other node is 1 + maximum of the height \# of its children. \# Return a number that is th eheight. def height (self): \# your code here \#TODO: Write an algorithm to delete a key in the tree. \# First, find the node in the tree with the key. \# Recommend drawing pictures to visualize these cases below before \# programming. \# Case 1: both children of the node are None \# .. in this case, deletion is easy: simply find out if the node with key is its \# parent's left/right child and set the corr. child to None in the parent node. \# Case 2: one of the child is None and the other is not. \# .- replace the node with its only child. In other words, \# modify the parent of the child to be the to be deleted node 's parent. \# also change the parent's left/right child appropriately. \# Case 3: both children of the parent are not None. \# .- first find its successor (go one step right and all the way to the left). \# .- function get_leftmost_descendant may be helpful here. \# -. replace the key of the node by its successor. \# .. delete the successor node. \# return: no return value specified def delete(self, key): (found, node_to_delete) = self.search ( key ) assert(found == True), f"key to be deleted: {key} - does not exist in the tree" \# your code here We will begin by implementing binary search tree data structure in python. Please read the descriptions of functions carefully and complete them according to description. You should be familiar with objects in python. There are many tutorials online that you can use for this : https://www.tutorialspoint.com/python/python_classes_objects.htm \# be the parent if we were to insert the key subsequently. def search(self, key): if self.key == key: return (True, self) \# your code here \#TODO: Complete the insert algorithm below \# To insert first search for it and find out \# the parent whose child the currently inserted key will be. \# Create a new node with that key and insert. \# return None if key already exists in the tree. \# return the new node corresponding to the inserted key otherwise. def insert(self, key): \# your code here \# TODO: Complete algorithm to compute height of the tree \# height of a node whose children are both None is defined \# to be 1 . \# height of any other node is 1 + maximum of the height \# of its children. \# Return a number that is th eheight. def height (self): \# your code here \#TODO: Write an algorithm to delete a key in the tree. \# First, find the node in the tree with the key. \# Recommend drawing pictures to visualize these cases below before \# programming. \# Case 1: both children of the node are None \# .. in this case, deletion is easy: simply find out if the node with key is its \# parent's left/right child and set the corr. child to None in the parent node. \# Case 2: one of the child is None and the other is not. \# .- replace the node with its only child. In other words, \# modify the parent of the child to be the to be deleted node 's parent. \# also change the parent's left/right child appropriately. \# Case 3: both children of the parent are not None. \# .- first find its successor (go one step right and all the way to the left). \# .- function get_leftmost_descendant may be helpful here. \# -. replace the key of the node by its successor. \# .. delete the successor node. \# return: no return value specified def delete(self, key): (found, node_to_delete) = self.search ( key ) assert(found == True), f"key to be deleted: {key} - does not exist in the tree" \# your code here
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
