Files
interactive-coding-challenges/graphs_trees/bst_successor/bst_successor_challenge.ipynb
Donne Martin 7b573ceaa3 Polish bst successor challenge and solution (#82)
Update constraints, test cases, tests, algorithm discussion, and code.
2016-06-25 22:06:35 -04:00

212 lines
5.2 KiB
Python

{
"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",
"* If the input is None, should we throw an exception?\n",
" * Yes\n",
"* Can we assume we already have a Node class that keeps track of parents?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"<pre>\n",
" _5_\n",
" / \\\n",
" 3 8\n",
" / \\ / \\\n",
" 2 4 6 12\n",
" / \\ / \\\n",
" 1 7 10 15\n",
" /\n",
" 9\n",
"\n",
"In: None Out: Exception\n",
"In: 4 Out: 5\n",
"In: 5 Out: 6\n",
"In: 8 Out: 9\n",
"In: 15 Out: None\n",
"</pre>"
]
},
{
"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",
"from nose.tools import raises\n",
"\n",
"\n",
"class TestBstSuccessor(object):\n",
"\n",
" @raises(Exception)\n",
" def test_bst_successor_empty(self):\n",
" bst_successor(None)\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",
" test.test_bst_successor_empty()\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.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}