简体   繁体   English

c中的树数据结构实现

[英]trees data structure implementation in c

this code was perfectly working when i was using integers now i want to insert strings so i changed the comparisons to strcomp and its not woorking any help appreciated link for the full code http://pastebin.com/6j1haZRF 这个代码完全正常工作,当我使用整数现在我想插入字符串,所以我改变了比较strcomp和它没有woorking任何帮助赞赏链接的完整代码http://pastebin.com/6j1haZRF

struct node * insert(struct node *root, char x[])
{

if(!root)
{
    root=(struct node*)malloc(sizeof(struct node));
    root->data = x;
    root->left = NULL;
    root->right = NULL;
    return(root);
}
if((a=strcmp(root->data,x))>0){
    root->left = insert(root->left,x);
}
else
{
    if(strcmp(root->data,x)<0)
        root->right = insert(root->right,x);
}
return(root);
}

Your input buffer x is mutated every time you call scanf . 每次调用scanf时,输入缓冲区x都会发生变化。 Unlike the integer case, where assigning will copy the integer, in this case assigning only copies the pointer to your string. 与整数情况不同,分配将复制整数,在这种情况下,分配仅复制指向字符串的指针。 you should assign a copy of the buffer as the data, perhaps with something like 你应该将缓冲区的副本分配为数据,也许是类似的东西

root->data = strdup(x);

You will also have to free this with free when destroying your tree. 您也将有释放这个free摧毁你的树时。

For the following structure 对于以下结构

struct node{
    char * data;
    struct node *left;
    struct node *right;

} *root=NULL,*temp;

you would have to separately allocate memory for data . 你必须单独为data分配内存。

Just the following would not work 只是以下不起作用

    root=(struct node*)malloc(sizeof(struct node));
    root->data = x;

Solution strategy 1 : Allocate memory according to need. 解决方案策略1 :根据需要分配内存。 Ie allocate enough memory to hold the string for that node. 即分配足够的内存来保存该节点的字符串。 Here, the code has to properly manage node->data , ie suitably allocate and de-allocate. 这里,代码必须正确地管理node->data ,即适当地分配和解除分配。

free( root->data );         // free previously allocated memory, if any
root->data = strdup( x );   // equivalent to malloc and memcpy

As an improvement, the memory request for data may be included in the malloc for node, thereby (a) avoiding memory fragmentation, (b) avoiding (per-malloc) overhead, (c) avoiding extra work while releasing memory ( free() of node would free memory of data ). 作为改进, data的内存请求可以包含在节点的malloc中,从而(a)避免内存碎片,(b)避免(每malloc)开销,(c)在释放内存时避免额外工作( free()节点将释放data内存)。

struct node {
    struct node *left;
    struct node *right;
    char * data;
};
size_t const xLen = strlen( x );
root = malloc( sizeof *root + xLen );
strncpy( root + sizeof root->left + sizeof root->right, x, xLen );

Solution strategy 2 : Have the node contain necessary memory for the string. 解决方案策略2 :让节点包含字符串所需的内存。 This way, there is no hassle to allocate and deallocate separately for the string. 这样,为字符串分别分配和释放没有麻烦。 However, on the flip side, the upper limit becomes same for all strings. 但是,另一方面,所有琴弦的上限都相同。 (It is a trade-off.) (这是一种权衡。)

char data[ MaxDataLen ];     // earlier, enum { MaxDataLen = 80 };
strncpy( root->data, x, MaxDataLen - 1 ); // copy chars 
root->data[ MaxDataLen - 1 ] = 0;         // NULL termination

The solution would be to allocate memory for node->data string every time you insert a Node or declare the following structure. 解决方案是每次插入节点或声明以下结构时为node-> data字符串分配内存。

struct node{
   char data[MaxData];
   struct node *left;
   struct node *right;

}*root=NULL,*temp;

The problem is that you have memory allocated for only one string (char a[10]), the first time your insert function will work, but the second time you are overwriting the variable a, and in your insert function you don't have a test case for string equals so it returns null. 问题是你的内存只分配给一个字符串(char a [10]),第一次你的插入函数工作,但第二次你覆盖变量a,而你的插入函数你没有字符串等于的测试用例,因此它返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM