**Theorem 3.4.1**: The number b_{n} of different binary trees on n vertices is given by

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.

**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)

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

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