Back to Freecodecamp

Step 16

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

latest958 B
Original Source

--description--

Now, to perform the actual insertion, define an empty insert method within the BinarySearchTree class and give it a self parameter.

--hints--

You should define an insert method with a self parameter within the BinarySearchTree class. Remember the pass keyword.

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

--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--    

--fcc-editable-region--