Question: Hi, I can't figure out the is_perfect function. Here is the method. The Data Structures that you can use are below. Note: YOU CAN ONLY
Hi, I can't figure out the is_perfect function. Here is the method. The Data Structures that you can use are below. Note: YOU CAN ONLY USE THE DATA STRUCTURES PROVIDED (no using python Lists or anything):
def is_perfect(self) -> bool: """ Returns True if the current tree is a perfect binary tree. Empty tree is considered perfect. Tree consisting of a single root node is perfect """ Code here
# ------------------------------------------------------------------ #
class Stack: """ Class implementing STACK ADT. Supported methods are: push, pop, top, is_empty DO NOT CHANGE THIS CLASS IN ANY WAY YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION """ def __init__(self): """ Initialize empty stack based on Python list """ self._data = [] def push(self, value: object) -> None: """ Add new element on top of the stack """ self._data.append(value) def pop(self) -> object: """ Remove element from top of the stack and return its value """ return self._data.pop() def top(self) -> object: """ Return value of top element without removing from stack """ return self._data[-1] def is_empty(self): """ Return True if the stack is empty, return False otherwise """ return len(self._data) == 0 def __str__(self): """ Return content of the stack as a string (for use with print) """ data_str = [str(i) for i in self._data] return "STACK: { " + ", ".join(data_str) + " }" class Queue: """ Class implementing QUEUE ADT. Supported methods are: enqueue, dequeue, is_empty DO NOT CHANGE THIS CLASS IN ANY WAY YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION """ def __init__(self): """ Initialize empty queue based on Python list """ self._data = [] def enqueue(self, value: object) -> None: """ Add new element to the end of the queue """ self._data.append(value) def dequeue(self) -> object: """ Remove element from the beginning of the queue and return its value """ return self._data.pop(0) def is_empty(self): """ Return True if the queue is empty, return False otherwise """ return len(self._data) == 0 def __str__(self): """ Return content of the stack as a string (for use with print) """ data_str = [str(i) for i in self._data] return "QUEUE { " + ", ".join(data_str) + " }" class TreeNode: """ Binary Search Tree Node class DO NOT CHANGE THIS CLASS IN ANY WAY """ def __init__(self, value: object) -> None: """ Init new Binary Search Tree DO NOT CHANGE THIS METHOD IN ANY WAY """ self.value = value # to store node's data self.left = None # pointer to root of left subtree self.right = None # pointer to root of right subtree def __str__(self): return str(self.value) class BST: def __init__(self, start_tree=None) -> None: """ Init new Binary Search Tree DO NOT CHANGE THIS METHOD IN ANY WAY """ self.root = None # populate BST with initial values (if provided) # before using this feature, implement add() method if start_tree is not None: for value in start_tree: self.add(value) def __str__(self) -> str: """ Return content of BST in human-readable form using in-order traversal DO NOT CHANGE THIS METHOD IN ANY WAY """ values = [] self._str_helper(self.root, values) return "TREE pre-order { " + ", ".join(values) + " }" def _str_helper(self, cur, values): """ Helper method for __str__. Does pre-order tree traversal DO NOT CHANGE THIS METHOD IN ANY WAY """ # base case if not cur: return # store value of current node values.append(str(cur.value)) # recursive case for left subtree self._str_helper(cur.left, values) # recursive case for right subtree self._str_helper(cur.right, values)
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
