{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Binary Search Trees\n", "\n", "## Agenda\n", "\n", "- Binary Trees & Binary Search Trees: definitions\n", "- Linked tree structure and Manual construction\n", "- Recursive binary search tree functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Binary Tree: def\n", "\n", "- A *binary tree* is a structure that is either empty, or consists of a *root* node containing a value and references to a left and right *sub-tree*, which are themselves binary trees." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Binary Search Tree (BSTree): def\n", "\n", "- A *binary search tree* is a binary tree where the value contained in every node is:\n", " - *greater than* all keys in its left subtree, and\n", " - *less than* all keys in its right subtree" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linked tree structure and Manual construction:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Node:\n", " def __init__(self, val, left=None, right=None):\n", " self.val = val\n", " self.left = left\n", " self.right = right" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Recursive bstree functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def min(t):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def max(t):\n", " pass" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def find(t, x):\n", " pass" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def traverse(t, fn):\n", " pass" ] } ], "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 }