Git Product home page Git Product logo

Comments (8)

kvedala avatar kvedala commented on May 23, 2024

There is an active pull-request in C++ for this file TheAlgorithms/C-Plus-Plus#891
It would be very helpful if you can weigh in on both of them.
Thank you for pointing out the possible error.

from c.

kvedala avatar kvedala commented on May 23, 2024

@liushubin-gitHub it would be great if you can take a look here as well. Thank you.
(PS: The static code checks are not so stringent here as C is a more primitive language than C++ 😄 )

from c.

liushubin-gitHub avatar liushubin-gitHub commented on May 23, 2024

@kvedala @bayegy LGTM

from c.

bayegy avatar bayegy commented on May 23, 2024

@kvedala
Hi, I am not sure if this will answer your question.
But I think the implementation of function findMaxInLeftST() in binary_search_tree.cpp is correct. it works fine.

I did not read the "active pull-request" thoroughly, but I have tested the function Remove() in binary_search_tree.cpp. It did not work in some cases. For example, If I want to delete 50 from the tree mentioned above, it throws segmentation fault error. I think the possible
correct implementation of Remove() could be:

void Remove(node **p, int x) {
    if ((*p) == NULL) {
        return;
    }
    if ((*p)->val == x) {
        if ((*p)->right == NULL && (*p)->left == NULL) {
            (*p) = NULL;
        } else if ((*p)->right == NULL) {
            (*p) = (*p)->left;
        } else if ((*p)->left == NULL) {
            (*p) = (*p)->right;
        } else {
            int y = findMaxInLeftST((*p)->left);
            (*p)->val = y;
            Remove(&((*p)->left), y);
        }
    } else if (x < (*p)->val) {
        Remove(&((*p)->left), x);
    } else {
        Remove(&((*p)->right), x);
    }
}

If you want to delete a node from a tree, just use:

Remove(&root, x);

This implementation have been tested worked in various situation. Also, correct me if I was wrong, please.

from c.

liushubin-gitHub avatar liushubin-gitHub commented on May 23, 2024

LGTM.+1
i have fixed it.segment fault is because null pointer check missing

from c.

kvedala avatar kvedala commented on May 23, 2024

Thank you, @liushubin-gitHub
@bayegy please feel free to create a pull-request with your suggested fixes. Along with the fixes, please update the code with structure and formatting as the other files like this in the repo. Thank you 😄

from c.

kvedala avatar kvedala commented on May 23, 2024

@bayegy can you review the PR #579 Thanks.

from c.

bayegy avatar bayegy commented on May 23, 2024

@kvedala yes, I have reviewed the PR. looks good to me

from c.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.