Polish check tree balance challenge and solution (#80)

Update constraints, test cases, tests, and code.
This commit is contained in:
Donne Martin 2016-06-25 08:42:16 -04:00 committed by GitHub
parent 996058195d
commit 7882ed9ae7
3 changed files with 52 additions and 17 deletions

View File

@ -36,7 +36,11 @@
"\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",
" * 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",
"* Can we assume this fits memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -46,6 +50,8 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* None -> No\n",
"* 1 -> Yes\n",
"* 5, 3, 8, 1, 4 -> Yes\n", "* 5, 3, 8, 1, 4 -> Yes\n",
"* 5, 3, 8, 9, 10 -> No" "* 5, 3, 8, 9, 10 -> No"
] ]
@ -120,7 +126,11 @@
"class TestCheckBalance(object):\n", "class TestCheckBalance(object):\n",
"\n", "\n",
" def test_check_balance(self):\n", " def test_check_balance(self):\n",
" assert_equal(check_balance(None), False)\n",
"\n",
" node = Node(5)\n", " node = Node(5)\n",
" assert_equal(check_balance(node), True)\n",
"\n",
" insert(node, 3)\n", " insert(node, 3)\n",
" insert(node, 8)\n", " insert(node, 8)\n",
" insert(node, 1)\n", " insert(node, 1)\n",
@ -134,6 +144,15 @@
" insert(node, 10)\n", " insert(node, 10)\n",
" assert_equal(check_balance(node), False)\n", " assert_equal(check_balance(node), False)\n",
"\n", "\n",
" node = Node(3)\n",
" insert(node, 2)\n",
" insert(node, 1)\n",
" insert(node, 5)\n",
" insert(node, 4)\n",
" insert(node, 6)\n",
" insert(node, 7)\n",
" assert_equal(check_balance(node), False)\n",
"\n",
" print('Success: test_check_balance')\n", " print('Success: test_check_balance')\n",
"\n", "\n",
"\n", "\n",
@ -158,21 +177,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -35,7 +35,11 @@
"\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",
" * 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",
"* Can we assume this fits memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -45,6 +49,8 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* None -> No\n",
"* 1 -> Yes\n",
"* 5, 3, 8, 1, 4 -> Yes\n", "* 5, 3, 8, 1, 4 -> Yes\n",
"* 5, 3, 8, 9, 10 -> No" "* 5, 3, 8, 9, 10 -> No"
] ]
@ -97,18 +103,20 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def __check_balance__(root):\n", "def _check_balance(root):\n",
" if not root:\n", " if not root:\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",
" min_h = min(left_min_h, right_min_h) + 1\n", " min_h = 1 + min(left_min_h, right_min_h)\n",
" max_h = max(left_max_h, right_max_h) + 1\n", " max_h = 1 + max(left_max_h, right_max_h)\n",
" 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",
"def check_balance(root):\n", "def check_balance(root):\n",
" balanced, _, _ = __check_balance__(root)\n", " if root is None:\n",
" return False\n",
" balanced, _, _ = _check_balance(root)\n",
" return balanced" " return balanced"
] ]
}, },
@ -142,7 +150,11 @@
"class TestCheckBalance(object):\n", "class TestCheckBalance(object):\n",
"\n", "\n",
" def test_check_balance(self):\n", " def test_check_balance(self):\n",
" assert_equal(check_balance(None), False)\n",
"\n",
" node = Node(5)\n", " node = Node(5)\n",
" assert_equal(check_balance(node), True)\n",
"\n",
" insert(node, 3)\n", " insert(node, 3)\n",
" insert(node, 8)\n", " insert(node, 8)\n",
" insert(node, 1)\n", " insert(node, 1)\n",
@ -199,21 +211,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -4,7 +4,11 @@ from nose.tools import assert_equal
class TestCheckBalance(object): class TestCheckBalance(object):
def test_check_balance(self): def test_check_balance(self):
assert_equal(check_balance(None), False)
node = Node(5) node = Node(5)
assert_equal(check_balance(node), True)
insert(node, 3) insert(node, 3)
insert(node, 8) insert(node, 8)
insert(node, 1) insert(node, 1)