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 & Analysis of Algorithms
    DC-321
    Progress0 / 28 topics
    Topics
    1. Introduction2. Role of Algorithms in Computing3. Analysis on Nature of Input and Size of Input4. Asymptotic Notations5. Big-O Notation6. Big-Ω Notation7. Big-Θ Notation8. Little-o Notation9. Little-ω Notation10. Sorting Algorithm Analysis11. Loop Invariants12. Recursion and Recurrence Relations13. Algorithm Design Techniques14. Brute Force Approach15. Divide-and-Conquer Approach16. Merge Sort17. Quick Sort18. Greedy Approach19. Dynamic Programming20. Elements of Dynamic Programming21. Search Trees22. Heaps23. Hashing24. Graph Algorithms25. Shortest Paths26. Sparse Graphs27. String Matching28. Introduction to Complexity Classes
    DC-321›Search Trees
    Design & Analysis of AlgorithmsTopic 21 of 28

    Search Trees

    8 minread
    1,410words
    Intermediatelevel

    Search Trees

    A search tree is a tree data structure that is used to organize data in a hierarchical manner and supports efficient searching, insertion, and deletion operations. The structure of the tree is typically designed to optimize searching for particular types of data. In computer science, search trees are often used to represent sorted collections of data, where each node in the tree contains a value, and subtrees are ordered in a way that allows efficient searching.

    Key Features of Search Trees

    • Nodes: Each node contains a key or value.
    • Edges: The connections between the nodes that represent the relationships between values in the tree.
    • Parent and Child Nodes: Each node, except the root, has one parent node and can have zero or more child nodes.
    • Search Property: The tree is structured in such a way that it supports fast searching, often using a comparison-based method to traverse the tree.

    Types of Search Trees

    1. Binary Search Tree (BST)

      A Binary Search Tree is a type of search tree where each node has at most two children, typically referred to as the left child and the right child. The key in each node follows these properties:

      • 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.
      • There are no duplicate keys in the tree.

      Operations in BST:

      • Search: Starts from the root and recursively checks whether the key is smaller or larger than the current node's key to decide whether to search the left or right subtree.
      • Insert: Start from the root, compare the new key with the current node's key, and insert it in the appropriate subtree (left or right) based on comparisons.
      • Delete: Deletion of a node may involve three cases:
        1. If the node has no children (leaf node), just remove it.
        2. If the node has one child, remove the node and replace it with its child.
        3. If the node has two children, replace it with the node's inorder successor or predecessor.

      Time Complexity:

      • Average Case: O(log⁡n)O(\log n)O(logn)
      • Worst Case: O(n)O(n)O(n) (when the tree is unbalanced)

      Example: A Binary Search Tree of the numbers [10, 5, 15, 3, 7, 12, 18] would look like this:

            10
           /  \
          5    15
         / \   /  \
        3   7 12  18
      
    2. Balanced Binary Search Trees

      In a Balanced Binary Search Tree, the tree is structured to maintain balance, meaning that the height of the tree is kept logarithmic in relation to the number of nodes. This ensures that the tree remains efficient for search, insertion, and deletion operations.

      Types of Balanced BSTs:

      • AVL Tree: In an AVL tree, the heights of the two child subtrees of any node differ by at most one. If this condition is violated during insertion or deletion, the tree is rebalanced using rotations.
      • Red-Black Tree: A red-black tree is a binary search tree where each node is colored either red or black. It maintains several properties that help keep the tree balanced, such as ensuring the path from the root to any leaf node has the same number of black nodes.

      Time Complexity for Balanced BSTs:

      • Search, Insert, Delete: O(log⁡n)O(\log n)O(logn), where nnn is the number of nodes.
    3. B-tree

      A B-tree is a self-balancing search tree data structure that generalizes the binary search tree. It is primarily used in databases and file systems for efficient storage and retrieval. A B-tree allows nodes to have more than two children, which helps to minimize the number of disk accesses required during searching.

      Properties of a B-tree:

      • Each node contains a number of keys and has a corresponding number of children.
      • The tree is balanced: All leaf nodes are at the same level.
      • Keys in each node are stored in sorted order.
      • Internal nodes act as guides to help find keys in the subtrees.

      Operations:

      • Search: Similar to binary search in BST, but it checks multiple keys within a node and traverses down the appropriate subtree.
      • Insert: Inserting a key may require splitting a node if the node becomes full.
      • Delete: Deletion might cause a node to become underfilled, necessitating merging or redistributing nodes.

      Time Complexity:

      • Search, Insert, Delete: O(log⁡n)O(\log n)O(logn), where nnn is the number of keys in the tree.
    4. Trie (Prefix Tree)

      A Trie (or prefix tree) is a special type of search tree used to store a dynamic set or associative array where the keys are usually strings. A trie stores strings by breaking them into individual characters and creating a tree where each node represents a character of a string.

      Properties of a Trie:

      • Each node contains a part of a key (typically one character or symbol).
      • Nodes that share a common prefix are grouped together.
      • It is particularly useful for string matching or searching for words with common prefixes.

      Operations:

      • Search: Traverse the trie node by node, following the characters of the string to check if it exists.
      • Insert: Insert characters of the string one by one into the trie.
      • Delete: Delete a string by removing the characters from the trie, making sure to adjust the tree structure to maintain its validity.

      Time Complexity:

      • Search, Insert, Delete: O(L)O(L)O(L), where LLL is the length of the string being processed.
    5. Splay Tree

      A Splay Tree is a self-adjusting binary search tree where every operation (search, insert, delete) moves the accessed node to the root of the tree. This is done through a series of tree rotations (referred to as splaying). This approach leads to faster access for frequently accessed nodes, but it doesn't guarantee strict balance.

      Operations:

      • Search: The node found is moved to the root, potentially speeding up future accesses to the same node.
      • Insert: Insert a new node and splay it to the root.
      • Delete: Delete a node and re-splay the tree.

      Time Complexity:

      • Amortized Time: O(log⁡n)O(\log n)O(logn) for search, insert, and delete.
      • Worst-case Time: O(n)O(n)O(n) for a sequence of operations, but it’s rare.
    6. Binary Heap

      A Binary Heap is a complete binary tree that satisfies the heap property. It is primarily used to implement priority queues. In a max-heap, the value of each node is greater than or equal to the values of its children, and in a min-heap, the value of each node is less than or equal to the values of its children.

      Operations:

      • Insert: Insert a new element while maintaining the heap property (performed by "bubbling up").
      • Extract: Extract the root element (either maximum or minimum), then maintain the heap property by "bubbling down."

      Time Complexity:

      • Insert: O(log⁡n)O(\log n)O(logn)
      • Extract Min/Max: O(log⁡n)O(\log n)O(logn)

    Summary of Search Trees

    • Binary Search Tree (BST): A binary tree that stores keys such that for any node, the left child has a smaller key and the right child has a larger key.
    • Balanced Binary Search Trees (AVL Tree, Red-Black Tree): Variants of BST that maintain balance to ensure logarithmic time complexity for search, insertion, and deletion.
    • B-tree: A generalization of the binary search tree with more than two children per node, used in databases and file systems.
    • Trie: A search tree used for string storage and retrieval, where nodes represent characters of strings.
    • Splay Tree: A self-adjusting BST where accessed nodes are moved to the root for faster access in subsequent operations.
    • Binary Heap: A complete binary tree used to implement priority queues, either in max-heap or min-heap form.

    Each type of search tree has its own strengths and weaknesses, and the choice of which one to use depends on the specific problem requirements, such as the need for efficient searching, sorting, insertion, deletion, or priority queue operations.

    Previous topic 20
    Elements of Dynamic Programming
    Next topic 22
    Heaps

    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 time8 min
      Word count1,410
      Code examples0
      DifficultyIntermediate