diff --git a/README.md b/README.md index 4ae2272..3daf5d5 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,7 @@ Challenges, solutions, and unit tests are presented in the form of **IPython/Jup | Create a linked list for each level of a binary tree | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb) | | Check if a binary tree is balanced | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/check_balance/check_balance_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/check_balance/check_balance_solution.ipynb) | | Determine if a tree is a valid binary search tree | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst_validate/bst_validate_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst_validate/bst_validate_solution.ipynb) | +| Find the in-order successor of a given node in a binary search tree. | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst_successor/bst_successor_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst_successor/bst_successor_solution.ipynb) | | Implement a binary search tree | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst/bst_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst/bst_solution.ipynb) | | Implement depth-first search on a graph | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_dfs/dfs_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_dfs/dfs_solution.ipynb) | | Implement breadth-first search on a graph | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_bfs/bfs_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_bfs/bfs_solution.ipynb) | diff --git a/graphs_trees/bst_successor/__init__.py b/graphs_trees/bst_successor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/graphs_trees/bst_successor/bst_successor_challenge.ipynb b/graphs_trees/bst_successor/bst_successor_challenge.ipynb new file mode 100644 index 0000000..756019d --- /dev/null +++ b/graphs_trees/bst_successor/bst_successor_challenge.ipynb @@ -0,0 +1,200 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Find the in-order successor of a given node in a binary search tree.\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)\n", + "* [Solution Notebook](#Solution-Notebook)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "\n", + "* If there is no successor, do we return None?\n", + " * Yes\n", + "* Can we assume we already have a Node class that keeps track of parents?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "
\n",
+    "          _5_\n",
+    "        /     \\\n",
+    "       3       8\n",
+    "      / \\    /   \\\n",
+    "     2   4  6    12\n",
+    "    /        \\   / \\\n",
+    "   1          7 10  15\n",
+    "               /\n",
+    "              9\n",
+    "\n",
+    "In: 4  Out: 5\n",
+    "In: 5  Out: 6\n",
+    "In: 8  Out: 9\n",
+    "In: 15 Out: None\n",
+    "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst_successor/bst_successor_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%run ../bst/bst.py\n", + "%load ../bst/bst.py" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def bst_successor(node):\n", + " # TODO: Implement me\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**The following unit test is expected to fail until you solve the challenge.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# %load test_bst_successor.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestBstSuccessor(object):\n", + "\n", + " def test_bst_successor(self):\n", + " nodes = {}\n", + " node = Node(5)\n", + " nodes[5] = node\n", + " nodes[3] = insert(node, 3)\n", + " nodes[8] = insert(node, 8)\n", + " nodes[2] = insert(node, 2)\n", + " nodes[4] = insert(node, 4)\n", + " nodes[6] = insert(node, 6)\n", + " nodes[12] = insert(node, 12)\n", + " nodes[1] = insert(node, 1)\n", + " nodes[7] = insert(node, 7)\n", + " nodes[10] = insert(node, 10)\n", + " nodes[15] = insert(node, 15)\n", + " nodes[9] = insert(node, 9)\n", + "\n", + " assert_equal(bst_successor(nodes[4]), 5)\n", + " assert_equal(bst_successor(nodes[5]), 6)\n", + " assert_equal(bst_successor(nodes[8]), 9)\n", + " assert_equal(bst_successor(nodes[15]), None)\n", + "\n", + " print('Success: test_bst_successor')\n", + "\n", + "\n", + "def main():\n", + " test = TestBstSuccessor()\n", + " test.test_bst_successor()\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst_successor/bst_successor_solution.ipynb) for a discussion on algorithms and code solutions." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/graphs_trees/bst_successor/bst_successor_solution.ipynb b/graphs_trees/bst_successor/bst_successor_solution.ipynb new file mode 100644 index 0000000..64de61a --- /dev/null +++ b/graphs_trees/bst_successor/bst_successor_solution.ipynb @@ -0,0 +1,234 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Find the in-order successor of a given node in a binary search tree.\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", + "* If there is no successor, do we return None?\n", + " * Yes\n", + "* Can we assume we already have a Node class that keeps track of parents?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "
\n",
+    "          _5_\n",
+    "        /     \\\n",
+    "       3       8\n",
+    "      / \\    /   \\\n",
+    "     2   4  6    12\n",
+    "    /        \\   / \\\n",
+    "   1          7 10  15\n",
+    "               /\n",
+    "              9\n",
+    "\n",
+    "In: 4  Out: 5\n",
+    "In: 5  Out: 6\n",
+    "In: 8  Out: 9\n",
+    "In: 15 Out: None\n",
+    "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "* If the node has a right subtree, return the left-most node in the right subtree\n", + "* Else, go up until you find a node that is its parent's left node\n", + " * If you get to the root (ie node.parent is None), return None\n", + " * Else, return the parent\n", + " \n", + "Complexity:\n", + "* Time: O(n)\n", + "* Space: O(h), where h is the height of the tree" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%run ../bst/bst.py" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def left_most_node(node):\n", + " if node is None:\n", + " return\n", + " if node.left is None:\n", + " return node.data\n", + " return left_most_node(node.left)\n", + "\n", + "\n", + "def bst_successor(node):\n", + " if node is None:\n", + " return\n", + " if node.right is not None:\n", + " return left_most_node(node.right)\n", + " else:\n", + " parent = node.parent\n", + " while parent is not None and parent.left is not node:\n", + " node = node.parent\n", + " parent = node.parent\n", + " if parent is None:\n", + " return\n", + " return parent.data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting test_bst_successor.py\n" + ] + } + ], + "source": [ + "%%writefile test_bst_successor.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestBstSuccessor(object):\n", + "\n", + " def test_bst_successor(self):\n", + " nodes = {}\n", + " node = Node(5)\n", + " nodes[5] = node\n", + " nodes[3] = insert(node, 3)\n", + " nodes[8] = insert(node, 8)\n", + " nodes[2] = insert(node, 2)\n", + " nodes[4] = insert(node, 4)\n", + " nodes[6] = insert(node, 6)\n", + " nodes[12] = insert(node, 12)\n", + " nodes[1] = insert(node, 1)\n", + " nodes[7] = insert(node, 7)\n", + " nodes[10] = insert(node, 10)\n", + " nodes[15] = insert(node, 15)\n", + " nodes[9] = insert(node, 9)\n", + "\n", + " assert_equal(bst_successor(nodes[4]), 5)\n", + " assert_equal(bst_successor(nodes[5]), 6)\n", + " assert_equal(bst_successor(nodes[8]), 9)\n", + " assert_equal(bst_successor(nodes[15]), None)\n", + "\n", + " print('Success: test_bst_successor')\n", + "\n", + "\n", + "def main():\n", + " test = TestBstSuccessor()\n", + " test.test_bst_successor()\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_bst_successor\n" + ] + } + ], + "source": [ + "%run -i test_bst_successor.py" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/graphs_trees/bst_successor/test_bst_successor.py b/graphs_trees/bst_successor/test_bst_successor.py new file mode 100644 index 0000000..2e57be5 --- /dev/null +++ b/graphs_trees/bst_successor/test_bst_successor.py @@ -0,0 +1,36 @@ +from nose.tools import assert_equal + + +class TestBstSuccessor(object): + + def test_bst_successor(self): + nodes = {} + node = Node(5) + nodes[5] = node + nodes[3] = insert(node, 3) + nodes[8] = insert(node, 8) + nodes[2] = insert(node, 2) + nodes[4] = insert(node, 4) + nodes[6] = insert(node, 6) + nodes[12] = insert(node, 12) + nodes[1] = insert(node, 1) + nodes[7] = insert(node, 7) + nodes[10] = insert(node, 10) + nodes[15] = insert(node, 15) + nodes[9] = insert(node, 9) + + assert_equal(bst_successor(nodes[4]), 5) + assert_equal(bst_successor(nodes[5]), 6) + assert_equal(bst_successor(nodes[8]), 9) + assert_equal(bst_successor(nodes[15]), None) + + print('Success: test_bst_successor') + + +def main(): + test = TestBstSuccessor() + test.test_bst_successor() + + +if __name__ == '__main__': + main() \ No newline at end of file