Chapter 6. Trees II

6.3 Binary Trees

A binary tree is an ordered tree in which every node has at most two children. A binary tree is proper if each internal node has two children.
Trees.pdf  8

Example: arithmetic expression, decision tree
Trees.pdf  8

6.3.1 The Binary Tree ADT

Trees.pdf  12

6.3.2 A Binary Tree Interface

html-6.14a (InspectableBinaryTree)
html-6.14b (BinaryTree)

6.3.3 Properties of Binary Trees

We denote the set of all nodes of a binary tree T, at the same depth d, as the level d of T.
Proposition 6.9: Let T be a proper binary tree with n nodes and let h denote the height of T. Then T has the following properties:
  1. The number of external nodes e in T is: h + 1 <= e <= 2h
  2. The number of internal nodes i in T is: h <= i <= 2h - 1
  3. The total number of nodes n in T is: 2h + 1 <= n <= 2h+1 - 1
  4. The height h of T is: log(n + 1) - 1 <= h <= (n - 1)/2
Proposition 6.10: In a proper binary tree  T, the number of external nodes e is 1 more than the number of internal nodes i, i.e. e = i + 1.

Trees.pdf  11

6.3.4 Traversals of a Binary Tree

Preorder Traversal of a Binary Tree
void binaryPreorderPrint(const Tree& T, const Position& v) 
{ cout << v.element(); // print element
if (isInternal(v)) // visit children
{ cout << " ";
binaryPreorderPrint(T, T.leftChild(v));
binaryPreorderPrint(T, T.rightChild(v));
}
}
Postorder Traversal of a Binary Tree
void binaryPostorderPrint(const Tree& T, const Position& v) 
{
if (isInternal(v)) // visit children
{ cout <<
" ";
binaryPostorderPrint(T, T.leftChild(v));
binaryPostorderPrint(T, T.rightChild(v));
}
cout << v.element(); // print element
}
Evaluating an Arithmetic Expression
Trees.pdf  15
O(n) time algorithm

Inorder Traversal of a Binary Tree
Trees.pdf  13
void binaryInorderPrint(const Tree& T, const Position& v) 
{
if (isInternal(v)) // visit left child
binaryInorderPrint(T, T.leftChild(v));
cout << v.element(); // print element
if (isInternal(v)) // visit right child
binaryInorderPrint(T, T.rightChild(v));
}
Visit "left to right"

Binary Search Trees
    Binary search tree
is a binary tree so that each internal node v stores an element e, such that:
Position searchBinaryTree(const Tree& T, const Position& v, const Object& e)
{
if (isInternal(v))
if (v.element() == e) return v; // found!
else if (v.element() < e)
searchBinaryTree(T, T.leftChild(v), e);
// search left subtree
else
searchBinaryTree(T, T.rightChild(v), e); // search right subtree
else return ... // not found!
}
The time for searching is a binary tree T proportional to the height of T, i.e. >= O(log n) and <= Omega(n)

A Unified Tree Traversal Framework

The Euler Tour Traversal of a Binary Tree
Trees.pdf  16


6.4 Data Structures for Representing Trees

6.4.1 A Vector-Based Structure for Binary Trees

6.4.2 A Linked Structure for Binary Trees

Trees.pdf  20

Nodes and Positions in a Binary Tree
html-6.27 (Node)
html-6.28 (Position)

Binary Tree Update Functions
html-6.29 (LinkedBinaryTree1)
html-6.30 (LinkedBinaryTree2)

LinkedBinaryTree.cpp

Copying a Binary Tree

6.4.3 A Linked Structure for General Trees

Trees.pdf  19

6.4.4 Representing General Trees with Binary Trees

A representation of a general (ordered) tree T  is obtained by transforming T  into a binary tree T '. The transformation is as follows:
[Picture]