mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move tree level lists to a class
This commit is contained in:
parent
cd50f73069
commit
1928712364
|
@ -4,19 +4,19 @@ from nose.tools import assert_equal
|
||||||
class TestTreeLevelLists(object):
|
class TestTreeLevelLists(object):
|
||||||
|
|
||||||
def test_tree_level_lists(self):
|
def test_tree_level_lists(self):
|
||||||
node = Node(5)
|
bst = BstLevelLists(Node(5))
|
||||||
insert(node, 3)
|
bst.insert(3)
|
||||||
insert(node, 8)
|
bst.insert(8)
|
||||||
insert(node, 2)
|
bst.insert(2)
|
||||||
insert(node, 4)
|
bst.insert(4)
|
||||||
insert(node, 1)
|
bst.insert(1)
|
||||||
insert(node, 7)
|
bst.insert(7)
|
||||||
insert(node, 6)
|
bst.insert(6)
|
||||||
insert(node, 9)
|
bst.insert(9)
|
||||||
insert(node, 10)
|
bst.insert(10)
|
||||||
insert(node, 11)
|
bst.insert(11)
|
||||||
|
|
||||||
levels = create_level_lists(node)
|
levels = bst.create_level_lists()
|
||||||
results_list = []
|
results_list = []
|
||||||
for level in levels:
|
for level in levels:
|
||||||
results = Results()
|
results = Results()
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"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",
|
"\n",
|
||||||
"* [Constraints](#Constraints)\n",
|
"* [Constraints](#Constraints)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
|
@ -91,7 +91,9 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def create_level_lists(root):\n",
|
"class BstLevelLists(Bst):\n",
|
||||||
|
"\n",
|
||||||
|
" def create_level_lists(self):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass"
|
" pass"
|
||||||
]
|
]
|
||||||
|
@ -136,19 +138,19 @@
|
||||||
"class TestTreeLevelLists(object):\n",
|
"class TestTreeLevelLists(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree_level_lists(self):\n",
|
" def test_tree_level_lists(self):\n",
|
||||||
" node = Node(5)\n",
|
" bst = BstLevelLists(Node(5))\n",
|
||||||
" insert(node, 3)\n",
|
" bst.insert(3)\n",
|
||||||
" insert(node, 8)\n",
|
" bst.insert(8)\n",
|
||||||
" insert(node, 2)\n",
|
" bst.insert(2)\n",
|
||||||
" insert(node, 4)\n",
|
" bst.insert(4)\n",
|
||||||
" insert(node, 1)\n",
|
" bst.insert(1)\n",
|
||||||
" insert(node, 7)\n",
|
" bst.insert(7)\n",
|
||||||
" insert(node, 6)\n",
|
" bst.insert(6)\n",
|
||||||
" insert(node, 9)\n",
|
" bst.insert(9)\n",
|
||||||
" insert(node, 10)\n",
|
" bst.insert(10)\n",
|
||||||
" insert(node, 11)\n",
|
" bst.insert(11)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" levels = create_level_lists(node)\n",
|
" levels = bst.create_level_lists()\n",
|
||||||
" results_list = []\n",
|
" results_list = []\n",
|
||||||
" for level in levels:\n",
|
" for level in levels:\n",
|
||||||
" results = Results()\n",
|
" results = Results()\n",
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"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",
|
"\n",
|
||||||
"* [Constraints](#Constraints)\n",
|
"* [Constraints](#Constraints)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
|
@ -103,13 +103,15 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def create_level_lists(root):\n",
|
"class BstLevelLists(Bst):\n",
|
||||||
" if root is None:\n",
|
"\n",
|
||||||
|
" def create_level_lists(self):\n",
|
||||||
|
" if self.root is None:\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
" results = []\n",
|
" results = []\n",
|
||||||
" current = []\n",
|
" current = []\n",
|
||||||
" parents = []\n",
|
" parents = []\n",
|
||||||
" current.append(root)\n",
|
" current.append(self.root)\n",
|
||||||
" while current:\n",
|
" while current:\n",
|
||||||
" results.append(current)\n",
|
" results.append(current)\n",
|
||||||
" parents = list(current)\n",
|
" parents = list(current)\n",
|
||||||
|
@ -163,19 +165,19 @@
|
||||||
"class TestTreeLevelLists(object):\n",
|
"class TestTreeLevelLists(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree_level_lists(self):\n",
|
" def test_tree_level_lists(self):\n",
|
||||||
" node = Node(5)\n",
|
" bst = BstLevelLists(Node(5))\n",
|
||||||
" insert(node, 3)\n",
|
" bst.insert(3)\n",
|
||||||
" insert(node, 8)\n",
|
" bst.insert(8)\n",
|
||||||
" insert(node, 2)\n",
|
" bst.insert(2)\n",
|
||||||
" insert(node, 4)\n",
|
" bst.insert(4)\n",
|
||||||
" insert(node, 1)\n",
|
" bst.insert(1)\n",
|
||||||
" insert(node, 7)\n",
|
" bst.insert(7)\n",
|
||||||
" insert(node, 6)\n",
|
" bst.insert(6)\n",
|
||||||
" insert(node, 9)\n",
|
" bst.insert(9)\n",
|
||||||
" insert(node, 10)\n",
|
" bst.insert(10)\n",
|
||||||
" insert(node, 11)\n",
|
" bst.insert(11)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" levels = create_level_lists(node)\n",
|
" levels = bst.create_level_lists()\n",
|
||||||
" results_list = []\n",
|
" results_list = []\n",
|
||||||
" for level in levels:\n",
|
" for level in levels:\n",
|
||||||
" results = Results()\n",
|
" results = Results()\n",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user