|
| 1 | +""" |
| 2 | +包括生成树,二叉搜索树的前后中遍历。 |
| 3 | +
|
| 4 | +二叉搜索树在比较优质的情况下搜索和插入时间都是 O(logn) 的。在极端情况下会退化为链表 O(n)。 |
| 5 | +将无序的链表塞进二叉搜索树里,按左根右中序遍历出来就是排序好的有序链表。 |
| 6 | +
|
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +# 生成树操作。 |
| 11 | + |
| 12 | +class TreeNode(object): |
| 13 | + |
| 14 | + def __init__(self , val, left=None, right=None): |
| 15 | + |
| 16 | + self.val = val |
| 17 | + self.left = left |
| 18 | + self.right = right |
| 19 | + |
| 20 | + |
| 21 | +class binarySearchTree(object): |
| 22 | + """ |
| 23 | + 二叉搜索树, |
| 24 | + 它的性质是左边节点都小于根节点,右边的都大于根节点。 |
| 25 | + 而且一般来说它是不存在重复元素的。 |
| 26 | +
|
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, root): |
| 30 | + if isinstance(root, TreeNode): |
| 31 | + print(1) |
| 32 | + self.root = root |
| 33 | + else: |
| 34 | + self.root = TreeNode(root) |
| 35 | + |
| 36 | + def add(self, value): |
| 37 | + # 从顶点开始遍历,找寻其合适的位置。 |
| 38 | + root = self.root |
| 39 | + while 1: |
| 40 | + if root.val < value: |
| 41 | + if root.right is None: |
| 42 | + if self.search(value): |
| 43 | + break |
| 44 | + root.right = TreeNode(value) |
| 45 | + break |
| 46 | + else: |
| 47 | + root = root.right |
| 48 | + continue |
| 49 | + |
| 50 | + if root.val > value: |
| 51 | + if root.left is None: |
| 52 | + if self.search(value): |
| 53 | + break |
| 54 | + root.left = TreeNode(value) |
| 55 | + break |
| 56 | + else: |
| 57 | + root = root.left |
| 58 | + continue |
| 59 | + |
| 60 | + if root.val == value: |
| 61 | + break |
| 62 | + |
| 63 | + def search(self, value): |
| 64 | + # 查找一个值是否存在于这颗树中。 |
| 65 | + return self._search(self.root, value) |
| 66 | + |
| 67 | + def _search(self, root, value): |
| 68 | + if root.val == value: |
| 69 | + return True |
| 70 | + |
| 71 | + if root.right: |
| 72 | + if root.val < value: |
| 73 | + return self._search(root.right, value) |
| 74 | + |
| 75 | + if root.left: |
| 76 | + if root.val > value: |
| 77 | + return self._search(root.left, value) |
| 78 | + |
| 79 | + return False |
| 80 | + |
| 81 | + def delete(self): |
| 82 | + pass |
| 83 | + |
| 84 | + def prevPrint(self): |
| 85 | + # 根左右 |
| 86 | + nodes = [self.root] |
| 87 | + result = [] |
| 88 | + while 1: |
| 89 | + if not nodes: |
| 90 | + return result |
| 91 | + node = nodes.pop() |
| 92 | + result.append(node.val) |
| 93 | + |
| 94 | + if node.right: |
| 95 | + nodes.append(node.right) |
| 96 | + |
| 97 | + if node.left: |
| 98 | + nodes.append(node.left) |
| 99 | + |
| 100 | + def _middlePrint(self, root, result): |
| 101 | + if root.left: |
| 102 | + self._middlePrint(root.left, result) |
| 103 | + |
| 104 | + result.append(root.val) |
| 105 | + |
| 106 | + if root.right: |
| 107 | + self._middlePrint(root.right,result) |
| 108 | + |
| 109 | + def middlePrint(self): |
| 110 | + # 左根右 |
| 111 | + result = [] |
| 112 | + self._middlePrint(self.root, result) |
| 113 | + |
| 114 | + return result |
| 115 | + |
| 116 | + def _suffPrint(self, root, result): |
| 117 | + if root.left: |
| 118 | + self._suffPrint(root.left, result) |
| 119 | + |
| 120 | + if root.right: |
| 121 | + self._suffPrint(root.right,result) |
| 122 | + |
| 123 | + result.append(root.val) |
| 124 | + |
| 125 | + def suffPrint(self): |
| 126 | + # 左右根 |
| 127 | + result = [] |
| 128 | + self._suffPrint(self.root, result) |
| 129 | + |
| 130 | + return result |
| 131 | + |
| 132 | + |
| 133 | +oneTree = binarySearchTree(5) |
| 134 | + |
| 135 | +for i in range(-5, 10): |
| 136 | + oneTree.add(i) |
| 137 | + |
| 138 | +print(oneTree.middlePrint()) |
| 139 | +print(oneTree.suffPrint()) |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
0 commit comments