L11 Trees and Traversals
L11 Trees and Traversals
Applications:
● Organization charts
● File systems
● Programming environments
What is a Tree
A tree can also be looked at as
having vertices and edges without
having any cycles.
b c d b c d b c d
f g f f g f f g f
e e e
b c d b c d b c d
f g f f g f f g f
e e e
b c d b c d b c d
f g f f g f f g f
e e e
b c d b c d b c d
f g f f g f f g f
e e e
○ Stores a value
Interface*
};
Height
if (p.isRoot()) {
return 0; // root has depth 0
} else {
return 1 + depth(T, p.parent()); // 1 + (depth of parent)
}
Trees }
Depth and
for (Iterator q = nodes.begin(); q != nodes.end(); ++q) {
if (q->isExternal()) {
h = max(h, depth(T, *q)); // get max depth among leaves
Height
}
}
return h;
}
int height2(const Tree& T, const Position& p) {
Trees if (p.isExternal()) return 0; //leaf has height 0
int h=0;
PositionList ch = p.children(); //list of children
The runtime of algorithm height1 is 𝑂(𝑛 + for (Iterator q = ch.begin(); q != ch.end(); ++q) {
σ𝑝(1 + 𝑑𝑝 )) h = max(h, height2(T, *q)
The runtime of algorithm height2 is return 1+h; // 1+max height of children
𝑂( σ𝑝(1 + 𝑐𝑝 )), where 𝑐𝑝 is the number of
children of a node p
Preorder
A traversal visits the
nodes of a tree in a
systematic manner
In a preorder traversal,
a node is visited before
its descendants
1. Make Money Fast! 6. 2.1 Stock Fraud
2. 1. Motivations 7. 2.2 Ponzi Scheme
Application: print a 3. 1.1 Greed 8. 2.3 Bank Robbery
4. 1.2 Avidity 9. References
structured document 5. 2. Methods
Preorder
A traversal visits the
nodes of a tree in a
systematic manner
In a preorder traversal,
a node is visited before
its descendants 1. Paper 8. §2.1
2. Title 9. §2.2
3. Abstract 10. §2.3
Application: print a 4. §1 11. §3
5. §1.1 12. §3.1
structured document 6. §1.2 13. §3.2
7. §2 14. References
void preorderPrint(const Tree& T, const Position& p) {
Preorder Print
cout << *p; // print element
PositionList ch = p.children(); // list of children
for (Iterator q = ch.begin(); q != ch.end(); ++q) {
cout << " ";
preorderPrint(T, *q);
Trees }
}
Postorder
In a postorder
traversal, a node is
visited after its
descendants
Application: compute
space used by files in a
1. h1c.doc 5. Stocks.cpp
directory and its 2. h1nc.doc 6. Robot.cpp
3. homeworks/ 7. programs/
subdirectories 4. DDR.cpp 8. todo.txt
9. cs16/
Postorder
In a postorder
traversal, a node is
visited after its
descendants
Application: compute
space used by files in a 1. Title 8. §2.3
2. Abstract 9. §2
directory and its 3. §1.1 10. §3.1
4. §1.2 11. §3.2
subdirectories 5. §1 12. §3
6. §2.1 13. References
7. §2.2 14. Paper
void postorderPrint(const Tree& T, const Position& p) {
Postorder Print
PositionList ch = p.children(); // list of children
for (Iterator q = ch.begin(); q != ch.end(); ++q) {
postorderPrint(T, *q);
cout << " ";
}
Trees cout << *p; // print element
}
Preorder and Postorder
Algorithm preOrder(v) { Algorithm postOrder(v) {
visit(v) for each child w of v {
for each child w of v { postOrder (w)
preOrder(w) }
} visit(v)
} }
Project 2
● The three notations are:
○ Postfix (Reverse Polish) Notation:
○ Infix Notation:
■ The standard notation we use where the operator is between the operands.
25
Question:
Let T be a tree with void parenPrint(const Tree& T, const Position& p) {
cout << *p; // print node's element
function? }
parenPrint(T, *q); // visit the next child
parenPrint(T, }
}
T.root())
Question:
Which of these are valid trees? If not,
why?
A B C D E F
Question:
There are three boxes:
a box containing two gold coins,
a box containing two silver coins,
a box containing one gold coin and a silver coin.
After choosing a box at random and pulling one coin, you pull a gold
coin. What is the probability that the box you chose has the two gold
coins.