Revised dfs challenge to be more self contained for simplicity.

This commit is contained in:
Donne Martin 2015-08-01 16:47:41 -04:00
parent 69f68287e3
commit e55c7ee1c6
3 changed files with 41 additions and 39 deletions

View File

@ -75,17 +75,6 @@
"## Code" "## Code"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run ../bst/bst.py"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -94,7 +83,25 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyNode(Node):\n", "class Node(object):\n",
"\n",
" def __init__(self, data):\n",
" self.data = data\n",
" self.left = None\n",
" self.right = None\n",
" \n",
" def insert(self, data):\n",
" if data <= self.data:\n",
" if self.left is None:\n",
" self.left = type(self)(data)\n",
" else:\n",
" self.left.insert(data)\n",
" else:\n",
" if self.right is None:\n",
" self.right = type(self)(data)\n",
" else:\n",
" self.right.insert(data)\n",
"\n",
" def in_order_traversal(self, visit_func):\n", " def in_order_traversal(self, visit_func):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
"\n", "\n",
@ -157,10 +164,6 @@
" root.post_order_traversal(sys.stdout.write)\n", " root.post_order_traversal(sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '13285')\n", " assert_equal(out.getvalue().strip(), '13285')\n",
"\n", "\n",
" with captured_output() as (out, err):\n",
" root.bfs(sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '52813')\n",
"\n",
" root = Node(1)\n", " root = Node(1)\n",
" root.insert(2)\n", " root.insert(2)\n",
" root.insert(3)\n", " root.insert(3)\n",

View File

@ -117,23 +117,30 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run ../bst/bst.py"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyNode(Node):\n", "class Node(object):\n",
"\n",
" def __init__(self, data):\n",
" self.data = data\n",
" self.left = None\n",
" self.right = None\n",
"\n",
" def insert(self, data):\n",
" if data <= self.data:\n",
" if self.left is None:\n",
" self.left = type(self)(data)\n",
" else:\n",
" self.left.insert(data)\n",
" else:\n",
" if self.right is None:\n",
" self.right = type(self)(data)\n",
" else:\n",
" self.right.insert(data)\n",
"\n",
" def in_order_traversal(self, visit_func):\n", " def in_order_traversal(self, visit_func):\n",
" if self.left is not None:\n", " if self.left is not None:\n",
" self.left.in_order_traversal(visit_func)\n", " self.left.in_order_traversal(visit_func)\n",
@ -165,7 +172,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -176,7 +183,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -216,10 +223,6 @@
" root.post_order_traversal(sys.stdout.write)\n", " root.post_order_traversal(sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '13285')\n", " assert_equal(out.getvalue().strip(), '13285')\n",
"\n", "\n",
" with captured_output() as (out, err):\n",
" root.bfs(sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '52813')\n",
"\n",
" root = Node(1)\n", " root = Node(1)\n",
" root.insert(2)\n", " root.insert(2)\n",
" root.insert(3)\n", " root.insert(3)\n",
@ -252,7 +255,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 4,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },

View File

@ -23,10 +23,6 @@ class TestDfs(object):
root.post_order_traversal(sys.stdout.write) root.post_order_traversal(sys.stdout.write)
assert_equal(out.getvalue().strip(), '13285') assert_equal(out.getvalue().strip(), '13285')
with captured_output() as (out, err):
root.bfs(sys.stdout.write)
assert_equal(out.getvalue().strip(), '52813')
root = Node(1) root = Node(1)
root.insert(2) root.insert(2)
root.insert(3) root.insert(3)