如何使完整的树递归? [英] How do I make complete tree recursive?

查看:133
本文介绍了如何使完整的树递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使一个完整的树递归,但它按顺序随机添加元素。如何将此二叉搜索树转换为完整的树。在此先感谢。

I am trying to make a complete tree recursive but It randomly adds element in order. How do I convert this binary search tree to a complete tree. Thanks in advance.

@Override
public boolean offer(E item) {

    if(root == null){
        root = new Node<E>(item);
        root.left = null;
        root.left = null;
    }
    Node<E> newNode = new Node<E>(item);
    insert(root, item);

    return false;
}

public void insert(Node<E> root, E value) {
    if (compare(value, root.data) > 0) {
      if (root.left != null) {
        insert(root.left, value);
      } else {
        System.out.println("Inserted " + value + " to left of "
            + root.data);
        root.left = new Node(value);
      }
    } else if (compare(value, root.data) < 0) {
      if (root.right != null) {
        insert(root.right, value);
      } else {
        System.out.println("  Inserted " + value + " to right of "
            + root.data);
        root.right = new Node(value);
      }
    }
}

推荐答案

请参阅我对该问题的评论。将二进制树转换为完整树(您只能谈论完整二叉树)的请求是荒谬的。



这个术语有两个含义完整的二叉树,其中一个也用来代替更确定的术语完美的二叉树 http://en.wikipedia.org/wiki/Binary_tree [ ^ ]。



在这两种情况下,树仍然是二叉树;和代码是一样的。它的完美性或完整性取决于用于填充树的输入数据。算法保持不变。同样,你的使一棵完整的树递归的概念只是用词不当。 递归是算法的特征,而不是数据结构:

http://en.wikipedia .org / wiki /递归 [ ^ ],

http://en.wikipedia.org/wiki/Recursion_%28computer_science%29 [ ^ ]。



尽管许多数据结构本身与递归有机相关(例如分形,或者,以某种微不足道的方式,所有树结构,不仅仅是二叉树),说使树递归只是一种字沙拉 http:// en .wikipedia.org / wiki / Word_salad [ ^ ] )。



-SA
Please see my comment to the question. The request of converting a binary tree into a "complete tree" (you can only talk about complete binary tree) is the absurd.

There are two meanings of this term "complete binary tree", one of them is also used instead of more certain term "perfect binary tree": http://en.wikipedia.org/wiki/Binary_tree[^].

In both cases, a tree remains a binary tree; and the code is the same. Its "perfectness" or "completeness" depends in input data used to populate the tree. The algorithm remains the same. Likewise, your notion of "making a complete tree recursive" is just a misnomer. "Recursive" is a characteristic of an algorithm, not a data structure:
http://en.wikipedia.org/wiki/Recursive[^],
http://en.wikipedia.org/wiki/Recursion_%28computer_science%29[^].

Even though many data structures are themselves organically related to recursion (such as fractals, or, in some trivial way, all the tree structures, not only binary trees), saying to "make a tree recursive" is just a kind of word salad (http://en.wikipedia.org/wiki/Word_salad[^]).

—SA


Coder_Jack写道:
Coder_Jack wrote:

如何将这个二叉搜索树转换为完整的树?

How do I convert this binary search tree to a complete tree?





如果我错了请纠正我:代码你posted不包含任何搜索树算法。



我建议从这里开始:使用C#2.0对数据结构进行广泛的检查 [ ^ ]。理解如何编写算法来搜索二叉树可能会有所帮助。



Please, correct me if i'm wrong: the code you posted doesn't contains any "search tree" algorithm.

I'd suggest to start here: An Extensive Examination of Data Structures Using C# 2.0 [^]. It might help to understand how to write algorithm to search binary tree.


这篇关于如何使完整的树递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆