Back to Freecodecamp

Step 12

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

latest1.4 KB
Original Source

--description--

If key < node.key evaluates to True, then the new node should be placed in the left subtree.

Delete pass and recursively call the _insert method with left child as the first argument and key as the second argument. Assign the result to the left attribute of the current node.

--hints--

You should remove the pass keyword from the if block.

js
({
  test: () => {
    assert.isFalse(
      runPython(
        `_Node(_code).find_class("BinarySearchTree").find_function("_insert").find_if("key < node.key").find_bodies()[0].has_pass()`
      )
    );
  },
});

You should call the self._insert method passing node.left and key as the arguments.

js
({ test: () => assert.match(code, /self\._insert\(\s*node\.left\s*,\s*key\s*\)/) });

You should assign the result of your self._insert() call to the left attribute of the current node.

js
({ test: () => assert.match(code, /node\.left\s*=\s*self\._insert\(\s*node\.left\s*,\s*key\s*\)/) });

--seed--

--seed-contents--

py
class TreeNode:

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


class BinarySearchTree:

    def __init__(self):
        self.root = None
--fcc-editable-region--    

    def _insert(self, node, key):
        if node is None:
            return TreeNode(key)

        if key < node.key:
            pass
--fcc-editable-region--