Mohamed Atef
Mohamed Atef

Reputation: 167

get succesor Binary Search Tree c++ Data structure

I have binary search tree and i want get successor of it's elements i see that my code is fine but i don't know what's wrong 115 is run time error and 5 is 5 and it should be 6

    #include<bits/stdc++.h>
    using namespace std;

    struct Node
    {
        int data;
        Node *root;
        Node *left;
        Node *right;

    };

    typedef Node *ptr;

     ptr search(ptr tree,int key)
    {
        if(tree==NULL)return tree;
        if(tree->data==key)
            return tree;
        else if(tree->data>key)
            search(tree->right,key);
        else search(tree->left,key);
    }

    ptr most_left(ptr tree)
    {
        ptr result=tree;
        if(tree==NULL)return NULL;
        while(result->left!=NULL)
            result=result->left;
        return result;
    }

    ptr most_right(ptr tree)
    {
        ptr result=tree;
        if(tree==NULL)return NULL;
        while(result->right!=NULL)
            result=result->right;
        return result;
    }

    ptr min_element(ptr tree)
    {
        ptr result=tree;
        if(tree==NULL)return NULL;
        while(result->left!=NULL)
            result=result->left;
        return result;
    }

    ptr max_element(ptr tree)
    {
        ptr result=tree;
        if(tree==NULL)return NULL;
        while(result->right!=NULL)
            result=result->right;
        return result;
    }
    ptr temp=NULL;
    ptr insert_element(ptr &tree,int key)
    {

        if(tree==NULL)
        {
        tree=new Node();
        tree->data=key;
        tree->root=temp;
        return tree;
        }
        else if(tree->data>=key)
        {
        temp=tree;  
        insert_element(tree->left,key);
        }
        else 
        {
            temp=tree;
            insert_element(tree->right,key);
        }

    }

    void in_order(ptr tree)
    {
        if(tree == NULL) return;
        in_order(tree->left);
        cout<<tree->data<<" ";
        in_order(tree->right);
    }

    void pre_order(ptr tree)
    {
        if(tree == NULL) return;

        cout<<tree->data<<" ";
        pre_order(tree->left);
        pre_order(tree->right);
    }

    void post_order(ptr tree)
    {
        if(tree == NULL) return;
        post_order(tree->left);
        post_order(tree->right);
        cout<<tree->data<<" ";
    }

    ptr succesor(ptr node)
    {
        if(node->right != NULL) return min_element(node->right);
         ptr y = node->root;
        while(y != NULL && node == y->right ) {
            node = y;
            y = y->root;

        }
        return y;
    }

    int main() 
    {   
        ptr tree = NULL;
        insert_element(tree,7);
        insert_element(tree,5);
        insert_element(tree,55);
        insert_element(tree,6);
        insert_element(tree,15);
        insert_element(tree,60);
        insert_element(tree,10);
        insert_element(tree,115);
        ptr inserted = insert_element(tree,5);
        insert_element(tree,6);
        insert_element(tree,12);
        cout<<"succesor "<<succesor(inserted)->data;
        cout<<"\nData Inserted \n"<<"In order : ";
        in_order(tree);
        cout<<endl;
        cout<<"Pre order : ";
        pre_order(tree);
        cout<<endl;
        cout<<"Post order : ";
        post_order(tree);
        cout<<endl;
        cout<<"Minimum : "<<min_element(tree)->data<<endl;
        cout<<"Maximum : "<<max_element(tree)->data<<endl;
        return 0;
    }

Upvotes: 0

Views: 121

Answers (1)

wru
wru

Reputation: 36

  1. "5 should be 6"
    No, the program is absolutely right.
    In the insert_element function you check for tree->data >= key (notice the >=), so if there is already an element with the same data as the key to be inserted, the new key will be inserted into the left subtree. This is why the successor of the last inserted 5 ist the previous inserted 5. If you replace >= with > the second 5 will be inserted into the right subtree, so its successor is 6 as expected.

  2. runtime error for successor of 115
    Of course, there is no successor of 115 because this is the greatest value. So the succesor function returns a NULL pointer which, if dereferenced, leads to undefined behavior.

By the way, the program has more undefined behavior. The functions search and insert_element may flow off the end of the functions without returning a value. The standard says:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function. (6.6.3)

In this case you are lucky because in most implementations the location where return values are stored keeps its value from the return statement of the recursive calls, but, of course, this should be fixed.

Upvotes: 2

Related Questions