Trees 2: - PowerPoint PPT Presentation

About This Presentation
Title:

Trees 2:

Description:

Any operation that performs some process on all the nodes in a tree must perform ... it does not work with Turbo C , Borland C version 5.02, or Visual C version ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 22
Provided by: catesh
Learn more at: https://www.kirkwood.edu
Category:
Tags: borland | trees

less

Transcript and Presenter's Notes

Title: Trees 2:


1
Trees 2
  • Traversals and related matters

2
Tree 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

3
Now, 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

4
Example 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

5
Pre-order traversal in action
Original tree
Results
K
K
I
K
I
R
W
R
K
W
O
O
O
O
D
D
6
Example 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

7
In-order traversal in action
Original tree
Results
K
K
I
I
R
W
K
R
K
W
O
O
O
O
D
D
8
Example 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

9
Post-order traversal in action
K
Original tree
Results
W
K
I
I
R
O
D
O
K
W
O
R
O
D
K
10
Backward 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

11
Backward 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

12
BTnode 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)
13
Generalized 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

14
Functions 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

15
Functions 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

16
Examples 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)

17
Template 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

18
Cautionary 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

19
More 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

20
Generalized 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)
21
Trees 2
  • Traversals and related matters
  • - ends -
Write a Comment
User Comments (0)
About PowerShow.com