|
| 1 | +class BinaryTree(): |
| 2 | + def __init__(self, val, parent = None, left = None, right = None): |
| 3 | + self.parent = parent |
| 4 | + self.value = val |
| 5 | + self.left = left |
| 6 | + self.right = right |
| 7 | + |
| 8 | +def Recur_Search(root, val): |
| 9 | + if root.value == None or root.value == val: |
| 10 | + return root |
| 11 | + if val < root.value: |
| 12 | + return recur_search(root.left, val) |
| 13 | + else: |
| 14 | + return recur_search(root.right, val) |
| 15 | + |
| 16 | +def Iter_Search(root, val): |
| 17 | + while root.value != None and val != root.value: |
| 18 | + if val < root.value: |
| 19 | + root = root.left |
| 20 | + else: |
| 21 | + root = root.right |
| 22 | + return root |
| 23 | + |
| 24 | +def MinofTree(root): |
| 25 | + while root.left != None: |
| 26 | + root = root.left |
| 27 | + return root |
| 28 | + |
| 29 | +def Recur_MinofTree(root): |
| 30 | + if root == None or root.left == None: |
| 31 | + return root |
| 32 | + else: |
| 33 | + return Recur_MinofTree(root.left) |
| 34 | + |
| 35 | +def MaxofTree(root): |
| 36 | + while root.right != None: |
| 37 | + root = root.right |
| 38 | + return root |
| 39 | + |
| 40 | +def Recur_MaxofTree(root): |
| 41 | + if root == None or root.right == None: |
| 42 | + return root |
| 43 | + else: |
| 44 | + return Recur_MaxofTree(root.right) |
| 45 | + |
| 46 | +def InorderTreeWalk(root): |
| 47 | + if root != None: |
| 48 | + InorderTreeWalk(root.left) |
| 49 | + print(root.value, end=' ' ) |
| 50 | + InorderTreeWalk(root.right) |
| 51 | + |
| 52 | +# if the right subtree of node is nonempty, then the Successor |
| 53 | +# of x is the leftmost node in the right subtree, if the right |
| 54 | +# subtree of node is empty and x has a Successor y, then y is the |
| 55 | +# lowest ancestor of x whose left child is also an ancestor of x |
| 56 | +def Successor(root): |
| 57 | + if root.right != None: |
| 58 | + return MinofTree(root.right) |
| 59 | + suc = root.parent |
| 60 | + while suc != None and root == suc.right: |
| 61 | + root = suc |
| 62 | + suc = suc.parent |
| 63 | + return suc |
| 64 | + |
| 65 | +def Predecessor(root): |
| 66 | + if root.left != None: |
| 67 | + return MaxofTree(root.right) |
| 68 | + pre = root.parent |
| 69 | + while pre != None and root == pre.left: |
| 70 | + root = pre |
| 71 | + pre = pre.left |
| 72 | + return pre |
| 73 | + |
| 74 | +#def Recur_Insert(root, val): |
| 75 | +# if root == None: |
| 76 | +# key = BinaryTree(val) |
| 77 | +# root = key |
| 78 | +# elif val < root.value: |
| 79 | +# Recur_Insert(root.left, val) |
| 80 | +# print('left') |
| 81 | +# print(root.left) |
| 82 | +# else: |
| 83 | +# Recur_Insert(root.right, val) |
| 84 | +# print('right') |
| 85 | + |
| 86 | +def TreeInsert(root, val): |
| 87 | + key = BinaryTree(val) |
| 88 | + p = None |
| 89 | + while root != None: |
| 90 | + p = root |
| 91 | + if key.value < root.value: |
| 92 | + root = root.left |
| 93 | + else: |
| 94 | + root = root.right |
| 95 | + key.parent = p |
| 96 | + if p == None: |
| 97 | + root = key |
| 98 | + elif key.value < p.value: |
| 99 | + p.left = key |
| 100 | + else: |
| 101 | + p.right = key |
| 102 | + |
| 103 | +def TreeDelete(root, val): |
| 104 | + key = Iter_Search(root, val) |
| 105 | + if key == None: |
| 106 | + return None |
| 107 | + d = None |
| 108 | + if key.left == None or key.right == None: |
| 109 | + d = key |
| 110 | + else: |
| 111 | + d = Successor(key) |
| 112 | + if d.left != None: |
| 113 | + cd = d.left |
| 114 | + else: |
| 115 | + cd = d.right |
| 116 | + if cd != None: |
| 117 | + cd.parent = d.parent |
| 118 | + if d.parent == None: |
| 119 | + root = cd |
| 120 | + elif d == d.parent.left: |
| 121 | + d.parent.left = x |
| 122 | + else: |
| 123 | + d.parent.right = x |
| 124 | + if d != key: |
| 125 | + key.value = d.value |
| 126 | + return d |
| 127 | + |
| 128 | +L = [3, 5, 6, 7, 10, 12, 13, 16, 18, 20, 23] |
| 129 | +root = BinaryTree(15) |
| 130 | +for i in L: |
| 131 | +# Recur_Insert(root, i) |
| 132 | + TreeInsert(root, i) |
| 133 | +InorderTreeWalk(root) |
| 134 | +print(root.left.value) |
| 135 | +print(Successor(root.left).value) |
| 136 | +print(Predecessor(root.left.right).value) |
| 137 | +print(MinofTree(root).value) |
| 138 | +print(MaxofTree(root).value) |
| 139 | +print(Recur_MinofTree(root).value) |
| 140 | +print(Recur_MaxofTree(root).value) |
0 commit comments