Lecture 7: Recursive Data Structures and Algorithm Exploration
Background Material
- Binary trees
- Binary search trees
- Grammars
Optional Reading
Topics
- Binary trees
- Representing binary trees using recursive lists.
- BinTree ::= [] | [value,BinTree,BinTree]
- Example: [1,[2,[],[]],[3,[],[4,[],[]]]] represents the tree with 1 at the root, whose left subtree contains
a single node with value 2, and whose right subtree is the tree with 3 at the root and whose left subtree
contains a single node with value 4.
- A simple recursive program to print a binary tree.
- Generating and counting all binary trees with a given number of nodes.
- Generating a random binary tree.
- Binary search trees (BST)
- Insertion, search, and building a BST
- Generating a random BST
- Comparing the distribution of random binary trees and BSTs
- Properties of binary trees
- Height: H(Empty) = -1, H(T) = max(H(left),H(right)) + 1
- Number of nodes: N(Empty) = 0, N(T) = N(left) + N(right) + 1. Relationship of the number of internal
nodes and the number of external nodes. Number of external = number of internal + 1.
- Internal path length: sum_{v in T} len(v), where len(v) is the number of edges from the root to the node v.
IPL(Empty) = 0, IPL(T) = IPL(left) + IPL(right) + N(T) - 1.
- Relationship of height and path length to cost of searching in BSTs.
- Empirical study of propeties of binary trees and BSTs
- Max and expected height
- Max and expected internal path length
Slides
- none.
Maple worksheets and programs
- bintree.mw - Worksheet containing
an empirical study of binary and binary search trees.
Include routines to generate and count binary trees.
Created: Oct. 26, 2006 by jjohnson@cs.drexel.edu