| 1 |
|
|---|
| 2 |
#include "tst.h" |
|---|
| 3 |
#include <stdio.h> |
|---|
| 4 |
#include <stdlib.h> |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
int tst_grow_node_free_list(struct tst *tst); |
|---|
| 8 |
int tst_insert(unsigned char *key, void *data, struct tst *tst, int option, void **exist_ptr) |
|---|
| 9 |
{ |
|---|
| 10 |
struct node *current_node; |
|---|
| 11 |
struct node *new_node_tree_begin = NULL; |
|---|
| 12 |
int key_index; |
|---|
| 13 |
int perform_loop = 1; |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
if (key == NULL) |
|---|
| 17 |
return TST_NULL_KEY; |
|---|
| 18 |
|
|---|
| 19 |
if(key[0] == 0) |
|---|
| 20 |
return TST_NULL_KEY; |
|---|
| 21 |
|
|---|
| 22 |
if(tst->head[(int)key[0]] == NULL) |
|---|
| 23 |
{ |
|---|
| 24 |
|
|---|
| 25 |
if(tst->free_list == NULL) |
|---|
| 26 |
{ |
|---|
| 27 |
if(tst_grow_node_free_list(tst) != 1) |
|---|
| 28 |
return TST_ERROR; |
|---|
| 29 |
} |
|---|
| 30 |
tst->head[(int)key[0]] = tst->free_list; |
|---|
| 31 |
|
|---|
| 32 |
tst->free_list = tst->free_list->middle; |
|---|
| 33 |
current_node = tst->head[(int)key[0]]; |
|---|
| 34 |
current_node->value = key[1]; |
|---|
| 35 |
if(key[1] == 0) |
|---|
| 36 |
{ |
|---|
| 37 |
current_node->middle = data; |
|---|
| 38 |
return TST_OK; |
|---|
| 39 |
} |
|---|
| 40 |
else |
|---|
| 41 |
perform_loop = 0; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
current_node = tst->head[(int)key[0]]; |
|---|
| 45 |
key_index = 1; |
|---|
| 46 |
while(perform_loop == 1) |
|---|
| 47 |
{ |
|---|
| 48 |
if(key[key_index] == current_node->value) |
|---|
| 49 |
{ |
|---|
| 50 |
|
|---|
| 51 |
if(key[key_index] == 0) |
|---|
| 52 |
{ |
|---|
| 53 |
if (option == TST_REPLACE) |
|---|
| 54 |
{ |
|---|
| 55 |
if (exist_ptr != NULL) |
|---|
| 56 |
*exist_ptr = current_node->middle; |
|---|
| 57 |
|
|---|
| 58 |
current_node->middle = data; |
|---|
| 59 |
return TST_OK; |
|---|
| 60 |
} |
|---|
| 61 |
else |
|---|
| 62 |
{ |
|---|
| 63 |
if (exist_ptr != NULL) |
|---|
| 64 |
*exist_ptr = current_node->middle; |
|---|
| 65 |
return TST_DUPLICATE_KEY; |
|---|
| 66 |
} |
|---|
| 67 |
} |
|---|
| 68 |
else |
|---|
| 69 |
{ |
|---|
| 70 |
if(current_node->middle == NULL) |
|---|
| 71 |
{ |
|---|
| 72 |
|
|---|
| 73 |
if(tst->free_list == NULL) |
|---|
| 74 |
{ |
|---|
| 75 |
if(tst_grow_node_free_list(tst) != 1) |
|---|
| 76 |
return TST_ERROR; |
|---|
| 77 |
} |
|---|
| 78 |
current_node->middle = tst->free_list; |
|---|
| 79 |
|
|---|
| 80 |
tst->free_list = tst->free_list->middle; |
|---|
| 81 |
new_node_tree_begin = current_node; |
|---|
| 82 |
current_node = current_node->middle; |
|---|
| 83 |
current_node->value = key[key_index]; |
|---|
| 84 |
break; |
|---|
| 85 |
} |
|---|
| 86 |
else |
|---|
| 87 |
{ |
|---|
| 88 |
current_node = current_node->middle; |
|---|
| 89 |
key_index++; |
|---|
| 90 |
continue; |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
if( ((current_node->value == 0) && (key[key_index] < 64)) || |
|---|
| 96 |
((current_node->value != 0) && (key[key_index] < |
|---|
| 97 |
current_node->value)) ) |
|---|
| 98 |
{ |
|---|
| 99 |
|
|---|
| 100 |
if (current_node->left == NULL) |
|---|
| 101 |
{ |
|---|
| 102 |
|
|---|
| 103 |
if(tst->free_list == NULL) |
|---|
| 104 |
{ |
|---|
| 105 |
if(tst_grow_node_free_list(tst) != 1) |
|---|
| 106 |
return TST_ERROR; |
|---|
| 107 |
} |
|---|
| 108 |
current_node->left = tst->free_list; |
|---|
| 109 |
|
|---|
| 110 |
tst->free_list = tst->free_list->middle; |
|---|
| 111 |
new_node_tree_begin = current_node; |
|---|
| 112 |
current_node = current_node->left; |
|---|
| 113 |
current_node->value = key[key_index]; |
|---|
| 114 |
if(key[key_index] == 0) |
|---|
| 115 |
{ |
|---|
| 116 |
current_node->middle = data; |
|---|
| 117 |
return TST_OK; |
|---|
| 118 |
} |
|---|
| 119 |
else |
|---|
| 120 |
break; |
|---|
| 121 |
} |
|---|
| 122 |
else |
|---|
| 123 |
{ |
|---|
| 124 |
current_node = current_node->left; |
|---|
| 125 |
continue; |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
else |
|---|
| 129 |
{ |
|---|
| 130 |
|
|---|
| 131 |
if (current_node->right == NULL) |
|---|
| 132 |
{ |
|---|
| 133 |
|
|---|
| 134 |
if(tst->free_list == NULL) |
|---|
| 135 |
{ |
|---|
| 136 |
if(tst_grow_node_free_list(tst) != 1) |
|---|
| 137 |
return TST_ERROR; |
|---|
| 138 |
} |
|---|
| 139 |
current_node->right = tst->free_list; |
|---|
| 140 |
|
|---|
| 141 |
tst->free_list = tst->free_list->middle; |
|---|
| 142 |
new_node_tree_begin = current_node; |
|---|
| 143 |
current_node = current_node->right; |
|---|
| 144 |
current_node->value = key[key_index]; |
|---|
| 145 |
break; |
|---|
| 146 |
} |
|---|
| 147 |
else |
|---|
| 148 |
{ |
|---|
| 149 |
current_node = current_node->right; |
|---|
| 150 |
continue; |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
do |
|---|
| 156 |
{ |
|---|
| 157 |
key_index++; |
|---|
| 158 |
|
|---|
| 159 |
if(tst->free_list == NULL) |
|---|
| 160 |
{ |
|---|
| 161 |
if(tst_grow_node_free_list(tst) != 1) |
|---|
| 162 |
{ |
|---|
| 163 |
current_node = new_node_tree_begin->middle; |
|---|
| 164 |
|
|---|
| 165 |
while (current_node->middle != NULL) |
|---|
| 166 |
current_node = current_node->middle; |
|---|
| 167 |
|
|---|
| 168 |
current_node->middle = tst->free_list; |
|---|
| 169 |
tst->free_list = new_node_tree_begin->middle; |
|---|
| 170 |
new_node_tree_begin->middle = NULL; |
|---|
| 171 |
|
|---|
| 172 |
return TST_ERROR; |
|---|
| 173 |
} |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
if(tst->free_list == NULL) |
|---|
| 178 |
{ |
|---|
| 179 |
if(tst_grow_node_free_list(tst) != 1) |
|---|
| 180 |
return TST_ERROR; |
|---|
| 181 |
} |
|---|
| 182 |
current_node->middle = tst->free_list; |
|---|
| 183 |
|
|---|
| 184 |
tst->free_list = tst->free_list->middle; |
|---|
| 185 |
current_node = current_node->middle; |
|---|
| 186 |
current_node->value = key[key_index]; |
|---|
| 187 |
} while(key[key_index] !=0); |
|---|
| 188 |
|
|---|
| 189 |
current_node->middle = data; |
|---|
| 190 |
return TST_OK; |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|