GithubHelp home page GithubHelp logo

ekan5h / avltree Goto Github PK

View Code? Open in Web Editor NEW
36.0 1.0 19.0 8 KB

C++ implementation of an AVL tree template.

C++ 100.00%
programming tree tree-structure data-structures avl-tree avl avl-tree-implementations avl-tree-code avltrees avl-implementations

avltree's People

Contributors

ekan5h avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

avltree's Issues

logic error

Line117:
height(head->left) >= height(head->right)

it should be relpaced with "height(head->left->left) >= height(head->left->right)"?

Similar to Line 124.

Segment Fault Bug

I found a case recently. which caused a segment fault.

Here is my test code.

#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;
}

Here is my testcase.

tc.txt

Reproduce bug

Add your code in front of my code, compile and run with the following commands.

g++ mycode.cpp -o mycode
cat tc.txt | ./mycode

bug in tree rebalancing

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

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.