|
1 | 1 | """ |
2 | 2 | Tree_sort algorithm. |
3 | 3 |
|
4 | | -Build a BST and in order traverse. |
| 4 | +Build a Binary Search Tree and then iterate thru it to get a sorted list. |
5 | 5 | """ |
| 6 | +from __future__ import annotations |
6 | 7 |
|
| 8 | +from collections.abc import Iterator |
| 9 | +from dataclasses import dataclass |
7 | 10 |
|
| 11 | + |
| 12 | +@dataclass |
8 | 13 | class Node: |
9 | | - # BST data structure |
10 | | - def __init__(self, val): |
11 | | - self.val = val |
12 | | - self.left = None |
13 | | - self.right = None |
14 | | - |
15 | | - def insert(self, val): |
16 | | - if self.val: |
17 | | - if val < self.val: |
18 | | - if self.left is None: |
19 | | - self.left = Node(val) |
20 | | - else: |
21 | | - self.left.insert(val) |
22 | | - elif val > self.val: |
23 | | - if self.right is None: |
24 | | - self.right = Node(val) |
25 | | - else: |
26 | | - self.right.insert(val) |
27 | | - else: |
28 | | - self.val = val |
29 | | - |
30 | | - |
31 | | -def inorder(root, res): |
32 | | - # Recursive traversal |
33 | | - if root: |
34 | | - inorder(root.left, res) |
35 | | - res.append(root.val) |
36 | | - inorder(root.right, res) |
37 | | - |
38 | | - |
39 | | -def tree_sort(arr): |
40 | | - # Build BST |
| 14 | + val: int |
| 15 | + left: Node | None = None |
| 16 | + right: Node | None = None |
| 17 | + |
| 18 | + def __iter__(self) -> Iterator[int]: |
| 19 | + if self.left: |
| 20 | + yield from self.left |
| 21 | + yield self.val |
| 22 | + if self.right: |
| 23 | + yield from self.right |
| 24 | + |
| 25 | + def __len__(self) -> int: |
| 26 | + return sum(1 for _ in self) |
| 27 | + |
| 28 | + def insert(self, val: int) -> None: |
| 29 | + if val < self.val: |
| 30 | + if self.left is None: |
| 31 | + self.left = Node(val) |
| 32 | + else: |
| 33 | + self.left.insert(val) |
| 34 | + elif val > self.val: |
| 35 | + if self.right is None: |
| 36 | + self.right = Node(val) |
| 37 | + else: |
| 38 | + self.right.insert(val) |
| 39 | + |
| 40 | + |
| 41 | +def tree_sort(arr: list[int]) -> tuple[int, ...]: |
| 42 | + """ |
| 43 | + >>> tree_sort([]) |
| 44 | + () |
| 45 | + >>> tree_sort((1,)) |
| 46 | + (1,) |
| 47 | + >>> tree_sort((1, 2)) |
| 48 | + (1, 2) |
| 49 | + >>> tree_sort([5, 2, 7]) |
| 50 | + (2, 5, 7) |
| 51 | + >>> tree_sort((5, -4, 9, 2, 7)) |
| 52 | + (-4, 2, 5, 7, 9) |
| 53 | + >>> tree_sort([5, 6, 1, -1, 4, 37, 2, 7]) |
| 54 | + (-1, 1, 2, 4, 5, 6, 7, 37) |
| 55 | + >>> tree_sort(range(10, -10, -1)) == tuple(sorted(range(10, -10, -1))) |
| 56 | + True |
| 57 | + """ |
41 | 58 | if len(arr) == 0: |
42 | | - return arr |
| 59 | + return tuple(arr) |
43 | 60 | root = Node(arr[0]) |
44 | | - for i in range(1, len(arr)): |
45 | | - root.insert(arr[i]) |
46 | | - # Traverse BST in order. |
47 | | - res = [] |
48 | | - inorder(root, res) |
49 | | - return res |
| 61 | + for item in arr[1:]: |
| 62 | + root.insert(item) |
| 63 | + return tuple(root) |
50 | 64 |
|
51 | 65 |
|
52 | 66 | if __name__ == "__main__": |
53 | | - print(tree_sort([10, 1, 3, 2, 9, 14, 13])) |
| 67 | + import doctest |
| 68 | + |
| 69 | + doctest.testmod() |
| 70 | + print(f"{tree_sort([5, 6, 1, -1, 4, 37, -3, 7]) = }") |
0 commit comments