0

I am having trouble finding the real trouble in my program to insert into a Binary Search tree.

//BST
#include<iostream>
using namespace std;

class bst
{
      public:
              int data;
      bst *lnext,*rnext,*root,*prev;
      
      public:
             bst()
             {
                  lnext=rnext=root=NULL;
             }
             
             bst* addBST(bst *p)
             {
                  if(root==NULL)//if tree is empty
                  {
                   root=p;
                   cout<<"\nRoot Inserted!";
                   return root;
                   }
                   
                   else
                   {
                  p= insert(root,p);
                  return p;
                  }
                   }
             bst* insert(bst *xroot, bst *newnode)
             {
                  if(xroot==NULL)
                   {
                     xroot = newnode;
            
                     
                                        cout<<"\nnode inserted!";
                     return newnode;
                    
                     }
                     
                     if(newnode->data < xroot->data)
                     {
                         cout<<xroot->data;
                          cout<<"\nLEss!";           
                     return  insert(xroot->lnext,newnode);
                      
                      }
                      else
                      {
                      cout<<"\nMore!";
                      return insert(xroot->rnext,newnode);
                      
                      }
                  }
                  
                  bst* minnode(bst *wroot)
                  {
                      if(wroot->lnext==NULL)
                        return wroot;
                        else
                         return minnode(wroot->lnext);
                         }
                  
                  
                  };
                  
                  main()
                  {
                        bst obj,*p,*p2,*p3,*temp;
                        p= new bst();
                        p2= new bst();
                        p3 = new bst();
                        int data;
                     
                        p->data=3;
                     
                      p= obj.addBST(p);
                        
                        p2->data=1;
                      p2=  obj.addBST(p2);
                        p3->data=5;
                       p3=  obj.addBST(p3);
                      
                        
                        temp=obj.minnode(obj.root);
                      
                        cout<<"Minimum element is ::"<<temp->data;
                        
                        system("pause");
                        return 0;
                        }

The problem is with lnext and rnext values. Please help me rectify the problem

Edited by Nick Evan: Added [CODE] tags

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Taywin
0

Is that a requirement to implement the insert() function as recursive??? Why not implement it in iterative format which is much more robust?

Anyway, you do not have a structure of a tree Node, but you have variables of pointers... They are not a Node if they are separated variables in the BST class. In other words, they are just plain pointer variables. You need to create a Node structure/class first. Then you can use it in the call (arrow directive).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.