Codepath

Adding a New Plant to the Collection

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-Medium
  • Time to complete: 15-20 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 represents a houseplant in the collection.
  • What operation needs to be performed?
    • A new node with the given plant name should be inserted into the BST while maintaining the BST properties.
  • What should be returned?
    • The function should return the root of the updated BST.
HAPPY CASE
Input: ["FiddleLeafFig", "Monstera", "SnakePlant"], "Aloe"
Output: The updated BST with "Aloe" as the left child of "Monstera".
Explanation: The tree is updated to include "Aloe" while maintaining the BST properties.

EDGE CASE
Input: [], "Aloe"
Output: A new tree with "Aloe" as the root.
Explanation: An empty tree should return a new tree with only the "Aloe" node.

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 (BST) problems, we want to consider the following approaches:

  • Insertion in BST: The problem requires inserting a new node into a BST, which involves finding the correct position to maintain the tree's ordered structure.
  • Recursion: A recursive approach can be used to traverse the tree and insert the new node at the correct position.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the BST to find the appropriate location for the new node based on the plant's name. Insert the new node while maintaining the BST properties.

1) If the current node (`collection`) is `None`, create a new `TreeNode` with the given plant name.
2) If the new plant name is less than the current node's value, recurse on the left subtree.
3) If the new plant name is greater than or equal to the current node's value, recurse on the right subtree.
4) After the recursive call, return the current node to link the updated subtree to the original tree.
5) Return the root of the updated tree.

⚠️ Common Mistakes

  • Forgetting to handle the case where the tree is empty.
  • Not properly returning the updated root after insertion.

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 add_plant(collection, name):
    # Base case: If the tree is empty, create a new node with the plant name
    if collection is None:
        return TreeNode(name)
    
    # Recursive case: Traverse the tree to find the correct insertion point
    if name < collection.val:
        collection.left = add_plant(collection.left, name)
    else:
        collection.right = add_plant(collection.right, name)
    
    return collection

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: `collection = TreeNode("FiddleLeafFig", TreeNode("Monstera"), TreeNode("SnakePlant"))`, `name = "Aloe"`
    - Execution: 
        - Start at "FiddleLeafFig", "Aloe" < "FiddleLeafFig", move to the left child "Monstera".
        - "Aloe" < "Monstera", left child is `None`, insert "Aloe" as the left child.
    - Output: The tree now has "Aloe" as the left child of "Monstera".
- Example 2:
    - Input: `collection = None`, `name = "Aloe"`
    - Execution: Tree is empty, so "Aloe" becomes the root.
    - Output: The tree now has a single node with "Aloe".

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 of a skewed tree, this could be O(N).
  • Space Complexity: O(H) due to the recursive call stack. This could be O(N) in the worst case for a skewed tree.
Fork me on GitHub