Member-only story
Binary tree fundamentals #2: Breadth first and depth first tree traversal
In my previous post, I talked about the concepts of a binary tree and its implementation in Java. In this post, I will continue to discuss about another important concept in binary tree data structure — tree traversal.
Table of contents
· Breadth first (or level order):
· Depth first:
∘ Inorder (left — root — right)
∘ Preorder (root — left — right)
∘ Postorder (left — right — root)
Tree traversal is a process of going through the entire tree, visiting each node of a tree exactly once. We can use tree traversal to count the number of nodes, search for a node in the tree, list the labels of tree nodes etc. Tree traversal can be broadly categorized into 2 methods: breadth first and depth first.
Breadth first (or level order):
In breadth-first method, we start from the root, then explore all child nodes at the same level before moving to the next level.