36A. Binary Trees


Trees

A tree is a collection of connected nodes. In a binary rooted ordered tree (which we call a tree), each node has exactly two subtrees, called its left subtree and its right subtree. Additionally, a node contains some information, which we will assume is an integer for simplicity.

A tree is either empty or nonempty. A nonempty tree has a node, called the root, which, along with the subtrees, make up the whole tree. It is common practice to show a diagram of a tree with the root at the top. Here is an example of a tree that has an integer at each node. Any subtree that is not shown is assumed to be an empty tree.

The root node contains 5, the left subtree is

and the right subtree has just one node, holding 15. Each of the subtrees of the node holding 15 is an empty tree.


Tree terminology

A node with two empty subtrees is called a leaf. All nodes that are not leaves are called nonleaves, or sometimes internal nodes.

If v is a node then the node just beneath v to the left (if there is one) is called the left child of v, and the node just beaneath v to the right (if there is one) is called the right child of v. The parent of a node is the node immediately above it. For example, if u is the left child of v then v is the parent of u.


Exercises

  1. What are the left and right subtrees of the following tree?

    Answer

  2. What are the left and right subtrees of the following tree?

    Answer

  3. What is the node at the top of a tree called? Answer

  4. What is a leaf? Answer