🌿Binary Search Tree

Follow Me

🌳 Binary Search Tree (BST)

πŸ”Ή Definition:

A Binary Search Tree is a type of binary tree in which each node has:

  • At most two children: left and right

  • Left child contains nodes less than the parent

  • Right child contains nodes greater than the parent

πŸ”§ Rules of BST:

  1. Left subtree has values less than the node

  2. Right subtree has values greater than the node

  3. No duplicate nodes allowed

🧠 Basic Operations:

OperationPurpose
Insertion()
    Add a new value    πŸ‘ˆ Click Here
Searching()
    Find a value    πŸ‘ˆ Click Here
Deletion()
    Remove a value    πŸ‘ˆ Click Here
InOrder()
    Traverse in sorted order    πŸ‘ˆ Click Here
PreOrder()
    Traverse node → left → right    πŸ‘ˆ Click Here
PostOrder()
    Traverse left → right → node    πŸ‘ˆ Click Here

Advantages:

  • Faster searching than regular binary tree (O(log n) for balanced trees)

  • In-order traversal gives sorted data

Disadvantages:

  • If unbalanced, time complexity can degrade to O(n)

  • Need rebalancing (e.g., using AVL or Red-Black Trees)

πŸ“Š Use Cases:

  • Search engines

  • Databases (indexing)

  • File systems


No comments:

Post a Comment

DSA using Python: Assignment - 2: Classes and Objects

Follow Me “In this assignment, we will learn how to use Python classes and objects to build reusable and structured code. The tasks cover re...