Move bst min to a class

This commit is contained in:
Donne Martin 2016-08-14 08:22:07 -04:00
parent 82c9725000
commit 857497115a
3 changed files with 50 additions and 49 deletions

View File

@ -90,15 +90,10 @@
},
"outputs": [],
"source": [
"def create_min_bst(array):\n",
" # TODO: Implement me\n",
" # Calls _create_min_bst\n",
" pass \n",
"class MinBst(object):\n",
"\n",
"\n",
"def _create_min_bst(array, start, end):\n",
" # TODO: Implement me\n",
" pass"
" def create_min_bst(self, array):\n",
" # TODO: Implement me"
]
},
{
@ -115,17 +110,6 @@
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run ../tree_height/height.py"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -138,15 +122,24 @@
"from nose.tools import assert_equal\n",
"\n",
"\n",
"def height(node):\n",
" if node is None:\n",
" return 0\n",
" return 1 + max(height(node.left),\n",
" height(node.right))\n",
"\n",
"\n",
"class TestBstMin(object):\n",
"\n",
" def test_bst_min(self):\n",
" min_bst = MinBst()\n",
" array = [0, 1, 2, 3, 4, 5, 6]\n",
" root = create_min_bst(array)\n",
" root = min_bst.create_min_bst(array)\n",
" assert_equal(height(root), 3)\n",
"\n",
" min_bst = MinBst()\n",
" array = [0, 1, 2, 3, 4, 5, 6, 7]\n",
" root = create_min_bst(array)\n",
" root = min_bst.create_min_bst(array)\n",
" assert_equal(height(root), 4)\n",
"\n",
" print('Success: test_bst_min')\n",

View File

@ -102,20 +102,21 @@
"from __future__ import division\n",
"\n",
"\n",
"def create_min_bst(array):\n",
" if array is None:\n",
" return\n",
" return _create_min_bst(array, 0, len(array)-1)\n",
"class MinBst(object):\n",
"\n",
" def create_min_bst(self, array):\n",
" if array is None:\n",
" return\n",
" return self._create_min_bst(array, 0, len(array)-1)\n",
"\n",
"def _create_min_bst(array, start, end):\n",
" if end < start:\n",
" return None\n",
" mid = (start + end) // 2\n",
" node = Node(array[mid])\n",
" node.left = _create_min_bst(array, start, mid-1)\n",
" node.right = _create_min_bst(array, mid+1, end)\n",
" return node"
" def _create_min_bst(self, array, start, end):\n",
" if end < start:\n",
" return None\n",
" mid = (start + end) // 2\n",
" node = Node(array[mid])\n",
" node.left = self._create_min_bst(array, start, mid-1)\n",
" node.right = self._create_min_bst(array, mid+1, end)\n",
" return node"
]
},
{
@ -128,17 +129,6 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run ../tree_height/height.py"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
@ -156,15 +146,24 @@
"from nose.tools import assert_equal\n",
"\n",
"\n",
"def height(node):\n",
" if node is None:\n",
" return 0\n",
" return 1 + max(height(node.left),\n",
" height(node.right))\n",
"\n",
"\n",
"class TestBstMin(object):\n",
"\n",
" def test_bst_min(self):\n",
" min_bst = MinBst()\n",
" array = [0, 1, 2, 3, 4, 5, 6]\n",
" root = create_min_bst(array)\n",
" root = min_bst.create_min_bst(array)\n",
" assert_equal(height(root), 3)\n",
"\n",
" min_bst = MinBst()\n",
" array = [0, 1, 2, 3, 4, 5, 6, 7]\n",
" root = create_min_bst(array)\n",
" root = min_bst.create_min_bst(array)\n",
" assert_equal(height(root), 4)\n",
"\n",
" print('Success: test_bst_min')\n",
@ -181,7 +180,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {
"collapsed": false
},

View File

@ -1,15 +1,24 @@
from nose.tools import assert_equal
def height(node):
if node is None:
return 0
return 1 + max(height(node.left),
height(node.right))
class TestBstMin(object):
def test_bst_min(self):
min_bst = MinBst()
array = [0, 1, 2, 3, 4, 5, 6]
root = create_min_bst(array)
root = min_bst.create_min_bst(array)
assert_equal(height(root), 3)
min_bst = MinBst()
array = [0, 1, 2, 3, 4, 5, 6, 7]
root = create_min_bst(array)
root = min_bst.create_min_bst(array)
assert_equal(height(root), 4)
print('Success: test_bst_min')