Construct Binary Search Tree from Preorder | C++ Placement Course | Lecture 28.3

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • Complete C++ Placement Course (Data Structures+Algorithm) : • C++ Full Course | C++...
    Telegram: t.me/apnikaksh...
    Instagram: / dhattarwalaman
    Notes of this Lecture:

ความคิดเห็น • 112

  • @kapil4457
    @kapil4457 2 ปีที่แล้ว +4

    A much simpler way to do this is :
    void insert(TreeNode *&root , int val){

    if(root->left==NULL && root->val > val){
    TreeNode *temp = new TreeNode(val);
    root->left = temp;
    return;
    }
    if(root->right==NULL && root->val < val){
    TreeNode *temp = new TreeNode(val);
    root->right = temp;
    return;
    }
    if(root->val > val){
    insert(root->left,val);
    }
    else if(root->val < val){
    insert(root->right ,val);
    }
    }
    //This is the main function
    TreeNode* bstFromPreorder(vector& preorder) {


    TreeNode *root = new TreeNode(preorder[0]);
    for(int i = 1 ; i

  • @apoorvo12
    @apoorvo12 3 ปีที่แล้ว +32

    I have watched only the First 50 lectures of C++ . Now , I am Here just to hit the like and i will be back upto this video after Semester Exams(During Holidays ) . Thank You !

    • @adityabhandari6688
      @adityabhandari6688 3 ปีที่แล้ว +12

      katai bewakoof aadmi ho yr bhai tum

    • @apoorvo12
      @apoorvo12 3 ปีที่แล้ว

      @@adityabhandari6688 Bhai tu Khud ko dekh le pehle phir dusro ko bolna

    • @adityabhandari6688
      @adityabhandari6688 3 ปีที่แล้ว +4

      @@apoorvo12 sorry bhai..lekin bhai kya zarurat hai ese aake like krne ki...jab dekhoga tab hi like kr dena

    • @apoorvo12
      @apoorvo12 3 ปีที่แล้ว

      @@adityabhandari6688 Baad mein ek ke bad ek video continues chalegi sirf samajhne pe dhayan hoga like and comments pe nahi isliye pehle he like kr li Videos

    • @adityabhandari6688
      @adityabhandari6688 3 ปีที่แล้ว +1

      @@apoorvo12 okay

  • @dorababuarig765
    @dorababuarig765 3 ปีที่แล้ว +2

    thankyou.found the video at the right time

  • @hustlewithVaibhav
    @hustlewithVaibhav 3 ปีที่แล้ว +3

    Thank you 🙏🙏

  • @prakanshusahu1455
    @prakanshusahu1455 3 ปีที่แล้ว +9

    May u please dryrun the code with the help of code u wrote,it would be more efficient.

    • @sleepypanda7172
      @sleepypanda7172 3 ปีที่แล้ว +1

      not possible now, all feedbacks are futile becuase they have already shot all the videos.

    • @adityabhandari6688
      @adityabhandari6688 3 ปีที่แล้ว

      @@sleepypanda7172 yes, but it really would have been great if they included dry run in the all the videos

    • @DivPrasad9903
      @DivPrasad9903 2 ปีที่แล้ว

      i also need dry run of this program

  • @rishavpanda949
    @rishavpanda949 3 ปีที่แล้ว +4

    Doubt : Which method has a better time complexity
    1) The method shown here
    2) Creating a BST like a BT which is, from inorder and preorder. Note: If we have preorder of BST, we also have inorder since inorder is sorted preorder.
    Please help out.
    Thanks in advance.

    • @kasyapdharanikota8570
      @kasyapdharanikota8570 3 ปีที่แล้ว +1

      I guess Both of them will have same complexity of O(n^2);

    • @HealedbyNature
      @HealedbyNature 3 ปีที่แล้ว +1

      bst is not totally same as bt...so this is the method

    • @signaturecomics7223
      @signaturecomics7223 2 ปีที่แล้ว

      Nahi bro usme space complexity jyd hogi agr is question ko use method se krege

    • @signaturecomics7223
      @signaturecomics7223 2 ปีที่แล้ว

      Ek extra vector use krna pdega inorder ke liye 🤓

    • @arghyadeepdas3475
      @arghyadeepdas3475 2 ปีที่แล้ว +1

      this method time complexity O(n)
      and 2) has time complexity O(n^2)

  • @rishabs5991
    @rishabs5991 3 ปีที่แล้ว +5

    We can construct the tree in the normal way also, right?

    • @youtuber-sl2xe
      @youtuber-sl2xe 3 ปีที่แล้ว +6

      Yes, but in that case time complexity would be O(n^2). Here it is O(n).
      www.geeksforgeeks.org/construct-bst-from-given-preorder-traversa/

    • @CarlJohnson-cb9xm
      @CarlJohnson-cb9xm 3 ปีที่แล้ว +2

      @@youtuber-sl2xe nlogn not n^2

    • @HealedbyNature
      @HealedbyNature 3 ปีที่แล้ว +2

      bst is known to reduce time complexity...that's why this method

  • @divvisatyasriram2650
    @divvisatyasriram2650 2 ปีที่แล้ว +1

    There is mistake
    root->left=constructBST
    We has to put root->data in place of key

    • @AdityaSingh-fd9cb
      @AdityaSingh-fd9cb 2 ปีที่แล้ว

      no bro.. we're using elements of preorder array as key.

  • @MusicalLifeViB
    @MusicalLifeViB 2 ปีที่แล้ว +1

    Why are we using pointer for preorder index and why not just simple integer variable, when we are incrementing and passing incremented value will only pass then why do we need especially pointer.
    Please help!

  • @avronilbanerjee5302
    @avronilbanerjee5302 2 ปีที่แล้ว

    It looks less complicated using deque
    node* BSTfromPreorder (int min, int max, deque &values)
    {
    if (values.empty())//no values left
    return NULL;
    int key = values.front();
    if (minright=BSTfromPreorder (key,max,values);
    return newnode;
    }
    else
    {
    return NULL;
    }
    }

  • @prashantkv8737
    @prashantkv8737 7 หลายเดือนก่อน

    please put a notes in description

  • @aiknowledge-n2s
    @aiknowledge-n2s ปีที่แล้ว

    Alhamdulillah 108/226. Up up and away.

  • @user-iu7yg7hk9u
    @user-iu7yg7hk9u ปีที่แล้ว

    why we need to do this we can simple use our precious function BuildBST from precious lactures , it can also create same BST

  • @aadeshsharma0001
    @aadeshsharma0001 3 ปีที่แล้ว +4

    what is the complexity of this solution?I think O(N) .

    • @SHASHANKRUSTAGII
      @SHASHANKRUSTAGII 3 ปีที่แล้ว

      O(n) is the optimal one , when you pass the left and right ranges,

  • @CodingMonster23
    @CodingMonster23 6 หลายเดือนก่อน +1

    Notes Please

  • @prempatrale4994
    @prempatrale4994 3 ปีที่แล้ว +1

    Thank you to complete aman bhaiya team 😊😄😊😊😊

  • @jigarjoshi9198
    @jigarjoshi9198 3 ปีที่แล้ว +2

    Perfect ❤️

  • @anushkakumari535
    @anushkakumari535 5 หลายเดือนก่อน

    I think pre-order se complexity kam ho rahi hai kyunki ismein Hume Har ek node pe Jake nhi dekhna hai array mein hi aishe sequence hai Jo hum jis node pe hain usi ke aas pass hoga is it right otherwise husein Har ek array element ke liye Har ek node pe Jake condition check karna pad jaegq

  • @krishnarajput4125
    @krishnarajput4125 2 ปีที่แล้ว +1

    what is the need of index pointer i mean cant we just directly take the value...

  • @pradhanan9413
    @pradhanan9413 2 ปีที่แล้ว

    i approve this video

  • @vishvrana3799
    @vishvrana3799 3 ปีที่แล้ว +1

    Wait...! When we print inorder sequence of BST ..it's alwasy in ascending order ..the. Y
    10 2 1 13 11
    Is printed
    Pla exaplain

  • @adityaagarwal2324
    @adityaagarwal2324 3 ปีที่แล้ว +3

    Didi, why u have written int * preorderidx instead of int preorderidx??

    • @tarunbisht8016
      @tarunbisht8016 3 ปีที่แล้ว +1

      cause we have to record that change in the value of i

    • @adityaagarwal2324
      @adityaagarwal2324 3 ปีที่แล้ว

      @@tarunbisht8016 ok

    • @tarunbisht8016
      @tarunbisht8016 3 ปีที่แล้ว +1

      @@adityaagarwal2324 if u don't wanna use a pointer just declare a static variable to keep track of the index but I will not recommend it

    • @adityaagarwal2324
      @adityaagarwal2324 3 ปีที่แล้ว

      @@tarunbisht8016 hmm...

    • @adityaagarwal2324
      @adityaagarwal2324 3 ปีที่แล้ว

      @@tarunbisht8016 in which year of college u r?

  • @AdityaSingh-fd9cb
    @AdityaSingh-fd9cb 2 ปีที่แล้ว +1

    why we need to pass pointers?? If I remove * and & then it is only printing left tree of BST.. why we need to use pointers.
    Please anybody explain!

  • @harshalgarg1149
    @harshalgarg1149 3 ปีที่แล้ว +3

    C++ Code -
    void construct(TreeNode* &root,vector& v, int &ind, int mini, int maxi)
    {
    if(ind>=v.size())
    return;

    if(v[ind]>mini and v[ind]left,v,ind,mini,v[ind-1]); // range (mini, v[ind-1])
    construct(root->right,v,ind,v[ind-1],maxi); // range (v[ind-1], maxi)
    }

    return;
    }

    TreeNode* bstFromPreorder(vector& v)
    {
    int ind = 0;
    int val = v[0];

    TreeNode* root=NULL;

    construct(root,v,ind,INT_MIN,INT_MAX);

    return root;
    }

  • @cmcatholic1798
    @cmcatholic1798 2 ปีที่แล้ว +1

    Can't we just use the normal building bst with an array method? That too has o(n) complexity

    • @AbhishekHawle-io2mz
      @AbhishekHawle-io2mz ปีที่แล้ว +1

      I tried that, and getting the same solution as shradhha di.. 🙂

  • @vishaltanush1059
    @vishaltanush1059 2 ปีที่แล้ว +2

    *preOrderIdx = *preOrderIdx + 1;
    i get a different ans if i use *preOrderIdx++;
    someone please explain;

    • @aayushpandey1288
      @aayushpandey1288 2 ปีที่แล้ว +2

      do this (*preOrderIdx)++

    • @NormieCyrox
      @NormieCyrox 2 ปีที่แล้ว

      you +1 incase of pointers and ++ in case of varialbles

  • @aaloBoy12477
    @aaloBoy12477 3 ปีที่แล้ว +2

    hello sir 🥰

  • @NaveensinglaYT
    @NaveensinglaYT 2 ปีที่แล้ว +1

    C++ code
    node* build_tree(vector arr, int &i, int bound)
    {
    if(i==arr.size() || arr[i]>bound) return NULL;
    node* root = new node(arr[i++]);
    root->left = build_tree(arr,i,root->val);
    root->right = build_tree(arr,i,bound);
    return root;
    }
    node* build_tree_from_preorder(vector &arr)
    {
    int i = 0;
    return build_tree(arr,i,INT_MAX);
    }

  • @MANPREETSINGH-ft9hd
    @MANPREETSINGH-ft9hd 3 ปีที่แล้ว +2

    can we construct BST from inorder alone? Please explain.

    • @jatinsaini2919
      @jatinsaini2919 3 ปีที่แล้ว

      no bro we can't create a BST from inorder alone because when we have have preorder of a bst then we also have inorder( if we arrange the elements of preorder in increasing order, we get inorder sequence) it means we have both preorder and inorder.

    • @jatinsaini2919
      @jatinsaini2919 3 ปีที่แล้ว

      also we can have multiple bst with same inorder sequene

    • @learningoverflow3138
      @learningoverflow3138 3 ปีที่แล้ว

      No there will be multiple BST possible not a particular one

    • @dorababuarig765
      @dorababuarig765 3 ปีที่แล้ว

      please answer my question bro

  • @3kdancevideosdancevideos146
    @3kdancevideosdancevideos146 3 ปีที่แล้ว

    Waiting

  • @shauryatuli2241
    @shauryatuli2241 3 ปีที่แล้ว +3

    Can someone please explain why can’t we just loop through the array and simply insert The smaller elements on left and larger elements on the right

    • @jatinsaini2919
      @jatinsaini2919 3 ปีที่แล้ว

      i think the time complexity will be high if we use your approach

    • @learningoverflow3138
      @learningoverflow3138 3 ปีที่แล้ว

      thats cause it wont create the tree related to the preorder

    • @deepanshumahto8086
      @deepanshumahto8086 3 ปีที่แล้ว +1

      i was thinking the same we could've used the insert function that was created in video 108 of the playlist

    • @youtuber-sl2xe
      @youtuber-sl2xe 3 ปีที่แล้ว

      In that case time complexity would be O(n^2). Here it is O(n).
      www.geeksforgeeks.org/construct-bst-from-given-preorder-traversa/

    • @piyushlohiya1977
      @piyushlohiya1977 3 ปีที่แล้ว +1

      @@youtuber-sl2xe i don't understand why its o(n^2) ... it should be o(nlogn) because searching its correct position will take logn time and iteration will take n time

  • @dorababuarig765
    @dorababuarig765 3 ปีที่แล้ว +1

    @doubt
    Can't we generate a bst like above with the post order traversal alone??
    step 1:starting from index=size-1 till index>=0;
    step 2:root->right;
    step 3:root->left;
    someone please answer??

  • @AdityaGupta-kv5ip
    @AdityaGupta-kv5ip ปีที่แล้ว

    iss mein preorderIdx ko pass by pointer ki jagah pass by reference nhi kar sakte kya??...pass by pointer mein new memory bhi create hogi and changes reflect ho jayenge but pass by reference mein new memory nhi create hogi and changes bhi reflect ho jayenge

  • @GM-pk4li
    @GM-pk4li 3 ปีที่แล้ว

    bhiya jaldi say upload karo naa sarray video love you bhiya i will pay you money when earn

  • @signaturecomics7223
    @signaturecomics7223 2 ปีที่แล้ว +1

    Ye code nhi chl rha😰😰
    Is input ke liye
    1345,3454,1,4,2,9
    ,if I am wrong someone please help me

  • @adityaagarwal2324
    @adityaagarwal2324 3 ปีที่แล้ว

    Notes upload kardo bhaiya plz...

  • @ujjwalverma2870
    @ujjwalverma2870 2 ปีที่แล้ว

    Bhiaya ab intna aage aa gaya hu course me toh aisa lag raha he piche ka bhul raha hu thoda
    Aise me kya karu isparr video bano na plss

  • @pranayagrawal5438
    @pranayagrawal5438 3 ปีที่แล้ว

    Java ka course kab aayega

  • @adityabhandari6688
    @adityabhandari6688 3 ปีที่แล้ว

    why are we making bst from a bst? Can someone please explain

    • @ankitbhardwaj7363
      @ankitbhardwaj7363 3 ปีที่แล้ว +1

      We are given the preorder traveral of a BST in the form of an array, We are creating a real BST with the help of that array .

    • @adityabhandari6688
      @adityabhandari6688 3 ปีที่แล้ว +1

      @@ankitbhardwaj7363 okay, so we are making bst from an array..okay thanx

  • @ManishMeena-ph2vo
    @ManishMeena-ph2vo 2 ปีที่แล้ว

    why are we using pointer here ? ,

  • @hopesalive2145
    @hopesalive2145 2 ปีที่แล้ว

    999k 😊😊😊😊

  • @JD-om6zk
    @JD-om6zk 2 ปีที่แล้ว

    comment number 100 😕

  • @instagramearner1458
    @instagramearner1458 2 ปีที่แล้ว +3

    ghanta malum nhi pad rha kya krr rahi hai ye

  • @StoryGicRohit
    @StoryGicRohit 3 ปีที่แล้ว

    GFG :)

    • @StoryGicRohit
      @StoryGicRohit 3 ปีที่แล้ว

      @@naveenshah8032 Any other resources bro ? if you found any ? Please share

  • @arijitbhakta9705
    @arijitbhakta9705 2 ปีที่แล้ว

    Didi thora easily samjhaiye na

  • @divyanshuyadav5524
    @divyanshuyadav5524 3 ปีที่แล้ว

    ❤️

  • @harpic949
    @harpic949 3 ปีที่แล้ว +8

    Mann i seriously feel that listening to a woman voice for straight 15 min when se speaking continuously and with a high pitch voice is sometimes irritating and difficult..... just my thoughts 😅

  • @gauravahuja4612
    @gauravahuja4612 2 ปีที่แล้ว +3

    Yaar itna ganda koi kaise padha sakta h

  • @amangoswami45
    @amangoswami45 ปีที่แล้ว

    Use stack for very small answer. Itna complicated kyu kiye ho