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

发布于 2024-10-15 22:36:34 字数 2302 浏览 2 评论 0原文

我已经在互联网上搜索有关此问题的帮助,但我需要帮助。这并不完全是二叉树的普通插入问题,因为我们不能直接处理树结构本身。我的教授自己写了这篇文章,并为我们提供了可以用来编写与二叉树相关的函数的函数。因此,我无法使用节点和指针之类的东西。这也是 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.

I've scoured the internet for help on this problem but I need help. This isn't exactly an ordinary insertion problem for a binary tree, since we don't get to work directly with the tree structure itself. My prof wrote that himself and has given us functions we can use to write functions pertaining to binary trees. As such, I can't use nodes and pointers and things. Also this is in C++.

Anyway, so here is the description of the recursive function I have to write ( along with my starting attempt at working with the problem). Notice it returns a new tree entirely, it does not actually add something to the existing tree.

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));
}
}

And here are the functions we can use:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2024-10-22 22:36:34

插入到零元素树中很容易:

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
            );
}

Inserting into a zero-element tree is easy:

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

Inserting into a one-element tree is also easy:

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);

In general, to insert a new element, you'll need to recreate all of its parents in this manner.


Part 2: Recursion

We have our base case (a zero-element tree). And we know how to attach a new subtree to the root of our existing tree.

So how to get the new subtree? Well, how about we just insert the element into the current subtree?

The following code will always attach the new element at the far left of the tree, but that should be trivial to correct once you understand it:

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
            );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文