Flower Finding - codepath/compsci_guides GitHub Wiki

Unit 8 Session 2 Standard (Click for link to problem statements)

Unit 8 Session 2 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10-15 mins
  • 🛠️ Topics: Trees, Binary Search Trees, Recursion

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What is the structure of the tree?
    • The tree is a Binary Search Tree (BST) where each node is a plant in the store.
  • What are we searching for in the tree?
    • We are searching for a specific flower name in the BST.
  • What should be returned?
    • The function should return True if the flower is found in the tree, and False otherwise.
HAPPY CASE
Input: ["Rose", "Lilac", "Tulip", "Daisy", "Lily", None, "Violet"], "Lilac"
Output: True
Explanation: "Lilac" exists in the tree as a node.

EDGE CASE
Input: ["Rose", "Lilac", "Tulip", "Daisy", "Lily", None, "Violet"], "Sunflower"
Output: False
Explanation: "Sunflower" does not exist in the tree, so the output is `False`.

2: M-atch

Match what this problem looks like to known categories of problems, e.g., Linked List or Dynamic Programming, and strategies or patterns in those categories.

For Binary Search Tree problems, we want to consider the following approaches:

  • Binary Search: Leverage the properties of a BST where left subtree nodes are smaller and right subtree nodes are larger than the current node. This allows for efficient searching.
  • Recursion: Recursively search the left or right subtree based on the comparison between the target and current node value.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use the properties of a BST to efficiently search for the target flower by recursively comparing the target value with the current node.

1) If the current node is `None`, return `False`.
2) Compare the target flower name with the current node's value:
    - If they are equal, return `True`.
    - If the target name is smaller, search the left subtree.
    - If the target name is larger, search the right subtree.
3) Continue this process until the target is found or a leaf node is reached.

⚠️ Common Mistakes

  • Forgetting to check if the tree is empty.
  • Not handling the case where the target flower is not present in the tree.

4: I-mplement

Implement the code to solve the algorithm.

class TreeNode():
    def __init__(self, value, left=None, right=None):
        self.val = value
        self.left = left
        self.right = right

def find_flower(inventory, name):
    if inventory is None:
        return False
    
    if inventory.val == name:
        return True
    elif name < inventory.val:
        return find_flower(inventory.left, name)
    else:
        return find_flower(inventory.right, name)

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

- Example 1:
    - Input: `garden = TreeNode("Rose", TreeNode("Lilac", TreeNode("Daisy"), TreeNode("Lily")), TreeNode("Tulip", None, TreeNode("Violet")))`, `name = "Lilac"`
    - Execution: 
        - Start at "Rose", "Lilac" < "Rose", move to the left child "Lily".
        - "Lilac" < "Lily", move to the right child "Lilac".
        - Found "Lilac", return `True`.
    - Output: `True`
- Example 2:
    - Input: `garden = TreeNode("Rose", TreeNode("Lilac", TreeNode("Daisy"), TreeNode("Lily")), TreeNode("Tulip", None, TreeNode("Violet")))`, `name = "Sunflower"`
    - Execution: 
        - Start at "Rose", "Sunflower" > "Rose", move to the right child "Tulip".
        - "Sunflower" > "Tulip", move to the right child "Violet".
        - "Sunflower" > "Violet", no right child, return `False`.
    - Output: `False`

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume N represents the number of nodes in the tree.

  • Time Complexity: O(H) where H is the height of the tree. In the worst case, this is O(N) for a skewed tree, but in the average case for a balanced BST, it's O(log N).
  • Space Complexity: O(H) due to the recursive call stack, which is also O(N) in the worst case and O(log N) in the average case.