Revised bfs challenge to be more self contained for simplicity.

This commit is contained in:
Donne Martin 2015-08-01 16:47:29 -04:00
parent a0b6421f79
commit 69f68287e3
2 changed files with 39 additions and 16 deletions

View File

@ -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"

View File

@ -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
},