mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move tree bfs to a class
This commit is contained in:
parent
b91b6a437b
commit
10a404c7ae
|
@ -87,9 +87,11 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def bfs(self, visit_func):\n",
|
"class BstBfs(Bst):\n",
|
||||||
" # TODO: Implement me\n",
|
"\n",
|
||||||
" pass"
|
" def bfs(self, visit_func):\n",
|
||||||
|
" # TODO: Implement me\n",
|
||||||
|
" pass"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -128,12 +130,12 @@
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bfs(self):\n",
|
" def test_bfs(self):\n",
|
||||||
" node = Node(5)\n",
|
" bst = BstBfs(Node(5))\n",
|
||||||
" insert(node, 2)\n",
|
" bst.insert(2)\n",
|
||||||
" insert(node, 8)\n",
|
" bst.insert(8)\n",
|
||||||
" insert(node, 1)\n",
|
" bst.insert(1)\n",
|
||||||
" insert(node, 3)\n",
|
" bst.insert(3)\n",
|
||||||
" bfs(node, self.results.add_result)\n",
|
" bst.bfs(self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[5, 2, 8, 1, 3]')\n",
|
" assert_equal(str(self.results), '[5, 2, 8, 1, 3]')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bfs')\n",
|
" print('Success: test_bfs')\n",
|
||||||
|
|
|
@ -97,16 +97,20 @@
|
||||||
"from collections import deque\n",
|
"from collections import deque\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def bfs(root, visit_func):\n",
|
"class BstBfs(Bst):\n",
|
||||||
" queue = deque()\n",
|
"\n",
|
||||||
" queue.append(root)\n",
|
" def bfs(self, visit_func):\n",
|
||||||
" while queue:\n",
|
" if self.root is None:\n",
|
||||||
" node = queue.popleft()\n",
|
" raise Exception('root is None')\n",
|
||||||
" visit_func(node)\n",
|
" queue = deque()\n",
|
||||||
" if node.left is not None:\n",
|
" queue.append(self.root)\n",
|
||||||
" queue.append(node.left)\n",
|
" while queue:\n",
|
||||||
" if node.right is not None:\n",
|
" node = queue.popleft()\n",
|
||||||
" queue.append(node.right)"
|
" visit_func(node)\n",
|
||||||
|
" if node.left is not None:\n",
|
||||||
|
" queue.append(node.left)\n",
|
||||||
|
" if node.right is not None:\n",
|
||||||
|
" queue.append(node.right)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -153,12 +157,12 @@
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bfs(self):\n",
|
" def test_bfs(self):\n",
|
||||||
" node = Node(5)\n",
|
" bst = BstBfs(Node(5))\n",
|
||||||
" insert(node, 2)\n",
|
" bst.insert(2)\n",
|
||||||
" insert(node, 8)\n",
|
" bst.insert(8)\n",
|
||||||
" insert(node, 1)\n",
|
" bst.insert(1)\n",
|
||||||
" insert(node, 3)\n",
|
" bst.insert(3)\n",
|
||||||
" bfs(node, self.results.add_result)\n",
|
" bst.bfs(self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[5, 2, 8, 1, 3]')\n",
|
" assert_equal(str(self.results), '[5, 2, 8, 1, 3]')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bfs')\n",
|
" print('Success: test_bfs')\n",
|
||||||
|
|
|
@ -7,12 +7,12 @@ class TestBfs(object):
|
||||||
self.results = Results()
|
self.results = Results()
|
||||||
|
|
||||||
def test_bfs(self):
|
def test_bfs(self):
|
||||||
node = Node(5)
|
bst = BstBfs(Node(5))
|
||||||
insert(node, 2)
|
bst.insert(2)
|
||||||
insert(node, 8)
|
bst.insert(8)
|
||||||
insert(node, 1)
|
bst.insert(1)
|
||||||
insert(node, 3)
|
bst.insert(3)
|
||||||
bfs(node, self.results.add_result)
|
bst.bfs(self.results.add_result)
|
||||||
assert_equal(str(self.results), '[5, 2, 8, 1, 3]')
|
assert_equal(str(self.results), '[5, 2, 8, 1, 3]')
|
||||||
|
|
||||||
print('Success: test_bfs')
|
print('Success: test_bfs')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user