[英]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->left
和node->right
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.