Simpilfied breadth first search challenge.

This commit is contained in:
Donne Martin 2015-08-01 17:48:12 -04:00
parent 31058abf3a
commit 332230c3a1
3 changed files with 50 additions and 65 deletions

View File

@ -63,6 +63,18 @@
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run ../bst/bst.py\n",
"%load ../bst/bst.py"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -71,27 +83,8 @@
},
"outputs": [],
"source": [
"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 = Node(data)\n",
" else:\n",
" self.left.insert(data)\n",
" else:\n",
" if self.right is None:\n",
" self.right = Node(data)\n",
" else:\n",
" self.right.insert(data)\n",
"\n",
" def bfs(self, visit_func):\n",
" # TODO: Implement me"
"def bfs(self, visit_func):\n",
" # TODO: Implement me"
]
},
{

View File

@ -76,6 +76,17 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run ../bst/bst.py"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
@ -84,35 +95,16 @@
"from collections import deque\n",
"\n",
"\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 = Node(data)\n",
" else:\n",
" self.left.insert(data)\n",
" else:\n",
" if self.right is None:\n",
" self.right = Node(data)\n",
" else:\n",
" self.right.insert(data)\n",
"\n",
" def bfs(self, visit_func):\n",
" queue = deque()\n",
" queue.append(self)\n",
" while len(queue) > 0:\n",
" node = queue.popleft()\n",
" visit_func(node.data)\n",
" if node.left is not None:\n",
" queue.append(node.left)\n",
" if node.right is not None:\n",
" queue.append(node.right)"
"def bfs(root, visit_func):\n",
" queue = deque()\n",
" queue.append(root)\n",
" while len(queue) > 0:\n",
" node = queue.popleft()\n",
" visit_func(node.data)\n",
" if node.left is not None:\n",
" queue.append(node.left)\n",
" if node.right is not None:\n",
" queue.append(node.right)"
]
},
{
@ -124,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"collapsed": false
},
@ -135,7 +127,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {
"collapsed": false
},
@ -157,14 +149,14 @@
"class TestBfs(object):\n",
"\n",
" def test_bfs(self):\n",
" root = Node(5)\n",
" root.insert(2)\n",
" root.insert(8)\n",
" root.insert(1)\n",
" root.insert(3)\n",
" node = Node(5)\n",
" insert(node, 2)\n",
" insert(node, 8)\n",
" insert(node, 1)\n",
" insert(node, 3)\n",
"\n",
" with captured_output() as (out, err):\n",
" root.bfs(sys.stdout.write)\n",
" bfs(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '52813')\n",
"\n",
" print('Success: test_bfs')\n",
@ -181,7 +173,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {
"collapsed": false
},

View File

@ -5,14 +5,14 @@ from nose.tools import assert_equal
class TestBfs(object):
def test_bfs(self):
root = Node(5)
root.insert(2)
root.insert(8)
root.insert(1)
root.insert(3)
node = Node(5)
insert(node, 2)
insert(node, 8)
insert(node, 1)
insert(node, 3)
with captured_output() as (out, err):
root.bfs(sys.stdout.write)
bfs(node, sys.stdout.write)
assert_equal(out.getvalue().strip(), '52813')
print('Success: test_bfs')