Chapter 6. Trees I
6.1 The Tree Abstract Data Type
A tree is an ADT that
stores
elements hierarchically. With the exception of the top (root) element, each element in a
tree has a parent element and
zero or more children.
Trees.pdf 1-2-3
6.1.1 Terminology and Basic
Properties
Trees.pdf 4
-
A tree
T is a set of nodes storing elements in a parent-child relationship with the
following properties:
- T
has a special node r, called
the root of T, with no parent node.
- Each node v
of T different from r has a unique parent node u.
- If node u
is the parent
of node v, we say that v is
a child of u.
- Two nodes that are children of the same parent
are siblings.
- A node is external
if
has no children, and it is internal
if it has one or more
children. External nodes are also known as leaves.
- The subtree
of T rooted at a node v is the tree consisting of all the
descendents of v in T (including v itself).
- An ancestor
of a node is
either the node itself or an ancestor of the parent of the node.
- A node v
is a descendent of a node u if u is an ancestor of v.
Ordered Trees
- A tree is ordered
if
there is a linear ordering defined for children of each node (siblings
ordering).
- A binary tree
is an
ordered tree in which every node has at most two children.
- A binary tree is proper
if each node has either zero or two children.
- For each internal node in a binary tree, we
label each child as
either being a left child or a
right child.
- The subtree rooted at a left or right child of
an internal node v
is called a left subtree or right subtree of v, respectively.
6.1.2 Tree Functions
Trees.pdf 5
6.1.3 A Tree Interface
html-6.1a
(InspectablePositionalContainer)
html-6.1b
(PositionalContainer)
html-6.1c
(InspectableTree)
html-6.1d (Tree)
6.2 Basic Algorithms on Trees
6.2.1 Running-Time Assumptions
|
Input
|
Output
|
|
root() |
None
|
Position
|
O(1)
|
parent(v) |
Position |
Position |
O(1)
|
isInternal(v)
|
Position |
bool
|
O(1)
|
isExternal(v) |
Position |
bool
|
O(1)
|
isRoot(v) |
Position |
bool
|
O(1)
|
children(v) |
Position |
Iterator of
positions |
O(cv)
|
swapElements(v,w) |
Two positions
|
Nine
|
O(1)
|
replaceElement(v,e)
|
A position and
an object
|
None
|
O(1)
|
elements() |
None
|
Iterator of
objects
|
O(n)
|
positions() |
None
|
Iterator of
positions
|
O(n)
|
6.2.2 Depth and Height
The depth of v is the number of ancestors of v, excluding v itself.
html-6.3 (depth)
The height of a node v in a tree T is also defined recursively.
- If v
is an external
node, then the height of v is
0.
- Otherwise, the height of v
is one plus the maximum height of a child of v.
The height of a tree T is the height of the root of T.
html-6.5 (height1)
html-6.6 (height2)
6.2.3 Preorder Traversal
1. node 2. descendents
Trees.pdf 6
Tree Printing: An Application of Preorder Traversal
html-6.9
(preorderPrint)
Using Preorder Traversal for Representing a Tree
html-6.10
(parentPrint)
6.2.4 Postorder Traversal
1. descendents 2. node
Trees.pdf
7
An Illustrative Use of the Postorder Traversal
html-6.12
(postorderPrint)
A Postorder Algorithm for Computing Disk Usage
html-6.13
(diskSpace)
Other Kinds of Traversals
For example, we could traverse a tree so that we visit all the nodes at
depth d before we visit the
nodes at depth d+1.