在二叉搜索树中将前序前驱与其后继连接

标签 c tree

实际上,在一次采访中,我被要求编写一段代码,通过该代码,二叉搜索树中的每个节点都有一个额外的指针,即“下一个”,我们必须将每个节点的这个指针连接到其前序后继者,任何一个都可以吗?建议我使用代码,因为我无法这样做。树节点具有以上结构:-

 struct node {
        int data ;
        struct node *left,*right;
        struct node *next; //this pointer should point to pre order successor 
        };

谢谢。

感谢你们破解了解决方案,下面是用 c 编写的整个代码:-

#include <stdio.h>
#include <stdlib.h>

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

struct node* getNode(int data)
{
        struct node* temp=(struct node*)malloc(sizeof(struct node));
        temp->left=temp->right=NULL;
        temp->data=data;
        return temp;
}

void insert(struct node**root,int data)
{
    if(!*root)
    {
        *root=(struct node*)malloc(sizeof(struct node));
        (*root)->left=(*root)->right=NULL;
        (*root)->data=data;
    }
    else if(data<(*root)->data)
        insert(&((*root)->left),data);
    else if(data>(*root)->data)
        insert(&((*root)->right),data);
}

struct node* preorderSuccessor(struct node* root,struct node* p)
{
    int top,i;
    struct node *arr[20];
    if(!root || !p)
        return NULL;
    if(p->left)
        return p->left;
    if(p->right)
        return p->right;
    top=-1;
    while(root->data!=p->data)
    {
        arr[++top]=root;
        if(p->data<root->data)
            root=root->left;
        else
            root=root->right;
    }
    for(i=top;i>=0;i--)
    {
        if(arr[i]->right)
        {
            if(p!=arr[i]->right)
                return arr[i]->right;
        }
        p=arr[i];
    }
    return NULL;

}

void connect(struct node* parent,struct node *r)
{
        if(r)
        {       connect(parent ,r->left);    
                r->next = preorderSuccessor(parent,r);
                connect(parent,r->right);
        }
}


int main()
{
        struct node* root=NULL,*temp=NULL;
        int arr[]={10,11,2,3,9,8,4,5},size,i;
        size=sizeof(arr)/sizeof(arr[0]);

        for(i=0;i<size;i++) 
          insert(&root,arr[i]);
        connect(root,root);
        struct node *ptr = root;
        while(ptr){
          // -1 is printed if there is no successor
          printf("Next of %d is %d \n", ptr->data, ptr->next? ptr->next->data: -1);
          ptr = ptr->next;
        }


        return 0;
}

最佳答案

正如 Eugene 所说:因此使用先序遍历来遍历树并连接。为此,您需要知道您最后访问的是哪个节点(如果有)。

您可以通过传递对前一个节点的引用来使用通常的递归方法来做到这一点。这必须是在整个递归过程中有效的变量的地址,因为前一个节点不一定更接近根。您可以使用全局变量,但在包装函数中创建的变量可能更好:

void connect_r(struct node *node, struct node **whence)
{
    if (node) {
        if (*whence) (*whence)->next = node;
        *whence = node;

        connect_r(node->left, whence);
        connect_r(node->right, whence);
    }
}

void connect(struct node *head)
{
    struct node *p = NULL;

    connect_r(head, &p);
    if (p) p->next = NULL;
}

指针pconnect ,其地址被传递给递归函数 connect_r保存 next 的节点接下来应该更新指针。更新不会发生在第一个访问的节点上。和 next最后访问的节点的成员必须显式设置为 NULL递归之后。

或者,您可以使用堆栈迭代连接节点:

void connect(struct node *head)
{
    struct node *p = NULL;
    struct node **prev = &p;

    struct node *stack[32];    // To do: Prevent overflow
    size_t nstack = 0;

    if (head) stack[nstack++] = head;

    while (nstack) {
        struct node *node = stack[--nstack];

        *prev = node;
        prev = &node->next;

        if (node->right) stack[nstack++] = node->right;
        if (node->left) stack[nstack++] = node->left;
    }

    *prev = NULL;
}

已连接 next指针是当前树的快照。节点的插入、删除和重新排列将呈现 next链无效。 (但只要树的左右链接一致,就可以通过调用 connect 使其再次有效。)

关于在二叉搜索树中将前序前驱与其后继连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34533745/

相关文章:

c - 跳台 - 组件 8086

C FIFO - 如何在等待客户端请求时读取服务器的标准输入

c - 将此 IP 解析为正确的长度?

java - 如何将修改后的预序树遍历数据填充到 Java 树对象中?

java - 如何在 SWT/JFace 中设置双击编辑表格单元格?

algorithm - 如何表示非二叉树以及如何对该树进行 LCA?

c - 是否有必要在程序中打开多次文件?

c - char 类型数组使用什么操作数?

python - 在 Python 的字典树中生成所有从叶到根的路径

algorithm - 在树结构中,找到根节点和 k 个节点之间的最高键