From 1928712364b048642f92131cb1682a4bb3cdf775 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 10 Sep 2016 07:42:54 -0400 Subject: [PATCH] Move tree level lists to a class --- .../tree_level_lists/test_tree_level_lists.py | 24 ++++---- .../tree_level_lists_challenge.ipynb | 34 ++++++----- .../tree_level_lists_solution.ipynb | 60 ++++++++++--------- 3 files changed, 61 insertions(+), 57 deletions(-) diff --git a/graphs_trees/tree_level_lists/test_tree_level_lists.py b/graphs_trees/tree_level_lists/test_tree_level_lists.py index 9a9b4b2..2fe0f7f 100644 --- a/graphs_trees/tree_level_lists/test_tree_level_lists.py +++ b/graphs_trees/tree_level_lists/test_tree_level_lists.py @@ -4,19 +4,19 @@ from nose.tools import assert_equal class TestTreeLevelLists(object): def test_tree_level_lists(self): - node = Node(5) - insert(node, 3) - insert(node, 8) - insert(node, 2) - insert(node, 4) - insert(node, 1) - insert(node, 7) - insert(node, 6) - insert(node, 9) - insert(node, 10) - insert(node, 11) + bst = BstLevelLists(Node(5)) + bst.insert(3) + bst.insert(8) + bst.insert(2) + bst.insert(4) + bst.insert(1) + bst.insert(7) + bst.insert(6) + bst.insert(9) + bst.insert(10) + bst.insert(11) - levels = create_level_lists(node) + levels = bst.create_level_lists() results_list = [] for level in levels: results = Results() diff --git a/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb b/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb index a2be9ab..5e3db80 100644 --- a/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb +++ b/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Create a linked list for each level of a binary tree.\n", + "## Problem: Create a list for each level of a binary tree.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -91,9 +91,11 @@ }, "outputs": [], "source": [ - "def create_level_lists(root):\n", - " # TODO: Implement me\n", - " pass" + "class BstLevelLists(Bst):\n", + "\n", + " def create_level_lists(self):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -136,19 +138,19 @@ "class TestTreeLevelLists(object):\n", "\n", " def test_tree_level_lists(self):\n", - " node = Node(5)\n", - " insert(node, 3)\n", - " insert(node, 8)\n", - " insert(node, 2)\n", - " insert(node, 4)\n", - " insert(node, 1)\n", - " insert(node, 7)\n", - " insert(node, 6)\n", - " insert(node, 9)\n", - " insert(node, 10)\n", - " insert(node, 11)\n", + " bst = BstLevelLists(Node(5))\n", + " bst.insert(3)\n", + " bst.insert(8)\n", + " bst.insert(2)\n", + " bst.insert(4)\n", + " bst.insert(1)\n", + " bst.insert(7)\n", + " bst.insert(6)\n", + " bst.insert(9)\n", + " bst.insert(10)\n", + " bst.insert(11)\n", "\n", - " levels = create_level_lists(node)\n", + " levels = bst.create_level_lists()\n", " results_list = []\n", " for level in levels:\n", " results = Results()\n", diff --git a/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb b/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb index 2cd6700..2ffc3b4 100644 --- a/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb +++ b/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Create a linked list for each level of a binary tree.\n", + "## Problem: Create a list for each level of a binary tree.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -103,23 +103,25 @@ }, "outputs": [], "source": [ - "def create_level_lists(root):\n", - " if root is None:\n", - " return\n", - " results = []\n", - " current = []\n", - " parents = []\n", - " current.append(root)\n", - " while current:\n", - " results.append(current)\n", - " parents = list(current)\n", + "class BstLevelLists(Bst):\n", + "\n", + " def create_level_lists(self):\n", + " if self.root is None:\n", + " return\n", + " results = []\n", " current = []\n", - " for parent in parents:\n", - " if parent.left is not None:\n", - " current.append(parent.left)\n", - " if parent.right is not None:\n", - " current.append(parent.right)\n", - " return results" + " parents = []\n", + " current.append(self.root)\n", + " while current:\n", + " results.append(current)\n", + " parents = list(current)\n", + " current = []\n", + " for parent in parents:\n", + " if parent.left is not None:\n", + " current.append(parent.left)\n", + " if parent.right is not None:\n", + " current.append(parent.right)\n", + " return results" ] }, { @@ -163,19 +165,19 @@ "class TestTreeLevelLists(object):\n", "\n", " def test_tree_level_lists(self):\n", - " node = Node(5)\n", - " insert(node, 3)\n", - " insert(node, 8)\n", - " insert(node, 2)\n", - " insert(node, 4)\n", - " insert(node, 1)\n", - " insert(node, 7)\n", - " insert(node, 6)\n", - " insert(node, 9)\n", - " insert(node, 10)\n", - " insert(node, 11)\n", + " bst = BstLevelLists(Node(5))\n", + " bst.insert(3)\n", + " bst.insert(8)\n", + " bst.insert(2)\n", + " bst.insert(4)\n", + " bst.insert(1)\n", + " bst.insert(7)\n", + " bst.insert(6)\n", + " bst.insert(9)\n", + " bst.insert(10)\n", + " bst.insert(11)\n", "\n", - " levels = create_level_lists(node)\n", + " levels = bst.create_level_lists()\n", " results_list = []\n", " for level in levels:\n", " results = Results()\n",