簡體   English   中英

迭代二叉搜索樹插入C語言

[英]Iterative Binary Search Tree Insert in C

這似乎是一個簡單的問題,我看了另一個線程,似乎我在做同樣的事情,但是沒有結果。

這是我的代碼,迭代地插入二叉搜索樹,以及結構和如何創建一個新節點:

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

node *create_node(int data) {
    node *n = calloc(1, sizeof(node));

    n->data = data;
    return n;
}

node *BST_insert_iterative(node *root, int data) {
    node *temp = root;

    if (root == NULL)
        return create_node(data);

    while (temp != NULL) {
        if (data > temp->data)
            temp = temp->right;
        else
            temp = temp->left;
    }

    temp = create_node(data);

    return root;
}

當我打印樹時,我只收到第一個節點:

Inserting 8  into BST...
Inserting 50     into BST...
Inserting 74     into BST...
Inserting 59     into BST...
Inserting 31     into BST...
Inserting 73     into BST...
Inserting 45     into BST...
Inserting 79     into BST...
Inserting 24     into BST...
Inserting 10     into BST...
In order traversal
8
Height of tree: 0

但是,使用遞歸插入函數:

node *BST_insert(node *root, int data) {
    if (root == NULL)
        return create_node(data);

    if (root->data >= data)
        root->left = BST_insert(root->left, data);

    else (root->data < data)
        root->right = BST_insert(root->right, data);

    return root;
}

它工作得很好,我得到:

Inserting 8  into BST...
Inserting 50     into BST...
Inserting 74     into BST...
Inserting 59     into BST...
Inserting 31     into BST...
Inserting 73     into BST...
Inserting 45     into BST...
Inserting 79     into BST...
Inserting 24     into BST...
Inserting 10     into BST...
In order traversal
8
10
24
31
45
50
59
73
74
79
Height of tree: 4 

在插入函數中,不將新節點存儲到樹中。 您只需存儲到局部變量temp並始終返回root

您必須保留指向要更新的鏈接的指針,以便將新節點插入樹中或樹的根目錄中。 這是一個修改版本:

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

node *create_node(int data) {
    node *n = calloc(1, sizeof(node));
    n->data = data;
    n->left = n->right = NULL;
    return n;
}

node *BST_insert_iterative(node *root, int data) {
    node **pp = &root;

    while (*pp != NULL) {
        if (data > (*pp)->data)
            pp = &(*pp)->right;
        else
            pp = &(*pp)->left;
    }
    *pp = create_node(data);
    return root;
}

筆記:

  • 空樹沒有特殊情況。
  • 請注意,這種方法不足以保持樹的平衡。

另請注意,遞歸函數在看起來像冗余測試時會出現語法錯誤。 你應該這樣簡化它:

node *BST_insert(node *root, int data) {
    if (root == NULL)
        return create_node(data);

    if (root->data >= data) {
        root->left = BST_insert(root->left, data);
    } else {
        root->right = BST_insert(root->right, data);
    }
    return root;
}

Chqrlie的回答有效,但我想在不使用雙指針的情況下完成。

在意識到我的root沒有連接到新創建的節點后,我能夠做到這一點。

這是解決方案:

node *BST_insert_iterative(node *root, int data)
{
    node *temp = root;
    int condition = 1;

    if (root == NULL)
        return create_node(data);

    while (condition)
    {
        if (data > temp->data)
        {
            if (temp->right == NULL)
            {
                temp->right = create_node(data);
                condition = 0;
            }
            else
                temp = temp->right;
        }
        else
        {
            if (temp->left == NULL)
            {
                temp->left = create_node(data);
                condition = 0;
            }
            else
                temp = temp->left;
        }

    }

    return root;
}

暫無
暫無

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

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