简体   繁体   English

如何从常规树创建二叉树?

[英]How to create a Binary Tree from a General Tree?

I have to solve the following constructor for a BinaryTree class in java: 我必须在java中为BinaryTree类解决以下构造函数:

BinaryTree(GeneralTree<T> aTree)

This method should create a BinaryTree (bt) from a General Tree (gt) as follows: 此方法应从常规树(gt)创建BinaryTree(bt ,如下所示:

Every Vertex from gt will be represented as a leaf in bt . 来自gt的每个顶点都将表示为bt中的叶子。

  • If gt is a leaf, then bt will be a leaf with the same value as gt 如果gt是一个叶子,那么bt将是一个与gt相同的叶子
  • If gt is not a leaf, then bt will be constructed as an empty root, a left subTree (lt) and a right subTree (lr). 如果gt不是叶子,那么bt将被构造为空根,左子树(lt)和右子树(lr)。 Lt is a stric binary tree created from the oldest subtree of gt (the left-most subtree) and lr is a stric binary tree created from gt without its left-most subtree. Lt是从最古老的子树gt(最左边的子树)创建的严格二叉树,而lr是从GT创建的严格二叉树,没有最左边的子树。

The frist part is trivial enough, but the second one is giving me some trouble. 第一部分是微不足道的,但第二部分给了我一些麻烦。 I've gotten this far: 我到目前为止:

public BinaryTree(GeneralTree<T> aTree){
        if (aTree.isLeaf()){
            root= new BinaryNode<T>(aTree.getRootData());
        }else{
            root= new BinaryNode<T>(null); // empty root
            LinkedList<GeneralTree<T>> childs = aTree.getChilds(); // Childs of the GT are implemented as a LinkedList of SubTrees
            child.begin(); //start iteration trough list
            BinaryTree<T> lt = new BinaryTree<T>(childs.element(0)); // first element = left-most child
            this.addLeftChild(lt);
            aTree.DeleteChild(hijos.elemento(0));
            BinaryTree<T> lr = new BinaryTree<T>(aTree);
            this.addRightChild(lr);
        }
    }

Is this the right way? 这是正确的方法吗? If not, can you think of a better way to solve this? 如果没有,你能想出更好的解决方法吗? This solution, for example, gives me a bunch of nodes with no data at all, I don't know if this is an issue of the problem itself or mine. 例如,这个解决方案给了我一堆没有数据的节点,我不知道这是问题本身还是我的问题。

Thank you! 谢谢!

The problem is that most trees cannot be validly reduced to a binary tree. 问题是大多数树无法有效地简化为二叉树。 Reading your comment you are fully aware of that. 阅读你的评论你完全清楚这一点。 Taking for example a tree with a root node with 3 children. 以例如具有3个子节点的根节点的树为例。 There is no direct way to make a binary tree out of this without sacrificing connectivity. 在不牺牲连接性的情况下,没有直接的方法可以从中创建二叉树。 That's where those empty nodes come from. 这就是那些空节点的来源。 With them, the structure of the general tree is preserved. 通过它们,可以保留一般树的结构。 You can reconstruct it, deleting the empty nodes and reassembling the tree from the two subtrees. 您可以重建它,删除空节点并从两个子树重新组装树。

I have not debugged your code. 我没有调试你的代码。 If it does what you said it would do, it is a good solution. 如果它按照你所说的那样做,那就是一个很好的解决方案。 Empty nodes sort of store the connectivity information of the general tree. 空节点排序存储一般树的连接信息。 They are allowed to be there. 他们被允许在那里。

There is another, widely known, way to make a binary tree from a general tree, with no "connectivity nodes". 另一种众所周知的方法是从一般树中生成二叉树,没有“连接节点”。 This method can be best understood like this: 这种方法可以这样理解:

Node{                Node{
 data;                data;
 first_child;   =>    left;
 next_sibling;        right; 
}                    }

This basically represents the list of children of the general tree as a linked list, with the addition of each node having a reference to the linked list of it's children. 这基本上将一般树的子项列表表示为链接列表,每个节点的添加都引用了它的子项的链接列表。 As you can see, this is structurally equivalent to a binary tree. 如您所见,这在结构上等同于二叉树。

So, in pseudocode (with edge cases omitted for simplicity) 因此,在伪代码中(为简单起见省略了边缘情况)

BinaryTree(gtree){
    root=BinaryNode(gtree.data,BinaryNode(gtree.children),null);
}

BinaryNode(List<gnode> sibs){
    BinaryNode(sibs.first.data,BinaryNode(sibs.first.children),BinaryTree(sibs.rest));
}

BinaryNode(data,left,right){
    data=data;
    left=left;
    right=right;
}

Of course, if you need to have the structure you described, this will be useless, but in general, this is a fairly good way to create a binary tree from a general tree. 当然,如果你需要拥有你描述的结构,这将是无用的,但一般来说,这是从一般树创建二叉树的一种相当好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM