简体   繁体   中英

Binary Search Tree displaying nodes recursively

I'm getting a problem with my display functions dealing with binary search trees. The main problem I'm having is getting the count variable to increment correctly in the recursive function so that I can number the nodes in ascending order.

Would making count a global variable work?

Here is the code for the function:

(p is pointing to the root of the tree, count is initially set to 1)

void displayAscend(nodePtr p, int count)
   {
      if (p->left != NULL)
         displayAscend(p->left, count);

      cout << count << ". " << p->acctNum << endl;
      count++;

      if (p->right != NULL)
         displayAscend(p->right, count);
   }

通过引用int&传递计数。

void displayAscend(nodePtr p, int& count)

change

displayAscend(p->left, count);

to

displayAscend(p->left, count+1);

and the same with the line containing p->right .

I suspect that you want something like this:

size_t displayAscend(nodePtr p, int count)
{
   size_t additional_count = 0;
   if (p->left != NULL)
      additional_count += displayAscend(p->left, count + additional_count);

   cout << count + additional_count << ". " << p->acctNum << endl;
   additional_count++;

   if (p->right != NULL)
      additional_count += displayAscend(p->right, count + additional_count);

   return additional_count;
}

You can use int in place of size_t if you prefer.

The reason is that each recursive call must return a count to its caller, for otherwise the caller cannot tell how many the recursive call has counted. The outermost caller, of course, can just discard the count if uninterested.

Passing by reference is another way to do it, as another answer observes, though not my preferred way. (I personally prefer to implement that strategy with explicit pointers.)

You ask whether making count a global variable would work. The answer is that, yes, it would work for the restricted purpose of your limited exercise, but it would represent abysmal programming practice. After all, what if you had several trees, each with its own count?

Update: Thanks to @JerryCoffin for pointing out the former error in my code. I have fixed it above. What is more, I have tested it with the following:

#include <vector>
#include <iostream>
using std::cout;
using std::endl;

struct node {
   node *left;
   node *right;
   int acctNum;
};

typedef node *nodePtr;

size_t displayAscend(nodePtr p, int count)
{
   size_t additional_count = 0;
   if (p->left != NULL)
      additional_count += displayAscend(p->left, count + additional_count);

   cout << count + additional_count << ". " << p->acctNum << endl;
   additional_count++;

   if (p->right != NULL)
      additional_count += displayAscend(p->right, count + additional_count);

   return additional_count;
}

int main() {
   node head;
   node n1;
   node n2;
   node n11;
   node n21;
   node n22;
   head.left  = &n1;
   head.right = &n2;
   n1  .left  = &n11;
   n1  .right = 0;
   n2  .left  = &n21;
   n2  .right = &n22;
   n11 .left  = 0;
   n11 .right = 0;
   n21 .left  = 0;
   n21 .right = 0;
   n22 .left  = 0;
   n22 .right = 0;
   n11 .acctNum = 100;
   n1  .acctNum = 202;
   head.acctNum = 300;
   n21 .acctNum = 400;
   n2  .acctNum = 500;
   n22 .acctNum = 600;
   displayAscend(&head, 0);
}

The output is

0. 100
1. 202
2. 300
3. 400
4. 500
5. 600

So, it works.

You'll have to pass the count variable by reference.

void displayAscend(nodePtr p, int & count)
   {
      if (p->left != NULL)
         displayAscend(p->left, count);

      cout << count << ". " << p->acctNum << endl;
      count++;

      if (p->right != NULL)
         displayAscend(p->right, count);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM