{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Challenge Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem: Create a list for each level of a binary tree.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", "* [Algorithm](#Algorithm)\n", "* [Code](#Code)\n", "* [Unit Test](#Unit-Test)\n", "* [Solution Notebook](#Solution-Notebook)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constraints\n", "\n", "* Is this a binary search tree?\n", " * Yes\n", "* Should each level be a list of nodes?\n", " * Yes\n", "* Can we assume we already have a Node class with an insert method?\n", " * Yes\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "* 5, 3, 8, 2, 4, 1, 7, 6, 9, 10, 11 -> [[5], [3, 8], [2, 4, 7, 9], [1, 6, 10], [11]]\n", "\n", "Note: Each number in the result is actually a node containing the number" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Algorithm\n", "\n", "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%run ../bst/bst.py\n", "%load ../bst/bst.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class BstLevelLists(Bst):\n", "\n", " def create_level_lists(self):\n", " # TODO: Implement me\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unit Test" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The following unit test is expected to fail until you solve the challenge.**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%run ../utils/results.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %load test_tree_level_lists.py\n", "import unittest\n", "\n", "\n", "class TestTreeLevelLists(unittest.TestCase):\n", "\n", " def test_tree_level_lists(self):\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 = bst.create_level_lists()\n", " results_list = []\n", " for level in levels:\n", " results = Results()\n", " for node in level:\n", " results.add_result(node)\n", " results_list.append(results)\n", "\n", " self.assertEqual(str(results_list[0]), '[5]')\n", " self.assertEqual(str(results_list[1]), '[3, 8]')\n", " self.assertEqual(str(results_list[2]), '[2, 4, 7, 9]')\n", " self.assertEqual(str(results_list[3]), '[1, 6, 10]')\n", " self.assertEqual(str(results_list[4]), '[11]')\n", "\n", " print('Success: test_tree_level_lists')\n", "\n", "\n", "def main():\n", " test = TestTreeLevelLists()\n", " test.test_tree_level_lists()\n", "\n", "\n", "if __name__ == '__main__':\n", " main()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution Notebook\n", "\n", "Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb) for a discussion on algorithms and code solutions." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.2" } }, "nbformat": 4, "nbformat_minor": 1 }