【问题标题】:C - tree insertion into specific positionC - 树插入特定位置
【发布时间】:2021-05-01 01:36:44
【问题描述】:

我想编写一个函数来向树中添加一个节点,该函数将根、节点的值、我想要添加新节点的节点的值和一个数字作为参数将节点添加为左或右孩子(偶数 = 右孩子,奇数 = 左孩子)。 我不明白我错在哪里,它与 BST 的概念相同,只是其他条件。 我将不胜感激。

例如:

                             6
                            / \
                          3    20

Tnode* insert (Tnode* root , int data , int Father , int leftOrRight) ; 插入(根,50,6,2) 树将是:

                           6
                          / \
                         3   50
                              \
                               20
Tnode* insert(Tnode* root, int data , int father , int leftOrRight)
{
    /* If the tree is empty, return a new node */
    if (root == NULL)
        return newNode(data);
    if (root->data == father)
            if (leftOrRight % 2 == 0)
                root->right = insert(root->right , data , father , leftOrRight);
            else
                root->left = insert(root->left , data , father , leftOrRight) ;
    return root ;
}

 Tnode* newNode(int data)
{
    Tnode* node = (Tnode*)malloc(sizeof(Tnode));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

【问题讨论】:

  • 找到父节点后,可以新建一个节点,向左或向右插入,不要再递归,除非要插入到叶子节点。我认为这就是问题所在。

标签: c algorithm recursion tree


【解决方案1】:

如果你已经找到了应该输入数据的节点,你就不需要递归了。

Tnode* insert(Tnode* root, int data , int father , int leftOrRight)
{
    /* If the tree is empty, return a new node */
    if (root == NULL)
        return newNode(data);
    if (root->data == father)
    {
        Tnode * node = newNode(data);
        if (leftOrRight % 2 == 0)
        {
           node -> left = root -> right; // can be node -> right as well
           root -> right = node;
        }
        else
        {
           node -> left = root -> left; // can be node -> right as well
           root -> left = node;
        }
    }
    else
    {
       // if the current node is not the required node check the leaves
       insert(root->left , data , father , leftOrRight);
       insert(root->right, data , father , leftOrRight);
    }
    return root; 
}

【讨论】:

    【解决方案2】:

    您编写的代码的第一件事是仅检查树的根是否为父亲。我假设您正在尝试检查整棵树。所以你需要递归调用它的左右孩子的插入函数来搜索整个树的父亲。

    Tnode* insert(Tnode* root, int data , int father , int leftOrRight)
    {
        /* If the tree is empty, return a new node */
        if (root == NULL)
            return newNode(data);
        if (root->data == father)
                if (leftOrRight % 2 == 0)
                    root->right = insert(root->right , data , father , leftOrRight);
                else
                    root->left = insert(root->left , data , father , leftOrRight) ;
        insert(root->left , data , father , leftOrRight);
        insert(root->right, data , father , leftOrRight); 
        return root; 
    }
    
     Tnode* newNode(int data)
    {
        Tnode* node = (Tnode*)malloc(sizeof(Tnode));
        node->data = data;
        node->left = node->right = NULL;
        return node;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      相关资源
      最近更新 更多