From cdd56b8e25ceaceec9a7c97abb4fa9be1c1998de Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Mon, 30 Oct 2023 23:38:10 +0100 Subject: [PATCH] fix construction of maps --- sortedmap.c | 6 +++--- sortedmap.h | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/sortedmap.c b/sortedmap.c index 415e317..448b809 100644 --- a/sortedmap.c +++ b/sortedmap.c @@ -47,9 +47,9 @@ SortedMap * put_map(SortedMap *this, char *key, void *value) { leaf->content.value = value; intern = malloc(sizeof(SortedMap)); intern->type = INTERN; - intern->key = this->key; - intern->content.children.l = cmp < 0 ? leaf : this; - intern->content.children.r = cmp < 0 ? this : leaf; + intern->key = cmp <= 0 ? key : this->key; + intern->content.children.l = cmp <= 0 ? leaf : this; + intern->content.children.r = cmp <= 0 ? this : leaf; return intern; case INTERN: if (cmp <= 0) { diff --git a/sortedmap.h b/sortedmap.h index 9bab76e..7482f5e 100644 --- a/sortedmap.h +++ b/sortedmap.h @@ -1,5 +1,9 @@ #ifndef SORTED_MAP #define SORTED_MAP +/* A map datastructure which maps strings to void*. + * A binary search tree putting string equal to or smaller than the current node left. + */ + enum nodetype { EMPTY, LEAF, INTERN }; typedef struct node {