【问题标题】:How to insert a linked list at node of binary search tree?如何在二叉搜索树的节点处插入链表?
【发布时间】:2018-08-30 04:08:18
【问题描述】:

我想填充一个位于二叉搜索树节点中的链表。 如果用户已经在列表中退出,则将 ip 添加到该特定用户的链接列表中。

这是我迄今为止尝试过的:

我的数据结构:

typedef struct ip{
    int ip;
    struct ip *ipNext;
}IP;

typedef struct bstNode
{
    char data[32];
    struct bstNode* left;
    struct bstNode* right;
    IP *ipHead; //Pointer to linked list. 
}bstNode;

这是我遇到问题的地方

我的插入函数将 IP 地址插入到用户列表中:

bstNode insertIP(bstNode *head, char *username, int ip){

 if (search(username)==1)
    {
        if (head->ipHead == NULL)
    {
        IP *temp; 
        temp = (IP*)malloc(sizeof(IP));
        temp->ip = ip;
        temp->ipNext = NULL;
    }else{
        head->ipHead->ip= ip;
        head->ipHead->ipNext=NULL;
    }
}

插入函数(可行):

bstNode *insert(bstNode *node, char *word)
{
    if(node==NULL){
        node= malloc(sizeof(bstNode));
        //IP* ipNode=malloc(sizeof(IP));
        strcpy(node->data, word);
        node->left=NULL;
        node->right=NULL;
    }
    else{
        if(strcmp(word, node->data)<0)
            node->left=insert(node->left, word);
        else if(strcmp(word, node->data)>0)
            node->right=insert(node->right, word);
    }
    return node;
}

搜索功能(有效):

void search(char* user, bstNode* root)  
{
    int res;
    if( root!= NULL ) {
        res = strcmp(root, root->data);
        if( res < 0)
            search( user, root->left);
        else if( res > 0)
            search( user, root->right);
        else
            printf("User Found\n");   
            return 1;                       
    }
    else printf("\nNot in tree\n");
    return 0;
}

【问题讨论】:

  • 在您的insertIP 函数中,如果您点击else,您将需要遍历您的列表直到-&gt;ipNext=NULL,然后在该地址分配插入tmp,并将值设置为@987654330 @。由于您将始终分配if (search(username)==1),因此temp 的分配应该在if (head-&gt;ipHead == NULL) 之前。并且... 没有必要将malloc 的返回值强制转换,没有必要。见:Do I cast the result of malloc?

标签: c dynamic data-structures linked-list binary-tree


【解决方案1】:

第一个问题:当您创建 temp 时,您不会将其分配给 head-&gt;ipHead

第二个问题:当ipHead 存在时,您仍然需要为新列表节点分配内存,如果您想在最后添加它,则必须遍历整个列表。您也可以将其添加到列表的开头。

第三个问题:如果找到用户名,搜索函数应该返回树中的节点。我猜你不想总是在第一个树节点上添加新的列表节点。我建议你在函数中使用局部变量来遍历树——而不是递归。

【讨论】:

    【解决方案2】:

    当您发现该用户已存在于树中时,首先您必须存储包含给定用户的bstNode。之后,您将不得不遍历该 bstNode 的链接列表以在最后附加 ip。如果链接的链接是NULL,您的代码还有一个问题。您必须将新节点地址分配给ipHead。目前您已经创建了一个临时节点并且没有将其分配给 head。

    【讨论】:

      【解决方案3】:

      最近我编写了一个二叉搜索树,其中每个树节点都有一个链表(链表节点是单链的),我使用了以下数据结构(如果有人需要整个代码,请告诉我)。

      // data in each
      // list node
      struct data_info
      {
          //body of data
      };
      
      typedef struct data_info Item;
      
      // list node
      typedef struct lnode
      {
          Item data_item;
          struct lnode * next;
      } Lnode;
      
      // List points to
      // first list node
      typedef Lnode * List;
      
      // tree node contains
      // linked list
      typedef struct trnode
      {
          List list_item;
          struct trnode * left;   // pointer to right branch
          struct trnode * right;  // pointer to left branch
      } Trnode;
      
      // tree of linked lists
      typedef struct tree
      {
          Trnode * root;          // pointer to root of tree
          int size;               // number of list nodes in tree
      } Tree;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        相关资源
        最近更新 更多