首页 文章

BST链接和案例不起作用

提问于
浏览
1

我有一个像这样的嵌套结构

typedef struct Node_link {
    struct Node_base *parent, *left, *right;
}Node_link;


typedef struct Node_base {
    struct Node_link link[2];
}Node_base;

typedef struct Node{
    struct Node_base base;
    int size;
    int *address;
}Node;


Node_base *head[2] ={NULL, NULL}; 
//head[0] stores int size and head[1] it's corresponding address

节点具有右,左和父链接,所有都是嵌套的,例如node-> left-> link.parent = node . 我要维护所有链接(父,左和右)并删除节点 . 我尝试了很多案例,但仍然遗漏了一些案例 . 有人能告诉我我需要使用的所有情况吗?或者转介一些材料?我搜索了很多但没有成功 . 我的插入功能如下:

Node_base *   insert(Node_base *location, Node_base *n) {
    if (head[0]==NULL)
    head[0]=n;
else
{
    if (location==NULL){
        location=n;
        return location;
    }
    else{

        if(((Node *)n)->size < ((Node *)location)->size){
            if(location->link[0].left==NULL)
            {
                location->link[0].left=n;
                location->link[0].left->link[0].parent=location;
            }
            else
                location->link[0].left=insert(location->link[0].left,n);
        return location;
    }
}

我对head [1]使用了相同的嵌套插入函数,它存储了head [0]中插入的节点的大小 .

1 回答

  • 0

    很难说出这里发生了什么 . 您的代码看起来并不像我见过的任何BST实现 . 为什么需要Node_Link结构? Node结构中的指针应该定义链接是什么 . 为什么是父指针?在标准的BST实施中不应该这样做 . 你需要的只是:

    struct node {
        node *left;
        node *right;
        void *data;
        int size;
    };
    
    struct bst {
        node *root;
    };
    

相关问题