Polish bst successor challenge and solution (#82)

Update constraints, test cases, tests, algorithm discussion, and code.
This commit is contained in:
Donne Martin
2016-06-25 22:06:35 -04:00
committed by GitHub
parent 042161dc3b
commit 7b573ceaa3
3 changed files with 46 additions and 15 deletions

View File

@@ -36,7 +36,11 @@
"\n", "\n",
"* If there is no successor, do we return None?\n", "* If there is no successor, do we return None?\n",
" * Yes\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", "* 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" " * Yes"
] ]
}, },
@@ -57,6 +61,7 @@
" /\n", " /\n",
" 9\n", " 9\n",
"\n", "\n",
"In: None Out: Exception\n",
"In: 4 Out: 5\n", "In: 4 Out: 5\n",
"In: 5 Out: 6\n", "In: 5 Out: 6\n",
"In: 8 Out: 9\n", "In: 8 Out: 9\n",
@@ -129,10 +134,15 @@
"source": [ "source": [
"# %load test_bst_successor.py\n", "# %load test_bst_successor.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
"from nose.tools import raises\n",
"\n", "\n",
"\n", "\n",
"class TestBstSuccessor(object):\n", "class TestBstSuccessor(object):\n",
"\n", "\n",
" @raises(Exception)\n",
" def test_bst_successor_empty(self):\n",
" bst_successor(None)\n",
"\n",
" def test_bst_successor(self):\n", " def test_bst_successor(self):\n",
" nodes = {}\n", " nodes = {}\n",
" node = Node(5)\n", " node = Node(5)\n",
@@ -160,6 +170,7 @@
"def main():\n", "def main():\n",
" test = TestBstSuccessor()\n", " test = TestBstSuccessor()\n",
" test.test_bst_successor()\n", " test.test_bst_successor()\n",
" test.test_bst_successor_empty()\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
@@ -192,7 +203,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.4.3" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@@ -35,7 +35,11 @@
"\n", "\n",
"* If there is no successor, do we return None?\n", "* If there is no successor, do we return None?\n",
" * Yes\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", "* 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" " * Yes"
] ]
}, },
@@ -56,6 +60,7 @@
" /\n", " /\n",
" 9\n", " 9\n",
"\n", "\n",
"In: None Out: Exception\n",
"In: 4 Out: 5\n", "In: 4 Out: 5\n",
"In: 5 Out: 6\n", "In: 5 Out: 6\n",
"In: 8 Out: 9\n", "In: 8 Out: 9\n",
@@ -72,11 +77,12 @@
"* If the node has a right subtree, return the left-most node in the right subtree\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", "* 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", " * If you get to the root (ie node.parent is None), return None\n",
" * The original input node must be the largest in the tree\n",
" * Else, return the parent\n", " * Else, return the parent\n",
" \n", " \n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\n", "* Time: O(h), where h is the height of the tree\n",
"* Space: O(h), where h is the height of the tree" "* Space: O(h), where h is the recursion depth (tree height), or O(1) if using an iterative approach"
] ]
}, },
{ {
@@ -107,7 +113,7 @@
"source": [ "source": [
"def left_most_node(node):\n", "def left_most_node(node):\n",
" if node is None:\n", " if node is None:\n",
" return\n", " return None\n",
" if node.left is None:\n", " if node.left is None:\n",
" return node.data\n", " return node.data\n",
" return left_most_node(node.left)\n", " return left_most_node(node.left)\n",
@@ -115,7 +121,7 @@
"\n", "\n",
"def bst_successor(node):\n", "def bst_successor(node):\n",
" if node is None:\n", " if node is None:\n",
" return\n", " raise Exception('Invalid input node')\n",
" if node.right is not None:\n", " if node.right is not None:\n",
" return left_most_node(node.right)\n", " return left_most_node(node.right)\n",
" else:\n", " else:\n",
@@ -124,7 +130,9 @@
" node = node.parent\n", " node = node.parent\n",
" parent = node.parent\n", " parent = node.parent\n",
" if parent is None:\n", " if parent is None:\n",
" return\n", " # We reached the root, the original input node\n",
" # must be the largest element in the tree.\n",
" return None\n",
" return parent.data" " return parent.data"
] ]
}, },
@@ -153,10 +161,15 @@
"source": [ "source": [
"%%writefile test_bst_successor.py\n", "%%writefile test_bst_successor.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
"from nose.tools import raises\n",
"\n", "\n",
"\n", "\n",
"class TestBstSuccessor(object):\n", "class TestBstSuccessor(object):\n",
"\n", "\n",
" @raises(Exception)\n",
" def test_bst_successor_empty(self):\n",
" bst_successor(None)\n",
"\n",
" def test_bst_successor(self):\n", " def test_bst_successor(self):\n",
" nodes = {}\n", " nodes = {}\n",
" node = Node(5)\n", " node = Node(5)\n",
@@ -184,6 +197,7 @@
"def main():\n", "def main():\n",
" test = TestBstSuccessor()\n", " test = TestBstSuccessor()\n",
" test.test_bst_successor()\n", " test.test_bst_successor()\n",
" test.test_bst_successor_empty()\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
@@ -226,7 +240,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.4.3" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@@ -1,8 +1,13 @@
from nose.tools import assert_equal from nose.tools import assert_equal
from nose.tools import raises
class TestBstSuccessor(object): class TestBstSuccessor(object):
@raises(Exception)
def test_bst_successor_empty(self):
bst_successor(None)
def test_bst_successor(self): def test_bst_successor(self):
nodes = {} nodes = {}
node = Node(5) node = Node(5)
@@ -30,6 +35,7 @@ class TestBstSuccessor(object):
def main(): def main():
test = TestBstSuccessor() test = TestBstSuccessor()
test.test_bst_successor() test.test_bst_successor()
test.test_bst_successor_empty()
if __name__ == '__main__': if __name__ == '__main__':