Python/data_structures/binary_tree
pre-commit-ci[bot] bc8df6de31
[pre-commit.ci] pre-commit autoupdate (#11322)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2)
- [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-03-13 07:52:41 +01:00
..
__init__.py Add __init__.py files in all the directories (#2503) 2020-09-28 19:42:36 +02:00
avl_tree.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
basic_binary_tree.py Update basic_binary_tree.py (#10388) 2023-10-16 16:43:53 +02:00
binary_search_tree_recursive.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
binary_search_tree.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
binary_tree_mirror.py Add more ruff rules (#8767) 2023-05-26 09:34:17 +02:00
binary_tree_node_sum.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
binary_tree_path_sum.py Binary tree path sum (#7748) 2022-10-27 23:03:01 +02:00
binary_tree_traversals.py binary_search_traversals.py made memory-friendly using generators. Fixes #8725 completely. (#9237) 2023-10-01 12:54:05 -04:00
diameter_of_binary_tree.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
diff_views_of_binary_tree.py [Binary Tree] Different views of binary tree added (#6965) 2022-10-17 22:30:01 +02:00
distribute_coins.py Fix ruff errors (#8936) 2023-08-09 13:25:30 +05:30
fenwick_tree.py fix: no implicit optional (#7984) 2022-11-15 14:55:14 +01:00
flatten_binarytree_to_linkedlist.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
floor_and_ceiling.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
inorder_tree_traversal_2022.py Make some ruff fixes (#8154) 2023-03-01 17:23:33 +01:00
is_sorted.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
is_sum_tree.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
lazy_segment_tree.py [pre-commit.ci] pre-commit autoupdate (#9013) 2023-08-29 15:18:10 +02:00
lowest_common_ancestor.py [mypy] Fix type annotations in data_structures/binary_tree/lowest_common_ancestor.py (#5757) 2021-11-03 21:34:08 +01:00
maximum_fenwick_tree.py Fix Max Fenwick Tree (#6328) 2022-09-14 09:24:55 +01:00
merge_two_binary_trees.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
mirror_binary_tree.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
non_recursive_segment_tree.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
number_of_possible_binary_trees.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
README.md Rename binary_tree_traversals.md to README.md (#10599) 2023-10-16 10:16:09 -04:00
red_black_tree.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
segment_tree_other.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
segment_tree.py update segmenttree docstrings Fixes #9943 (#9975) 2023-10-17 03:25:07 +02:00
serialize_deserialize_binary_tree.py serialize deserialize binary tree (#9625) 2023-10-30 00:07:21 +01:00
symmetric_tree.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00
treap.py Add pep8-naming to pre-commit hooks and fixes incorrect naming conventions (#7062) 2022-10-13 00:54:20 +02:00
wavelet_tree.py [pre-commit.ci] pre-commit autoupdate (#11322) 2024-03-13 07:52:41 +01:00

Binary Tree Traversal

Overview

The combination of binary trees being data structures and traversal being an algorithm relates to classic problems, either directly or indirectly.

If you can grasp the traversal of binary trees, the traversal of other complicated trees will be easy for you.

The following are some common ways to traverse trees.

  • Depth First Traversals (DFS): In-order, Pre-order, Post-order

  • Level Order Traversal or Breadth First or Traversal (BFS)

There are applications for both DFS and BFS.

Stack can be used to simplify the process of DFS traversal. Besides, since tree is a recursive data structure, recursion and stack are two key points for DFS.

Graph for DFS:

binary-tree-traversal-dfs

The key point of BFS is how to determine whether the traversal of each level has been completed. The answer is to use a variable as a flag to represent the end of the traversal of current level.

Pre-order Traversal

The traversal order of pre-order traversal is root-left-right.

Algorithm Pre-order

  1. Visit the root node and push it into a stack.

  2. Pop a node from the stack, and push its right and left child node into the stack respectively.

  3. Repeat step 2.

Conclusion: This problem involves the classic recursive data structure (i.e. a binary tree), and the algorithm above demonstrates how a simplified solution can be reached by using a stack.

If you look at the bigger picture, you'll find that the process of traversal is as followed. Visit the left subtrees respectively from top to bottom, and visit the right subtrees respectively from bottom to top. If we are to implement it from this perspective, things will be somewhat different. For the top to bottom part we can simply use recursion, and for the bottom to top part we can turn to stack.

In-order Traversal

The traversal order of in-order traversal is left-root-right.

So the root node is not printed first. Things are getting a bit complicated here.

Algorithm In-order

  1. Visit the root and push it into a stack.

  2. If there is a left child node, push it into the stack. Repeat this process until a leaf node reached.

    At this point the root node and all the left nodes are in the stack.

  3. Start popping nodes from the stack. If a node has a right child node, push the child node into the stack. Repeat step 2.

It's worth pointing out that the in-order traversal of a binary search tree (BST) is a sorted array, which is helpful for coming up simplified solutions for some problems.

Post-order Traversal

The traversal order of post-order traversal is left-right-root.

This one is a bit of a challenge. It deserves the hard tag of LeetCode.

In this case, the root node is printed not as the first but the last one. A cunning way to do it is to:

Record whether the current node has been visited. If 1) it's a leaf node or 2) both its left and right subtrees have been traversed, then it can be popped from the stack.

As for 1) it's a leaf node, you can easily tell whether a node is a leaf if both its left and right are null.

As for 2) both its left and right subtrees have been traversed, we only need a variable to record whether a node has been visited or not. In the worst case, we need to record the status for every single node and the space complexity is O(n). But if you come to think about it, as we are using a stack and start printing the result from the leaf nodes, it makes sense that we only record the status for the current node popping from the stack, reducing the space complexity to O(1).

Level Order Traversal

The key point of level order traversal is how do we know whether the traversal of each level is done. The answer is that we use a variable as a flag representing the end of the traversal of the current level.

binary-tree-traversal-bfs

Algorithm Level-order

  1. Visit the root node, put it in a FIFO queue, put in the queue a special flag (we are using null here).

  2. Dequeue a node.

  3. If the node equals null, it means that all nodes of the current level have been visited. If the queue is empty, we do nothing. Or else we put in another null.

  4. If the node is not null, meaning the traversal of current level has not finished yet, we enqueue its left subtree and right subtree respectively.

Bi-color marking

We know that there is a tri-color marking in garbage collection algorithm, which works as described below.

  • The white color represents "not visited".

  • The gray color represents "not all child nodes visited".

  • The black color represents "all child nodes visited".

Enlightened by tri-color marking, a bi-color marking method can be invented to solve all three traversal problems with one solution.

The core idea is as follow.

  • Use a color to mark whether a node has been visited or not. Nodes yet to be visited are marked as white and visited nodes are marked as gray.

  • If we are visiting a white node, turn it into gray, and push its right child node, itself, and it's left child node into the stack respectively.

  • If we are visiting a gray node, print it.

Implementation of pre-order and post-order traversal algorithms can be easily done by changing the order of pushing the child nodes into the stack.

Reference: LeetCode