From 13d08b80fd3423c9c6372f78ca9ac65a49b2ed22 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 1 Aug 2015 17:48:52 -0400 Subject: [PATCH] Simpilfied depth first search challenge. --- graphs_trees/tree_dfs/dfs_challenge.ipynb | 75 ++++++++-------- graphs_trees/tree_dfs/dfs_solution.ipynb | 101 ++++++++++------------ graphs_trees/tree_dfs/test_dfs.py | 32 +++---- 3 files changed, 95 insertions(+), 113 deletions(-) diff --git a/graphs_trees/tree_dfs/dfs_challenge.ipynb b/graphs_trees/tree_dfs/dfs_challenge.ipynb index 80ac533..6125431 100644 --- a/graphs_trees/tree_dfs/dfs_challenge.ipynb +++ b/graphs_trees/tree_dfs/dfs_challenge.ipynb @@ -75,6 +75,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, @@ -83,33 +95,14 @@ }, "outputs": [], "source": [ - "class Node(object):\n", + "def in_order_traversal(node, visit_func):\n", + " # TODO: Implement me\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", + "def pre_order_traversal(node, visit_func):\n", + " # TODO: Implement me\n", "\n", - " def in_order_traversal(self, visit_func):\n", - " # TODO: Implement me\n", - "\n", - " def pre_order_traversal(self, visit_func):\n", - " # TODO: Implement me\n", - "\n", - " def post_order_traversal(self, visit_func):\n", - " # TODO: Implement me" + "def post_order_traversal(node, visit_func):\n", + " # TODO: Implement me" ] }, { @@ -146,40 +139,40 @@ "class TestDfs(object):\n", "\n", " def test_dfs(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.in_order_traversal(sys.stdout.write)\n", + " in_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '12358')\n", "\n", " with captured_output() as (out, err):\n", - " root.pre_order_traversal(sys.stdout.write)\n", + " pre_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '52138')\n", "\n", " with captured_output() as (out, err):\n", - " root.post_order_traversal(sys.stdout.write)\n", + " post_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '13285')\n", "\n", - " root = Node(1)\n", - " root.insert(2)\n", - " root.insert(3)\n", - " root.insert(4)\n", - " root.insert(5)\n", + " node = Node(1)\n", + " insert(node, 2)\n", + " insert(node, 3)\n", + " insert(node, 4)\n", + " insert(node, 5)\n", "\n", " with captured_output() as (out, err):\n", - " root.in_order_traversal(sys.stdout.write)\n", + " in_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '12345')\n", "\n", " with captured_output() as (out, err):\n", - " root.pre_order_traversal(sys.stdout.write)\n", + " pre_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '12345')\n", "\n", " with captured_output() as (out, err):\n", - " root.post_order_traversal(sys.stdout.write)\n", + " post_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '54321')\n", "\n", " print('Success: test_dfs')\n", diff --git a/graphs_trees/tree_dfs/dfs_solution.ipynb b/graphs_trees/tree_dfs/dfs_solution.ipynb index 47bca60..bdb6380 100644 --- a/graphs_trees/tree_dfs/dfs_solution.ipynb +++ b/graphs_trees/tree_dfs/dfs_solution.ipynb @@ -117,50 +117,39 @@ { "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 Node(object):\n", + "def in_order_traversal(node, visit_func):\n", + " if node is not None:\n", + " in_order_traversal(node.left, visit_func)\n", + " visit_func(node.data)\n", + " in_order_traversal(node.right, visit_func)\n", "\n", - " def __init__(self, data):\n", - " self.data = data\n", - " self.left = None\n", - " self.right = None\n", + "def pre_order_traversal(node, visit_func):\n", + " if node is not None:\n", + " visit_func(node.data)\n", + " pre_order_traversal(node.left, visit_func)\n", + " pre_order_traversal(node.right, visit_func)\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", - " visit_func(self.data)\n", - " if self.right is not None:\n", - " self.right.in_order_traversal(visit_func)\n", - "\n", - " def pre_order_traversal(self, visit_func):\n", - " visit_func(self.data)\n", - " if self.left is not None:\n", - " self.left.pre_order_traversal(visit_func)\n", - " if self.right is not None:\n", - " self.right.pre_order_traversal(visit_func)\n", - "\n", - " def post_order_traversal(self, visit_func):\n", - " if self.left is not None:\n", - " self.left.post_order_traversal(visit_func)\n", - " if self.right is not None:\n", - " self.right.post_order_traversal(visit_func)\n", - " visit_func(self.data)" + "def post_order_traversal(node, visit_func):\n", + " if node is not None:\n", + " post_order_traversal(node.left, visit_func)\n", + " post_order_traversal(node.right, visit_func)\n", + " visit_func(node.data)" ] }, { @@ -172,7 +161,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -183,7 +172,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -205,40 +194,40 @@ "class TestDfs(object):\n", "\n", " def test_dfs(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.in_order_traversal(sys.stdout.write)\n", + " in_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '12358')\n", "\n", " with captured_output() as (out, err):\n", - " root.pre_order_traversal(sys.stdout.write)\n", + " pre_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '52138')\n", "\n", " with captured_output() as (out, err):\n", - " root.post_order_traversal(sys.stdout.write)\n", + " post_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '13285')\n", "\n", - " root = Node(1)\n", - " root.insert(2)\n", - " root.insert(3)\n", - " root.insert(4)\n", - " root.insert(5)\n", + " node = Node(1)\n", + " insert(node, 2)\n", + " insert(node, 3)\n", + " insert(node, 4)\n", + " insert(node, 5)\n", "\n", " with captured_output() as (out, err):\n", - " root.in_order_traversal(sys.stdout.write)\n", + " in_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '12345')\n", "\n", " with captured_output() as (out, err):\n", - " root.pre_order_traversal(sys.stdout.write)\n", + " pre_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '12345')\n", "\n", " with captured_output() as (out, err):\n", - " root.post_order_traversal(sys.stdout.write)\n", + " post_order_traversal(node, sys.stdout.write)\n", " assert_equal(out.getvalue().strip(), '54321')\n", "\n", " print('Success: test_dfs')\n", @@ -255,7 +244,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "collapsed": false }, diff --git a/graphs_trees/tree_dfs/test_dfs.py b/graphs_trees/tree_dfs/test_dfs.py index 7bd9e9a..44397e1 100644 --- a/graphs_trees/tree_dfs/test_dfs.py +++ b/graphs_trees/tree_dfs/test_dfs.py @@ -5,40 +5,40 @@ from nose.tools import assert_equal class TestDfs(object): def test_dfs(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.in_order_traversal(sys.stdout.write) + in_order_traversal(node, sys.stdout.write) assert_equal(out.getvalue().strip(), '12358') with captured_output() as (out, err): - root.pre_order_traversal(sys.stdout.write) + pre_order_traversal(node, sys.stdout.write) assert_equal(out.getvalue().strip(), '52138') with captured_output() as (out, err): - root.post_order_traversal(sys.stdout.write) + post_order_traversal(node, sys.stdout.write) assert_equal(out.getvalue().strip(), '13285') - root = Node(1) - root.insert(2) - root.insert(3) - root.insert(4) - root.insert(5) + node = Node(1) + insert(node, 2) + insert(node, 3) + insert(node, 4) + insert(node, 5) with captured_output() as (out, err): - root.in_order_traversal(sys.stdout.write) + in_order_traversal(node, sys.stdout.write) assert_equal(out.getvalue().strip(), '12345') with captured_output() as (out, err): - root.pre_order_traversal(sys.stdout.write) + pre_order_traversal(node, sys.stdout.write) assert_equal(out.getvalue().strip(), '12345') with captured_output() as (out, err): - root.post_order_traversal(sys.stdout.write) + post_order_traversal(node, sys.stdout.write) assert_equal(out.getvalue().strip(), '54321') print('Success: test_dfs')