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"
|
"## Code"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run ../bst/bst.py\n",
|
||||||
|
"%load ../bst/bst.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
@ -83,33 +95,14 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Node(object):\n",
|
"def in_order_traversal(node, visit_func):\n",
|
||||||
|
" # TODO: Implement me\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self, data):\n",
|
"def pre_order_traversal(node, visit_func):\n",
|
||||||
" self.data = data\n",
|
" # TODO: Implement me\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",
|
"\n",
|
||||||
" def in_order_traversal(self, visit_func):\n",
|
"def post_order_traversal(node, visit_func):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me"
|
||||||
"\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"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -146,40 +139,40 @@
|
||||||
"class TestDfs(object):\n",
|
"class TestDfs(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_dfs(self):\n",
|
" def test_dfs(self):\n",
|
||||||
" root = Node(5)\n",
|
" node = Node(5)\n",
|
||||||
" root.insert(2)\n",
|
" insert(node, 2)\n",
|
||||||
" root.insert(8)\n",
|
" insert(node, 8)\n",
|
||||||
" root.insert(1)\n",
|
" insert(node, 1)\n",
|
||||||
" root.insert(3)\n",
|
" insert(node, 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '12358')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '52138')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '13285')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" root = Node(1)\n",
|
" node = Node(1)\n",
|
||||||
" root.insert(2)\n",
|
" insert(node, 2)\n",
|
||||||
" root.insert(3)\n",
|
" insert(node, 3)\n",
|
||||||
" root.insert(4)\n",
|
" insert(node, 4)\n",
|
||||||
" root.insert(5)\n",
|
" insert(node, 5)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '12345')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '12345')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '54321')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_dfs')\n",
|
" print('Success: test_dfs')\n",
|
||||||
|
|
|
@ -117,50 +117,39 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run ../bst/bst.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"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",
|
"\n",
|
||||||
" def __init__(self, data):\n",
|
"def pre_order_traversal(node, visit_func):\n",
|
||||||
" self.data = data\n",
|
" if node is not None:\n",
|
||||||
" self.left = None\n",
|
" visit_func(node.data)\n",
|
||||||
" self.right = None\n",
|
" pre_order_traversal(node.left, visit_func)\n",
|
||||||
|
" pre_order_traversal(node.right, visit_func)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def insert(self, data):\n",
|
"def post_order_traversal(node, visit_func):\n",
|
||||||
" if data <= self.data:\n",
|
" if node is not None:\n",
|
||||||
" if self.left is None:\n",
|
" post_order_traversal(node.left, visit_func)\n",
|
||||||
" self.left = type(self)(data)\n",
|
" post_order_traversal(node.right, visit_func)\n",
|
||||||
" else:\n",
|
" visit_func(node.data)"
|
||||||
" 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)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -172,7 +161,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
@ -183,7 +172,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
@ -205,40 +194,40 @@
|
||||||
"class TestDfs(object):\n",
|
"class TestDfs(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_dfs(self):\n",
|
" def test_dfs(self):\n",
|
||||||
" root = Node(5)\n",
|
" node = Node(5)\n",
|
||||||
" root.insert(2)\n",
|
" insert(node, 2)\n",
|
||||||
" root.insert(8)\n",
|
" insert(node, 8)\n",
|
||||||
" root.insert(1)\n",
|
" insert(node, 1)\n",
|
||||||
" root.insert(3)\n",
|
" insert(node, 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '12358')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '52138')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '13285')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" root = Node(1)\n",
|
" node = Node(1)\n",
|
||||||
" root.insert(2)\n",
|
" insert(node, 2)\n",
|
||||||
" root.insert(3)\n",
|
" insert(node, 3)\n",
|
||||||
" root.insert(4)\n",
|
" insert(node, 4)\n",
|
||||||
" root.insert(5)\n",
|
" insert(node, 5)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '12345')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '12345')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\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",
|
" assert_equal(out.getvalue().strip(), '54321')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_dfs')\n",
|
" print('Success: test_dfs')\n",
|
||||||
|
@ -255,7 +244,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,40 +5,40 @@ from nose.tools import assert_equal
|
||||||
class TestDfs(object):
|
class TestDfs(object):
|
||||||
|
|
||||||
def test_dfs(self):
|
def test_dfs(self):
|
||||||
root = Node(5)
|
node = Node(5)
|
||||||
root.insert(2)
|
insert(node, 2)
|
||||||
root.insert(8)
|
insert(node, 8)
|
||||||
root.insert(1)
|
insert(node, 1)
|
||||||
root.insert(3)
|
insert(node, 3)
|
||||||
|
|
||||||
with captured_output() as (out, err):
|
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')
|
assert_equal(out.getvalue().strip(), '12358')
|
||||||
|
|
||||||
with captured_output() as (out, err):
|
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')
|
assert_equal(out.getvalue().strip(), '52138')
|
||||||
|
|
||||||
with captured_output() as (out, err):
|
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')
|
assert_equal(out.getvalue().strip(), '13285')
|
||||||
|
|
||||||
root = Node(1)
|
node = Node(1)
|
||||||
root.insert(2)
|
insert(node, 2)
|
||||||
root.insert(3)
|
insert(node, 3)
|
||||||
root.insert(4)
|
insert(node, 4)
|
||||||
root.insert(5)
|
insert(node, 5)
|
||||||
|
|
||||||
with captured_output() as (out, err):
|
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')
|
assert_equal(out.getvalue().strip(), '12345')
|
||||||
|
|
||||||
with captured_output() as (out, err):
|
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')
|
assert_equal(out.getvalue().strip(), '12345')
|
||||||
|
|
||||||
with captured_output() as (out, err):
|
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')
|
assert_equal(out.getvalue().strip(), '54321')
|
||||||
|
|
||||||
print('Success: test_dfs')
|
print('Success: test_dfs')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user