lec7_trees (1)
lec7_trees (1)
Trees
COL 106
4
The tree data structure
5
6
The tree data structure
7
7
The tree data structure
8
To organize file-systems
9
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
12
Terminology
Terminology
Phylogenetic trees have nodes with degree 2 or 0:
The tree data structure
14
Terminology
All other nodes are said to be internal nodes, that is, they
are internal to the tree
The tree data structure
15
Terminology
Leaf nodes:
The tree data structure
16
Terminology
Internal nodes:
The tree data structure
17
Terminology
These trees are equal if the order of the children is
ignored (Unordered trees )
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
23
Terminology
Terminology
The height of this tree is 17
17
The tree data structure
25
Terminology
Terminology
Terminology
All descendants (including itself) of the indicated node
The tree data structure
28
Terminology
All ancestors (including itself) of the indicated node
The tree data structure
29
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)
Example: XHTML
Example: XHTML
body of page
<p>This is a paragraph with some
<u>underlined</u> text.</p>
</body>
</html>
paragraph
underlining
The tree data structure
32
Example: XHTML
Example: XHTML
Tree ADT
• Data: Nodes in the tree • Query methods:
• Generic methods: • boolean isInternal(p)
• integer size() • boolean isLeaf (p)
• boolean isEmpty() • boolean isRoot(p)
• Accessor methods: • Update methods:
• node root() update methods may be
defined by data structures
• node parent(p)
implementing the Tree ADT
• list<node> children(p) (focus on access methods
for now)
34
The tree data structure
35
A D F
A D F
0 0
C E C E
35
The tree data structure
36
}
The tree data structure
37
37
The tree data structure
38
38
The tree data structure
39
Tree Traversals
• A traversal visits the nodes of a tree in a
systematic manner
39
The tree data structure
40
40
The tree data structure
Preorder Traversal
41
Algorithm preOrder(v)
visit(v)
for each child w of v
preOrder(w)
Lecture 5: Trees
Preorder Traversal
P
M L
S E
R
A A
T E
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)
43
Postorder Traversal
P
M L
S E
R
A A
T E
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)
M L
S E
R
A A
T E
47
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 at each node?
4. Given the directory structure of files, print the
complete file names for each file ?
48
The tree data structure
49
Binary Trees
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
50
The tree data structure
51
* *
2 - 3 b
a 1
51
How many leaves L does a complete binary tree of
height h have?
L = 2h.
52
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
54
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.
55
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
56
What if the tree is not complete (but proper) ?
57
BinaryTree ADT
58