Polish bst check balanced challenge and solution (#84)

Update constraints, tests, and code.
This commit is contained in:
Donne Martin
2016-06-25 22:13:15 -04:00
committed by GitHub
parent 589ff06b15
commit d3fd7bc0a1
3 changed files with 23 additions and 10 deletions

View File

@@ -36,7 +36,7 @@
"\n", "\n",
"* Is a balanced tree one where the heights of two sub trees of any node doesn't differ by more than 1?\n", "* Is a balanced tree one where the heights of two sub trees of any node doesn't differ by more than 1?\n",
" * Yes\n", " * Yes\n",
"* If this is called on a None input, should we return False?\n", "* If this is called on a None input, should we raise an exception?\n",
" * Yes\n", " * Yes\n",
"* Can we assume we already have a Node class with an insert method?\n", "* Can we assume we already have a Node class with an insert method?\n",
" * Yes\n", " * Yes\n",
@@ -121,13 +121,16 @@
"source": [ "source": [
"# %load test_check_balance.py\n", "# %load test_check_balance.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 TestCheckBalance(object):\n", "class TestCheckBalance(object):\n",
"\n", "\n",
" def test_check_balance(self):\n", " @raises(Exception)\n",
" assert_equal(check_balance(None), False)\n", " def test_check_balance_empty(self):\n",
" check_balance(None)\n",
"\n", "\n",
" def test_check_balance(self):\n",
" node = Node(5)\n", " node = Node(5)\n",
" assert_equal(check_balance(node), True)\n", " assert_equal(check_balance(node), True)\n",
"\n", "\n",
@@ -158,6 +161,7 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestCheckBalance()\n", " test = TestCheckBalance()\n",
" test.test_check_balance_empty()\n",
" test.test_check_balance()\n", " test.test_check_balance()\n",
"\n", "\n",
"\n", "\n",

View File

@@ -35,7 +35,7 @@
"\n", "\n",
"* Is a balanced tree one where the heights of two sub trees of any node doesn't differ by more than 1?\n", "* Is a balanced tree one where the heights of two sub trees of any node doesn't differ by more than 1?\n",
" * Yes\n", " * Yes\n",
"* If this is called on a None input, should we return False?\n", "* If this is called on a None input, should we raise an exception?\n",
" * Yes\n", " * Yes\n",
"* Can we assume we already have a Node class with an insert method?\n", "* Can we assume we already have a Node class with an insert method?\n",
" * Yes\n", " * Yes\n",
@@ -104,7 +104,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def _check_balance(root):\n", "def _check_balance(root):\n",
" if not root:\n", " if root is None:\n",
" return (True, 0, 0)\n", " return (True, 0, 0)\n",
" left_balanced, left_min_h, left_max_h = _check_balance(root.left)\n", " left_balanced, left_min_h, left_max_h = _check_balance(root.left)\n",
" right_balanced, right_min_h, right_max_h = _check_balance(root.right)\n", " right_balanced, right_min_h, right_max_h = _check_balance(root.right)\n",
@@ -113,9 +113,10 @@
" balanced = left_balanced and right_balanced and abs(max_h-min_h) <= 1\n", " balanced = left_balanced and right_balanced and abs(max_h-min_h) <= 1\n",
" return (balanced, min_h, max_h)\n", " return (balanced, min_h, max_h)\n",
"\n", "\n",
"\n",
"def check_balance(root):\n", "def check_balance(root):\n",
" if root is None:\n", " if root is None:\n",
" return False\n", " raise Exception('No root node')\n",
" balanced, _, _ = _check_balance(root)\n", " balanced, _, _ = _check_balance(root)\n",
" return balanced" " return balanced"
] ]
@@ -145,13 +146,16 @@
"source": [ "source": [
"%%writefile test_check_balance.py\n", "%%writefile test_check_balance.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 TestCheckBalance(object):\n", "class TestCheckBalance(object):\n",
"\n", "\n",
" def test_check_balance(self):\n", " @raises(Exception)\n",
" assert_equal(check_balance(None), False)\n", " def test_check_balance_empty(self):\n",
" check_balance(None)\n",
"\n", "\n",
" def test_check_balance(self):\n",
" node = Node(5)\n", " node = Node(5)\n",
" assert_equal(check_balance(node), True)\n", " assert_equal(check_balance(node), True)\n",
"\n", "\n",
@@ -182,6 +186,7 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestCheckBalance()\n", " test = TestCheckBalance()\n",
" test.test_check_balance_empty()\n",
" test.test_check_balance()\n", " test.test_check_balance()\n",
"\n", "\n",
"\n", "\n",

View File

@@ -1,11 +1,14 @@
from nose.tools import assert_equal from nose.tools import assert_equal
from nose.tools import raises
class TestCheckBalance(object): class TestCheckBalance(object):
def test_check_balance(self): @raises(Exception)
assert_equal(check_balance(None), False) def test_check_balance_empty(self):
check_balance(None)
def test_check_balance(self):
node = Node(5) node = Node(5)
assert_equal(check_balance(node), True) assert_equal(check_balance(node), True)
@@ -36,6 +39,7 @@ class TestCheckBalance(object):
def main(): def main():
test = TestCheckBalance() test = TestCheckBalance()
test.test_check_balance_empty()
test.test_check_balance() test.test_check_balance()