## Graph Theory Lecture Notes 5

### Counting Binary Trees

Let bn denote the number of binary trees on n vertices. We define, for convenience, b0 = 1. Clearly, b1 = 1. For n > 1, a binary tree on n vertices has a left subtree on say j vertices and a right subtree on n-1-j vertices. To count the number of trees we would add up the possibilities for each value of j, thus:
bn = b0bn-1 + b1bn-2 + ... + bn-1b0.
This recursion relation is known as the Catalan Recursion and the numbers bn are known as the Catalan numbers. The Catalan numbers solve a large number of different looking counting problems. For example, the number of ways to insert a pair of parentheses (correctly) in a string of n+1 letters, the number of triangulations of a convex (n+2)-gon into n triangles by n-1 diagonals what do not intersect in their interiors, the number of lattice paths from (0,0) to (n,n) with steps (0,1) or (1,0), never rising above the line y=x, and the number of sequences of n 1's and n -1's so that each partial sum is nonnegative.

Theorem 3.4.1: The number bn of different binary trees on n vertices is given by

### Tree traversal

A stack is a sequence of elements such that each new element is added (pushed) onto one end, called the top of the stack, and an element is removed (popped) from the same end. Also, known as a FILO (First In Last Out) buffer.

A queue is a sequence of elements such that each new element is added (enqueued) to one end, called the back of the queue, and an element is removed (dequeued) from the other end, called the front. Also known as a FIFO (First In First Out) buffer.

The level-order of an ordered tree is a listing of the vertices in increasing order of depth, such that the vertices of equal depth are listed according to their prescribed order.

Level-Order Traversal

• Enqueue root.
• While queue is not empty:
• Dequeue a vertex and write it on the output list.
• Enqueue its children left to right.
A Left-first walk around a tree starts at the root and walks alongside the edges keeping the edges always on the left, until returning to the root after visiting all the edges.

Three types of tree traversals

Left Pre-Order Traversal
In a left-first walk, record each vertex the first time it is seen (and not again).
• Push root onto stack.
• While stack is not empty:
• Pop a vertex off stack, and write it on output list.
• Push its children, right to left onto stack.
Post-Order Traversal
In a left-first walk, record each vertex the last time it is seen (and not before).
• Push root onto stack
• While stack is not empty
• If top(stack) is unmarked, mark it and push its children right to left onto stack,
• Else, pop stack to output list.
In-Order Traversal
In a left-first walk, record each leaf as soon as you see it and each internal vertex the second time you see it.(To get a correct in-order traversal this needs to be modified as follows: When you encounter a vertex with no left-child, adjust the walk as if there was a left-child but with no vertex at the end of the edge. Alternatively, for each internal vertex with no left child, record it the first time you see it.)
• Push root onto stack
• While stack is not empty
• v := top(stack)
• While v has a left-child
• Push leftchild(v) onto stack
• v:= leftchild(v)
• v := pop(stack)
• List v
• If v has a right-child
• Push rightchild(v) onto stack
• v := rightchild(v)
• Else
• While stack not empty and v has no right-child
• v := pop(stack)
• List v
• If v has a right-child
• Push rightchild(v) onto stack
• v := rightchild(v)
An expression tree for an arithmetic expression is either a single vertex labeled by an identifier or is described by the following recursive rule:
• The operator to be executed is the root
• The left subtree is an expression tree for the left operand.
• The right subtree is an expression tree for the right operand.
Prefix, postfix, and infix notations for an expression are obtained by traversing the expression tree in pre-order, post-order or in-order methods.

### Binary Search Trees

A random-access table is a database whose domain is a sequence of entries, each having two fields. The fields consist of the data and a key, whose value determines the entry's position in the database.

A binary search tree (BST) is an ordered binary tree, each of whose vertices is assigned a key, such that the key assigned to any vertex v is greater than the key at each vertex of the left subtree at v and less than the key at any vertex of the right subtree of v.

BST Search

Input: A binary search tree T and a target key t.
Output: A vertex v of T such that key(v) = t if t is found, or NULL if t is not found.

v := root(T)
While (v is not NULL) and (t is not key(v))
If t > key(v) then v := rightchild(v) else v := leftchild(v)
Return v.

A binary tree is balanced if for every vertex, the number of vertices in its left and right subtrees differ by at most one.

Inserting and Deleting in a Binary Search Tree

Inserting vertices and deleting vertices that have only one child is straightforward. The only difficulty arises when you are deleting a vertex with two children. If v is such a vertex, replace v by the leftmost (minimum) element u of the right subtree of v and replace u with the right subtree of u (it doesn't have a left subtree).