Move tree check balance to a class (#99)

This commit is contained in:
Donne Martin 2016-09-07 05:46:47 -04:00 committed by GitHub
parent dfeb7f4d80
commit d1d1abd3db
3 changed files with 98 additions and 89 deletions

View File

@ -92,9 +92,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def check_balance(root):\n", "class BstBalance(Bst):\n",
" # TODO: Implement me\n", "\n",
" pass" " def check_balance(self):\n",
" # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@ -128,33 +130,34 @@
"\n", "\n",
" @raises(Exception)\n", " @raises(Exception)\n",
" def test_check_balance_empty(self):\n", " def test_check_balance_empty(self):\n",
" check_balance(None)\n", " bst = BstBalance(None)\n",
" bst.check_balance()\n",
"\n", "\n",
" def test_check_balance(self):\n", " def test_check_balance(self):\n",
" node = Node(5)\n", " bst = BstBalance(Node(5))\n",
" assert_equal(check_balance(node), True)\n", " assert_equal(bst.check_balance(), True)\n",
"\n", "\n",
" insert(node, 3)\n", " bst.insert(3)\n",
" insert(node, 8)\n", " bst.insert(8)\n",
" insert(node, 1)\n", " bst.insert(1)\n",
" insert(node, 4)\n", " bst.insert(4)\n",
" assert_equal(check_balance(node), True)\n", " assert_equal(bst.check_balance(), True)\n",
"\n", "\n",
" node = Node(5)\n", " bst = BstBalance(Node(5))\n",
" insert(node, 3)\n", " bst.insert(3)\n",
" insert(node, 8)\n", " bst.insert(8)\n",
" insert(node, 9)\n", " bst.insert(9)\n",
" insert(node, 10)\n", " bst.insert(10)\n",
" assert_equal(check_balance(node), False)\n", " assert_equal(bst.check_balance(), False)\n",
"\n", "\n",
" node = Node(3)\n", " bst = BstBalance(Node(3))\n",
" insert(node, 2)\n", " bst.insert(2)\n",
" insert(node, 1)\n", " bst.insert(1)\n",
" insert(node, 5)\n", " bst.insert(5)\n",
" insert(node, 4)\n", " bst.insert(4)\n",
" insert(node, 6)\n", " bst.insert(6)\n",
" insert(node, 7)\n", " bst.insert(7)\n",
" assert_equal(check_balance(node), False)\n", " assert_equal(bst.check_balance(), True)\n",
"\n", "\n",
" print('Success: test_check_balance')\n", " print('Success: test_check_balance')\n",
"\n", "\n",

View File

@ -66,11 +66,10 @@
"However, we could check whether the tree is balanced while also checking for the heights.\n", "However, we could check whether the tree is balanced while also checking for the heights.\n",
"\n", "\n",
"* Base case: If the root is None, return 0\n", "* Base case: If the root is None, return 0\n",
"* Recursively check whether the left sub tree is balanced, and get its maximum and minimum height\n", "* Recursively check whether the left sub tree is balanced, and get its height left_height\n",
"* Recursively Check whether the right sub tree is balanced, and get its maximum and minimum height\n", "* Recursively check whether the right sub tree is balanced, and get its height right_height\n",
"* Calculate the maximum height and minimum height of the current tree\n", "* Compare left_height and right_height\n",
"* If both sub-trees are balanced, and the maximum and minimum height of the current tree doesn't differ by more than 1, then the current tree is balanced. Otherwise, it is not\n", "* Return 1 + max(left_height, right_height)\n",
"* Return whether the current tree is balanced, and the maximum height and minimum height of the current tree\n",
" \n", " \n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\n", "* Time: O(n)\n",
@ -99,26 +98,31 @@
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": true
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def _check_balance(root):\n", "class BstBalance(Bst):\n",
" if root is None:\n",
" return (True, 0, 0)\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",
" min_h = 1 + min(left_min_h, right_min_h)\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",
" return (balanced, min_h, max_h)\n",
"\n", "\n",
" def _check_height(self, node):\n",
" if node is None:\n",
" return 0\n",
" left_height = self._check_height(node.left)\n",
" if left_height == -1:\n",
" return -1\n",
" right_height = self._check_height(node.right)\n",
" if right_height == -1:\n",
" return -1\n",
" diff = abs(left_height-right_height)\n",
" if diff > 1:\n",
" return -1\n",
" return 1 + max(left_height, right_height)\n",
"\n", "\n",
"def check_balance(root):\n", " def check_balance(self):\n",
" if root is None:\n", " if self.root is None:\n",
" raise Exception('No root node')\n", " raise Exception('No root node')\n",
" balanced, _, _ = _check_balance(root)\n", " height = self._check_height(self.root)\n",
" return balanced" " return height != -1"
] ]
}, },
{ {
@ -153,33 +157,34 @@
"\n", "\n",
" @raises(Exception)\n", " @raises(Exception)\n",
" def test_check_balance_empty(self):\n", " def test_check_balance_empty(self):\n",
" check_balance(None)\n", " bst = BstBalance(None)\n",
" bst.check_balance()\n",
"\n", "\n",
" def test_check_balance(self):\n", " def test_check_balance(self):\n",
" node = Node(5)\n", " bst = BstBalance(Node(5))\n",
" assert_equal(check_balance(node), True)\n", " assert_equal(bst.check_balance(), True)\n",
"\n", "\n",
" insert(node, 3)\n", " bst.insert(3)\n",
" insert(node, 8)\n", " bst.insert(8)\n",
" insert(node, 1)\n", " bst.insert(1)\n",
" insert(node, 4)\n", " bst.insert(4)\n",
" assert_equal(check_balance(node), True)\n", " assert_equal(bst.check_balance(), True)\n",
"\n", "\n",
" node = Node(5)\n", " bst = BstBalance(Node(5))\n",
" insert(node, 3)\n", " bst.insert(3)\n",
" insert(node, 8)\n", " bst.insert(8)\n",
" insert(node, 9)\n", " bst.insert(9)\n",
" insert(node, 10)\n", " bst.insert(10)\n",
" assert_equal(check_balance(node), False)\n", " assert_equal(bst.check_balance(), False)\n",
"\n", "\n",
" node = Node(3)\n", " bst = BstBalance(Node(3))\n",
" insert(node, 2)\n", " bst.insert(2)\n",
" insert(node, 1)\n", " bst.insert(1)\n",
" insert(node, 5)\n", " bst.insert(5)\n",
" insert(node, 4)\n", " bst.insert(4)\n",
" insert(node, 6)\n", " bst.insert(6)\n",
" insert(node, 7)\n", " bst.insert(7)\n",
" assert_equal(check_balance(node), False)\n", " assert_equal(bst.check_balance(), True)\n",
"\n", "\n",
" print('Success: test_check_balance')\n", " print('Success: test_check_balance')\n",
"\n", "\n",

View File

@ -6,33 +6,34 @@ class TestCheckBalance(object):
@raises(Exception) @raises(Exception)
def test_check_balance_empty(self): def test_check_balance_empty(self):
check_balance(None) bst = BstBalance(None)
bst.check_balance()
def test_check_balance(self): def test_check_balance(self):
node = Node(5) bst = BstBalance(Node(5))
assert_equal(check_balance(node), True) assert_equal(bst.check_balance(), True)
insert(node, 3) bst.insert(3)
insert(node, 8) bst.insert(8)
insert(node, 1) bst.insert(1)
insert(node, 4) bst.insert(4)
assert_equal(check_balance(node), True) assert_equal(bst.check_balance(), True)
node = Node(5) bst = BstBalance(Node(5))
insert(node, 3) bst.insert(3)
insert(node, 8) bst.insert(8)
insert(node, 9) bst.insert(9)
insert(node, 10) bst.insert(10)
assert_equal(check_balance(node), False) assert_equal(bst.check_balance(), False)
node = Node(3) bst = BstBalance(Node(3))
insert(node, 2) bst.insert(2)
insert(node, 1) bst.insert(1)
insert(node, 5) bst.insert(5)
insert(node, 4) bst.insert(4)
insert(node, 6) bst.insert(6)
insert(node, 7) bst.insert(7)
assert_equal(check_balance(node), False) assert_equal(bst.check_balance(), True)
print('Success: test_check_balance') print('Success: test_check_balance')