Unit III: Trees

Table of Contents

1. Introduction to Trees

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.


2. Binary Search Trees (BST)

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:

Operations on BST


3. Tree Traversals

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

Exam Tip: In-order BST

Performing an In-order traversal on a Binary Search Tree always yields the elements in ascending sorted order.


4. Threaded Binary Trees

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.


5. Height-Balanced (AVL) Trees

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.

Rotations for Balancing

To restore balance after insertion or deletion, four types of rotations are used:

  1. LL (Left-Left): Single right rotation.
  2. RR (Right-Right): Single left rotation.
  3. LR (Left-Right): Left rotation followed by right rotation.
  4. RL (Right-Left): Right rotation followed by left rotation.

6. Heap Tree

A Heap is a specialized complete binary tree that satisfies the heap property:

Common Mistakes


Frequently Asked Questions

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.