|
| 1 | +""" |
| 2 | +Binary Tree Flattening Algorithm |
| 3 | +
|
| 4 | +This code defines an algorithm to flatten a binary tree into a linked list |
| 5 | +represented using the right pointers of the tree nodes. It uses in-place |
| 6 | +flattening and demonstrates the flattening process along with a display |
| 7 | +function to visualize the flattened linked list. |
| 8 | +https://www.geeksforgeeks.org/flatten-a-binary-tree-into-linked-list |
| 9 | +
|
| 10 | +Author: Arunkumar A |
| 11 | +Date: 04/09/2023 |
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | + |
| 16 | +class TreeNode: |
| 17 | + """ |
| 18 | + A TreeNode has data variable and pointers to TreeNode objects |
| 19 | + for its left and right children. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, data: int) -> None: |
| 23 | + self.data = data |
| 24 | + self.left: TreeNode | None = None |
| 25 | + self.right: TreeNode | None = None |
| 26 | + |
| 27 | + |
| 28 | +def build_tree() -> TreeNode: |
| 29 | + """ |
| 30 | + Build and return a sample binary tree. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + TreeNode: The root of the binary tree. |
| 34 | +
|
| 35 | + Examples: |
| 36 | + >>> root = build_tree() |
| 37 | + >>> root.data |
| 38 | + 1 |
| 39 | + >>> root.left.data |
| 40 | + 2 |
| 41 | + >>> root.right.data |
| 42 | + 5 |
| 43 | + >>> root.left.left.data |
| 44 | + 3 |
| 45 | + >>> root.left.right.data |
| 46 | + 4 |
| 47 | + >>> root.right.right.data |
| 48 | + 6 |
| 49 | + """ |
| 50 | + root = TreeNode(1) |
| 51 | + root.left = TreeNode(2) |
| 52 | + root.right = TreeNode(5) |
| 53 | + root.left.left = TreeNode(3) |
| 54 | + root.left.right = TreeNode(4) |
| 55 | + root.right.right = TreeNode(6) |
| 56 | + return root |
| 57 | + |
| 58 | + |
| 59 | +def flatten(root: TreeNode | None) -> None: |
| 60 | + """ |
| 61 | + Flatten a binary tree into a linked list in-place, where the linked list is |
| 62 | + represented using the right pointers of the tree nodes. |
| 63 | +
|
| 64 | + Args: |
| 65 | + root (TreeNode): The root of the binary tree to be flattened. |
| 66 | +
|
| 67 | + Examples: |
| 68 | + >>> root = TreeNode(1) |
| 69 | + >>> root.left = TreeNode(2) |
| 70 | + >>> root.right = TreeNode(5) |
| 71 | + >>> root.left.left = TreeNode(3) |
| 72 | + >>> root.left.right = TreeNode(4) |
| 73 | + >>> root.right.right = TreeNode(6) |
| 74 | + >>> flatten(root) |
| 75 | + >>> root.data |
| 76 | + 1 |
| 77 | + >>> root.right.right is None |
| 78 | + False |
| 79 | + >>> root.right.right = TreeNode(3) |
| 80 | + >>> root.right.right.right is None |
| 81 | + True |
| 82 | + """ |
| 83 | + if not root: |
| 84 | + return |
| 85 | + |
| 86 | + # Flatten the left subtree |
| 87 | + flatten(root.left) |
| 88 | + |
| 89 | + # Save the right subtree |
| 90 | + right_subtree = root.right |
| 91 | + |
| 92 | + # Make the left subtree the new right subtree |
| 93 | + root.right = root.left |
| 94 | + root.left = None |
| 95 | + |
| 96 | + # Find the end of the new right subtree |
| 97 | + current = root |
| 98 | + while current.right: |
| 99 | + current = current.right |
| 100 | + |
| 101 | + # Append the original right subtree to the end |
| 102 | + current.right = right_subtree |
| 103 | + |
| 104 | + # Flatten the updated right subtree |
| 105 | + flatten(right_subtree) |
| 106 | + |
| 107 | + |
| 108 | +def display_linked_list(root: TreeNode | None) -> None: |
| 109 | + """ |
| 110 | + Display the flattened linked list. |
| 111 | +
|
| 112 | + Args: |
| 113 | + root (TreeNode | None): The root of the flattened linked list. |
| 114 | +
|
| 115 | + Examples: |
| 116 | + >>> root = TreeNode(1) |
| 117 | + >>> root.right = TreeNode(2) |
| 118 | + >>> root.right.right = TreeNode(3) |
| 119 | + >>> display_linked_list(root) |
| 120 | + 1 2 3 |
| 121 | + >>> root = None |
| 122 | + >>> display_linked_list(root) |
| 123 | +
|
| 124 | + """ |
| 125 | + current = root |
| 126 | + while current: |
| 127 | + if current.right is None: |
| 128 | + print(current.data, end="") |
| 129 | + break |
| 130 | + print(current.data, end=" ") |
| 131 | + current = current.right |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + print("Flattened Linked List:") |
| 136 | + root = build_tree() |
| 137 | + flatten(root) |
| 138 | + display_linked_list(root) |
0 commit comments