Back to Freecodecamp

Step 10

curriculum/challenges/english/blocks/learn-tree-traversal-by-building-a-binary-search-tree/65c4f22498d22ed775ef8efb.md

latest1.7 KB
Original Source

--description--

Now you need to check if the node parameter is None. If it is, this means that the method has reached a leaf node or an empty spot in the tree where the new node should be inserted.

Inside the _insert method body, replace pass with an if statement that checks if node is None.

Inside the new if block, return TreeNode(key) to create a new TreeNode instance with the provided key. This will become the new leaf node, effectively inserting the key into the tree.

--hints--

You should not have pass in your _insert method.

js
({ test: () => assert.isFalse(runPython(`_Node(_code).find_class("BinarySearchTree").find_function("_insert").has_pass()`)) })

You should write an if statement to check if node is None.

js
({ test: () =>
  {
    const transformedCode = code.replace(/\r/g, "");        
    const ins = __helpers.python.getDef("\n"+transformedCode, "_insert");
    const {function_body} = ins;    
    assert(function_body.match(/^\s{8}if\s+node\s+is\s+None\s*:/m));
  }
})

You should return TreeNode(key) from the if block.

js
({ test: () =>
  {
    const transformedCode = code.replace(/\r/g, "");        
    const ins = __helpers.python.getDef("\n"+transformedCode, "_insert");
    const {function_body} = ins;    
    assert(function_body.match(/^(\s{8})if\s+node\s+is\s+None\s*:\s*^\1\s{4}return\s+TreeNode\s*\(\s*key\s*\)/m));
  }
})

--seed--

--seed-contents--

py
class TreeNode:

    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

--fcc-editable-region--

class BinarySearchTree:

    def __init__(self):
        self.root = None

    def _insert(self, node, key):
        pass
--fcc-editable-region--