Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
924 B
2
Indexable
Never
    BST(vector<int>&v){
        root = build(0 , v.size() - 1 , v);
       // cout << root->data << endl;
    }
   node *build (int l , int r , vector<int>&v){
        if(r >= v.size() || l > r)return NULL;
        int mid = (r + l) / 2 ;
        //assert(mid >= 0 && mid < v.size());
        node *newnode = new node(v[mid]);
        //cout << v[mid] << " " << l <<  " " << mid - 1  << " " << r << " "  << " " << mid << endl;
         newnode -> left = build( l , mid - 1  ,v);
         newnode -> right = build(mid + 1  , r ,v);
        return newnode;
    }
    void insert(int d){
        root = insert(root,  d);
    }
    node *insert(node *temp, int data){
        if (temp == NULL)
            return new node(data);
        if(data > temp->data){
           temp->right = insert(temp->right, data);
        }
        else{
            temp->left = insert(temp->left, data);
        }
    }
Leave a Comment