tags:

views:

79

answers:

1

i am trying to implement binary search tree

 #include <iostream>
    using namespace std;
     class BST{
     private :
          int v,*n,vn;
          struct  node{
              int val;
              node *left,*right;
              node(int i){  val=i; left=right=0;}

          };

           node *root;


          node *rinsert( node *p,int t){


              if (p==0) {
                  p=new node(t);
                  n++;
              }
              else  if( t<p->val){
                  p->left=rinsert(p->left,t);
              } else if (t>p->val){
                  p->right=rinsert(p->right,t);
              }
               return p;

          }
          void traverse( node *p){

              if (p==0)
                   return ;
              traverse(p->left);
              v[vn++]=p->val;
              traverse(p->right);
          }





     public :
         BST(int maxlens,int maxval){ root=0; n=0;}
         int size() { return n;}
         void insert(int t){ root=rinsert(root,t);}
         void report(int *x) {v=x; vn=0; traverse(root);}

     };


     int main(){


          return 0;
     }

but this code has a few bugs if anybody run this code will see this bugs please help me what is wrong in this code?

+2  A: 

v[vn++]=p->val;

v is an int(not an array of integers), so v[vn++] is syntactically incorrect.

Perhaps you wanted to print the value in the node during the inorder traversal.

Prasoon Saurav
He's loading them into an array he passes in via `report`. But yes, `int v` needs to be `int *v`.
Rup