0
votes

How do I get the code to say at Insertion:

  • Root Node is Added (1st insert),
  • Node Added to Left (2nd insert),
  • Node Added to Right (3rd insert),
  • Node Added to Right (4th insert),

right below when each node is added.

There's something wrong with my display. I can't get it to run. Code is as below in the link. I need to display the tree structure from the result of all insertion.

http://goo.gl/NXAwrE

void BinarySearchTree::display(tree_node *ptr, int level)
{
    int i;
    if (ptr != NULL)
    {
        display(ptr->right, level+1);
        cout<<endl;
        if (ptr == root)
            cout<<"Root->:  ";
        else
        {
            for (i = 0;i < level;i++)
                cout<<"       ";
    }
        cout<<ptr->data;
        display(ptr->left, level+1);
    }
}

case 5:  cout<<"Display BST:"<<endl;
         b.display(tmp,1);
         cout<<endl;
         break;
1

1 Answers

0
votes

Make void BinarySearchTree::display(tree_node *ptr, int level) private and create another public function:

void BinarySearchTree::display() {
    display(root, 1);
}

Note: Don't forget to update the class definition.

Then, use b.display(); instead of b.display(tmp,1);