mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Revised bfs challenge to be more self contained for simplicity.
This commit is contained in:
parent
a0b6421f79
commit
69f68287e3
|
@ -71,7 +71,24 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class MyNode(Node):\n",
|
||||
"class Node(object):\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 = Node(data)\n",
|
||||
" else:\n",
|
||||
" self.left.insert(data)\n",
|
||||
" else:\n",
|
||||
" if self.right is None:\n",
|
||||
" self.right = Node(data)\n",
|
||||
" else:\n",
|
||||
" self.right.insert(data)\n",
|
||||
"\n",
|
||||
" def bfs(self, visit_func):\n",
|
||||
" # TODO: Implement me"
|
||||
|
|
|
@ -76,17 +76,6 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run ../bst/bst.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -95,7 +84,24 @@
|
|||
"from collections import deque\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class MyNode(Node):\n",
|
||||
"class Node(object):\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 = Node(data)\n",
|
||||
" else:\n",
|
||||
" self.left.insert(data)\n",
|
||||
" else:\n",
|
||||
" if self.right is None:\n",
|
||||
" self.right = Node(data)\n",
|
||||
" else:\n",
|
||||
" self.right.insert(data)\n",
|
||||
"\n",
|
||||
" def bfs(self, visit_func):\n",
|
||||
" queue = deque()\n",
|
||||
|
@ -118,7 +124,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -129,7 +135,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -175,7 +181,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue
Block a user