mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-08 02:18:03 +00:00
Move tree height to a class
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
def height(node):
|
class BstHeight(Bst):
|
||||||
if node is None:
|
|
||||||
return 0
|
def height(self, node):
|
||||||
return 1 + max(height(node.left),
|
if node is None:
|
||||||
height(node.right))
|
return 0
|
||||||
|
return 1 + max(self.height(node.left),
|
||||||
|
self.height(node.right))
|
||||||
@@ -88,9 +88,11 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def height(node):\n",
|
"class BstHeight(Bst):\n",
|
||||||
" # TODO: Implement me\n",
|
"\n",
|
||||||
" pass"
|
" def height(self, node):\n",
|
||||||
|
" # TODO: Implement me\n",
|
||||||
|
" pass"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -122,13 +124,13 @@
|
|||||||
"class TestHeight(object):\n",
|
"class TestHeight(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_height(self):\n",
|
" def test_height(self):\n",
|
||||||
" root = Node(5)\n",
|
" bst = BstHeight(Node(5))\n",
|
||||||
" assert_equal(height(root), 1)\n",
|
" assert_equal(bst.height(bst.root), 1)\n",
|
||||||
" insert(root, 2)\n",
|
" bst.insert(2)\n",
|
||||||
" insert(root, 8)\n",
|
" bst.insert(8)\n",
|
||||||
" insert(root, 1)\n",
|
" bst.insert(1)\n",
|
||||||
" insert(root, 3)\n",
|
" bst.insert(3)\n",
|
||||||
" assert_equal(height(root), 3)\n",
|
" assert_equal(bst.height(bst.root), 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_height')\n",
|
" print('Success: test_height')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -91,33 +91,15 @@
|
|||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"Overwriting height.py\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"%%writefile height.py\n",
|
|
||||||
"def height(node):\n",
|
|
||||||
" if node is None:\n",
|
|
||||||
" return 0\n",
|
|
||||||
" return 1 + max(height(node.left), \n",
|
|
||||||
" height(node.right))"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 3,
|
|
||||||
"metadata": {
|
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run height.py"
|
"class BstHeight(Bst):\n",
|
||||||
|
"\n",
|
||||||
|
" def height(self, node):\n",
|
||||||
|
" if node is None:\n",
|
||||||
|
" return 0\n",
|
||||||
|
" return 1 + max(self.height(node.left),\n",
|
||||||
|
" self.height(node.right))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -129,7 +111,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -150,13 +132,13 @@
|
|||||||
"class TestHeight(object):\n",
|
"class TestHeight(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_height(self):\n",
|
" def test_height(self):\n",
|
||||||
" root = Node(5)\n",
|
" bst = BstHeight(Node(5))\n",
|
||||||
" assert_equal(height(root), 1)\n",
|
" assert_equal(bst.height(bst.root), 1)\n",
|
||||||
" insert(root, 2)\n",
|
" bst.insert(2)\n",
|
||||||
" insert(root, 8)\n",
|
" bst.insert(8)\n",
|
||||||
" insert(root, 1)\n",
|
" bst.insert(1)\n",
|
||||||
" insert(root, 3)\n",
|
" bst.insert(3)\n",
|
||||||
" assert_equal(height(root), 3)\n",
|
" assert_equal(bst.height(bst.root), 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_height')\n",
|
" print('Success: test_height')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -172,7 +154,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ from nose.tools import assert_equal
|
|||||||
class TestHeight(object):
|
class TestHeight(object):
|
||||||
|
|
||||||
def test_height(self):
|
def test_height(self):
|
||||||
root = Node(5)
|
bst = BstHeight(Node(5))
|
||||||
assert_equal(height(root), 1)
|
assert_equal(bst.height(bst.root), 1)
|
||||||
insert(root, 2)
|
bst.insert(2)
|
||||||
insert(root, 8)
|
bst.insert(8)
|
||||||
insert(root, 1)
|
bst.insert(1)
|
||||||
insert(root, 3)
|
bst.insert(3)
|
||||||
assert_equal(height(root), 3)
|
assert_equal(bst.height(bst.root), 3)
|
||||||
|
|
||||||
print('Success: test_height')
|
print('Success: test_height')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user