diff --git a/graphs_trees/tree_bfs/bfs_challenge.ipynb b/graphs_trees/tree_bfs/bfs_challenge.ipynb index aadbcaf..467f036 100644 --- a/graphs_trees/tree_bfs/bfs_challenge.ipynb +++ b/graphs_trees/tree_bfs/bfs_challenge.ipynb @@ -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" ] }, { diff --git a/graphs_trees/tree_bfs/bfs_solution.ipynb b/graphs_trees/tree_bfs/bfs_solution.ipynb index 634c4e5..21cf5e6 100644 --- a/graphs_trees/tree_bfs/bfs_solution.ipynb +++ b/graphs_trees/tree_bfs/bfs_solution.ipynb @@ -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 }, diff --git a/graphs_trees/tree_bfs/test_bfs.py b/graphs_trees/tree_bfs/test_bfs.py index a93b2cb..6eba107 100644 --- a/graphs_trees/tree_bfs/test_bfs.py +++ b/graphs_trees/tree_bfs/test_bfs.py @@ -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')