| 294 | self.root = del_node(self.root, data) |
| 295 | |
| 296 | def __str__( |
| 297 | self, |
| 298 | ) -> str: # a level traversale, gives a more intuitive look on the tree |
| 299 | output = "" |
| 300 | q = MyQueue() |
| 301 | q.push(self.root) |
| 302 | layer = self.get_height() |
| 303 | if layer == 0: |
| 304 | return output |
| 305 | cnt = 0 |
| 306 | while not q.is_empty(): |
| 307 | node = q.pop() |
| 308 | space = " " * int(math.pow(2, layer - 1)) |
| 309 | output += space |
| 310 | if node is None: |
| 311 | output += "*" |
| 312 | q.push(None) |
| 313 | q.push(None) |
| 314 | else: |
| 315 | output += str(node.get_data()) |
| 316 | q.push(node.get_left()) |
| 317 | q.push(node.get_right()) |
| 318 | output += space |
| 319 | cnt = cnt + 1 |
| 320 | for i in range(100): |
| 321 | if cnt == math.pow(2, i) - 1: |
| 322 | layer = layer - 1 |
| 323 | if layer == 0: |
| 324 | output += "\n*************************************" |
| 325 | return output |
| 326 | output += "\n" |
| 327 | break |
| 328 | output += "\n*************************************" |
| 329 | return output |
| 330 | |
| 331 | |
| 332 | def _test() -> None: |