NatureDevil
NatureDevil

Reputation: 483

Printing a binary tree with specified indent level in C++

I'm having trouble printing a binary tree. Basically each node contains two values - key and data. The problem is for this assignment,

I am expected to use double-space instead of \t. Basically it means, \t = 2 spaces, \t\t = 4 spaces, \t\t\t = 6 spaces

My problem is, I have implemented the printIndented method in the following way -

void 
TreeDictionary::printIndented(TreeNode * node, int level) {
        if (node == NULL) 
               return; 
        if (level == 0) 
        { 
            std::cout << node->_key << ':' << node->_data << "\n"; 
            level++; 
            printIndented(node->_left, level); 
            printIndented(node->_right, level); 
        } 
        else 
        { 
                if ((node->_left == NULL) && (node->_right == NULL)) 
                        cout << '\t'; 
                cout << "\t" << node->_key << ':' << node->_data << endl; 
                if ((node->_left != NULL) && (node->_right != NULL)) 
                        cout << '\t'; 
                printIndented(node->_left, level); 
                if ((node->_left != NULL) && (node->_right != NULL)) 
                        cout << '\t'; 
                printIndented(node->_right, level);     
        } 
}

which is giving me an output as follows -

---=WHAT I AM GETTING=---
pineapple:0
        kiwi:1
                grapes:3
                apple:5
                orange:6
        lime:8
        olives:9
                mango:10
        strawberry:4
                watermelon:7

---=EXPECTED OUTPUT=--- (as you see it prints 2 spaces instead of each \t)
pineapple:0
  kiwi:1
    grapes:3
      apple:5
        NULL
        NULL
      NULL
    orange:6
      lime:8
        NULL
        olives:9
          mango:10
            NULL
            NULL
          NULL
      NULL
  strawberry:4
    NULL
    watermelon:7
      NULL
      NULL

It seems like I am unable to account for the NULL values and print NULL whenever a NULL entry is found. All help is appreciated!

NOTE : The expected output is for default indent-level that is 0 for this program.

Upvotes: 0

Views: 1933

Answers (2)

Achint
Achint

Reputation: 817

You're not really using the information that you're getting from knowing the level that you're at. Think of it as indenting from the left most edge of the screen. Per level you want to indent it by 2*level spaces. From the output, it looks like a NULL node prints out NULL. You could try something along the lines of:

Indent(Treenode *node, int level){
  std::cout << std::string(2*level, ' ');
  if(node == NULL){
     std::cout << "NULL" << std::endl;
  }else{
    std::cout << node->_key << ":" << node->_data << std::endl;
    level += 1;
    Indent(node->_right, level);
    Indent(node->left, level);
  }
}

Upvotes: 2

Simon
Simon

Reputation: 2469

First of all, you should also check if the first node, at level 0, has childs. Else you could run into an error if there is only one node in your tree.

Second, I encourage you to use curling braces after the if statements, this is confusing and not very readable:

if((node->_left != NULL) && (node->_right != NULL)) { 
    cout << "\t"; //or in your case maybe: cout << " "; //see point 3
}

Third point, when you want to print a space instead of a '\t' you should do ' ' instead of '\t'. You also use "\t", which is the syntax for printing a string instead of a character, this is not wrong but I think you need to understand the principle. '\t' = const char, "\t" = const string

4th, I don't really understand what criterium you maintain for printing with different kind of indentation, so if you can explain what you are trying to accomplish with the if statements that would be great, then I can explain it to you as well.

If you have any questions left or you find this not helpfull just tell me :) Good luck!

Upvotes: 0

Related Questions