簡體   English   中英

如何在C中將數字插入二進制搜索樹?

[英]How to insert numbers into a binary search tree in C?

我試圖通過調用main函數的TREEinsert函數將數字列表插入二進制搜索樹中,但是無論何時我的程序運行,它都不會打印任何插入二進制搜索樹中的數字。 我已經遍歷了TREEinsert函數和Displayinorder函數,但是似乎找不到任何一個函數的問題。 我對二進制搜索樹有基本的了解,並查看了許多示例,以期了解BST的工作原理。 任何指導將大有幫助。

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



void TREEclear(struct node* t)
{
     t = NULL;
}

void TREEinsert(struct node*  t, int x)
{

    //insert in BST
    if(t == NULL)
    {
        t = (struct node*)malloc(sizeof(struct node));
        t->info = x;
        t->left = NULL;
        t->right = NULL;
    }
    else
    {
        if (x < t->info)
        TREEinsert(t->left, x);
        if (x > t->info)
            TREEinsert(t->right, x);
    }
}

void Displayinorder(struct node* t)
{
    //in order: (LC)(P)(RC)
    if (t != NULL)
    {
        Displayinorder(t->left); //LC
        printf("%d\t", t->info); //P
        Displayinorder(t->right); //RC
    }
}

struct node *root;

int main()
{
    TREEclear(root);

    TREEinsert(root, 5);
    TREEinsert(root, 8);
    TREEinsert(root, 2);
    TREEinsert(root, 6);

    Displayinorder(root);

    printf("\n\n");
    system("PAUSE");
    return 0;
 }

節點指針tTREEinsert的局部變量。 您對其所做的任何更改都不會反映在調用函數中。

您應該從調用函數將根指針的地址作為struct node *p傳遞。 遞歸時,分別傳遞當前節點的左指針或右指針的地址。

這是如何做:

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

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


void TREEclear(struct node *t)
{
    t = NULL;
}

void TREEinsert(struct node **t, int x)
{
    if (*t == NULL) {
        *t = malloc(sizeof(**t));
        // TODO: check allocation success

        (*t)->info = x;
        (*t)->left = NULL;
        (*t)->right = NULL;
    } else {
        if (x < (*t)->info) TREEinsert(&(*t)->left, x);
        if (x > (*t)->info) TREEinsert(&(*t)->right, x);
    }
}

void Displayinorder(struct node *t)
{
    if (t != NULL) {
        Displayinorder(t->left);
        printf("%d\n", t->info);
        Displayinorder(t->right);
    }

}

int main()
{
    struct node *root = NULL;

    TREEinsert(&root, 5);
    TREEinsert(&root, 8);
    TREEinsert(&root, 2);
    TREEinsert(&root, 6);

    Displayinorder(root);

    // TODO: need to free the tree nodes

    return 0;
}

請注意,您將&root傳遞給了插入功能,該功能可能需要對其進行修改,而只是將root傳遞給顯示功能,該功能僅需對其進行檢查。

我已經擺脫了您的TREEclear函數。 首先,它與原始TREEinsert存在相同的問題:它修改了局部變量,而在main中沒有任何改變。 其次,此函數應free所有節點,而不僅僅是將根節點設置為NULL (也就是說,您應該編寫此函數,以便可以在使用后取消分配樹。)

TREEinsert函數中的t是一個局部變量,因此從那里有兩個選擇:要么返回它並具有下面的代碼,要么參數t應該是一個struct node**
所以第一個選擇是:

 struct node* TREEinsert(struct node*  t, int x)
 {

  //insert in BST
  if(t == NULL)
  {

    t = (struct node*)malloc(sizeof(struct node));
    t->info = x;
    t->left = NULL;
    t->right = NULL;
    return t ;
  }
 else
  {
    if (x < t->info)
        return(TREEinsert(t->left, x));
    if (x > t->info)
       return(TREEinsert(t->right, x));
  }


}

第二種選擇是:

void TREEinsert(struct node **t, int x)
{
    if (*t == NULL) {
        *t = malloc(sizeof(**t));
        (*t)->info = x;
        (*t)->left = NULL;
        (*t)->right = NULL;
    } else {
        if (x < (*t)->info) 
            TREEinsert(&(*t)->left, x);
        if (x > (*t)->info) 
            TREEinsert(&(*t)->right, x);
    }
}

旁注:您可能要檢查您的malloc是否成功。

首先,函數TREEclear不會將原始的3的頭設置為零。 它處理一個頭部的副本。 原始磁頭未更改。

函數的名稱也令人困惑。 最好將其命名為TREEinit

它可以看起來如下

void TREEinit( struct node * *t)
{
    *t = NULL;
}

遞歸函數TREEinsert看起來像

void TREEinsert( struct node * *t, int x )
{
    if ( *t == NULL )
    {
        *t = ( struct node* )malloc( sizeof( struct node ) );
        ( *t )->info = x;
        ( *t )->left = NULL;
        ( *t )->right = NULL;
    }
    else if (  x < ( *t )->info )
    {       
        TREEinsert( &( *t )->left, x );
    }
    else if ( x > ( *t )->info )
    {
        TREEinsert( &( *t )->right, x );
    }
}

它應該被稱為

TREEinsert( &root, 5 );

考慮到您還需要編寫一個函數,該函數將為樹釋放所有分配的內存。

There is two way to fixed it
1. change the return type of Insert function 


Struct node * TREEinsert(){
              //your code

            return the local variable( t)
           }

2. If you don't want to change the return type of function then change the argument you are passing to Insert function
            TREEinsert(&root, intVariable);
    and in  TREEinsert(Struct node **,int);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM