ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Design and Analysis of Algorithms
    COMP3138
    Progress0 / 53 topics
    Topics
    1. Introduction to Algorithm Design2. Data Structures3. Efficiency in Algorithms4. Analysis of Algorithms5. Mathematical Review6. Mathematical Analysis of Algorithms7. Types of Functions8. Order of Growth9. Asymptotic Notations10. Sorting Algorithms11. Selection Sort Algorithm12. Example and Analysis of Selection Sort13. Insertion Sort Algorithm14. Divide and Conquer Algorithms15. Merge Sort Algorithm16. Quick Sort Algorithm17. Bucket Sort Algorithm18. Radix Sort Algorithm19. Counting Sort Algorithm20. Heap Sort Basics21. Heap Algorithms22. Heap Properties and Examples23. Heap Operations24. Heap Sort Algorithm Analysis25. Heap Insertion and Deletion26. Tree Based Algorithms and Hashing27. Red Black Tree Basics28. Binary Search Tree Basics29. Tree Searching Algorithms30. Analysis of Tree Searching Algorithms31. Analysis of Insertion and Deletion in BST32. Hashing Basics33. Examples of Hash Functions34. Analysis of Collision Resolution Techniques35. Dynamic Programming36. 0-1 Knapsack Problem37. Fractional Knapsack Problem38. Longest Common Subsequence39. Shortest Path Finding40. Matrix Chain Multiplication41. Assembly Line Chain Problem42. Greedy Algorithms43. Prim's Algorithm44. Kruskal's Algorithm45. Dijkstra's Algorithm46. Huffman Coding47. NP-Completeness48. Polynomial Time Verification49. Reducibility50. NP-Completeness Proofs51. Randomized Algorithms52. Particle Swarm Optimization53. Genetic Algorithms
    COMP3138›Binary Search Tree Basics
    Design and Analysis of AlgorithmsTopic 28 of 53

    Binary Search Tree Basics

    7 minread
    1,271words
    Intermediatelevel

    Binary Search Tree (BST) Basics

    A Binary Search Tree (BST) is a type of binary tree where each node has at most two children, referred to as the left and right children. The tree is organized in such a way that the nodes follow a specific ordering property that makes searching for an element efficient.

    Properties of a Binary Search Tree (BST)

    A Binary Search Tree has the following properties:

    1. Node Structure:

      • Each node contains three elements: a key/value, a left child, and a right child.
      • The left child of a node contains values smaller than the node's key, while the right child contains values greater than the node's key.
    2. Ordering Property:

      • For each node:
        • All keys in the left subtree of a node are smaller than the node’s key.
        • All keys in the right subtree of a node are greater than the node’s key.
      • This property holds for all nodes in the tree, not just the root.
    3. Binary Tree Structure:

      • It is a binary tree, meaning that each node has at most two children (a left child and a right child).
    4. Uniqueness of Keys:

      • In a standard BST, each node must have a unique key (i.e., no duplicates).

    Basic Operations on a BST

    The main operations performed on a Binary Search Tree (BST) include search, insert, delete, and traversal. Let's look at each operation in detail.


    1. Search Operation in a BST

    The search operation finds the node with a specific key (or value) in the tree.

    Steps for Searching:

    1. Start from the root of the tree.
    2. Compare the key with the current node:
      • If the key is equal to the node's key, the search is successful, and you return the node.
      • If the key is smaller than the current node’s key, search in the left subtree.
      • If the key is greater than the current node’s key, search in the right subtree.
    3. Repeat the process recursively or iteratively until the key is found or you reach a NULL (empty) child, meaning the key is not present in the tree.

    Time Complexity:

    • Average Case: O(log n) if the tree is balanced.
    • Worst Case: O(n) if the tree degenerates into a linked list (i.e., if all nodes are inserted in sorted order).

    2. Insert Operation in a BST

    Inserting a new node into the BST requires following the ordering property to place the new node in the correct position.

    Steps for Insertion:

    1. Start from the root of the tree.
    2. Compare the new key with the current node’s key:
      • If the new key is smaller, move to the left child.
      • If the new key is greater, move to the right child.
    3. Continue moving left or right until you find an empty spot (a NULL child) where the new node can be inserted.

    Time Complexity:

    • Average Case: O(log n) if the tree is balanced.
    • Worst Case: O(n) if the tree becomes unbalanced (e.g., inserting in sorted order).

    3. Delete Operation in a BST

    Deleting a node from a BST is more complicated than insertion, as it requires handling three different cases depending on the number of children the node has.

    Three Cases of Deletion:

    1. Node to be deleted has no children (leaf node):

      • Simply remove the node from the tree.
    2. Node to be deleted has one child:

      • Remove the node and replace it with its child (either left or right).
    3. Node to be deleted has two children:

      • Find the in-order successor (or in-order predecessor) of the node:
        • The in-order successor is the node with the smallest key in the right subtree (or the largest key in the left subtree).
      • Replace the node to be deleted with its in-order successor, then delete the successor node (which will have at most one child).

    Time Complexity:

    • Average Case: O(log n) if the tree is balanced.
    • Worst Case: O(n) if the tree becomes unbalanced.

    4. Traversal Operations

    A traversal is a process of visiting all the nodes in a tree. There are several ways to traverse a BST, and these are divided into two categories: Depth-First Traversals and Breadth-First Traversals.

    Depth-First Traversals (DFS):

    1. In-order Traversal:

      • Visit the left subtree → Visit the node → Visit the right subtree.
      • In-order traversal of a BST produces the keys in ascending order.
    2. Pre-order Traversal:

      • Visit the node → Visit the left subtree → Visit the right subtree.
    3. Post-order Traversal:

      • Visit the left subtree → Visit the right subtree → Visit the node.

    Breadth-First Traversal (Level-order Traversal):

    • This traversal visits all nodes at each level from top to bottom, and from left to right within each level. It is typically implemented using a queue.

    Time Complexity of Traversals:

    • All Depth-First Traversals: O(n), where n is the number of nodes.
    • Level-order Traversal: O(n), because it visits all nodes.

    Advantages of BSTs

    1. Efficient Search: In a balanced BST, searching for a key takes O(log n) time, which is much faster than a linear search (O(n)).
    2. Ordered Data: The nodes in a BST are organized in such a way that in-order traversal always gives the data in sorted order, making it useful for applications where sorted data is needed.
    3. Dynamic Set of Data: Unlike arrays or linked lists, a BST allows dynamic insertion and deletion of elements, making it highly flexible for certain applications.

    Disadvantages of BSTs

    1. Unbalanced Trees: If elements are inserted in a sorted order (or nearly sorted), the tree can become unbalanced and degrade into a linked list, leading to O(n) time for search, insert, and delete operations.
    2. Complexity of Balancing: Unlike simpler data structures, maintaining a balanced BST often requires extra effort (e.g., using self-balancing trees like AVL trees or Red-Black trees) to ensure efficient operations.

    Balanced BSTs (Self-Balancing Trees)

    To prevent the tree from degenerating into a linear structure (linked list), self-balancing trees have been developed, such as:

    • AVL Trees: A self-balancing BST where the difference in heights of left and right subtrees is at most 1.
    • Red-Black Trees: A type of binary search tree where each node has a color (red or black) and balancing is maintained with specific properties.

    These trees ensure that the height of the tree remains logarithmic, guaranteeing O(log n) time complexity for all operations.


    Example of a Binary Search Tree

    Consider inserting the following sequence of numbers into a BST: 50, 30, 20, 40, 70, 60, 80.

    1. Insert 50 as the root.

          50
      
    2. Insert 30 (smaller than 50).

          50
         /
       30
      
    3. Insert 20 (smaller than 50 and 30).

          50
         /
       30
      /
      

    20

    
    4. Insert `40` (smaller than `50`, greater than `30`).
    
       50
      /
    30
    

    /
    20 40

    
    5. Insert `70` (greater than `50`).
    
       50
      /  \
    30    70
    

    /
    20 40

    
    6. Insert `60` (greater than `50`, smaller than `70`).
    
       50
      /  \
    30    70
    

    / \ / 20 40 60

    
    7. Insert `80` (greater than `50` and `70`).
    
       50
      /  \
    30    70
    

    / \ /
    20 40 60 80

    
    ---
    
    ### **Summary**
    
    A **Binary Search Tree (BST)** is a tree structure that maintains an ordered set of data, where each node has a key, and the left and right subtrees follow the ordering property: left child keys are smaller, and right child keys are larger. BSTs support efficient operations like **search**, **insert**, **delete**, and **traversal**, with **O(log n)** time complexity on average for balanced trees
    
    Previous topic 27
    Red Black Tree Basics
    Next topic 29
    Tree Searching Algorithms

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,271
      Code examples0
      DifficultyIntermediate