diff --git a/graphs_trees/tree_dfs/dfs_challenge.ipynb b/graphs_trees/tree_dfs/dfs_challenge.ipynb index 1748729..80ac533 100644 --- a/graphs_trees/tree_dfs/dfs_challenge.ipynb +++ b/graphs_trees/tree_dfs/dfs_challenge.ipynb @@ -75,17 +75,6 @@ "## Code" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%run ../bst/bst.py" - ] - }, { "cell_type": "code", "execution_count": null, @@ -94,7 +83,25 @@ }, "outputs": [], "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", " # TODO: Implement me\n", "\n", @@ -157,10 +164,6 @@ " root.post_order_traversal(sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '13285')\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.insert(2)\n", " root.insert(3)\n", diff --git a/graphs_trees/tree_dfs/dfs_solution.ipynb b/graphs_trees/tree_dfs/dfs_solution.ipynb index 0a0b26d..47bca60 100644 --- a/graphs_trees/tree_dfs/dfs_solution.ipynb +++ b/graphs_trees/tree_dfs/dfs_solution.ipynb @@ -117,23 +117,30 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%run ../bst/bst.py" - ] - }, - { - "cell_type": "code", - "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "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", " if self.left is not None:\n", " self.left.in_order_traversal(visit_func)\n", @@ -165,7 +172,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -176,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -216,10 +223,6 @@ " root.post_order_traversal(sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '13285')\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.insert(2)\n", " root.insert(3)\n", @@ -252,7 +255,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": { "collapsed": false }, diff --git a/graphs_trees/tree_dfs/test_dfs.py b/graphs_trees/tree_dfs/test_dfs.py index f5763e5..7bd9e9a 100644 --- a/graphs_trees/tree_dfs/test_dfs.py +++ b/graphs_trees/tree_dfs/test_dfs.py @@ -23,10 +23,6 @@ class TestDfs(object): root.post_order_traversal(sys.stdout.write) 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.insert(2) root.insert(3)