Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2: Tree Operations & Algorithms

Lesson 8 of 14 in the free Data Structure notes on Siksha Sarovar, written by Rohit Jangra.

Tree Traversal Algorithms

Traversal is visiting each node exactly once in a systematic way. Trees use Recursion for traversal.

Traversal Methods — Comparison

TraversalOrderMnemonicKey Use Case
InorderLeft → Root → RightLNRBST sorted output
PreorderRoot → Left → RightNLRClone/copy a tree
PostorderLeft → Right → RootLRNDelete a tree
Level-OrderLevel by level (BFS)L0, L1, L2...Print tree by levels

Worked Example: All Traversals

        50
       /  \
      30    70
     / \   / \
    20  40 60  80
TraversalOutputExplanation
Inorder20, 30, 40, 50, 60, 70, 80Sorted! (BST property)
Preorder50, 30, 20, 40, 70, 60, 80Root first
Postorder20, 40, 30, 60, 80, 70, 50Root last
Level-Order50, 30, 70, 20, 40, 60, 80Level by level

Binary Search Tree (BST) Property

For every node:

  • Left subtree contains only values less than the node.
  • Right subtree contains only values greater than the node.
  • Both subtrees are also BSTs (recursive property).

BST Operations — Time Complexity

OperationAverage CaseWorst Case (Skewed)Notes
SearchO(log n)O(n)Halves search space each step
InsertO(log n)O(n)Find correct position, then link
DeleteO(log n)O(n)Most complex operation
Min/MaxO(log n)O(n)Go all-left / all-right
Inorder TraversalO(n)O(n)Must visit all nodes

BST Search Algorithm

  1. If Root is NULL → Not Found.
  2. If Key == Root → Found!
  3. If Key < Root → Search Left subtree.
  4. If Key > Root → Search Right subtree.
This is a divide-and-conquer approach — each comparison eliminates half the tree.

BST Insertion

Find the correct empty position by following BST property:

  • Key < Current → Go Left.
  • Key > Current → Go Right.
  • When you reach NULL → Insert here.

BST Deletion — The 3 Cases

CaseConditionActionDifficulty
Case 1Node is a LeafSimply remove it (set parent's pointer to NULL)Easy
Case 2Node has 1 ChildBypass: link parent directly to the childMedium
Case 3Node has 2 ChildrenReplace with Inorder Successor (smallest in right subtree), then delete successorHard

Worked Example — Delete 30 from the BST above (2 children):

  1. Node 30 has 2 children (20 and 40).
  2. Find Inorder Successor of 30 = smallest in right subtree = 40.
  3. Replace 30's value with 40.
  4. Delete old 40 node (which is a leaf — Case 1).
  5. Result:
        50
       /  \
      40    70
     /    / \
    20   60  80

Inorder Predecessor vs Successor

TermDefinitionHow to Find
Inorder PredecessorLargest value smaller than currentGo Left once, then all the way Right
Inorder SuccessorSmallest value larger than currentGo Right once, then all the way Left