"<small><i>This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement a binary search tree with an insert method.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we assume we are working with valid integers?\n",
" * Yes\n",
"* Can we assume all left descendents <= n < all right descendents?\n",
" * Yes\n",
"* For simplicity, can we use just a Node class without a wrapper Tree class?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"### Insert\n",
"\n",
"Insert will be tested through the following traversal:\n",
"\n",
"### In-Order Traversal (Provided)\n",
"\n",
"* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\n",
"* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"### Insert\n",
"\n",
"* If the data is <= the current node's data\n",
" * If the current node's left child is None, set it to Node(data)\n",
" * Else, recursively call insert on the left child\n",
"* Else\n",
" * If the current node's right child is None, set it to Node(data)\n",
" * Else, recursively call insert on the right child\n",