aya
aya

Reputation: 1

not working function with no errors

I've implemented a function to display an avl tree after inserting nodes into it like this

template<class nodetype>
void AVLtree<nodetype>::display() const
{
 display(Proot);
}

template<class nodetype>
void AVLtree<nodetype>::display(Node<nodetype> * ptr) const
{
 if(ptr==0)
  return ;
 cout<<ptr->value<<" ";
 display(ptr->Pleft);
 display(ptr->Pright);
}

after compiling,there were no errors ,the program worked but nothing were printed on the screen

help me please....!! thanks

Upvotes: 0

Views: 145

Answers (4)

sp3tsnaz
sp3tsnaz

Reputation: 506

first of all you need a conceptual overhaul.. Who told you you can compare ptr with 0 ? Are you trying to suggest that a pointer if nullified is equal to 0 ? Thats totally wrong .. try this :

#include<iostream>
#include<conio.h>

using namespace std;

int main () {

    int *p = NULL ;
    cout<<*p<<endl;

    getch();
    return 0;
}

and there your program crashes ... compare ptr with NULL instead of 0 ..

Upvotes: -2

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

One technique I like to use is to add delimiters around debug strings:

cout << "-->" << ptr->value << "<--" << endl;

That way, it's easy to distinguish empty output from no output.

You may also want to add something that shows when your entire tree is empty:

void AVLtree<nodetype>::display() const
{
  if (Proot)
    display(Proot);
  else
    cout << "empty tree" << endl;
}

Upvotes: 3

AnT stands with Russia
AnT stands with Russia

Reputation: 320381

(Assuming that your tree is built correctly). Screen output is normally line-buffered. You need to output std::endl to std::cout (or at least '\n' character) for something to appear on the screen.

Of course, if your tree is built incorrectly, the root pointer might be null and nothing would be printed for obvious reasons (just a guess).

Finally, even if your tree is built correctly, but all data (ptr->value) is, say, just strings that are empty (for example) or contain only whitespace, then no wonder you can't see anything on the screen.

Upvotes: 4

Thomas Matthews
Thomas Matthews

Reputation: 57678

If you are printing on a console window, this function may be of use:

void Pause(void)
{
    cout << "\nPaused, press ENTER to continue.\n";
    cin.ignore(10000, '\n');
    return;
}

Call this function before any exit methods or return statements in main.

Upvotes: 1

Related Questions