|
| 1 | +import sys |
| 2 | +sys.path.append('../queue_and_stack') |
| 3 | +from dll_queue import Queue |
| 4 | +from dll_stack import Stack |
| 5 | + |
| 6 | + |
| 7 | +class BinarySearchTree: |
| 8 | + def __init__(self, value): |
| 9 | + self.value = value #root |
| 10 | + self.left = None |
| 11 | + self.right = None |
| 12 | + self.stack = Stack() |
| 13 | + self.queue = Queue() |
| 14 | + |
| 15 | + # Insert the given value into the tree |
| 16 | + def insert(self, value): |
| 17 | + # pass |
| 18 | + if value < self.value: |
| 19 | + if self.left is None: |
| 20 | + self.left = BinarySearchTree(value) |
| 21 | + else: |
| 22 | + self.left.insert(value) |
| 23 | + else: |
| 24 | + if self.right is None: |
| 25 | + self.right = BinarySearchTree(value) |
| 26 | + else: |
| 27 | + self.right.insert(value) |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + # Return True if the tree contains the value |
| 32 | + # False if it does not |
| 33 | + def contains(self, target): |
| 34 | + # pass |
| 35 | + if self.value == target: |
| 36 | + return True |
| 37 | + |
| 38 | + if self.left is None and self.right is None: |
| 39 | + return False |
| 40 | + |
| 41 | + if target < self.value and self.left is not None: |
| 42 | + return self.left.contains(target) |
| 43 | + elif self.right is not None: |
| 44 | + return self.right.contains(target) |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + # Return the maximum value found in the tree |
| 49 | + def get_max(self): |
| 50 | + # pass |
| 51 | + if self.value is None: |
| 52 | + return None |
| 53 | + |
| 54 | + if self.right is None: |
| 55 | + return self.value |
| 56 | + else: |
| 57 | + return self.right.get_max() |
| 58 | + |
| 59 | + # Call the function `cb` on the value of each node |
| 60 | + # You may use a recursive or iterative approach |
| 61 | + def for_each(self, cb): |
| 62 | + # pass |
| 63 | + if self.value is None: |
| 64 | + return None |
| 65 | + cb(self.value) |
| 66 | + if self.right is not None: |
| 67 | + self.right.for_each(cb) |
| 68 | + if self.left is not None: |
| 69 | + self.left.for_each(cb) |
| 70 | + |
| 71 | + # DAY 2 Project ----------------------- |
| 72 | + |
| 73 | + # Print all the values in order from low to high |
| 74 | + # Hint: Use a recursive, depth first traversal |
| 75 | + def in_order_print(self, node): |
| 76 | + # pass |
| 77 | + current_node = node |
| 78 | + print(current_node.value) |
| 79 | + if current_node.right is not None: |
| 80 | + self.stack.push(current_node.right) |
| 81 | + elif current_node.left is not None: |
| 82 | + self.stack.push(current_node.left) |
| 83 | + |
| 84 | + if not self.stack.len(): |
| 85 | + return |
| 86 | + self.in_order_print(self.stack.pop()) |
| 87 | + |
| 88 | + # Print the value of every node, starting with the given node, |
| 89 | + # in an iterative breadth first traversal |
| 90 | + def bft_print(self, node): |
| 91 | + # pass |
| 92 | + # use a queue data structure |
| 93 | + current_node = node |
| 94 | + # enqueue the starting node on to the queue |
| 95 | + self.queue.enqueue(current_node) |
| 96 | + # loop while the queue has data |
| 97 | + while self.queue.len() > 0: |
| 98 | + # dequeue the current it em off the queue |
| 99 | + self.queue.dequeue() |
| 100 | + # print the current value |
| 101 | + print(current_node) |
| 102 | + # if the current node has a left child |
| 103 | + if current_node.left: |
| 104 | + # enqueue the left child on to the queue |
| 105 | + self.queue.enqueue(current_node.left) |
| 106 | + # if the current node has a right child |
| 107 | + if current_node.right: |
| 108 | + # enqueue right child on to the queue |
| 109 | + self.queue.enqueue(current_node.right) |
| 110 | + |
| 111 | + # Print the value of every node, starting with the given node, |
| 112 | + # in an iterative depth first traversal |
| 113 | + def dft_print(self, node): |
| 114 | + # pass |
| 115 | + # pass |
| 116 | + # use a queue data structure |
| 117 | + current_node = node |
| 118 | + # enqueue the starting node on to the queue |
| 119 | + self.stack.push(current_node) |
| 120 | + # loop while the queue has data |
| 121 | + while self.stack.len() > 0: |
| 122 | + # dequeue the current it em off the queue |
| 123 | + self.stack.pop() |
| 124 | + # print the current value |
| 125 | + print(current_node) |
| 126 | + # if the current node has a left child |
| 127 | + if current_node.left: |
| 128 | + # enqueue the left child on to the queue |
| 129 | + self.stack.push(current_node.left) |
| 130 | + # if the current node has a right child |
| 131 | + if current_node.right: |
| 132 | + # enqueue right child on to the queue |
| 133 | + self.stack.push(current_node.right) |
| 134 | + |
| 135 | + |
| 136 | + # STRETCH Goals ------------------------- |
| 137 | + # Note: Research may be required |
| 138 | + |
| 139 | + # Print In-order recursive DFT |
| 140 | + def pre_order_dft(self, node): |
| 141 | + pass |
| 142 | + |
| 143 | + # Print Post-order recursive DFT |
| 144 | + def post_order_dft(self, node): |
| 145 | + pass |
0 commit comments