# C Tutorial, Part VII

## Binary Trees

In a graph-theoretic sense, a tree is a connected, undirected, acyclic graph. In a data structure sense, a tree is accessed beginning at a node distinguished as the root. Every node in the tree is either a leaf (a node with no children/descendants) or an interior node.

In a formal sense, a tree is either

• empty (no nodes), or
• a root and zero or more subtrees

A binary tree is a tree in which each node has at most 2 children. A binary search tree (BST) is a binary tree in which the values stored in the left subtree are less than the value stored at the node and the values stored in the right subtree are greater than the value stored at the node. A BST has search performance of O(log n) assuming it's balanced.

Other terms to be aware of are

• depth - the length of the path (# of edges) from the root to a node
• height - the length of the longest path from the node to a leaf
• height of the tree - the length of the longest path from the root to a leaf (thus, the height of a 1-node tree, consisting solely of the root, is 0)

Some interesting things to consider:

• What is the maximum number of nodes on a tree of height n?
• What is the minimum height of a tree with n nodes? The max?
• How many distinct traversals are possible on a binary tree and what might their output be useful for?

The code for "standard" binary tree operations in C is not that different from what you'd write in Java. We've provided code that implements most of the standard ops in b-tree-code.c. I encourage you to take a look at the `printTree` function which makes use of a non-standard traversal of the tree to print a "reasonable" approximation of the tree rotated 90 degrees.