二叉树插入(按顺序排序)

时间:2011-02-07 00:00:20

标签: c++ recursion tree binary-tree

我已经在网上搜索了这个问题的帮助,但我需要帮助。这对于二叉树来说并不是一个普通的插入问题,因为我们不能直接使用树结构本身。我的教授自己编写并给了我们可以用来编写与二叉树有关的函数的函数。因此,我不能使用节点和指针和东西。这也是用C ++。

无论如何,所以这里是我必须编写的递归函数的描述(以及我开始尝试处理问题)。请注意,它完全返回一个新树,它实际上并没有向现有树添加内容。

tree_t insert_tree(int elt, tree_t tree)
{
    /* 
    // REQUIRES; tree is a sorted binary tree
    // EFFECTS: returns a new tree with elt inserted at a leaf such that 
    //          the resulting tree is also a sorted binary tree.
    //
    //          for example, inserting 1 into the tree:
    //
    //                           4
    //                         /   \
    //                        /      \
    //                       2        5
    //                      / \      / \
    //                         3 
    //                        / \
    //
    // would yield
    //                           4
    //                         /   \
    //                        /      \
    //                       2        5
    //                      / \      / \
    //                     1   3 
    //                    / \ / \
    // 
    // Hint: an in-order traversal of a sorted binary tree is always a
    //       sorted list, and there is only one unique location for
    //       any element to be inserted.
    */

if (elt < elt(tree_left(tree)){
    return insert_tree(tree_left(left));
} else {
    return insert_tree(tree_right(right));
}
}

以下是我们可以使用的功能:

extern bool tree_isEmpty(tree_t tree);
    // EFFECTS: returns true if tree is empty, false otherwise

extern tree_t tree_make();
    // EFFECTS: creates an empty tree.

extern tree_t tree_make(int elt, tree_t left, tree_t right);
    // EFFECTS: creates a new tree, with elt as it's element, left as
    //          its left subtree, and right as its right subtree

extern int tree_elt(tree_t tree);
    // REQUIRES: tree is not empty
    // EFFECTS: returns the element at the top of tree.

extern tree_t tree_left(tree_t tree);
    // REQUIRES: tree is not empty
    // EFFECTS: returns the left subtree of tree

extern tree_t tree_right(tree_t tree);
    // REQUIRES: tree is not empty
    // EFFECTS: returns the right subtree of tree

extern void tree_print(tree_t tree);
    // MODIFIES: cout
    // EFFECTS: prints tree to cout.

1 个答案:

答案 0 :(得分:2)

插入零元素树很容易:

return tree_make(elt, tree_make(), tree_make());

插入单元素树也很容易:

tree_t new_node = tree_make(elt, tree_make(), tree_make());
if(elt < tree_elt(tree))
    return tree_make(tree_elt(tree), new_node, tree_right(tree));
else
    return tree_make(tree_elt(tree), tree_left(tree), new_node);

通常,要插入新元素,您需要以这种方式重新创建其所有父元素。


第2部分:递归

我们有基本案例(零元素树)。我们知道如何将新子树附加到现有树的根。

那么如何获得新的子树?那么,我们如何将元素插入当前子树呢?

以下代码将始终在树的最左侧附加新元素,但是一旦您理解它,这应该是无关紧要的:

tree_t tree_insert(int elt, tree_t tree)
{
    if(tree_empty(tree)) //base case
        return tree_make(elt, tree_make(), tree_make());
    else
        return tree_make( // make a new node
            tree_elt(tree) // with the same value as the current one
            tree_insert(elt, tree_left(tree)) //insert into the left subtree
            tree_right(tree) // keep the right subtree the same
            );
}