title: Iterator Method for Binary Tree in C
body: <p>I have been given the following code to make an iterator method to use on a binary tree in C. That is to apply a function to every value in the binary tree.</p>
  
  <p><code>int bst_char_iterate(bst_char *t, char (*fun)(char item))</code></p>
  
  <p>"Bst_char *t" equating to a pointer to the tree, "*fun" equating to a pointer to a function call and "item" being the value of which the function is called upon.</p>
  
  <p>I thought iterators should really be done as void functions, so I am lost as to how to approach this.</p>
  
  <p>Please help!</p>
  
  <p>Edit: The instruction I got was the following:</p>
  
  <blockquote>
    <p>Add a new method to the library that applies a function to every value
    in the tree. (This is an iterator method for trees.)</p>
    
    <p>int bst_char_iterate(bst_char *t, char (*fun)(char item))</p>
    
    <p>NB: The BST property will only be preserved by this method if the
    function passed to it is monotonic. A function f is monotonic whenever</p>
    
    <p>x &lt;= y (arrow) f(x) &lt;= f(y).</p>
  </blockquote>
  
  <p>Edit 2: The following has not worked for me:</p>
  
  <pre><code>int bst_char_iterate(bst_char *t, char (*fun)(char item)) {
      assert(t!=NULL);
      struct node * p = t-&gt;root;
      if (p!=NULL) {
         p-&gt;item = fun(p-&gt;item);
         p-&gt;left-&gt;item = bst_char_iterate(t,fun(p-&gt;left));
         p-&gt;right-&gt;item = bst_char_iterate(t,fun(p-&gt;right));
      } else {
         return 0;
      }
  }
  </code></pre>
  
  <p>With quite a few errors. Can anyone help?</p>