|
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 | + val: int |
| 15 | + left: Node | None = None |
| 16 | + right: Node | None = None |
14 | 17 |
|
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 |
| 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 |
29 | 24 |
|
| 25 | + def __len__(self) -> int: |
| 26 | + return sum(1 for _ in self) |
30 | 27 |
|
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) |
| 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) |
37 | 39 |
|
38 | 40 |
|
39 |
| -def tree_sort(arr): |
| 41 | +def tree_sort(arr: list[int]) -> tuple[int, ...]: |
40 | 42 | """
|
41 |
| -
|
| 43 | + >>> tree_sort([]) |
| 44 | + () |
| 45 | + >>> tree_sort((1,)) |
| 46 | + (1,) |
| 47 | + >>> tree_sort((1, 2)) |
| 48 | + (1, 2) |
42 | 49 | >>> tree_sort([5, 2, 7])
|
43 |
| - [2, 5, 7] |
44 |
| -
|
45 |
| - >>> tree_sort([5, -4, 9, 2, 7]) |
46 |
| - [-4, 2, 5, 7, 9] |
47 |
| -
|
| 50 | + (2, 5, 7) |
| 51 | + >>> tree_sort((5, -4, 9, 2, 7)) |
| 52 | + (-4, 2, 5, 7, 9) |
48 | 53 | >>> tree_sort([5, 6, 1, -1, 4, 37, 2, 7])
|
49 |
| - [-1, 1, 2, 4, 5, 6, 7, 37] |
50 |
| -
|
| 54 | + (-1, 1, 2, 4, 5, 6, 7, 37) |
| 55 | + >>> tree_sort(range(10, -10, -1)) == tuple(sorted(range(10, -10, -1))) |
| 56 | + True |
51 | 57 | """
|
52 |
| - # Build BST |
53 | 58 | if len(arr) == 0:
|
54 |
| - return arr |
| 59 | + return tuple(arr) |
55 | 60 | root = Node(arr[0])
|
56 |
| - for i in range(1, len(arr)): |
57 |
| - root.insert(arr[i]) |
58 |
| - # Traverse BST in order. |
59 |
| - res = [] |
60 |
| - inorder(root, res) |
61 |
| - return res |
| 61 | + for item in arr[1:]: |
| 62 | + root.insert(item) |
| 63 | + return tuple(root) |
62 | 64 |
|
63 | 65 |
|
64 | 66 | if __name__ == "__main__":
|
65 | 67 | import doctest
|
66 | 68 |
|
67 | 69 | doctest.testmod()
|
| 70 | + print(f"{tree_sort([5, 6, 1, -1, 4, 37, -3, 7]) = }") |
0 commit comments