Tree Traversals: ECE 250 Algorithms and Data Structures
Tree Traversals: ECE 250 Algorithms and Data Structures
Tree traversals
ece.uwaterloo.ca
dwharder@alumni.uwaterloo.ca
Outline
When the root returns a value, that is the height of the tree
Tree traversals
15
4.3.4.2 Printing a Hierarchy
/
usr/
bin/
local/
var/
adm/
cron/
log/
We then do:
1. Before the children are traversed, we must:
a) Indent an appropriate number of tabs, and
b) Print the name of the directory followed by a '/'
2. In recursively traversing the children:
a) A value of one plus the current tab level must be passed to the children, and
b) No information must be passed back
3. Once all children have been traversed, we are finished
Tree traversals
17
4.3.4.2 Printing a Hierarchy
bin/ 12
local/ 15
usr/ 31
adm/ 6
cron/ 5
log/ 9
var/ 23
/ 61
Tree traversals
20
4.3.4.2 Determining Memory Usage
We then do:
1. Before the children are traversed, we must:
a) Initialize the memory usage to that in the current directory.
2. In recursively traversing the children:
a) A value of one plus the current tab level must be passed to the children, and
b) Each child will return the memory used within its directories and this must
be added to the current memory usage.
3. Once all children have been traversed, we must:
a) Print the appropriate number of tabs,
b) Print the name of the directory followed by a "/ ", and
c) Print the memory used by this directory and its descendants
Tree traversals
21
4.3.4.3 Printing a Hierarchy
print_tabs( depth );
std::cout << value()->directory_name() << "/ " << usage << std::endl;
return usage;
}
Tree traversals
22
Summary
References
Tree traversals
24
Usage Notes
• These slides are made publicly available on the web for anyone to
use
• If you choose to use them, or a part thereof, for a course at another
institution, I ask only three things:
– that you inform me that you are using the slides,
– that you acknowledge my work, and
– that you alert me of any mistakes which I made or changes which you
make, and allow me the option of incorporating such changes (with an
acknowledgment) in my set of slides
Sincerely,
Douglas Wilhelm Harder, MMath
dwharder@alumni.uwaterloo.ca