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 PortalCOMP3138Tree Searching Algorit…
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
    6 min read
    Topic 29 of 53
    Topic Study Note Illustration
    29

    Tree Searching Algorithms

    Searching in a tree involves finding a specific value (or node) based on a given key. The process of searching depends on the type of tree being...

    Tree Searching Algorithms

    Searching in a tree involves finding a specific value (or node) based on a given key. The process of searching depends on the type of tree being used. Common tree structures include Binary Search Trees (BSTs), AVL trees, Red-Black trees, and general trees. Let's break down how searching works in these different types of trees.


    1. Searching in a Binary Search Tree (BST)

    In a Binary Search Tree, the search operation takes advantage of the tree's specific ordering property, where for every node:

    • The left child contains smaller values.
    • The right child contains larger values.

    Steps for Searching in a BST:

    1. Start at the root: Begin your search from the root node of the tree.
    2. Compare the key with the current node’s value:
      • If the key is equal to the current node’s key, you've found the node and the search is complete.
      • If the key is smaller, move to the left child (since all values in the left subtree are smaller).
      • If the key is larger, move to the right child (since all values in the right subtree are larger).
    3. Repeat the process: Continue comparing and moving left or right until you find the key or reach a NULL child (i.e., the key is not in the tree).
    4. End the search: The search ends when the key is found or when the traversal reaches a leaf node (indicating that the key does not exist in the tree).

    Time Complexity:

    • Average Case: O(log n), where n is the number of nodes. This is because the tree is balanced, and you halve the search space with each comparison.
    • Worst Case: O(n) if the tree is unbalanced (e.g., inserted in sorted order, degenerating into a linked list).

    2. Searching in a Balanced Tree (e.g., AVL Tree, Red-Black Tree)

    In a balanced tree (such as an AVL tree or a Red-Black tree), the tree structure ensures that the height is balanced, maintaining O(log n) time complexity for search operations.

    Steps for Searching in a Balanced Tree:

    • The steps for searching in balanced trees are similar to those for a Binary Search Tree (BST) because both trees maintain the ordering property. However, the difference is that in balanced trees, the height is kept logarithmic, ensuring that the search remains efficient.
    1. Start at the root.
    2. Compare the key with the current node’s value:
      • If the key is equal to the current node's key, return the node.
      • If the key is smaller, move to the left child.
      • If the key is larger, move to the right child.
    3. Repeat the process until the key is found or the search reaches a NULL node.

    Time Complexity:

    • O(log n) for both AVL trees and Red-Black trees, as they are balanced, and the search space is halved at each step.

    3. Searching in a General Tree

    In a general tree (a tree that doesn't necessarily follow a specific ordering like BSTs), the search can be more complicated, since the left and right children do not have any specific order. This type of tree is typically used for hierarchical data, where there’s no requirement for sorting.

    Steps for Searching in a General Tree:

    1. Start at the root of the tree.

    2. Traverse each child node:

      • At each node, compare the key with the node's value.
      • If a match is found, return the node.
      • Otherwise, recursively search all child nodes of the current node.
    3. Use Depth-First Search (DFS) or Breadth-First Search (BFS) to explore the tree:

      • DFS: Traverse down one path (using a stack or recursion), exploring as far as possible before backtracking.
      • BFS: Traverse the tree level by level, exploring all nodes at the current level before moving on to the next.
    4. Repeat the search process until the key is found or all nodes are explored.

    Time Complexity:

    • O(n), where n is the number of nodes, because in the worst case, you might have to visit every node in the tree.

    4. Searching in a Trie (Prefix Tree)

    A Trie is a special type of tree used to store a collection of strings or sequences. It is especially useful in problems like autocomplete, spell checking, and dictionary search.

    Steps for Searching in a Trie:

    1. Start at the root of the Trie.
    2. Iterate through each character of the string (key) to be searched.
      • For each character, move to the corresponding child node.
      • If at any point, the character doesn't have a corresponding child, the key is not in the Trie.
    3. If you successfully reach the end of the key and the current node marks the end of a valid string, then the key exists in the Trie.

    Time Complexity:

    • O(k), where k is the length of the string being searched. The search does not depend on the total number of keys but only on the length of the key.

    Summary

    The search operation in trees can vary depending on the type of tree:

    • Binary Search Tree (BST): Efficient searching with O(log n) time complexity in a balanced tree, though it can degrade to O(n) in an unbalanced tree.
    • Balanced Trees (e.g., AVL, Red-Black): Maintain O(log n) search complexity by ensuring the tree remains balanced.
    • General Trees: Search can be slower, with a O(n) time complexity, as nodes are not ordered.
    • Trie: Optimized for string searches with O(k) time complexity, where k is the length of the key.

    The choice of tree for searching depends on the type of data and the specific requirements of the problem, such as whether fast searching, insertion, or ordered data retrieval is needed.

    ← Previous topic 28
    Binary Search Tree Basics
    Next topic 30 →
    Analysis of 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%
    6mremaining
    956words
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Design and Analysis of Algorithms