|
| 1 | + |
| 2 | +# Binary search trees are a data structure that enforce an ordering over |
| 3 | +# the data they store. That ordering in turn makes it a lot more efficient |
| 4 | +# at searching for a particular piece of data in the tree. |
| 5 | + |
| 6 | +# This part of the project comprises two days: |
| 7 | +# 1. Implement the methods `insert`, `contains`, `get_max`, and `for_each` |
| 8 | +# on the BSTNode class. |
| 9 | +# 2. Implement the `in_order_print`, `bft_print`, and `dft_print` methods |
| 10 | +# on the BSTNode class. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +class BSTNode: |
| 15 | + def __init__(self, value): |
| 16 | + self.value = value |
| 17 | + self.left = None |
| 18 | + self.right = None |
| 19 | + |
| 20 | + # Insert the given value into the tree |
| 21 | + def insert(self, value): |
| 22 | + # compare the value to the root's value to determine which direction |
| 23 | + # we're gonna go in |
| 24 | + # if the value < root's value |
| 25 | + if value < self.value: |
| 26 | + # go left |
| 27 | + # how do we go left? |
| 28 | + # we have to check if there is another node on the left side |
| 29 | + if self.left: |
| 30 | + # then self.left is a Node |
| 31 | + # now what? |
| 32 | + self.left.insert(value) |
| 33 | + else: |
| 34 | + # then we can park the value here |
| 35 | + self.left = BSTNode(value) |
| 36 | + # else the value >= root's value |
| 37 | + else: |
| 38 | + # go right |
| 39 | + # how do we go right? |
| 40 | + # we have to check if there is another node on the right side |
| 41 | + if self.right: |
| 42 | + # then self.right is a Node |
| 43 | + self.right.insert(value) |
| 44 | + else: |
| 45 | + self.right = BSTNode(value) |
| 46 | + |
| 47 | + # Return True if the tree contains the value |
| 48 | + # False if it does not |
| 49 | + def contains(self, target): |
| 50 | + currentNode = self |
| 51 | + # if current node is too big |
| 52 | + if currentNode.value > target: |
| 53 | + # go left |
| 54 | + if currentNode.left: |
| 55 | + # increment the currentNode |
| 56 | + currentNode = currentNode.left |
| 57 | + # return for recursion to work |
| 58 | + return currentNode.contains(target) |
| 59 | + elif currentNode.value < target: |
| 60 | + # go right |
| 61 | + if currentNode.right: |
| 62 | + # increment the currentNode |
| 63 | + currentNode = currentNode.right |
| 64 | + # return for recursion to work |
| 65 | + return currentNode.contains(target) |
| 66 | + elif currentNode.value == target: |
| 67 | + # we have a match |
| 68 | + return True |
| 69 | + # if no match found |
| 70 | + else: |
| 71 | + return False |
| 72 | + |
| 73 | + # Return the maximum value found in the tree |
| 74 | + |
| 75 | + def get_max(self): |
| 76 | + if not self.right: |
| 77 | + return self.value |
| 78 | + return self.right.get_max() |
| 79 | + |
| 80 | + # Call the function `fn` on the value of each node |
| 81 | + def for_each(self, fn): |
| 82 | + # current node |
| 83 | + currentNode = self |
| 84 | + fn(currentNode.value) |
| 85 | + if self.left: |
| 86 | + nextLeft = self.left |
| 87 | + nextLeft.for_each(fn) |
| 88 | + if self.right: |
| 89 | + nextRight = self.right |
| 90 | + nextRight.for_each(fn) |
| 91 | + |
| 92 | + # Part 2 ----------------------- |
| 93 | + |
| 94 | + # Print all the values in order from low to high |
| 95 | + # Hint: Use a recursive, depth first traversal |
| 96 | + |
| 97 | + def in_order_print(self, node=None): |
| 98 | + # everything will get printed... so.. |
| 99 | + |
| 100 | + # if self has a 'left', self is BIGGER |
| 101 | + if self.left: |
| 102 | + self.left.in_order_print(self) |
| 103 | + |
| 104 | + print(self.value) |
| 105 | + |
| 106 | + # if self has a 'right', self is SMALLER |
| 107 | + if self.right: |
| 108 | + self.right.in_order_print(self) |
| 109 | + |
| 110 | + # Print the value of every node, starting with the given node, |
| 111 | + # in an iterative breadth first traversal FIFO |
| 112 | + |
| 113 | + def bft_print(self, node=None): |
| 114 | + Que = [] |
| 115 | + Que.append(self) |
| 116 | + |
| 117 | + while len(Que) > 0: |
| 118 | + current = Que.pop(0) |
| 119 | + print(current.value) |
| 120 | + |
| 121 | + if current.left: |
| 122 | + Que.append(current.left) |
| 123 | + |
| 124 | + if current.right: |
| 125 | + Que.append(current.right) |
| 126 | + |
| 127 | + # Print the value of every node, starting with the given node, |
| 128 | + # in an iterative depth first traversal LIFO |
| 129 | + def dft_print(self, node=None): |
| 130 | + stack= [] |
| 131 | + stack.append(self) |
| 132 | + |
| 133 | + while len(stack) > 0: |
| 134 | + current= stack.pop(len(stack)-1) |
| 135 | + print(current.value) |
| 136 | + |
| 137 | + if current.right: |
| 138 | + stack.append(current.right) |
| 139 | + if current.left: |
| 140 | + stack.append(current.left) |
| 141 | + |
| 142 | + # Stretch Goals ------------------------- |
| 143 | + # Note: Research may be required |
| 144 | + |
| 145 | + # Print Pre-order recursive DFT |
| 146 | + def pre_order_dft(self, node): |
| 147 | + pass |
| 148 | + |
| 149 | + # Print Post-order recursive DFT |
| 150 | + def post_order_dft(self, node): |
| 151 | + pass |
| 152 | + |
| 153 | + |
| 154 | +# bst = BSTNode(1) |
| 155 | +# bst.insert(8) |
| 156 | +# bst.insert(5) |
| 157 | +# bst.insert(7) |
| 158 | +# bst.insert(6) |
| 159 | +# bst.insert(3) |
| 160 | +# bst.insert(4) |
| 161 | +# bst.insert(2) |
| 162 | + |
| 163 | +# bst.bft_print() |
0 commit comments