mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move tree dfs to a class
This commit is contained in:
parent
10a404c7ae
commit
b4ed90e649
|
@ -99,17 +99,19 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def in_order_traversal(node, visit_func):\n",
|
||||
"class BstDfs(Bst):\n",
|
||||
"\n",
|
||||
" def in_order_traversal(self, node, visit_func):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"def pre_order_traversal(node, visit_func):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
" def pre_order_traversal(self, node, visit_func):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"def post_order_traversal(node, visit_func):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
" def post_order_traversal(self,node, visit_func):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -148,39 +150,39 @@
|
|||
" self.results = Results()\n",
|
||||
"\n",
|
||||
" def test_dfs(self):\n",
|
||||
" node = Node(5)\n",
|
||||
" insert(node, 2)\n",
|
||||
" insert(node, 8)\n",
|
||||
" insert(node, 1)\n",
|
||||
" insert(node, 3)\n",
|
||||
" bst = BstDfs(Node(5))\n",
|
||||
" bst.insert(2)\n",
|
||||
" bst.insert(8)\n",
|
||||
" bst.insert(1)\n",
|
||||
" bst.insert(3)\n",
|
||||
"\n",
|
||||
" in_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[1, 2, 3, 5, 8]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" pre_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" post_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" node = Node(1)\n",
|
||||
" insert(node, 2)\n",
|
||||
" insert(node, 3)\n",
|
||||
" insert(node, 4)\n",
|
||||
" insert(node, 5)\n",
|
||||
" bst = BstDfs(Node(1))\n",
|
||||
" bst.insert(2)\n",
|
||||
" bst.insert(3)\n",
|
||||
" bst.insert(4)\n",
|
||||
" bst.insert(5)\n",
|
||||
"\n",
|
||||
" in_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" pre_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" post_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
|
||||
"\n",
|
||||
" print('Success: test_dfs')\n",
|
||||
|
|
|
@ -132,23 +132,25 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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)\n",
|
||||
" in_order_traversal(node.right, visit_func)\n",
|
||||
"class BstDfs(Bst):\n",
|
||||
"\n",
|
||||
"def pre_order_traversal(node, visit_func):\n",
|
||||
" if node is not None:\n",
|
||||
" visit_func(node)\n",
|
||||
" pre_order_traversal(node.left, visit_func)\n",
|
||||
" pre_order_traversal(node.right, visit_func)\n",
|
||||
" def in_order_traversal(self, node, visit_func):\n",
|
||||
" if node is not None:\n",
|
||||
" self.in_order_traversal(node.left, visit_func)\n",
|
||||
" visit_func(node)\n",
|
||||
" self.in_order_traversal(node.right, visit_func)\n",
|
||||
"\n",
|
||||
"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)"
|
||||
" def pre_order_traversal(self, node, visit_func):\n",
|
||||
" if node is not None:\n",
|
||||
" visit_func(node)\n",
|
||||
" self.pre_order_traversal(node.left, visit_func)\n",
|
||||
" self.pre_order_traversal(node.right, visit_func)\n",
|
||||
"\n",
|
||||
" def post_order_traversal(self, node, visit_func):\n",
|
||||
" if node is not None:\n",
|
||||
" self.post_order_traversal(node.left, visit_func)\n",
|
||||
" self.post_order_traversal(node.right, visit_func)\n",
|
||||
" visit_func(node)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -195,39 +197,39 @@
|
|||
" self.results = Results()\n",
|
||||
"\n",
|
||||
" def test_dfs(self):\n",
|
||||
" node = Node(5)\n",
|
||||
" insert(node, 2)\n",
|
||||
" insert(node, 8)\n",
|
||||
" insert(node, 1)\n",
|
||||
" insert(node, 3)\n",
|
||||
" bst = BstDfs(Node(5))\n",
|
||||
" bst.insert(2)\n",
|
||||
" bst.insert(8)\n",
|
||||
" bst.insert(1)\n",
|
||||
" bst.insert(3)\n",
|
||||
"\n",
|
||||
" in_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[1, 2, 3, 5, 8]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" pre_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" post_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" node = Node(1)\n",
|
||||
" insert(node, 2)\n",
|
||||
" insert(node, 3)\n",
|
||||
" insert(node, 4)\n",
|
||||
" insert(node, 5)\n",
|
||||
" bst = BstDfs(Node(1))\n",
|
||||
" bst.insert(2)\n",
|
||||
" bst.insert(3)\n",
|
||||
" bst.insert(4)\n",
|
||||
" bst.insert(5)\n",
|
||||
"\n",
|
||||
" in_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" pre_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
||||
" self.results.clear_results()\n",
|
||||
"\n",
|
||||
" post_order_traversal(node, self.results.add_result)\n",
|
||||
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
||||
" assert_equal(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
|
||||
"\n",
|
||||
" print('Success: test_dfs')\n",
|
||||
|
|
|
@ -7,39 +7,39 @@ class TestDfs(object):
|
|||
self.results = Results()
|
||||
|
||||
def test_dfs(self):
|
||||
node = Node(5)
|
||||
insert(node, 2)
|
||||
insert(node, 8)
|
||||
insert(node, 1)
|
||||
insert(node, 3)
|
||||
bst = BstDfs(Node(5))
|
||||
bst.insert(2)
|
||||
bst.insert(8)
|
||||
bst.insert(1)
|
||||
bst.insert(3)
|
||||
|
||||
in_order_traversal(node, self.results.add_result)
|
||||
bst.in_order_traversal(bst.root, self.results.add_result)
|
||||
assert_equal(str(self.results), "[1, 2, 3, 5, 8]")
|
||||
self.results.clear_results()
|
||||
|
||||
pre_order_traversal(node, self.results.add_result)
|
||||
bst.pre_order_traversal(bst.root, self.results.add_result)
|
||||
assert_equal(str(self.results), "[5, 2, 1, 3, 8]")
|
||||
self.results.clear_results()
|
||||
|
||||
post_order_traversal(node, self.results.add_result)
|
||||
bst.post_order_traversal(bst.root, self.results.add_result)
|
||||
assert_equal(str(self.results), "[1, 3, 2, 8, 5]")
|
||||
self.results.clear_results()
|
||||
|
||||
node = Node(1)
|
||||
insert(node, 2)
|
||||
insert(node, 3)
|
||||
insert(node, 4)
|
||||
insert(node, 5)
|
||||
bst = BstDfs(Node(1))
|
||||
bst.insert(2)
|
||||
bst.insert(3)
|
||||
bst.insert(4)
|
||||
bst.insert(5)
|
||||
|
||||
in_order_traversal(node, self.results.add_result)
|
||||
bst.in_order_traversal(bst.root, self.results.add_result)
|
||||
assert_equal(str(self.results), "[1, 2, 3, 4, 5]")
|
||||
self.results.clear_results()
|
||||
|
||||
pre_order_traversal(node, self.results.add_result)
|
||||
bst.pre_order_traversal(bst.root, self.results.add_result)
|
||||
assert_equal(str(self.results), "[1, 2, 3, 4, 5]")
|
||||
self.results.clear_results()
|
||||
|
||||
post_order_traversal(node, self.results.add_result)
|
||||
bst.post_order_traversal(bst.root, self.results.add_result)
|
||||
assert_equal(str(self.results), "[5, 4, 3, 2, 1]")
|
||||
|
||||
print('Success: test_dfs')
|
||||
|
|
Loading…
Reference in New Issue
Block a user