A Tree is a non-linear hierarchical data structure that consists of nodes connected by edges. Unlike linear structures (arrays, lists), trees represent relationships like "parent-child" hierarchies.
A Binary Tree is a tree where each node has at most two children (Left and Right). A Binary Search Tree (BST) is specialized such that:
Traversal is the process of visiting all nodes in a tree exactly once. These can be implemented Recursively or Iteratively.
| Traversal Type | Order of Visitation |
|---|---|
| In-order | Left Subtree → Root → Right Subtree |
| Pre-order | Root → Left Subtree → Right Subtree |
| Post-order | Left Subtree → Right Subtree → Root |
Performing an In-order traversal on a Binary Search Tree always yields the elements in ascending sorted order.
Standard binary trees have many NULL pointers in leaf nodes. Threaded Binary Trees utilize these NULL pointers to point to the in-order predecessor or successor.
An AVL Tree is a self-balancing binary search tree. The heights of the two child subtrees of any node differ by at most one.
Balance Factor (BF) = Height(Left Subtree) - Height(Right Subtree)
For an AVL tree, BF must be -1, 0, or 1 for every node.
To restore balance after insertion or deletion, four types of rotations are used:
A Heap is a specialized complete binary tree that satisfies the heap property:
Q: What is the time complexity of searching in a BST?
Average case: O(log n). Worst case (skewed tree): O(n). This is why balanced trees like AVL are used.
Q: Why use Threaded Binary Trees?
They make it easier to find the in-order successor/predecessor without extra memory for a stack during traversal.