简体   繁体   English

使用递归函数在二进制搜索树中插入项目

[英]using recursive function to insert item in Binary search tree

Issue : How to insert item in BST when pointer is in nested structure? 问题:当指针处于嵌套结构中时,如何在BST中插入项目?
Language : C only. 语言:仅C。

I know about binary search tree and how to do insert delete and print. 我知道二进制搜索树以及如何执行插入删除和打印。 But this time I have nested structure and inner structure contains pointers. 但是这次我有嵌套结构,内部结构包含指针。 SO I need help /hint how to do that. 所以我需要帮助/提示该怎么做。

Example traditionally we have structure like this 传统上我们有这样的示例结构

    struct node 
   {
     int data;
     struct node* left;
     struct node* right;
   }

And to insert node at appropriate place it is something like this 并在适当的位置插入节点是这样的

struct node* insert(struct node* node, int data)
 {
     if (node == NULL) 
    {
       // code to implement root code;
       node = create_node(); 
     }
     else
     {
       // 2. Otherwise, recur down the tree
       if (data <= node->data) 
       { 
         node->left = insert(node->left, data);
       } 
       else 
      {
        node->right = insert(node->right, data);
      }
     return(node);
     }
 }

But what I have now is nested structure 但是我现在拥有的是嵌套结构

struct link
{
   struct link *left;
   struct link *right;
};

struct item
{
   struct link link; 
   uint8_t c;
};

Since here item does not have pointer to left and right , how would I insert item in recursive fashion. 由于这里item没有指向left和right的指针,我将如何以递归方式插入item。 my attempt 我的尝试

 struct item* insert_item( item* root, uint8_t key )
{
    if( !root )
    {
        root = create_item( key ); // some function create_item to create first item
    }
    /* Otherwise, recur down the tree */
    else
   {
        if( key < root->c )
        {
            insert_item( ); // The node  does not have pointer ?? how would I traverse left or right?
        }
        else
        {
          // how would I apply recursive to right side of tree?

        }
   }
  return root;
}

In insert_item() use something like this to traverse left or right: insert_item()使用类似以下的方法向左或向右移动:

root.link->left
root.link->right

But remember, in your insert method you are returning void except *node like traditional insertion. 但是请记住,在insert方法中,除了*node以外,您insert返回void类似于传统的插入。

Note, Your struct node* insert(struct node* node, int data) will give Undefined Behavior because of no return statement when node == NULL . 注意,由于node == NULL时没有return语句,因此您的struct node* insert(struct node* node, int data)将给出Undefined Behavior

EDIT: As OP asked in the comment, "but root.link->left is of type link. how it will work ?" 编辑:正如OP在评论中问的那样,“但是root.link-> left是link类型。它将如何工作?”

So change 所以改变

struct link
{
   struct link *left;
   struct link *right;
};

to, 至,

struct link
{
   struct item *left;
   struct item *right;
};

That will solve your problem. 那将解决您的问题。 But don't forget the forward declaration of struct item . 但是不要忘记struct item的正向声明。 Otherwise in struct link compiler will raise error as it don't know what item is. 否则,在struct link编译器会产生错误,因为它不知道什么item是。

The solution is to use casts. 解决方案是使用强制类型转换。

int insert(struct link** node, uint8_t data) {
  if (*node == NULL) {
    // code to implement root code;
    *node = malloc( sizeof(struct item) );
    if(*node == NULL) return -1;
    ( (struct item*) *node)->c = data;
    ( (struct item*) *node)->link.left = ( (struct item*) *node)->link.right = NULL;
  } else {
    // 2. Otherwise, recur down the tree
    int rc;
    if (data <= ( (struct item*) *node)->c) { 
      rc = insert(&( ( (struct item*) *node)->link.left ), data);
      if( rc < 0 ) return rc;
    } else {
      rc = insert(&( ( (struct item*) *node)->link.right ), data);
      if( rc < 0 ) return rc;
    }
  }
  return 0;
}

Note that I made a few changes to your code. 请注意,我对您的代码做了一些更改。 Namely, I no longer assume that node->left and node->right are not assigned. 即,我不再假定未分配node->leftnode->right

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

相关问题 二进制搜索树插入功能不会更新二进制搜索树 - Binary search tree insert function does not update binary search tree 使用 C 中的比较 function 递归地插入二叉搜索树 - Insert in Binary search tree recursively using comparison function in C 判断树是否为二叉搜索树(BST)的递归函数(修改后的代码) - recursive function that tells if a Tree is a Binary Search Tree ( BST ) (Modified code) C中的二进制搜索树插入功能 - Binary Search Tree Insert Function In C 用于二叉搜索树 (BST) 的插入 function 中的指针 - Pointers in insert function for Binary Search Tree (BST) 二叉树递归函数 - Binary Tree Recursive Function 如何编写递归函数来使用 C 中的有序遍历在二叉树中搜索键? - How to write a recursive function to search for a key in a binary tree using in-order traversal in C? 动态数组不从二叉搜索树中递归填充 function - Dynamic array does not fill in a recursive function from a binary search tree 在C语言中使用递归函数进行二进制搜索 - Binary Search Using Recursive Function in C 使用迭代插入函数在第二次插入后初始化二叉搜索树时出现分段错误 - Segmentation fault in inititalizing a binary search tree after second insertion using a iterative insert function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM