[英]Binary search tree insert function does not update binary search tree
我正在嘗試獲取一個文本文件,並將其轉換為二進制搜索樹。 這是我的函數,它接收文本文件並將其傳遞給bst_insert:
bst* get_counts(char *filename) {
bst* new = create_bst();
FILE *fp;
char buffer[500];
char* delim = " .,'/'\n;?><:";
char* token;
if (!(fp = fopen(filename, "r"))) {
fprintf(stderr, "Could not open file.");
exit(0);
}
while(fgets(buffer, 500, fp)) {
token = strtok(buffer, delim);
while (token) {
wordcount* w = (wordcount*)malloc(sizeof(wordcount));
w->count = 0;
strcpy(w->word, token);
bst_insert(new, (void*)w, compare_words);
token = strtok(NULL, delim);
}
}
fclose(fp);
return new;;
}
這是bst_insert,然后調用bst_insert:
void bst_insert(bst *tree, void *item, int (*compare)(void *, void *))
{
if (tree)
tree->root = bstnode_insert(tree->root, item, compare);
}
bstnode* bstnode_insert(bstnode *node, void *item,
int (*compare)(void *, void *))
{
bstnode *new_node = (bstnode*)malloc(sizeof(bstnode));
new_node->item = item;
new_node->rsub = NULL;
new_node->lsub = NULL;
if (!node) {
node = (bstnode*)malloc(sizeof(bstnode));
node->item = item;
node->rsub = NULL;
node->lsub = NULL;
return node;
}
wordcount *w1 = (wordcount*) item;
wordcount *w2 = (wordcount*)node->item;
int comp = compare(w1->word, w2->word);
if (comp < 0) {
if (node->lsub)
node->lsub = bstnode_insert(node->lsub, item, compare);
else {
node->lsub = new_node;
}
}
else if (comp > 0) {
if (node->rsub)
node->rsub = bstnode_insert(node->rsub, item, compare);
else {
node->rsub = new_node;
}
} else {
bstnode_insert(node->rsub, item, compare);
}
return node;
}
由於某種原因,我的函數最終只能創建樹的第一個節點,我不確定為什么。 我想讓它成為整個節點。
這是(最低)二叉樹實現:
首先是創建樹的函數:
Tree_t* create_dynamic_tree()
{
Tree_t* my_tree = NULL;
my_tree = (Tree_t*) malloc(sizeof(Tree_t));
my_tree->number_of_nodes = 0;
my_tree->root = NULL;
my_tree->max_key = 0;
return my_tree;
}
一棵樹在哪里
typedef struct Tree_t
{
int number_of_nodes;
Node_t* root;
}Tree_t;
一個節點是:
struct Node_t
{
void* data;
int key;
Node_t* left_child;
Node_t* right_child;
};
現在您需要一個添加數據的函數,應該是這樣的:
int data_add(Tree_t* tree_dest , void* data , int* data_key)
{
if(tree_dest == NULL || data == NULL )
{
return -1;
}
int key = 0;
Node_t* parent;
key = *data_key;
if(key<0)
{
return -2;
}
if(tree_dest->number_of_nodes == 0)
{
Node_t* temp = NULL;
temp = (Node_t*) malloc(sizeof(Node_t));
temp->data = data;
temp->key = key;
temp->left_child = NULL;
temp->right_child = NULL;
tree_dest->number_of_nodes++;
tree_dest->root = temp;
return key;
}
exists_t existance = not_exist;
exists_t* exist_ptr = NULL;
exist_ptr = &existance;
if(is_key_exists(tree_dest , &parent ,key , exist_ptr) == 0)
{
if(existance == exist)
{
return -1;
}
else
{
if(insert_node(tree_dest , &parent ,data , key) == -1)
{
return -1;
}
}
}
if(tree_dest->max_key < key)
{
tree_dest->max_key = key;
}
//free(parent);
return key;
}
上面的函數使用以下函數:
--is_key_exists()
int is_key_exists(Tree_t* the_tree , Node_t** parent , int key , exists_t* exist_t)
{
if(the_tree == NULL )
{
return -1;
}
Node_t* root = the_tree->root;
search_node_parent(the_tree , &root , parent , key);
if(root != NULL)
{
*exist_t = exist;
return 0;
}
else
{
*exist_t = not_exist;
return 0;
}
}
--insert_node()
int insert_node(Tree_t* tree_dest , Node_t** parent ,void* data , int key)
{
if(tree_dest == NULL || data == NULL || parent == NULL )
{
return -1;
}
Node_t* new_node = NULL;
new_node = (Node_t*) malloc(sizeof(Node_t));
new_node->data = data;
new_node->key = key;
new_node->left_child = NULL;
new_node->right_child = NULL;
if(key < (*parent)->key )
{
(*parent)->left_child = new_node;
}
if(key > (*parent)->key)
{
(*parent)->right_child = new_node;
}
tree_dest->number_of_nodes++;
if(tree_dest->max_key < key)
{
tree_dest->max_key = key;
}
return 0;
}
我在這里所做的是獲取void *數據,指向您需要的所有內容的指針(此處使用的char *或結構/聯合),將數據封裝在節點內,然后使用id(在您的可能是文件中的行數)。 希望對您有幫助,或者對將來需要(基本)樹實現的其他人有所幫助。 這就是插入所需的全部內容。如果您想要一棵平衡的樹,下一步將是重新哈希。
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.