我正在尝试将文本文件转换为二叉搜索树。这是我的函数,它接受文本文件并将其传递给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;
}
这是bst_insert,然后调用bst_insert:
{{1}}
由于某种原因,我的函数最终只会创建树的第一个节点,我不知道为什么。我希望它能够制作整个节点。
答案 0 :(得分:0)
这是(最小)二叉树实现:
首先是创建树的函数:
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 * data,指向你需要的所有东西(你在这里使用的char *或结构/联合),将数据封装在节点内并使用id将其添加到树中(在您的情况下可能是文件中的行数)。 我希望这可以帮助您或将来需要(基本)树实现的任何其他人。 这就是插入所需的全部内容。如果你想要一个平衡的树,那么下一步就是重新散列。
谢谢。