33A. Trees


Trees

In general, a tree can either be empty or has a node (called its root) that contains some information and that has a collection of subtrees. We will concentrate on just one kind of tree, a binary ordered tree. Each node in such a tree has two subtrees, called its left subtree and its right subtree. Since binary ordered trees are the only kind that we are going to look at, we just call them trees.

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 stored at each node. Any subtree that is not shown is assumed to be an empty tree.

The root node contains 5, its left subtree is

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


Tree terminology

We have seen the concepts of a node, the root of a tree, the left subtree of a tree and the right subtree of a tree.

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 at the root of the left subtree of v (if there is one) is called the left child of v, and the node at the root of the right subtree (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