[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AVL tree silently accept duplicate keys

From: Domingo Alvarez Duarte
Subject: AVL tree silently accept duplicate keys
Date: Mon, 10 Aug 2020 14:28:23 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0

Hello !

Replacing usages of AVL tree by SplayTree I found that AVL tree silently accepts duplicate keys on "avl_insert_node" see example bellow, there is only one way to find by key "avl_find_node" which make it prone to write incorrect code:


#include <stdio.h>
#include <stdlib.h>
#include "avl.h"

int main(int argc, char *argv[])
    AVL *tree = avl_create_tree(avl_strcmp, NULL);
    AVLNODE *node1 = avl_insert_node(tree, "one");
    printf("node1 = %p\n", node1);
    AVLNODE *node2 = avl_insert_node(tree, "one");
    printf("node2 = %p\n", node2);
    AVLNODE *node3 = avl_insert_node(tree, "one");
    printf("node3 = %p\n", node3);
    AVLNODE *node = avl_find_node(tree, "one");
    printf("node = %p\n", node);
    avl_delete_node(tree, node1);
    node = avl_find_node(tree, "one");
    printf("node = %p\n", node);
    avl_delete_node(tree, node2);
    node = avl_find_node(tree, "one");
    printf("node = %p\n", node);
    return 0;




gcc -g -o test-avl test-avl.c -I../src/misc -I../src ../src/.libs/libglpk.a




node1 = 0x55f599e6d908
node2 = 0x55f599e6d940
node3 = 0x55f599e6d978
node = 0x55f599e6d940
node = 0x55f599e6d940
node = 0x55f599e6d978


Cheers !

reply via email to

[Prev in Thread] Current Thread [Next in Thread]