【问题标题】:Some of the elements are not getting added to the Binary Search Tree一些元素没有被添加到二叉搜索树中
【发布时间】:2020-11-13 07:31:08
【问题描述】:

我正在用 C 语言实现一个带有双向链表的二叉搜索树。 我没有得到预期的输出。 一些元素没有被添加到树中,特别是一些正确的孩子。

我需要帮助来解决这个问题。这是我的代码,

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

struct node *root=NULL;
struct node *temp=NULL;

struct node* createnode(int data){
    struct node *newNode= (struct node*)malloc(sizeof(struct node));
    newNode->data= data;
    newNode->left= NULL;
    newNode->right= NULL;

    return newNode;
}

void createBinaryTree(){
    int arr[100],size,i;
    printf("Enter no.of elements:");
    scanf("%d",&size);
    printf("\nEnter elements:");
    for(i=0;i<size;++i){
        scanf("%d",&arr[i]);
    }
    root= createnode(arr[0]);
    for(i=1;i<size;i++){
        temp=root;
        while(arr[i]>(temp->data)){
            if(temp->right==NULL){
                temp->right=createnode(arr[i]);
                continue;
            }
            temp=temp->right;
            
        }
        while(arr[i]<(temp->data)){
            if(temp->left==NULL){
                temp->left=createnode(arr[i]);
                continue;
            }
            temp=temp->left;
            
        }
    }
}

【问题讨论】:

  • 那不是一个双向链表,它看起来像一个 BST。我们可以双向遍历prev和next的双向链表
  • continue 不是你想要的:它强制 while 循环的下一次迭代,你应该使用 break 或 goto 代替
  • 还要考虑以下情况:1 10 2 3。添加最后一个元素时,您应该从右到左。但是,您的代码在您向左走后不允许向右走。
  • 请提供带有硬编码示例数据的minimal reproducible example 以证明您的问题。

标签: c linked-list binary-tree binary-search-tree doubly-linked-list


【解决方案1】:

您的代码:

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

struct node *root=NULL;
struct node *temp=NULL;

struct node* createnode(int data){
    struct node *newNode= (struct node*)malloc(sizeof(struct node));
    newNode->data= data;
    newNode->left= NULL;
    newNode->right= NULL;

    return newNode;
}

void createBinaryTree(){
    int arr[100],size,i;
    printf("Enter no.of elements:");
    scanf("%d",&size);
    printf("\nEnter elements:");
    for(i=0;i<size;++i){
        scanf("%d",&arr[i]);
    }
    root= createnode(arr[0]);
    for(i=1;i<size;i++){
        temp=root;
        while(arr[i]>(temp->data)){
            if(temp->right==NULL){
                temp->right=createnode(arr[i]);
                continue;
            }
            temp=temp->right;
            
        }
        while(arr[i]<(temp->data)){
            if(temp->left==NULL){
                temp->left=createnode(arr[i]);
                continue;
            }
            temp=temp->left;
            
        }
    }
}

跳过某些元素的原因是您添加的方式错误。这 2 个 while 循环是问题所在。

假设你有树:


     10
    /  \
   5   15
  / \    \
 2   7   17

而你想输入数字 6。

您的第一个时间将被跳过。

然后用你的 2nd 而你将去 5 号的节点。

在那之后你会退出那段时间,什么都不会发生。

你不会达到那个 NULL。

因此,要毫无问题地添加节点,只需使用一个 while 循环,只要未达到 NULL 就可以工作,并在该循环内使用 if 语句来检查您必须在每个节点中遵循的路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多