{ "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": [ "# Solution 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)" ] }, { "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", "We can use either a depth-first or a breadth-first search. Intuitively, it seems like a breadth-first search might be a better fit as we are creating a linked list for each level.\n", "\n", "We can use a modified breadth-first search that keeps track of parents as we build the linked list for the current level.\n", "\n", "* Append the root to the current level's linked list `current`\n", "* While the `current` is not empty:\n", " * Add `current` to `results`\n", " * Set `parents` to `current` to prepare to go one level deeper\n", " * Clear `current` so it can hold the next level\n", " * For each `parent` in `parents`, add the children to `current`\n", "* Return the results\n", " \n", "Complexity:\n", "* Time: O(n)\n", "* Space: O(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class BstLevelLists(Bst):\n", "\n", " def create_level_lists(self):\n", " if self.root is None:\n", " return\n", " results = []\n", " current = []\n", " 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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unit Test" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%run ../utils/results.py" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting test_tree_level_lists.py\n" ] } ], "source": [ "%%writefile 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": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success: test_tree_level_lists\n" ] } ], "source": [ "%run -i test_tree_level_lists.py" ] } ], "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 }