Back to Freecodecamp

Step 14

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

latest1.4 KB
Original Source

--description--

Inside your elif clause, replace pass with a call to the _insert method with right child of the current node as the first argument and key as the second argument. Assign the result to the right attribute of the current node.

--hints--

You should remove the pass keyword from the elif block.

js
({
  test: () => {
    const pyClassStr = runPython(
      `str(_Node(_code).find_class("BinarySearchTree"))`
    );
    assert.notInclude(pyClassStr, "pass");
  },
});

You should call the self._insert() method passing node.right and key as the arguments.

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

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

js
({ test: () => assert.match(code, /node\.right\s*=\s*self\._insert\(\s*node\.right\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:
            node.left = self._insert(node.left, key)
        elif key > node.key:
            pass

--fcc-editable-region--