Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help writing Python code for Binary Search Tree implementation. I wrote the Queue class we are supposed to use for creating the BST. We
Need help writing Python code for Binary Search Tree implementation. I wrote the Queue class we are supposed to use for creating the BST. We were also told that we can/should use helper methods that make our code easier to write, read, and understand. We can use both iterative and recursive functions. _______________________________________________________________________________________________________________ class Queue: '''Implements an array-based, efficient first-in first-out Abstract Data Type using a Python array (faked using a List)''' def __init__(self, capacity): '''Creates an empty Queue with a capacity''' self.queue = [None] * capacity self.capacity = capacity self.numitems = self.front = self.back = 0 def is_empty(self): '''Returns True if the Queue is empty, and False otherwise''' return self.numitems == 0 def is_full(self): '''Returns True if the Queue is full, and False otherwise''' return self.numitems == self.capacity def enqueue(self, item): '''If Queue is not full, enqueues (adds) item to Queue If Queue is full when enqueue is attempted, raises IndexError''' if Queue.is_full(self): raise IndexError else: self.queue[self.back] = item self.numitems += 1 self.back += 1 Queue.indexcheck(self) def dequeue(self): '''If Queue is not empty, dequeues (removes) item from Queue and returns item. If Queue is empty when dequeue is attempted, raises IndexError''' if Queue.is_empty(self): raise IndexError else: item = self.queue[self.front] self.queue[self.front] = None self.numitems -= 1 self.front += 1 Queue.indexcheck(self) return item def size(self): '''Returns the number of elements currently in the Queue, not the capacity''' return self.numitems # functions added by student: def indexcheck(self): '''If the front or back reach the end of the stack, returns them to the beginning in order to simulate a "wrap around" effect.''' if self.front == self.capacity: self.front = 0 if self.back == self.capacity: self.back = 0
__________________________________________________________________________________________________________________________ NEED HELP WITH FOLLOWING CODE: __________________________________________________________________________________________________________________________ from queue_array import Queue class TreeNode: def __init__(self, key, data, left=None, right=None): self.key = key self.data = data self.left = left self.right = right class BinarySearchTree: def __init__(self): # Returns empty BST self.root = None def is_empty(self): # returns True if tree is empty, else False pass def search(self, key): # returns True if key is in a node of the tree, else False pass def insert(self, key, data=None): # inserts new node w/ key and data # If an item with the given key is already in the BST, # the data in the tree will be replaced with the new data # Example creation of node: temp = TreeNode(key, data) pass def find_min(self): # returns a tuple with min key and data in the BST # returns None if the tree is empty pass def find_max(self): # returns a tuple with max key and data in the BST # returns None if the tree is empty pass def tree_height(self): # return the height of the tree # returns None if tree is empty pass def inorder_list(self): # return Python list of BST keys representing in-order traversal of BST pass def preorder_list(self): # return Python list of BST keys representing pre-order traversal of BST pass def level_order_list(self): # return Python list of BST keys representing level-order traversal of BST # You MUST use your queue_array data structure to implement this method q = Queue(25000) # Don't change this! pass
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started