0

I am running into what I believe is a recursive issue which I do not know how to resolve. I am designing a hashed array tree structure and I want to print out the "lowest level" in the hashed array tree. I have an array which keeps track of how many nodes are on each level, and I can calculate the lowest level based on this. Here is the code:

template <typename T>
ostream &operator << ( ostream &output, const HashNode<T> &node ){
if(node.stemNode == 0){
  //leafnode                                                                                                                                                                   

  for(int i=0; i < node.numElements; i++){

    output << "\n-------------------------------------------" << endl;
    output << "This Node's Level: " << node.nodeLevel << endl;
    output << "\nKey: " << node.keyArray[i] << endl;
    output << "\nData: " << *(node.dataArray[i])  << endl;
  }

}else if (node.stemNode == 1) {
  //stem node                                                                                                                                                                   

  for(int j=0; j<5; j++){
    if(node.childArray[j] != NULL){
      output << *(node.childArray[j]);
    }
  }

}
int ctr = 0;
for(int i=8; i>=0; i--){
  if(levels[i] == 0){
    ctr++;
  }
}
int lowest = 9-(ctr+1);
//output << "\nLowest Number of Levels: " << 9-(ctr+1) << endl;                                                                                                                 

return output << "\n\nLowest Level: " << lowest << " " << ctr2 << endl;
}

What happens is that when I print out output, the phrase "Lowest Level" will print out multiple times. I believe this occurs because I traverse my tree recursively. I tried to overcome this by just saving the "lowest level" value to a variable as shown above, but this did not solve the problem. The line continues to print multiple times. Is there a way to overcome this?

Jeremy Fisher
  • 2,510
  • 7
  • 30
  • 59
  • Can you describe what is supposed to happen when you have multiple leaf nodes? Is is supposed to not print out "Lowest Level" for the stem nodes and then print "Lowest Level" for every leaf node or for every leaf node in a stem node or should it print out all the stem nodes and then "Lowest Level" and then all the leaf nodes? – nwp Apr 28 '14 at 05:03
  • So when I have multiple leaf nodes, the keys in each of the leaf nodes print out. What I want to happen is that after the keys are printed out for each leaf node, I want to print out the "lowest level" attribute only once. What's actually happening is that the keys will print out, but the "lowest level" attribute prints out multiple times, approximately 8 or 9 times I believe. – Jeremy Fisher Apr 28 '14 at 05:08
  • "I want to print out the "lowest level" attribute only once" - once per stem node or once total? – nwp Apr 28 '14 at 08:46
  • I would only like to print it once total – Jeremy Fisher Apr 28 '14 at 14:17

2 Answers2

0

You always send the "Lowest level" string to output, regardless of whether it's a stemnode or not. Move the "Lowest level" line to inside if(stemnode == 0) and just return output by default.

A rough rule of recursion; if you only have one return statement you probably have issues with your end conditions.

dutt
  • 7,909
  • 11
  • 52
  • 85
0

From the comments I know that the goal is to have the "Lowest Level" part printed once. This can be done with small modifications of your original code. Change your original printing function to this function head:

template <typename T>
ostream &printHashNode(ostream &output, const HashNode<T> &node){
    //your old code leaving out the "Lowest Level" part

and remove the "Lowest Level" part. Then make a new function for printing:

template <typename T>
ostream &operator << (ostream &output, const HashNode<T> &node){
    return output << printHashNode(output, node) << "\n\nLowest Level: " << lowest << " " << ctr2 << endl;
}

If you require the value ctr2 calculated by printHashNode you can also pass that back from it.

nwp
  • 9,623
  • 5
  • 38
  • 68
  • As a side note: [avoid using endl](http://stackoverflow.com/questions/8311058/n-or-n-or-stdendl-to-stdcout) to avoid buffering. – nwp Apr 28 '14 at 14:36