# Lecture 7: Recursive Data Structures and Algorithm Exploration

### Background Material

• Binary trees
• Binary search trees
• Grammars

• Data structures text

### Topics

1. Binary trees
1. Representing binary trees using recursive lists.
1. BinTree ::= [] | [value,BinTree,BinTree]
2. 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.
2. A simple recursive program to print a binary tree.
3. Generating and counting all binary trees with a given number of nodes.
4. Generating a random binary tree.
2. Binary search trees (BST)
1. Insertion, search, and building a BST
2. Generating a random BST
3. Comparing the distribution of random binary trees and BSTs
3. Properties of binary trees
1. Height: H(Empty) = -1, H(T) = max(H(left),H(right)) + 1
2. 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.
3. 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.
4. Relationship of height and path length to cost of searching in BSTs.
4. Empirical study of propeties of binary trees and BSTs
1. Max and expected height
2. Max and expected internal path length

1. 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