{"cells":[{"cell_type":"markdown","metadata":{},"source":"* Binary Search Trees\n=====================\n\n"},{"cell_type":"markdown","metadata":{},"source":["## Agenda\n\n"]},{"cell_type":"markdown","metadata":{},"source":["- Binary Trees & Binary Search Trees: definitions\n- Linked tree structure and Manual construction\n- Recursive binary search tree functions\n\n"]},{"cell_type":"markdown","metadata":{},"source":["## Binary Tree: def\n\n"]},{"cell_type":"markdown","metadata":{},"source":["- A *binary tree* is a structure that is either empty, or consists of a\n\n*root* node containing a value and references to a left and right\n*sub-tree*, which are themselves binary trees.\n\nNaming nodes:\n\n- The single node in a binary tree without a parent is the root node of\n\nthe tree\n\n- We say that a given node is the *parent* of its left and right *child*\n\nnodes; nodes with the same parent are called *siblings*\n\n- If a node has no children we call it a *leaf* node; otherwise, we call\n\nit an *internal* node\n\nBinary tree metrics (note: alternative defs are sometimes used!):\n\n- The *depth* of a node is the number of nodes from the root of the tree\n\nto that node (inclusive)\n\n- The *height* of a node is the number of nodes on the longest path from\n\nthat node down to a leaf (inclusive)\n\nCategorizing binary trees:\n\n- A *complete* binary tree is one where all but the last level are\n\nfilled, and in the last level leaves are as far to the left as\npossible\n\n- A *perfect* binary tree is one where all internal nodes have 2\n\nchildren, and all leaves have the same depth\n\n- A *balanced* binary tree is … ?\n\n"]},{"cell_type":"markdown","metadata":{},"source":["## Binary Search Tree (BSTree): def\n\n"]},{"cell_type":"markdown","metadata":{},"source":["- A *binary search tree* is a binary tree where the value contained in\n\nevery node is:\n\n- *greater than* all keys in its left subtree, and\n- *less than* all keys in its right subtree\n\n"]},{"cell_type":"markdown","metadata":{},"source":["## Linked tree structure and Manual construction:\n\n"]},{"cell_type":"code","execution_count":1,"metadata":{},"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\n\n def __repr__(self):\n def str_rec(t,depth):\n if not t:\n return \"\"\n else:\n return ((\"\\t\" * depth)\n + str(t.val)\n + \"\\n\" + str_rec(t.left, depth + 1)\n + str_rec(t.right, depth + 1))\n\n return str_rec(self, 0)"]},{"cell_type":"markdown","metadata":{},"source":["## Recursive bstree functions\n\n"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["def tmin(t):\n pass"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["import sys\n\ndef max_with_none(*nums):\n result = None\n for n in nums:\n if not result:\n result = n\n elif n:\n result = max(result,n)\n return result\n\ndef tmax(t: Node):\n if not t:\n return None\n return max_with_none(t.val, tmax(t.left), tmax(t.right))"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["def find(t, x):\n if not t:\n return False\n if t.val == x:\n return True\n if t.val > x:\n return find(t.left, x)\n if t.val < x:\n return find(t.right, x)"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["import builtins\nmax = builtins.max\ndef height(t):\n if not t:\n return 0\n return 1 + max([height(t.left), height(t.right)])"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["def visit(t):\n pass"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["def map(t,f):\n f(t.val)\n if t.left:\n map(t.left, f)\n if t.right:\n map(t.right, f)"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["myt = Node(3, Node(1), Node(5))\n#print(f\"height: {height(myt)}\")\nmyt\nheight(myt)\ntmax(myt)"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["map(myt,lambda x: print (x))"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["print(f\"\"\"find 3: {find(myt, 3)}\nfind 5: {find(myt, 5)}\nfind 1: {find(myt, 1)}\nfind 2: {find(myt, 2)}\"\"\")"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":[""]}],"metadata":{"org":null,"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.5.2"}},"nbformat":4,"nbformat_minor":0}