lundi 22 mars 2021

Build a max segment tree and tree is being built but query to give max between range is not working

I have build max segment tree which gives maximum no. between given range. The issue that I am facing is that, queryy function is not working and I am unable to detect where the loop hole lies.

here is code how I have written

    #include<iostream>
using namespace std;

class node {
public: 
    
    int maxval;
    class node *left;
    class node *right;

    int leftrange;
    int rightrange;


};

class node *root=NULL;

class node *createnode()
{
    class node *p;
    //p=(class node*)malloc(sizeof(class node));
    p=new node;

    return p;
}

void buildmaxseg(class node *p,int a[],int l,int r,int c)
{
    class node *temp;
    temp=createnode();
    int val=a[l];
    for(int i=l;i<=r;i++)
    {
        if(a[i]>val)
        {
            val=a[i];
        }
    }

    temp->maxval=val;
    temp->leftrange=l;
    temp->rightrange=r;
    
    if(root==NULL)
    {
        root=temp;
    }

    if(l!=r)
    {
        int q=(l+r)/2;
        buildmaxseg(temp,a,l,q,1);
        buildmaxseg(temp,a,q+1,r,2);


    }
    else{

        temp->left=NULL;
        temp->right=NULL;
    }
    if(c==1)
    {
        p->left=temp;
    }
    else if(c==2)
    {
        p->right=temp;
    }




}

void traverse(class node *p)
{
    cout<<p->maxval<<" ( "<<p->leftrange<<" , "<<p->rightrange<<" )\n";
    if(p->left!=NULL)
    {
        traverse(p->left);
    }
    if(p->right !=NULL)
    {
        traverse(p->right);
    }

}

void update(class node *p,int val,int index)
{

    if(p->leftrange<=index && index<=p->rightrange)
    {
        if(p->maxval<val)
        {
            p->maxval=val;
        }
        if(p->left !=NULL)
        {
            update(p->left,val,index);
        }
        if(p->right !=NULL)
        {
            update(p->right,val,index);
        }
    }

}

int queryy(class node *p,int l,int r)
{

    cout<<"being called\n";
    if(p->leftrange>r || p->rightrange<l)
    {
        cout<<"out of range\n";
        return 0;
    }
    else if(p->leftrange>=l && p->rightrange<=r)
    {
        cout<<" max val\n";
        return p->maxval;
    }
    else {

        int lr=0,rr=0;
        if(p->left!=NULL)
        {
            lr=queryy(p->left,l,r);

        }
        if(p->right!=NULL)
        {
            rr=queryy(p->right,l,r);
        }
        if(lr>=rr)
        {
            return lr;
        }
        else{
            return rr;
        }

    }


}

int main()
{

    int b[10];
    for(int i=0;i<10;i++)
    {
        b[i]=i+1;
    }

    buildmaxseg(root,b,0,9,0);
    traverse(root);

    update(root,7,2);
    traverse(root);
    int z;
    z=queryy(root,2,4);
    cout<<z<<"\n";

    return 0;
}

the output that it gives 10 ( 0 , 9 ) 5 ( 0 , 4 ) 3 ( 0 , 2 ) 2 ( 0 , 1 ) 1 ( 0 , 0 ) 2 ( 1 , 1 ) 3 ( 2 , 2 ) 5 ( 3 , 4 ) 4 ( 3 , 3 ) 5 ( 4 , 4 ) 10 ( 5 , 9 ) 8 ( 5 , 7 ) 7 ( 5 , 6 ) 6 ( 5 , 5 ) 7 ( 6 , 6 ) 8 ( 7 , 7 ) 10 ( 8 , 9 ) 9 ( 8 , 8 ) 10 ( 9 , 9 ) 10 ( 0 , 9 ) 7 ( 0 , 4 ) 7 ( 0 , 2 ) 2 ( 0 , 1 ) 1 ( 0 , 0 ) 2 ( 1 , 1 ) 7 ( 2 , 2 ) 5 ( 3 , 4 ) 4 ( 3 , 3 ) 5 ( 4 , 4 ) 10 ( 5 , 9 ) 8 ( 5 , 7 ) 7 ( 5 , 6 ) 6 ( 5 , 5 ) 7 ( 6 , 6 ) 8 ( 7 , 7 ) 10 ( 8 , 9 ) 9 ( 8 , 8 ) 10 ( 9 , 9 )

and it ends with blank and not even a garbage value if you can help finding loop holes

Aucun commentaire:

Enregistrer un commentaire