Comments (8)
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.
@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.
from c.
@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.
LGTM.+1
i have fixed it.segment fault is because null pointer check missing
from c.
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.
@bayegy can you review the PR #579 Thanks.
from c.
@kvedala yes, I have reviewed the PR. looks good to me
from c.
Related Issues (20)
- [OTHER] Shouldn't we update the copyright notice's time to 2023? HOT 5
- [FEATURE] Add the C linter to a separate Python script HOT 2
- [BUG] Segmentation fault in `queue.c` HOT 2
- [FEATURE] Update CMake to a newer version HOT 2
- [FEATURE] Add Windows builds back HOT 5
- [OTHER] Inconsistent coding style with `.clang-format` HOT 10
- [FEATURE] improve the naming of variables HOT 7
- [FEATURE] Can we have one more folder related to design pattern. HOT 1
- Fet: add Postfix To Infix HOT 10
- Fet: add "Prefix Evaluation" algo HOT 6
- [FEATURE] [NEW ALGO] Infix to Prefix HOT 6
- [FEATURE] RADIX SORT HOT 3
- [FEATURE][NEW ALGO] factorial recursion HOT 4
- [BUG] Broken/Wrong code in C/conversions /hexadecimal_to_octal.c HOT 3
- [FEATURE] Add Preemptive Priority Scheduling Algorithm HOT 5
- [FEATURE] Add a C code for implementing Koch Curve. HOT 3
- Inclusion of Line Drawing Algorithms HOT 4
- no 'return' in newNode function HOT 2
- [FEATURE] FCFS Scheduling Algorithm HOT 2
- [FEATURE] Adding Fintech HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c.