10.17. Binary Trees Part 2¶
10.17.1. Binary Trees Part 2¶
10.17.1.1. 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 \(d\), then all leaves except possibly level \(d\) are completely full. The bottom level has all nodes to the left side.
10.17.1.2. 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 \(n-1\) internal nodes has \(n\) leaves.
10.17.1.3. Full Binary Tree Theorem (2)¶
Induction Step: Given tree T with \(n\) internal nodes, pick internal node \(I\) with two leaf children. Remove \(I\)’s children, call resulting tree T’.
By induction hypothesis, T’ is a full binary tree with \(n\) leaves.
Restore \(I\)’s two children. The number of internal nodes has now gone up by 1 to reach \(n\). The number of leaves has also gone up by 1.
10.17.1.4. 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.
10.17.1.5. Dictionary¶
10.17.1.6. .¶
.
10.17.1.7. 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?
10.17.1.8. Binary Search Trees¶
10.17.1.9. BST as a Dictionary (1)¶
10.17.1.10. BST as a Dictionary (2)¶
10.17.1.11. BST findhelp¶
10.17.1.12. BST inserthelp¶
10.17.1.13. BST deletemax¶
10.17.1.14. BST removehelp¶
10.17.1.15. .¶
.
10.17.1.16. BST Analysis¶
Find: \(O(d)\)
Insert: \(O(d)\)
Delete: \(O(d)\)
\(d =\) depth of the tree
\(d\) is \(O(\log n)\) if the tree is balanced.
What is the worst case cost? When?

