ekan5h / avltree Goto Github PK
View Code? Open in Web Editor NEWC++ implementation of an AVL tree template.
C++ implementation of an AVL tree template.
Line117:
height(head->left) >= height(head->right)
it should be relpaced with "height(head->left->left) >= height(head->left->right)"?
Similar to Line 124.
I found a case recently. which caused a segment fault.
#include<vector>
int main(){
AVL<int> tree;
vector<int> v;
int x;
while(cin >> x)
{
getchar();
v.push_back(x);
tree.insert(x);
}
for(auto &&x : v)
{
tree.remove(x);
}
return 0;
}
Add your code in front of my code, compile and run with the following commands.
g++ mycode.cpp -o mycode
cat tc.txt | ./mycode
the rebalancing of the tree in the remove function right is done this way:
if(bal>1){
if(x > head->left->key){
return rightRotation(head);
}else{
head->left = leftRotation(head->left);
return rightRotation(head);
}
}else if(bal < -1){
if(x < head->right->key){
return leftRotation(head);
}else{
head->right = rightRotation(head->right);
return leftRotation(head);
}
}
This code doesn't always work. I think that
if(x > head->left->key)
should be replaced with
if(height(head->left) > height(head->right))
and the same applies for
if(x < head->right->key)
that should be replaced with
if(height(head->right) > height(head->left))
I was researching for an excercise I had to do and I think that this should work. I am a novice on github and I am only submitting this because I've seen another guy report (I think) the same problem. Let me know if this is helpful
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.