Chapter: Tree Data Structure
"Just like a tree grows from its root, every algorithm
branches from the basics. Mastering the Tree will help to
understand the structure of complexity."
, What is a Binary Tree?
A Binary Tree is a hierarchical data structure where each node has at most two
children, typically referred to as the left and right child.
Example:
• Each circle represents a node.
• Arrows show parent → child relationships.
Types of Traversals:-
Traversal is the process of visiting each node in a tree systematically.
1. Preorder (Root → Left → Right)
Visit the root, then the left subtree, then the right subtree.
➤ Example: A B D E C F G
2. Inorder (Left → Root → Right)
Visit the left subtree, then the root, then the right subtree.
➤ Example: D B E A F C G
Used in Binary Search Trees to get sorted output.
3. Postorder (Left → Right → Root)
Visit both subtrees first, then the root.
➤ Example: D E B F G C A
4. Level-Order (Breadth First Search)
Visit all nodes level by level from top to bottom using a queue.
➤ Example: A B C D E F G