{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tree Representation Implementation (Lists)\n", "\n", "Below is a representation of a Tree using a list of lists. Refer to the video lecture for an explanation and a live coding demonstration!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def BinaryTree(r):\n", " return [r, [], []]\n", "\n", "def insertLeft(root,newBranch):\n", " t = root.pop(1)\n", " if len(t) > 1:\n", " root.insert(1,[newBranch,t,[]])\n", " else:\n", " root.insert(1,[newBranch, [], []])\n", " return root\n", "\n", "def insertRight(root,newBranch):\n", " t = root.pop(2)\n", " if len(t) > 1:\n", " root.insert(2,[newBranch,[],t])\n", " else:\n", " root.insert(2,[newBranch,[],[]])\n", " return root\n", "\n", "def getRootVal(root):\n", " return root[0]\n", "\n", "def setRootVal(root,newVal):\n", " root[0] = newVal\n", "\n", "def getLeftChild(root):\n", " return root[1]\n", "\n", "def getRightChild(root):\n", " return root[2]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }