diff --git a/graphs_trees/bst/bst.py b/graphs_trees/bst/bst.py index 57df388..39a9836 100644 --- a/graphs_trees/bst/bst.py +++ b/graphs_trees/bst/bst.py @@ -11,20 +11,19 @@ class Node(object): def insert(root, data): + # Constraint: Assume we are working with valid ints if root is None: root = Node(data) return root if data <= root.data: if root.left is None: - root.left = Node(data) + root.left = insert(root.left, data) root.left.parent = root - return root.left else: - return insert(root.left, data) + insert(root.left, data) else: if root.right is None: - root.right = Node(data) + root.right = insert(root.right, data) root.right.parent = root - return root.right else: - return insert(root.right, data) \ No newline at end of file + insert(root.right, data) \ No newline at end of file diff --git a/graphs_trees/bst/bst_challenge.ipynb b/graphs_trees/bst/bst_challenge.ipynb index a3c1a34..bcf6340 100644 --- a/graphs_trees/bst/bst_challenge.ipynb +++ b/graphs_trees/bst/bst_challenge.ipynb @@ -33,6 +33,8 @@ "source": [ "## Constraints\n", "\n", + "* Can we insert None values?\n", + " * No\n", "* Can we assume we are working with valid integers?\n", " * Yes\n", "* Can we assume all left descendents <= n < all right descendents?\n", @@ -40,7 +42,9 @@ "* For simplicity, can we use just a Node class without a wrapper Tree class?\n", " * Yes\n", "* Do we have to keep track of the parent nodes?\n", - " * This is optional" + " * This is optional\n", + "* Can we assume this fits in memory?\n", + " * Yes" ] }, { @@ -53,12 +57,14 @@ "\n", "Insert will be tested through the following traversal:\n", "\n", - "### In-Order Traversal (Provided)\n", + "### In-Order Traversal\n", "\n", "* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\n", "* 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." + "If the `root` input is `None`, return a tree with the only element being the new root node.\n", + "\n", + "You do not have to code the in-order traversal, it is part of the unit test." ] }, { @@ -206,7 +212,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.0" } }, "nbformat": 4, diff --git a/graphs_trees/bst/bst_solution.ipynb b/graphs_trees/bst/bst_solution.ipynb index 54f9445..3bab5e0 100644 --- a/graphs_trees/bst/bst_solution.ipynb +++ b/graphs_trees/bst/bst_solution.ipynb @@ -33,6 +33,8 @@ "source": [ "## Constraints\n", "\n", + "* Can we insert None values?\n", + " * No\n", "* Can we assume we are working with valid integers?\n", " * Yes\n", "* Can we assume all left descendents <= n < all right descendents?\n", @@ -40,7 +42,9 @@ "* For simplicity, can we use just a Node class without a wrapper Tree class?\n", " * Yes\n", "* Do we have to keep track of the parent nodes?\n", - " * This is optional" + " * This is optional\n", + "* Can we assume this fits in memory?\n", + " * Yes" ] }, { @@ -53,12 +57,14 @@ "\n", "Insert will be tested through the following traversal:\n", "\n", - "### In-Order Traversal (Provided)\n", + "### In-Order Traversal\n", "\n", "* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\n", "* 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." + "If the `root` input is `None`, return a tree with the only element being the new root node.\n", + "\n", + "You do not have to code the in-order traversal, it is part of the unit test." ] }, { @@ -122,23 +128,22 @@ "\n", "\n", "def insert(root, data):\n", + " # Constraint: Assume we are working with valid ints\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", + " root.left = insert(root.left, data)\n", " root.left.parent = root\n", - " return root.left\n", " else:\n", - " return insert(root.left, data)\n", + " insert(root.left, data)\n", " else:\n", " if root.right is None:\n", - " root.right = Node(data)\n", + " root.right = insert(root.right, data)\n", " root.right.parent = root\n", - " return root.right\n", " else:\n", - " return insert(root.right, data)" + " insert(root.right, data)" ] }, { @@ -272,7 +277,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.0" } }, "nbformat": 4,