|
| 1 | +# Binary search tree |
| 2 | + |
| 3 | +class BSTNode: |
| 4 | + def __init__(self, dat): |
| 5 | + self.data = dat |
| 6 | + self.parent = None |
| 7 | + self.left = None |
| 8 | + self.right = None |
| 9 | + |
| 10 | + def insert(self, dat): |
| 11 | + if dat < self.data: |
| 12 | + if not self.left: |
| 13 | + n = BSTNode(dat) |
| 14 | + self.left = n |
| 15 | + n.parent = self |
| 16 | + else: |
| 17 | + self.left.insert(dat) |
| 18 | + else: |
| 19 | + if not self.right: |
| 20 | + n = BSTNode(dat) |
| 21 | + self.right = n |
| 22 | + n.parent = self |
| 23 | + else: |
| 24 | + self.right.insert(dat) |
| 25 | + |
| 26 | + def pretty_print(self, level): |
| 27 | + print ' ' * level + str(self.data), '(h=%d)' % self.height() |
| 28 | + if self.left: |
| 29 | + self.left.pretty_print(level + 1) |
| 30 | + if self.right: |
| 31 | + self.right.pretty_print(level + 1) |
| 32 | + |
| 33 | + def search(self, dat): |
| 34 | + if self.data == dat: |
| 35 | + return self |
| 36 | + elif dat < self.data: |
| 37 | + if self.left: |
| 38 | + return self.left.search(dat) |
| 39 | + else: |
| 40 | + return None |
| 41 | + else: |
| 42 | + assert dat > self.data |
| 43 | + if self.right: |
| 44 | + return self.right.search(dat) |
| 45 | + else: |
| 46 | + return None |
| 47 | + |
| 48 | + def search_by_rank(self, r): |
| 49 | + if self.rank == r: |
| 50 | + return self |
| 51 | + elif r < self.rank: |
| 52 | + if self.left: |
| 53 | + return self.left.search_by_rank(r) |
| 54 | + else: |
| 55 | + return None |
| 56 | + else: |
| 57 | + assert r > self.rank |
| 58 | + if self.right: |
| 59 | + return self.right.search_by_rank(r) |
| 60 | + else: |
| 61 | + return None |
| 62 | + |
| 63 | + def predecessor(self): |
| 64 | + if self.left: |
| 65 | + p = self.left |
| 66 | + while p.right: |
| 67 | + p = p.right |
| 68 | + return p |
| 69 | + else: |
| 70 | + p = self.parent |
| 71 | + while p and p.data > self.data: |
| 72 | + p = p.parent |
| 73 | + return p |
| 74 | + |
| 75 | + def successor(self): |
| 76 | + if self.right: |
| 77 | + s = self.right |
| 78 | + while s.left: |
| 79 | + s = s.left |
| 80 | + return s |
| 81 | + else: |
| 82 | + p = self.parent |
| 83 | + while p and p.data < self.data: |
| 84 | + p = p.parent |
| 85 | + return p |
| 86 | + |
| 87 | + def preorder(self, callback): |
| 88 | + callback(self) |
| 89 | + if self.left: |
| 90 | + self.left.preorder(callback) |
| 91 | + if self.right: |
| 92 | + self.right.preorder(callback) |
| 93 | + |
| 94 | + def inorder(self, callback): |
| 95 | + if self.left: |
| 96 | + self.left.inorder(callback) |
| 97 | + callback(self) |
| 98 | + if self.right: |
| 99 | + self.right.inorder(callback) |
| 100 | + |
| 101 | + def delete(self): |
| 102 | + if self.left and self.right: |
| 103 | + s = self.successor() |
| 104 | + # gypsy switch! |
| 105 | + self.data = s.data |
| 106 | + s.delete() |
| 107 | + elif self.left: |
| 108 | + l_child = self.left |
| 109 | + if self.parent.left == self: |
| 110 | + self.parent.left = l_child |
| 111 | + l_child.parent = self.parent |
| 112 | + elif self.parent.right == self: |
| 113 | + self.parent.right = l_child |
| 114 | + l_child.parent = self.parent |
| 115 | + elif self.right: |
| 116 | + r_child = self.right |
| 117 | + if self.parent.left == self: |
| 118 | + self.parent.left = r_child |
| 119 | + r_child.parent = self.parent |
| 120 | + elif self.parent.right == self: |
| 121 | + self.parent.right = r_child |
| 122 | + r_child.parent = self.parent |
| 123 | + else: |
| 124 | + # no children |
| 125 | + if self.parent: |
| 126 | + if self.parent.left == self: |
| 127 | + self.parent.left = None |
| 128 | + elif self.parent.right == self: |
| 129 | + self.parent.right = None |
| 130 | + |
| 131 | + def height(self): |
| 132 | + if not self.left and not self.right: |
| 133 | + return 0 |
| 134 | + else: |
| 135 | + l_height = r_height = 0 |
| 136 | + if self.left: |
| 137 | + l_height = self.left.height() |
| 138 | + if self.right: |
| 139 | + r_height = self.right.height() |
| 140 | + return 1 + max(l_height, r_height) |
| 141 | + |
| 142 | + def mirror(self): |
| 143 | + n = BSTNode(self.data) |
| 144 | + if self.right: |
| 145 | + n.left = self.right.mirror() |
| 146 | + n.left.parent = n |
| 147 | + if self.left: |
| 148 | + n.right = self.left.mirror() |
| 149 | + n.right.parent = n |
| 150 | + return n |
| 151 | + |
| 152 | + |
| 153 | +tree = BSTNode(10) |
| 154 | +tree.insert(2) |
| 155 | +tree.insert(4) |
| 156 | +tree.insert(1) |
| 157 | +tree.insert(5) |
| 158 | +tree.insert(15) |
| 159 | +tree.insert(12) |
| 160 | + |
| 161 | +tree.pretty_print(0) |
| 162 | + |
| 163 | +tree.insert(13) |
| 164 | +print '---' |
| 165 | +tree.pretty_print(0) |
| 166 | + |
| 167 | +print '--- MIRROR ---' |
| 168 | +tree.mirror().pretty_print(0) |
| 169 | + |
| 170 | + |
| 171 | +def search_check(n): |
| 172 | + assert tree.search(n.data) == n |
| 173 | + |
| 174 | +tree.preorder(search_check) |
| 175 | + |
| 176 | +print '---' |
| 177 | +def print_successor(n): |
| 178 | + s = n.successor() |
| 179 | + if s: |
| 180 | + print n.data, 'SUCCESSOR:', s.data |
| 181 | + else: |
| 182 | + print n.data, 'NO SUCCESSOR' |
| 183 | + |
| 184 | +tree.preorder(print_successor) |
| 185 | + |
| 186 | +print '---' |
| 187 | +def print_predecessor(n): |
| 188 | + p = n.predecessor() |
| 189 | + if p: |
| 190 | + print n.data, 'PREDECESSOR:', p.data |
| 191 | + else: |
| 192 | + print n.data, 'NO PREDECESSOR' |
| 193 | + |
| 194 | +tree.preorder(print_predecessor) |
| 195 | + |
| 196 | +def pred_succ_check(n): |
| 197 | + p = n.predecessor() |
| 198 | + if p: |
| 199 | + assert p.successor() == n |
| 200 | + s = n.successor() |
| 201 | + if s: |
| 202 | + assert s.predecessor() == n |
| 203 | + print n.data, "passed pred_succ_check!" |
| 204 | + |
| 205 | +print '---' |
| 206 | +tree.preorder(pred_succ_check) |
| 207 | + |
| 208 | +rank = 0 |
| 209 | +def augment_with_rank(n): |
| 210 | + global rank |
| 211 | + n.rank = rank |
| 212 | + rank += 1 |
| 213 | + |
| 214 | +tree.inorder(augment_with_rank) |
| 215 | +print '--- RANKS ---' |
| 216 | +def print_with_ranks(n): |
| 217 | + print n.data, 'RANK:', n.rank |
| 218 | + |
| 219 | +tree.preorder(print_with_ranks) |
| 220 | + |
| 221 | +print '--- SEARCH BY RANK ---' |
| 222 | +for r in xrange(rank): |
| 223 | + print r, tree.search_by_rank(r).data |
| 224 | + |
| 225 | +print '---' |
| 226 | +tree.search(1).delete() |
| 227 | +tree.pretty_print(0) |
| 228 | +print '---' |
| 229 | +tree.search(2).delete() |
| 230 | +tree.pretty_print(0) |
| 231 | +print '---' |
| 232 | +tree.search(5).delete() |
| 233 | +tree.pretty_print(0) |
| 234 | +print '---' |
| 235 | +tree.search(10).delete() |
| 236 | +tree.pretty_print(0) |
| 237 | +print '---' |
| 238 | +tree.search(15).delete() |
| 239 | +tree.pretty_print(0) |
| 240 | +print '---' |
| 241 | +tree.search(12).delete() |
| 242 | +tree.pretty_print(0) |
| 243 | +print '---' |
| 244 | +tree.search(4).delete() |
| 245 | +tree.pretty_print(0) |
| 246 | + |
| 247 | + |
0 commit comments