|
| 1 | +from typing import Optional, List |
| 2 | +from datastructures.trees.binary.search_tree import BinarySearchTree |
| 3 | +from datastructures.trees.binary.node import BinaryTreeNode |
| 4 | +from datastructures.trees.types import T |
| 5 | + |
| 6 | + |
| 7 | +class ThreadedBinarySearchTree(BinarySearchTree): |
| 8 | + """ |
| 9 | + A fully (double-)threaded binary search tree. |
| 10 | +
|
| 11 | + Every node's left/right pointer is EITHER a real child link OR a "thread" - |
| 12 | + a shortcut straight to that node's inorder predecessor / successor. Which |
| 13 | + one it is gets tracked with a boolean flag per side. This lets us traverse |
| 14 | + the whole tree in sorted order with O(1) extra space: no recursion, no |
| 15 | + explicit stack. |
| 16 | + """ |
| 17 | + |
| 18 | + def insert_node(self, value: Optional[T]) -> None: |
| 19 | + if self.root is None: |
| 20 | + self.root = BinaryTreeNode(value) |
| 21 | + return None |
| 22 | + current = self.root |
| 23 | + |
| 24 | + while True: |
| 25 | + # No duplicates |
| 26 | + if value == current.data: |
| 27 | + return None |
| 28 | + if value < current.data: |
| 29 | + if not current.left_thread and current.left is not None: |
| 30 | + current = current.left |
| 31 | + continue |
| 32 | + |
| 33 | + new_node = BinaryTreeNode(value) |
| 34 | + new_node.left = current.left # inherit current's predecessor |
| 35 | + new_node.left_thread = True |
| 36 | + new_node.right = current # current is the new successor |
| 37 | + new_node.right_thread = True |
| 38 | + current.left = new_node |
| 39 | + current.left_thread = False |
| 40 | + return None |
| 41 | + else: |
| 42 | + if not current.right_thread and current.right is not None: |
| 43 | + current = current.right |
| 44 | + continue |
| 45 | + new_node = BinaryTreeNode(value) |
| 46 | + new_node.right = current.right # inherit current's successor |
| 47 | + new_node.right_thread = True |
| 48 | + new_node.left = current # current is the new predecessor |
| 49 | + new_node.left_thread = True |
| 50 | + current.right = new_node |
| 51 | + current.right_thread = False |
| 52 | + return None |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def _leftmost(node: Optional[BinaryTreeNode]) -> Optional[BinaryTreeNode]: |
| 56 | + if node is None: |
| 57 | + return None |
| 58 | + while not node.left_thread and node.left is not None: |
| 59 | + node = node.left |
| 60 | + return node |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def _rightmost(node: Optional[BinaryTreeNode]) -> Optional[BinaryTreeNode]: |
| 64 | + if node is None: |
| 65 | + return None |
| 66 | + while not node.right_thread and node.right is not None: |
| 67 | + node = node.right |
| 68 | + return node |
| 69 | + |
| 70 | + def successor(self, node: BinaryTreeNode) -> Optional[BinaryTreeNode]: |
| 71 | + if node.right_thread: |
| 72 | + return node.right |
| 73 | + return self._leftmost(node.right) |
| 74 | + |
| 75 | + def predecessor(self, node: BinaryTreeNode) -> Optional[BinaryTreeNode]: |
| 76 | + if node.left_thread: |
| 77 | + return node.left |
| 78 | + return self._rightmost(node.left) |
| 79 | + |
| 80 | + def inorder_traversal(self) -> List[T]: |
| 81 | + """Iterative inorder traversal using threads only - no stack needed.""" |
| 82 | + result = [] |
| 83 | + cur = self._leftmost(self.root) |
| 84 | + while cur is not None: |
| 85 | + result.append(cur.data) |
| 86 | + cur = self.successor(cur) |
| 87 | + return result |
| 88 | + |
| 89 | + def reverse_inorder_traversal(self) -> List[T]: |
| 90 | + """Same idea, walking predecessor threads right-to-left.""" |
| 91 | + result = [] |
| 92 | + cur = self._rightmost(self.root) |
| 93 | + while cur is not None: |
| 94 | + result.append(cur.data) |
| 95 | + cur = self.predecessor(cur) |
| 96 | + return result |
0 commit comments