mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Simpilfied depth first search challenge.
This commit is contained in:
parent
332230c3a1
commit
13d08b80fd
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
},
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue
Block a user