Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Trees
COL 106
Amit Kumar
Shweta Agrawal
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
10
To store phylogenetic data
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
boolean hasNext();
E next();
}
Getting an Iterator
You get an iterator from an ADT by calling
the method iterator();
while(iter.hasNext()) {
process iter.next()
}
Adding Iterators to
SimpleArrayList is easy
First, we add the iterator() method to
SimpleArrayList:
public Iterator<E> iterator(){
return new
ArrayListIterator<E>(this);
}
Then we implement the iterator class for Lists:
import java.util.*;
public class ArrayListIterator<E>
implements Iterator<E> {
// *** fields ***
private SimpleArrayList<E> list;
private int curPos;
public ArrayListIterator(
SimpleArrayList<E> list) {
this.list = list;
curPos = 0;
}
public boolean hasNext() {
return curPos < list.size();
}
public E next() {
if (!hasNext()) throw
new NoSuchElementException();
E result = list.get(curPos);
curPos++;
return result;
}
}
The tree data structure
Position ADT 43
Delhi
43
The tree data structure
44
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)
• positionIterator positions() • object replaceElement(p, o)
• Accessor methods: • Additional update methods may
be defined by data structures
• position root()
implementing the Tree ADT
• position parent(p)
• positionIterator children(p)
44
The tree data structure
45
A D F
A D F
0 0
C E C E
45
The tree data structure
46
46
The tree data structure
47
Tree Traversals
• A traversal visits the nodes of a tree in a
systematic manner
47
The tree data structure
48
48
The tree data structure
49
Preorder Traversal
Process the root
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
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)
51
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
55
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(vleft) + height(vright)] + 1
58
The tree data structure
59
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
60
The tree data structure
61
* *
2 - 3 b
a 1
61
How many leaves L does a proper binary tree of height
h have?
L = 2h.
64
What is the height h of a proper binary tree with L
leaves?
leaves = 1 height = 0
leaves = 2 height = 1
leaves = 4 height = 2
Since L = 2h
log2L = log22h
h = log2L
66
The number of nodes n of a proper 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.
67
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
68
BinaryTree ADT
69