{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<small><i>This notebook was prepared by Marco Guajardo. Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Challenge Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem: Implement a binary search tree with insert, delete, different traversals & max/min node values\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",
    "* Is this a binary tree?\n",
    "  * Yes\n",
    "* Is the root set to None initially?\n",
    "  * Yes\n",
    "* Do we care if the tree is balanced?\n",
    "  * No\n",
    "* What do we return for the traversals?\n",
    "  * Return a list of the data in the desired order\n",
    "* What type of data can the tree hold?\n",
    "  * Assume the tree only takes ints. In a realistic example, we'd use a hash table to convert other types to ints."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Cases\n",
    "\n",
    "### Insert \n",
    "\n",
    "* Always start with the root\n",
    "* If value is less than the root, go to the left child\n",
    "* if value is more than the root, go to the right child\n",
    "\n",
    "\n",
    "### Delete\n",
    "\n",
    "* Deleting a node from a binary tree is tricky. Make sure you arrange the tree correctly when deleting a node.\n",
    "* Here are some basic [instructions](http://www.algolist.net/Data_structures/Binary_search_tree/Removal)\n",
    "* If the value to delete isn't on the tree return False\n",
    "\n",
    "### Traverals \n",
    "\n",
    "* In order traversal -left, center, right\n",
    "* Pre order traversal - center, left, right\n",
    "* Post order traversal - left, right, center\n",
    "* Return list for all traverals \n",
    "\n",
    "### Max & Min\n",
    "* Find the max node in the binary search tree\n",
    "* Find the min node in the binary search tree\n",
    "\n",
    "### treeIsEmpty\n",
    "* check if the tree is empty\n",
    "\n",
    "\n",
    "## Algorithm\n",
    "\n",
    "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/binary_tree_implementation/binary_tree_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": [
    "class Node (object):\n",
    "    def __init__ (self, data=None):\n",
    "        #TODO:implement me\n",
    "        pass\n",
    "    \n",
    "    def __str__ (self):\n",
    "        #TODO:implement me\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class BinaryTree (object):\n",
    "    def __init__ (self):\n",
    "        #TODO:implement me\n",
    "        pass\n",
    "    \n",
    "    def insert (self, newData):\n",
    "        #TODO:implement me\n",
    "        pass\n",
    "    \n",
    "    def delete (self, key):\n",
    "        #TODO:implement me\n",
    "        pass\n",
    "    \n",
    "    def maxNode (self):\n",
    "        #TODO:implement me\n",
    "        pass\n",
    "    \n",
    "    def minNode (self):\n",
    "        #TODO:implement me\n",
    "        pass\n",
    "    \n",
    "    def printPostOrder (self):\n",
    "        #TODO:implement me\n",
    "        pass\n",
    "    \n",
    "    def printPreOrder (self):\n",
    "        #TODO:implement me\n",
    "        pass\n",
    "    \n",
    "    def printInOrder (self):\n",
    "        #TODO:implement me\n",
    "        pass\n",
    "    \n",
    "    def treeIsEmpty (self):\n",
    "        #TODO: implement me\n",
    "        pass\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from nose.tools import assert_equal\n",
    "\n",
    "class TestBinaryTree(object):\n",
    "\n",
    "\tdef test_insert_traversals (self):\n",
    "\t\tmyTree = BinaryTree()\n",
    "\t\tmyTree2 = BinaryTree()\n",
    "\t\tfor num in [50, 30, 70, 10, 40, 60, 80, 7, 25, 38]:\n",
    "\t\t\tmyTree.insert(num)\n",
    "\t\t[myTree2.insert(num) for num in range (1, 100, 10)]\n",
    "\n",
    "\t\tprint(\"Test: insert checking with in order traversal\")\n",
    "\t\texpectVal = [7, 10, 25, 30, 38, 40, 50, 60, 70, 80]\n",
    "\t\tassert_equal(myTree.printInOrder(), expectVal)\n",
    "\t\texpectVal = [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]\n",
    "\t\tassert_equal(myTree2.printInOrder(), expectVal)\n",
    "\n",
    "\t\tprint(\"Test: insert checking with post order traversal\")\n",
    "\t\texpectVal = [7, 25, 10, 38, 40, 30, 60, 80, 70, 50]\n",
    "\t\tassert_equal(myTree.printPostOrder(), expectVal)\n",
    "\t\texpectVal = [91, 81, 71, 61, 51, 41, 31, 21, 11, 1]\n",
    "\t\tassert_equal(myTree2.printPostOrder(), expectVal)\n",
    "\n",
    "\n",
    "\t\tprint(\"Test: insert checking with pre order traversal\")\n",
    "\t\texpectVal = [50, 30, 10, 7, 25, 40, 38, 70, 60, 80]\n",
    "\t\tassert_equal(myTree.printPreOrder(), expectVal)\n",
    "\t\texpectVal = [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]\n",
    "\t\tassert_equal(myTree2.printPreOrder(), expectVal)\n",
    "\n",
    "\n",
    "\t\tprint(\"Success: test_insert_traversals\")\n",
    "\n",
    "\tdef test_max_min_nodes (self):\n",
    "\t\tmyTree = BinaryTree()\n",
    "\t\tmyTree.insert(5)\n",
    "\t\tmyTree.insert(1)\n",
    "\t\tmyTree.insert(21)\n",
    "\n",
    "\t\tprint(\"Test: max node\")\n",
    "\t\tassert_equal(myTree.maxNode(), 21)\n",
    "\t\tmyTree.insert(32)\n",
    "\t\tassert_equal(myTree.maxNode(), 32)\n",
    "\n",
    "\t\tprint(\"Test: min node\")\n",
    "\t\tassert_equal(myTree.minNode(), 1)\n",
    "\n",
    "\t\tprint(\"Test: min node inserting negative number\")\n",
    "\t\tmyTree.insert(-10)\n",
    "\t\tassert_equal(myTree.minNode(), -10)\n",
    "\n",
    "\t\tprint(\"Success: test_max_min_nodes\")\n",
    "\n",
    "\tdef test_delete (self):\n",
    "\t\tmyTree = BinaryTree()\n",
    "\t\tmyTree.insert(5)\n",
    "\n",
    "\t\tprint(\"Test: delete\")\n",
    "\t\tmyTree.delete(5)\n",
    "\t\tassert_equal(myTree.treeIsEmpty(), True)\n",
    "\t\t\n",
    "\t\tprint(\"Test: more complex deletions\")\n",
    "\t\t[myTree.insert(x) for x in range(1, 5)]\n",
    "\t\tmyTree.delete(2)\n",
    "\t\tassert_equal(myTree.root.rightChild.data, 3)\n",
    "        \n",
    "\t\tprint(\"Test: delete invalid value\")\n",
    "\t\tassert_equal(myTree.delete(100), False)\n",
    "\n",
    "\n",
    "\t\tprint(\"Success: test_delete\")\n",
    "\n",
    "def main():\n",
    "    testing = TestBinaryTree()\n",
    "    testing.test_insert_traversals()\n",
    "    testing.test_max_min_nodes()\n",
    "    testing.test_delete()\n",
    "    \n",
    "if __name__=='__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**The following unit test is expected to fail until you solve the challenge.**"
   ]
  },
  {
   "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/binary_tree_implementation/binary_tree_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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
