ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3138Binary Search Tree Bas…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3138

    Design and Analysis of Algorithms

    Progress0 / 53 done
    Topics53
    1Introduction to Algorithm Design2Data Structures3Efficiency in Algorithms4Analysis of Algorithms5Mathematical Review6Mathematical Analysis of Algorithms7Types of Functions8Order of Growth9Asymptotic Notations10Sorting Algorithms11Selection Sort Algorithm12Example and Analysis of Selection Sort13Insertion Sort Algorithm14Divide and Conquer Algorithms15Merge Sort Algorithm16Quick Sort Algorithm17Bucket Sort Algorithm18Radix Sort Algorithm19Counting Sort Algorithm20Heap Sort Basics21Heap Algorithms22Heap Properties and Examples23Heap Operations24Heap Sort Algorithm Analysis25Heap Insertion and Deletion26Tree Based Algorithms and Hashing27Red Black Tree Basics28Binary Search Tree Basics29Tree Searching Algorithms30Analysis of Tree Searching Algorithms31Analysis of Insertion and Deletion in BST32Hashing Basics33Examples of Hash Functions34Analysis of Collision Resolution Techniques35Dynamic Programming360-1 Knapsack Problem37Fractional Knapsack Problem38Longest Common Subsequence39Shortest Path Finding40Matrix Chain Multiplication41Assembly Line Chain Problem42Greedy Algorithms43Prim's Algorithm44Kruskal's Algorithm45Dijkstra's Algorithm46Huffman Coding47NP-Completeness48Polynomial Time Verification49Reducibility50NP-Completeness Proofs51Randomized Algorithms52Particle Swarm Optimization53Genetic Algorithms
    8 min read
    Topic 28 of 53
    Topic Study Note Illustration
    28

    Binary Search Tree Basics

    Visual Reference Binary Search Tree Wikipedia A Binary Search Tree (BST) is a type of binary tree where each node has at most two children, referred to...

    Binary Search Tree (BST) Basics

    Binary search tree
    Visual ReferenceBinary Search Tree
    Wikipedia

    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):

    Breadth-first search
    Visual ReferenceBreadth-First Search
    Wikipedia
    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    8mremaining
    1.3kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Design and Analysis of Algorithms