10 Trees

Version 8.01

A. Binary Trees and Their Traversals

10.1 Binary Trees

Terminology

Binary tree

(Recursively defined:) A binary tree is either empty or it consists of a root node, a left subtree, and a right subtree; both subtrees are binary trees.

Child

The root of a subtree of a node.

Parent

A node having a child.

Sibling

Child of the same parent.

Root

The node which has no parent.

Leaf

A node having no child.

Internal node

A node that has at least one child. Could be any non-leaf node, including the root.

Depth

The number of lines (not nodes) on a path to the root. (The root’s depth is 0; its children have depth 1.)

Level

All the nodes at a particular depth (say, 3).

Height

The depth of the deepest node; the maximum depth of nodes in the tree.

Descendant

The descandants of a node include itself, its children, and their descendants.

Proper descendant

A descendant of a node, except the node itself.

Ancestor

The ancestors of a node include itself, its parent, and its parent’s ancestors.

Proper ancestor

An ancestor of a node, except the node itself.

Shapes of binary trees

Linear

Each internal node has one child; does it matter on which side?

Perfect

Each internal node has two children; all leaves at the same depth. (Usually called “complete”)

Size and Height Rules

Implementations

Application

10.2 Tree Traversal

(See diagram WHICH IS TO BE ADDED)

B. General Trees and Their Applications

10.3 General Trees

Representations

  1. Root node with a list or array or set of children.

  2. “First child, next sibling” representation2 uses less space than representation 1:

    This general tree

    Figure 7. A general tree.
    Figure 7. A general tree.

    is represented as

            A
           /
          G → E → C
         /       /
        B       D → F

which is really (or can represent — show how) a binary tree, but with the left child (/) interpreted as “first child”, and the right child (→) interpreted as “next sibling”.

Example: An Intelligent Tic-Tac-Toe Player

Minimax algorithm used in game playing (chess, checkers, etc.), and real-life conflicts, such as war.

The “game tree” is not explicitly built up in memory as a data structure, but illustrates the strategy; if it’s in memory at all, it’s implicit in the stack of recursive procedure calls.

More Applications of General Trees

(Not in the textbook)

The XML Document Object Model (DOM)

Footnotes


  1. Revision history:
    • Version 8.0, 2014 Nov 10. Moved functional parts to functional-trees.md
    • Version 7.2.1, 2013 Nov 6. Minor edit.
    • Version 7.2, 2010 Oct 27. Typographical correction (|Theta| → Θ).
    • Version 7.1, 2010 Oct 26. Added Haskell XML processing examples.
    • Version 7, 2010 Oct 23. Converted to markdown, new diagrams, added Haskell binary and general trees.
    • Version 6, 2009 Oct 26. Converted to rst. Expanded definitions of terms. Struggled with diagrams.
    • Version 5, 2008 Oct 18. Minor re-organization, improved HTML (less preformatted text).
    • Version 4, 2007 Oct. 29. Typographical corrections, slightly improved formatting.
    • Version 3, 2007 Oct 22. Partially reformatted as HTML.
    • Version 2, complete, 2006 Nov 2.
    • Version 1, incomplete, 2006 Oct 19.
  2. We call it “first child, next sibling” — but which is first? The choice is arbitrary, unless we have an ordered tree, and then the children (subtrees) are assigned a particular order.