|
| 1 | +''' |
| 2 | +A binary search Tree |
| 3 | +''' |
| 4 | + |
| 5 | +class Node: |
| 6 | + |
| 7 | + def __init__(self, label): |
| 8 | + self.label = label |
| 9 | + self.left = None |
| 10 | + self.right = None |
| 11 | + |
| 12 | + def getLabel(self): |
| 13 | + return self.label |
| 14 | + |
| 15 | + def setLabel(self, label): |
| 16 | + self.label = label |
| 17 | + |
| 18 | + def getLeft(self): |
| 19 | + return self.left |
| 20 | + |
| 21 | + def setLeft(self, left): |
| 22 | + self.left = left |
| 23 | + |
| 24 | + def getRight(self): |
| 25 | + return self.right |
| 26 | + |
| 27 | + def setRight(self, right): |
| 28 | + self.right = right |
| 29 | + |
| 30 | + |
| 31 | +class BinarySearchTree: |
| 32 | + |
| 33 | + def __init__(self): |
| 34 | + self.root = None |
| 35 | + |
| 36 | + def insert(self, label): |
| 37 | + # Create a new Node |
| 38 | + new_node = Node(label) |
| 39 | + # If Tree is empty |
| 40 | + if self.empty(): |
| 41 | + self.root = new_node |
| 42 | + else: |
| 43 | + #If Tree is not empty |
| 44 | + parent_node = None |
| 45 | + curr_node = self.root |
| 46 | + #While we don't get to a leaf |
| 47 | + while curr_node is not None: |
| 48 | + #We keep reference of the parent node |
| 49 | + parent_node = curr_node |
| 50 | + #If node label is less than current node |
| 51 | + if new_node.getLabel() < curr_node.getLabel(): |
| 52 | + #We go left |
| 53 | + curr_node = curr_node.getLeft() |
| 54 | + else: |
| 55 | + #Else we go right |
| 56 | + curr_node = curr_node.getRight() |
| 57 | + #We insert the new node in a leaf |
| 58 | + if new_node.getLabel() < parent_node.getLabel(): |
| 59 | + parent_node.setLeft(new_node) |
| 60 | + else: |
| 61 | + parent_node.setRight(new_node) |
| 62 | + |
| 63 | + def getNode(self, label): |
| 64 | + curr_node = None |
| 65 | + #If the tree is not empty |
| 66 | + if(not self.empty()): |
| 67 | + #Get tree root |
| 68 | + curr_node = self.getRoot() |
| 69 | + #While we don't find the node we look for |
| 70 | + #I am using lazy evaluation here to avoid NoneType Attribute error |
| 71 | + while curr_node is not None and curr_node.getLabel() is not label: |
| 72 | + #If node label is less than current node |
| 73 | + if label < curr_node.getLabel(): |
| 74 | + #We go left |
| 75 | + curr_node = curr_node.getLeft() |
| 76 | + else: |
| 77 | + #Else we go right |
| 78 | + curr_node = curr_node.getRight() |
| 79 | + return curr_node |
| 80 | + |
| 81 | + def empty(self): |
| 82 | + if self.root is None: |
| 83 | + return True |
| 84 | + return False |
| 85 | + |
| 86 | + def preShow(self, curr_node): |
| 87 | + if curr_node is not None: |
| 88 | + print(curr_node.getLabel()) |
| 89 | + self.preShow(curr_node.getLeft()) |
| 90 | + self.preShow(curr_node.getRight()) |
| 91 | + |
| 92 | + def getRoot(self): |
| 93 | + return self.root |
| 94 | + |
| 95 | +''' |
| 96 | +Example |
| 97 | + 8 |
| 98 | + / \ |
| 99 | + 3 10 |
| 100 | + / \ \ |
| 101 | + 1 6 14 |
| 102 | + / \ / |
| 103 | + 4 7 13 |
| 104 | +''' |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + t = BinarySearchTree() |
| 109 | + t.insert(8) |
| 110 | + t.insert(3) |
| 111 | + t.insert(1) |
| 112 | + t.insert(6) |
| 113 | + t.insert(4) |
| 114 | + t.insert(7) |
| 115 | + t.insert(10) |
| 116 | + t.insert(14) |
| 117 | + t.insert(13) |
| 118 | + |
| 119 | + t.preShow(t.getRoot()) |
| 120 | + |
| 121 | + if(t.getNode(6) is not None): |
| 122 | + print("The label 6 exists") |
| 123 | + else: |
| 124 | + print("The label 6 doesn't exist") |
| 125 | + |
| 126 | + if(t.getNode(-1) is not None): |
| 127 | + print("The label -1 exists") |
| 128 | + else: |
| 129 | + print("The label -1 doesn't exist") |
| 130 | + |
0 commit comments