Back to Freecodecamp

Step 17

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

latest1.2 KB
Original Source

--description--

The insert method will be called by the user. In addition to the self parameter, it will also need a key parameter. This parameter will be the key value to insert into the binary search tree.

Add key as the second parameter to the function definition.

--hints--

The insert method should contain the self parameter.

js
({ test: () => assert.match(code, /def\s+insert\s*\(\s*self\s*,/) });

The insert method should contain the key parameter.

js
({ test: () => assert.match(code, /def\s+insert\s*\(\s*self\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

    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:
            node.right = self._insert(node.right, key)
        return node

--fcc-editable-region--            
    def insert(self):
        pass

--fcc-editable-region--