Title: Trees 2:
1Trees 2
- Traversals and related matters
2Tree traversal
- Any operation that performs some process on all
the nodes in a tree must perform a tree traversal - Traversal refers to visiting each node in turn
- Traversal is a recursive process we visit each
node, and we visit each node in the subtree of
which the node is the root
3Now, more ways to climb!
- There are three basic tree traversal patterns,
referring to the order in which nodes are visited
and processed - Pre-order visit root, then left subtree, then
right - In-order visit left subtree, then root, then
right - Post-order visit left subtree, then right, then
root
4Example printing all nodes
- // pre-order traversal
- template ltclass Itemgt
- void print (BTtnode ltItemgt root)
-
- if (root ! NULL)
-
- cout ltlt root-gtdata ltlt endl // 1
- print (root-gtleft) // 2
- print (root-gtright) // 3
-
5Pre-order traversal in action
Original tree
Results
K
K
I
K
I
R
W
R
K
W
O
O
O
O
D
D
6Example printing all nodes
- // in-order traversal
- template ltclass Itemgt
- void print (BTtnode ltItemgt root)
-
- if (root ! NULL)
-
- print (root-gtleft) // 2
- cout ltlt root-gtdata ltlt endl // 1
- print (root-gtright) // 3
-
7In-order traversal in action
Original tree
Results
K
K
I
I
R
W
K
R
K
W
O
O
O
O
D
D
8Example printing all nodes
- // post-order traversal
- template ltclass Itemgt
- void print (BTtnode ltItemgt root)
-
- if (root ! NULL)
-
- print (root-gtleft) // 2
- print (root-gtright) // 3
- cout ltlt root-gtdata ltlt endl // 1
-
9Post-order traversal in action
K
Original tree
Results
W
K
I
I
R
O
D
O
K
W
O
R
O
D
K
10Backward in-order traversal
- As the previous examples have shown, none of the
traversal methods print the nodes in the order in
which theyre arranged - One way to depict the nodes in a tree is by
printing it sideways, with the leftmost node
being the root, roots children above and below
root on the right, and so on - This can be accomplished using backward in-order
traversal
11Backward in-order traversal
- The steps are
- process right subtree (recursive call)
- process root
- process left subtree (recursive call)
- Combine this with the idea of printing spaces for
indentation at each new level of the tree, and
you get the function on the next slide
12BTnode print() function
template ltclass Itemgt void print(const
BTnodeltItemgt ptr, int depth) if (ptr !
NULL) print(ptr-gtright( ), depth1) cout
ltlt setw(4depth) ltlt ptr-gtdata ltlt
endl print(ptr-gtleft( ), depth1)
13Generalized traversals
- Each of the examples we have seen focused on
printing data values in the binary tree nodes - A more useful traversal function would have the
ability to perform any operation on the nodes in
a binary tree - to do so, however, requires the
introduction of a new concept
14Functions as parameters
- In C, we can pass functions (not just function
calls) as parameters, provided we set things up
correctly - For example, consider the prototype below
- void apply(void f(int), int data, int n)
- The first argument to function apply is any
function f, which must be a void function with an
int reference parameter
15Functions as parameters
- Implementation of apply function
- void apply(void f(int), int data, int n)
-
- for (int x 0 x lt n x)
- f(datax)
-
- Apply performs function f on every element of
array data
16Examples of calls to apply
- Suppose we have the following functions
- void square(int x) x x
- void assignRand(int x) x rand() x
- Then we can write calls to apply using the
example functions as parameters (assuming array
and nums are int arrays) - apply (square, array, 200)
- apply (assignRand, nums, 50)
17Template version of apply()
- We can generalize the function further by making
it a template function - template ltclass Item, class numTypegt
- void apply(void f(Item), Item data, numType n)
-
- for (int x0 x lt n x)
- f(datax)
-
- Function f can now be any void function with a
single reference parameter of any type
18Cautionary note on template version of apply()
- This version does not work with all C
compilers it does not work with Turbo C,
Borland C version 5.02, or Visual C version 6
but does work with Dev-C - Textbook suggests taking generalization a step
further, as shown below - templateltclass Process, class Item, class
NumTypegt - void apply(Process f, Item data, NumType n)
- but I couldnt find a compiler this works with
19More generalized traversals
- We can apply the lessons learned in creating the
apply() function to the task of generalizing the
traversal functions - For example, the preorder() function could be
rewritten as shown on the next slide - Note that this is analogous to the second version
of apply(), because this actually works with an
available compiler
20Generalized preorder function
template ltclass Item, class nodegt void preorder
(void f(Item), node ptr) if (ptr !
NULL) f(ptr-gtdata()) preorder(f,
ptr-gtleft) preorder(f, ptr-gtright)
21Trees 2
- Traversals and related matters
- - ends -