VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-for-binary-search-tree/

⇱ C Program for Binary Search Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program for Binary Search Tree

Last Updated : 13 Dec, 2025

Binary Search Tree (BST) is a special type of binary tree that maintains its elements in a sorted order. For every node in the BST:

  • All nodes in its left subtree have values less than the node’s value.
  • All nodes in its right subtree have values greater than the node’s value.

This property ensures that each comparison allows the operation to skip about half of the remaining tree, making BST operations much faster than linear structures like arrays or linked lists.

Key Properties

  • Unique ordering of elements means duplicates are usually not allowed.
  • Inorder traversal of a BST gives sorted order of elements.
  • Average height: O(log n) (for balanced BST).
  • Worst case height: O(n) (when tree becomes skewed).
👁 binary-search-tree

Output
60 found
 20 40 30 60 80 70 50 
 50 30 20 40 70 60 80 
 20 30 40 50 60 70 80 
After Delete: 
 20 30 40 50 60 80 

Operations

Search

  • Find whether a given key exists in the BST.
  • Time Complexity: average O(log n) and O(n) worst case

Insertion

  • Insert a new node while maintaining BST property.
  • Compare key with current node and move left/right recursively or iteratively.
  • Time Complexity: average O(logn) and O(n) worst case

Deletion

Remove a node while keeping BST valid.

  • Node has no children means remove directly.
  • Node has one child means replace node with its child.
  • Node has two children means replace node with inorder successor/predecessor and delete that successor/predecessor.
  • Time Complexity: average O(logn) and O(n) worst case

Traversal
The four common tree traversals are Inorder (Left, Root, Right) which gives nodes in sorted order for a BST, Preorder (Root, Left, Right), Postorder (Left, Right, Root), and Level-order, which traverses the tree level by level using a queue.

Application of Binary Search Tree

  • Searching and indexing (e.g., maps, sets).
  • Dynamic sorting and range queries.
  • Implementing symbol tables in compilers.
  • Used in advanced structures (AVL Tree, Red-Black Tree, Splay Tree).
Comment
Article Tags: