The tree data structure
Trees
COL 106
Acknowledgement :Many slides are courtesy
Douglas Harder, UWaterloo
1
The tree data structure
2
Trees
A rooted tree data structure stores information in nodes
– Similar to linked lists:
• There is a first node, or root
• Each node has variable number of references to successors
(children)
• Each node, other than the root, has exactly one node as its
predecessor (or parent)
The tree data structure
3
Tree Terminology (2)
• We also use words like ancestor and descendent
3
The tree data structure
4
What are trees suitable for ?
4
The tree data structure
To store organization of departments
5
5
The tree data structure
6
To capture the evolution of languages
6
The tree data structure
7
Markup elements in a webpage
7
The tree data structure
To store phylogenetic data
8
This will be our running example. Will illustrate tree
concepts using actual phylogenetic data. 8
The tree data structure
9
Terminology
All nodes will have zero or more child nodes or children
– I has three children: J, K and L
For all nodes other than the root node, there is one
parent node
– H is the parent of I
The tree data structure
10
Terminology
The degree of a node is defined as the number of its
children: deg(I) = 3
Nodes with the same parent are siblings
– J, K, and L are siblings
The tree data structure
11
Terminology
Phylogenetic trees have nodes with degree 2 or 0:
The tree data structure
12
Terminology
Nodes with degree zero are also called leaf nodes
All other nodes are said to be internal nodes, that is, they
are internal to the tree
The tree data structure
13
Terminology
Leaf nodes:
The tree data structure
14
Terminology
Internal nodes:
The tree data structure
15
Terminology
These trees are equal if the order of the children is
ignored (Unordered trees )
They are different if order is relevant (ordered
trees)
– We will usually examine ordered trees (linear orders)
– In a hierarchical ordering, order is not relevant
The tree data structure
16
Terminology
The shape of a rooted tree gives a natural
flow from the root node, or just root
The tree data structure
17
Terminology
A path is a sequence of nodes
(a0, a1, ..., an)
where ak + 1 is a child of ak is
The length of this path is n
E.g., the path (B, E, G)
has length 2
The tree data structure
18
Terminology
Paths of length 10 (11 nodes) and 4 (5 nodes)
Start of these paths
End of these paths
The tree data structure
19
Terminology
For each node in a tree, there exists a unique path from
the root node to that node
The length of this path is the depth of the node, e.g.,
– E has depth 2
– L has depth 3
The tree data structure
20
Terminology
Nodes of depth up to 17
0
14
17
The tree data structure
21
Tree Terminology
• A tree is a collection of elements (nodes)
• Each node may have 0 or more successors
• (Unlike a list, which has 0 or 1 successor)
• Each node has exactly one predecessor
• Except the starting / top node, called the root
• Links from node to its successors are called branches
• Successors of a node are called its children
• Predecessor of a node is called its parent
• Nodes with same parent are siblings
• Nodes with no children are called leaves
21
The tree data structure
22
Tree Terminology
• Subtree of a node:
A tree whose root is a child of that node
• Level of a node:
A measure of its distance from the root:
Level of the root = 1
Level of other nodes = 1 + level of parent
Chapter 8: Trees 22
The tree data structure
23
Level 1
(root)
Level 2
Level 2
Level 2
Chapter 8: Trees 23
The tree data structure
24
Terminology
The height of a tree is defined as the
maximum depth of any node within the
tree
The height of a tree with one node is 0
– Just the root node
For convenience, we define the height of
the empty tree to be –1
The tree data structure
25
Terminology
The height of this tree is 17
17
The tree data structure
26
Terminology
If a path exists from node a to node b:
– a is an ancestor of b
– b is a descendent of a
Thus, a node is both an ancestor and a
descendant of itself
– We can add the adjective strict to exclude
equality: a is a strict descendent of b if a is a
descendant of b but a ≠ b
The tree data structure
27
Terminology
The descendants of node B are B, C, D, E, F, and G:
The ancestors of node I are I, H, and A:
The tree data structure
28
Terminology
All descendants (including itself) of the indicated node
The tree data structure
29
Terminology
All ancestors (including itself) of the indicated node
The tree data structure
30
Terminology
Another approach to a tree is to define the tree
recursively:
– A degree-0 node is a tree
– A node with degree n is a tree if it has n children and all
of its children are disjoint trees (i.e., with no intersecting
nodes)
Given any node a within a tree
with root r, the collection of a and
all of its descendants is said to
be a subtree of the tree with
root a
The tree data structure
31
Binary Tree
• A binary tree is a tree with the • Applications:
following properties: • arithmetic expressions
• Each internal node has two children • decision processes
• The children of a node are an ordered • searching
pair
• We call the children of an internal A
node left child and right child
• Alternative recursive definition: a
binary tree is either B C
• a tree consisting of a single node, or
• a tree whose root has an ordered pair
of children, each of which is a disjoint
D E F G
binary tree
H I
31
The tree data structure
32
Arithmetic Expression Tree
• Binary tree associated with an arithmetic expression
• internal nodes: operators
• leaves: operands
• Example: arithmetic expression tree for the expression
(2 * (a - 1) + (3 * b))
* *
2 - 3 b
a 1
32
The tree data structure
33
Fullness and Completeness
• (In computer science) trees grow from the top down
• New values inserted in new leaf nodes
• A binary tree is full if all leaves are at the same level
• In a full tree, every node has 0 or 2 non-null children
Chapter 8: Trees 33
The tree data structure
34
General (Non-Binary) Trees
• Nodes can have any number of children
Chapter 8: Trees 34
The tree data structure
35
Example: XHTML
Consider the following XHTML document
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a <u>Heading</u></h1>
<p>This is a paragraph with some
<u>underlined</u> text.</p>
</body>
</html>
The tree data structure
36
Example: XHTML
The nested tags define a tree rooted at the HTML tag
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a <u>Heading</u></h1>
<p>This is a paragraph with some
<u>underlined</u> text.</p>
</body>
</html>
The tree data structure
37
Tree ADT
• We use positions to abstract • Query methods:
nodes • boolean isInternal(p)
• Generic methods: • boolean isLeaf (p)
• integer size() • boolean isRoot(p)
• boolean isEmpty() • Update methods:
• objectIterator elements() • swapElements(p, q)
• Accessor methods: • object replaceElement(p, o)
• Additional update methods may
• node root()
be defined by data structures
• node parent(p) implementing the Tree ADT
• nodeIterator
37
The tree data structure
38
A Linked Structure for General Trees
• A node is represented by
an object storing
• Element 0
• Parent node
• Sequence of children B
nodes
0 0
A D F
A D F
0 0
C E C E
38
The tree data structure
39
A Linked Structure for General Trees
Class Node {
Object element;
Node parent;
List<Node> Children; // or array of Nodes
}
The tree data structure
40
Tree using Array
• Each node contains a field for data and an array
of pointers to the children for that node
– Missing child will have null pointer
• Tree is represented by pointer to root
• Allows access to ith child in O(1) time
• Very wasteful in space when only few nodes in
tree have many children (most pointers are null)
40
The tree data structure
41
Tree Traversals
• A traversal visits the nodes of a tree in a
systematic manner
• We will see three types of traversals
• Pre-order
• Post-order
•
41
The tree data structure
42
Flavors of (Depth First) Traversal
• In a preorder traversal, a node is visited before
its descendants
• In a postorder traversal, a node is visited after
its descendants
• In an inorder traversal a node is visited after
its left subtree and before its right subtree
42
The tree data structure
Preorder Traversal
43
1 Process the root
2 Process the nodes in the all subtrees in their order
Algorithm preOrder(v)
visit(v)
for each child w of v
preOrder(w)
Lecture 5: Trees
The tree data structure
44
Visualizing Tree Traversals (2)
Preorder:
a, b, d, g, e,
h, c, f, i, j
Chapter 8: Trees 44
The tree data structure
45
Postorder traversal
1. Process the nodes in all subtrees in their order
2. Process the root
Algorithm postOrder(v)
for each child w of v
postOrder(w)
visit(v)
45
The tree data structure
46
Visualizing Tree Traversals (4)
Postorder:
g, d, h, e, b,
i, j, f, c, a
Chapter 8: Trees 46
The tree data structure
47
Inorder traversal
1. Process the nodes in the left subtree
2. Process the root
3. Process the nodes in the right subtree
Algorithm InOrder(v)
InOrder(v->left)
visit(v)
InOrder(v->right)
For simplicity, we consider tree having at most 2
children, though it can be generalized.
47
The tree data structure
48
Visualizing Tree Traversals (3)
Inorder:
d, g, b, h, e,
a, i, f, j, c
Chapter 8: Trees 48
Computing Height of Tree
Can be computed using the following idea:
1. The height of a leaf node is 0
2. The height of a node other than the leaf is the
maximum of the height of the left subtree and the
height of the right subtree plus 1.
Height(v) = max[height(vàleft) + height(vàright)] + 1
Details left as exercise.
49
More examples
Which traversal will use if:
1. Want to evaluate the depth of every node ?
2. Given a tree representing arithmetic expression,
print it in postfix notation ?
3. Given the directory structure of files, figure out the
total memory usage ?
4. Given the directory structure of files, print the
complete file names for each file ?
50
The tree data structure
51
Arithmetic Expression Tree
• Binary tree associated with an arithmetic expression
• internal nodes: operators
• leaves: operands
• Example: arithmetic expression tree for the expression
(2 * (a - 1) + (3 * b))
* *
2 - 3 b
a 1
51
The tree data structure
Complete Binary Tree
52
• A complete binary tree is a binary tree which is completely
filled with the possible exception of the bottom level, which is
filled left to right.
52
How many leaves L does a complete binary tree of
height h have?
The number of leaves at depth d = 2d
If the height of the tree is h it has 2h
leaves.
L = 2h.
53
What is the height h of a complete binary tree with
L leaves?
leaves = 1 height = 0
leaves = 2 height = 1
leaves = 4 height = 2
leaves = L height = Log2L
Since L = 2h
log2L = log22h
h = log2L
Data Structures and Algorithms 54
The number of internal nodes of a complete binary
tree of height h is ?
Internal nodes = 0 height = 0
Internal nodes = 1 height = 1
Internal nodes = 1 + 2 height = 2
Internal nodes = 1 + 2 + 4 height = 3
1 + 2 + 22 + . . . + 2 h-1 = 2h -1 Geometric series
Thus, a complete binary tree of height = h has 2h-1 internal
nodes.
55
The number of nodes n of a complete
binary tree of height h is ?
nodes = 1 height = 0
nodes = 3 height = 1
nodes = 7 height = 2
nodes = 2h+1- 1 height = h
Since L = 2h
and since the number of internal nodes = 2h-1 the
total number of nodes n = 2h+ 2h-1 = 2(2h) – 1 = 2h+1- 1.
56
If the number of nodes is n then what is the
height?
nodes = 1 height = 0
nodes = 3 height = 1
nodes = 7 height = 2
nodes = n height = Log2(n+1) - 1
Since n = 2h+1-1
n + 1 = 2h+1
Log2(n+1) = Log2 2h+1
Log2(n+1) = h+1
h = Log2(n+1) - 1
57
What if the tree is not complete (but proper) ?
Height could lie in the range [log n, n/2]
Number of leaves = Number of internal nodes + 1
58
BinaryTree ADT
• The BinaryTree ADT • Update methods
extends the Tree may be defined by
ADT, i.e., it inherits data structures
all the methods of implementing the
the Tree ADT BinaryTree ADT
• Additional
methods:
• node leftChild(p)
• node rightChild(p)
• node sibling(p) 59