template.c (1885B)
1 void __TreeMap_put(struct TreeMap* self, char *key, int value) { 2 struct TreeMapEntry *cur, *left, *right; 3 int cmp; 4 struct TreeMapEntry *old, *new; 5 char *new_key; 6 7 cur = self->__root; 8 left = NULL; 9 right = NULL; 10 11 /* TODO: Loop through the tree from the root. If the matches 12 * the node, update the value and return. Ad the tree is scanned, 13 * keep track of the node containing largest key less than "key" 14 * in the variable left and the node containing the smallest key 15 * greater than "key" in the variable "right". So if the key is 16 * not found, left will be the closest lower neighbor or null 17 * and right will be the closest greater neighbor or null. 18 */ 19 20 /* Not found - time to insert into the linked list after old */ 21 new = malloc(sizeof(*new)); 22 23 /* TODO: Set up the new node with its new data. */ 24 25 /* Empty list - add first entry */ 26 if ( self->__head == NULL ) { 27 self->__head = new; 28 self->__root = new; 29 return; 30 } 31 32 /* Keep this in here - it will help you debug the above code */ 33 __Map_check(self, left, key, right); 34 35 /* TODO: Insert into the sorted linked list */ 36 37 /* TODO: Insert into the tree */ 38 39 } 40 41 int __TreeMap_get(struct TreeMap* self, char *key, int def) 42 { 43 int cmp; 44 struct TreeMapEntry *cur; 45 46 if ( self == NULL || key == NULL || self->__root == NULL ) return def; 47 48 cur = self->__root; 49 50 /* TODO: scan down the tree and if the key is found, return the value. 51 * If the key is not found, return the default value (def). 52 */ 53 54 return def; 55 } 56 57 struct TreeMapEntry* __TreeMapIter_next(struct TreeMapIter* self) 58 { 59 /* Advance the iterator. Recall that when we first 60 * create the iterator __current points to the first item 61 * so we must return an item and then advance the iterator. 62 */ 63 return NULL; 64 }