From 69f68287e32d28bf493de69577eafe15a214c1ff Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 1 Aug 2015 16:47:29 -0400 Subject: [PATCH] Revised bfs challenge to be more self contained for simplicity. --- graphs_trees/tree_bfs/bfs_challenge.ipynb | 19 +++++++++++- graphs_trees/tree_bfs/bfs_solution.ipynb | 36 +++++++++++++---------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/graphs_trees/tree_bfs/bfs_challenge.ipynb b/graphs_trees/tree_bfs/bfs_challenge.ipynb index 1e3eaae..aadbcaf 100644 --- a/graphs_trees/tree_bfs/bfs_challenge.ipynb +++ b/graphs_trees/tree_bfs/bfs_challenge.ipynb @@ -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" diff --git a/graphs_trees/tree_bfs/bfs_solution.ipynb b/graphs_trees/tree_bfs/bfs_solution.ipynb index 9451320..634c4e5 100644 --- a/graphs_trees/tree_bfs/bfs_solution.ipynb +++ b/graphs_trees/tree_bfs/bfs_solution.ipynb @@ -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 },