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

View File

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

View File

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