Trees Col 106: Acknowledgement:Many Slides Are Courtesy Douglas Harder, Uwaterloo
Trees Col 106: Acknowledgement:Many Slides Are Courtesy Douglas Harder, Uwaterloo
Trees
COL 106
3
The tree data structure
4
4
The tree data structure
5
The tree data structure
6
6
The tree data structure
7
7
The tree data structure
Terminology
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
Terminology
Phylogenetic trees have nodes with degree 2 or 0:
The tree data structure
12
Terminology
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
Terminology
Terminology
Terminology
Paths of length 10 (11 nodes) and 4 (5 nodes)
Terminology
For each node in a tree, there exists a unique path from
the root node to that node
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
Terminology
The height of this tree is 17
17
The tree data structure
26
Terminology
Terminology
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
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
* *
2 - 3 b
a 1
32
The tree data structure
33
Chapter 8: Trees 33
The tree data structure
34
Chapter 8: Trees 34
The tree data structure
35
Example: XHTML
Example: XHTML
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 D F
A D F
0 0
C E C E
38
The tree data structure
39
}
The tree data structure
40
40
The tree data structure
41
Tree Traversals
• A traversal visits the nodes of a tree in a
systematic manner
41
The tree data structure
42
42
The tree data structure
Preorder Traversal
43
Algorithm preOrder(v)
visit(v)
for each child w of v
preOrder(w)
Lecture 5: Trees
The tree data structure
44
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
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)
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
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
* *
2 - 3 b
a 1
51
The tree data structure
52
How many leaves L does a complete binary tree of
height h have?
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
Since L = 2h
log2L = log22h
h = log2L
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
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
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) ?
58
BinaryTree ADT