diff --git a/graphs_trees/bst/bst.py b/graphs_trees/bst/bst.py index ce6a16b..57df388 100644 --- a/graphs_trees/bst/bst.py +++ b/graphs_trees/bst/bst.py @@ -11,6 +11,9 @@ class Node(object): def insert(root, data): + if root is None: + root = Node(data) + return root if data <= root.data: if root.left is None: root.left = Node(data) diff --git a/graphs_trees/bst/bst_challenge.ipynb b/graphs_trees/bst/bst_challenge.ipynb index df23770..a3c1a34 100644 --- a/graphs_trees/bst/bst_challenge.ipynb +++ b/graphs_trees/bst/bst_challenge.ipynb @@ -56,7 +56,9 @@ "### 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" + "* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5\n", + "\n", + "If the `root` input is `None`, return a tree with the only element being the new root node." ] }, { @@ -158,7 +160,7 @@ " assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n", " self.results.clear_results()\n", "\n", - " node = Node(1)\n", + " node = insert(None, 1)\n", " insert(node, 2)\n", " insert(node, 3)\n", " insert(node, 4)\n", @@ -190,21 +192,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.4.3" } }, "nbformat": 4, diff --git a/graphs_trees/bst/bst_solution.ipynb b/graphs_trees/bst/bst_solution.ipynb index 36facb0..54f9445 100644 --- a/graphs_trees/bst/bst_solution.ipynb +++ b/graphs_trees/bst/bst_solution.ipynb @@ -56,7 +56,9 @@ "### 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" + "* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5\n", + "\n", + "If the `root` input is `None`, return a tree with the only element being the new root node." ] }, { @@ -67,6 +69,7 @@ "\n", "### Insert\n", "\n", + "* If the root is None, return Node(data)\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", @@ -76,8 +79,10 @@ "\n", "Complexity:\n", "\n", - "* Time: O(log n)\n", - "* Space: O(1)" + "* Time: O(h), where h is the height of the tree\n", + " * In a balanced tree, the height is O(nlogn)\n", + " * In the worst case we have a linked list structure with O(n)\n", + "* Space: O(m), where m is the recursion depth, or O(1) if using an iterative approach" ] }, { @@ -117,6 +122,9 @@ "\n", "\n", "def insert(root, data):\n", + " if root is None:\n", + " root = Node(data)\n", + " return root\n", " if data <= root.data:\n", " if root.left is None:\n", " root.left = Node(data)\n", @@ -208,7 +216,7 @@ " assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n", " self.results.clear_results()\n", "\n", - " node = Node(1)\n", + " node = insert(None, 1)\n", " insert(node, 2)\n", " insert(node, 3)\n", " insert(node, 4)\n", @@ -250,21 +258,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.4.3" } }, "nbformat": 4, diff --git a/graphs_trees/bst/test_bst.py b/graphs_trees/bst/test_bst.py index 24f9f43..7f4f895 100644 --- a/graphs_trees/bst/test_bst.py +++ b/graphs_trees/bst/test_bst.py @@ -16,7 +16,7 @@ class TestTree(object): assert_equal(str(self.results), '[1, 2, 3, 5, 8]') self.results.clear_results() - node = Node(1) + node = insert(None, 1) insert(node, 2) insert(node, 3) insert(node, 4)