|
| 1 | +class BinarySearchTree: |
| 2 | + class Node: |
| 3 | + def __init__(self, value): |
| 4 | + self.left = None |
| 5 | + self.right = None |
| 6 | + self.value = value |
| 7 | + |
| 8 | + def search(self, root, key): |
| 9 | + if root is None or root.value == key: |
| 10 | + return root |
| 11 | + |
| 12 | + if root.value < key: |
| 13 | + return self.search(root.right, key) |
| 14 | + |
| 15 | + return self.search(root.left, key) |
| 16 | + |
| 17 | + def insert(self, root, node): |
| 18 | + if root is None: |
| 19 | + root = node |
| 20 | + else: |
| 21 | + if root.value < node.value: |
| 22 | + if root.right is None: |
| 23 | + root.right = node |
| 24 | + else: |
| 25 | + self.insert(root.right, node) |
| 26 | + else: |
| 27 | + if root.left is None: |
| 28 | + root.left = node |
| 29 | + else: |
| 30 | + self.insert(root.left, node) |
| 31 | + |
| 32 | + def in_order(self, root): |
| 33 | + if root: |
| 34 | + self.in_order(root.left) |
| 35 | + print(root.value) |
| 36 | + self.in_order(root.right) |
| 37 | + |
| 38 | + def pre_order(self, root): |
| 39 | + if root: |
| 40 | + print(root.value) |
| 41 | + self.pre_order(root.left) |
| 42 | + self.pre_order(root.right) |
| 43 | + |
| 44 | + def post_order(self, root): |
| 45 | + if root: |
| 46 | + self.post_order(root.left) |
| 47 | + self.post_order(root.right) |
| 48 | + print(root.value) |
| 49 | + |
| 50 | + def min_value_node(self, node): |
| 51 | + current = node |
| 52 | + while current.left is not None: |
| 53 | + current = current.left |
| 54 | + return current |
| 55 | + |
| 56 | + def max_value_node(self, node): |
| 57 | + current = node |
| 58 | + while current.right is not None: |
| 59 | + current = current.right |
| 60 | + return current |
| 61 | + |
| 62 | + def delete_node(self, root, val): |
| 63 | + if root is None: |
| 64 | + return root |
| 65 | + |
| 66 | + if val < root.value: |
| 67 | + root.left = self.delete_node(root.left, val) |
| 68 | + |
| 69 | + elif val > root.value: |
| 70 | + root.right = self.delete_node(root.right, val) |
| 71 | + |
| 72 | + else: |
| 73 | + if root.left is None: |
| 74 | + temp = root.right |
| 75 | + root = None |
| 76 | + return temp |
| 77 | + |
| 78 | + elif root.right is None: |
| 79 | + temp = root.left |
| 80 | + root = None |
| 81 | + return temp |
| 82 | + |
| 83 | + temp = self.min_value_node(root.right) |
| 84 | + root.value = temp.value |
| 85 | + root.right = self.delete_node(root.right, temp.value) |
| 86 | + |
| 87 | + return root |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + x = BinarySearchTree() |
| 92 | + root = BinarySearchTree.Node(10) |
| 93 | + x.insert(root, BinarySearchTree.Node(7)) |
| 94 | + x.insert(root, BinarySearchTree.Node(12)) |
| 95 | + x.insert(root, BinarySearchTree.Node(5)) |
| 96 | + x.insert(root, BinarySearchTree.Node(18)) |
| 97 | + x.insert(root, BinarySearchTree.Node(13)) |
| 98 | + x.in_order(root) |
| 99 | + print() |
| 100 | + x.delete_node(root, 12) |
| 101 | + x.in_order(root) |
0 commit comments