.. _Binary2: .. raw:: html .. |--| unicode:: U+2013 .. en dash .. |---| unicode:: U+2014 .. em dash, trimming surrounding whitespace :trim: .. odsalink:: AV/Binary/FullCompCON.css .. odsalink:: AV/Binary/BSTCON.css .. This file is part of the OpenDSA eTextbook project. See .. http://algoviz.org/OpenDSA for more details. .. Copyright (c) 2012-2013 by the OpenDSA Project Contributors, and .. distributed under an MIT open source license. .. avmetadata:: :author: Cliff Shaffer =================== Binary Trees Part 2 =================== Binary Trees Part 2 ------------------- Full and Complete Binary Trees ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Full binary tree: Each node is either a leaf or internal node with exactly two non-empty children. Complete binary tree: If the height of the tree is :math:`d`, then all leaves except possibly level :math:`d` are completely full. The bottom level has all nodes to the left side. .. inlineav:: FullCompCON dgm :align: center Full Binary Tree Theorem (1) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **Theorem:** The number of leaves in a non-empty full binary tree is one more than the number of internal nodes. **Proof** (by Mathematical Induction): **Base case:** A full binary tree with 1 internal node must have two leaf nodes. **Induction Hypothesis:** Assume any full binary tree **T** containing :math:`n-1` internal nodes has :math:`n` leaves. Full Binary Tree Theorem (2) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **Induction Step:** Given tree **T** with :math:`n` internal nodes, pick internal node :math:`I` with two leaf children. Remove :math:`I`'s children, call resulting tree **T'**. By induction hypothesis, **T'** is a full binary tree with :math:`n` leaves. Restore :math:`I`'s two children. The number of internal nodes has now gone up by 1 to reach :math:`n`. The number of leaves has also gone up by 1. Full Binary Tree Corollary ~~~~~~~~~~~~~~~~~~~~~~~~~~ **Theorem:** The number of null pointers in a non-empty tree is one more than the number of nodes in the tree. **Proof:** Replace all null pointers with a pointer to an empty leaf node. This is a full binary tree. Dictionary ~~~~~~~~~~ .. codeinclude:: Design/Dictionary :tag: DictionaryADT . ~ . Dictionary (2) ~~~~~~~~~~~~~~ * How can we implement a dictionary? * We know about array-based lists and linked lists. * They might be sorted or unsorted. * What are the pros and cons? Binary Search Trees ~~~~~~~~~~~~~~~~~~~ .. odsafig:: Images/BSTShape2.png :width: 500 :align: center :capalign: justify :figwidth: 90% :alt: Two Binary Search Trees BST as a Dictionary (1) ~~~~~~~~~~~~~~~~~~~~~~~ .. codeinclude:: Binary/BST :tag: BSTa BST as a Dictionary (2) ~~~~~~~~~~~~~~~~~~~~~~~ .. codeinclude:: Binary/BST :tag: BSTb BST ``findhelp`` ~~~~~~~~~~~~~~~~ .. inlineav:: BSTsearchCON ss :output: show BST ``inserthelp`` ~~~~~~~~~~~~~~~~~~ .. inlineav:: BSTinsertCON ss :output: show BST ``deletemax`` ~~~~~~~~~~~~~~~~~ .. inlineav:: BSTdeletemaxCON ss :output: show BST ``removehelp`` ~~~~~~~~~~~~~~~~~~ .. inlineav:: BSTremoveCON ss :output: show . ~ . BST Analysis ~~~~~~~~~~~~ Find: :math:`O(d)` Insert: :math:`O(d)` Delete: :math:`O(d)` :math:`d =` depth of the tree :math:`d` is :math:`O(\log n)` if the tree is balanced. What is the worst case cost? When? .. odsascript:: AV/Binary/FullCompCON.js .. odsascript:: AV/Binary/BSTsearchCON.js .. odsascript:: AV/Binary/BSTinsertCON.js .. odsascript:: AV/Binary/BSTdeletemaxCON.js .. odsascript:: AV/Binary/BSTremoveCON.js