如何从普通树创建二叉树?

发布于 2024-09-05 16:44:51 字数 1303 浏览 4 评论 0原文

我必须解决java中BinaryTree类的以下构造函数:

BinaryTree(GeneralTree<T> aTree)

此方法应该从通用树(gt)创建一个BinaryTree(bt),如下所示:

每个顶点来自gt 将表示为 bt 中的叶子。

  • 如果gt是叶子,则bt将是与gt具有相同值的叶子
  • 如果gt不是叶子,则bt将被构造为空根,左子树(lt)和右子树(lr)。 Lt 是从 gt 最旧的子树(最左边的子树)创建的严格二叉树,lr 是从 gt 创建的严格二叉树,没有最左边的子树。

第一部分很琐碎,但第二部分给我带来了一些麻烦。我已经走到这一步了:

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

这是正确的方法吗?如果没有,你能想出更好的方法来解决这个问题吗?例如,这个解决方案给了我一堆根本没有数据的节点,我不知道这是问题本身的问题还是我的问题。

谢谢你!

I have to solve the following constructor for a BinaryTree class in java:

BinaryTree(GeneralTree<T> aTree)

This method should create a BinaryTree (bt) from a General Tree (gt) as follows:

Every Vertex from gt will be represented as a leaf in bt.

  • If gt is a leaf, then bt will be a leaf with the same value as 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). 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.

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!

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

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

发布评论

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

评论(2

此生挚爱伱 2024-09-12 16:44:51

问题是大多数树无法有效地简化为二叉树。
读了你的评论你就完全意识到了这一点。
以一棵具有根节点和 3 个子节点的树为例。没有直接的方法可以在不牺牲连接性的情况下创建二叉树。这就是那些空节点的来源。有了它们,一般树的结构就被保留了。您可以重建它,删除空节点并从两个子树重新组装树。

我没有调试过你的代码。如果它按照你所说的那样做,那么这是一个很好的解决方案。空节点存储了一般树的连通性信息。他们被允许在那里。

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. 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.

沉溺在你眼里的海 2024-09-12 16:44:51

还有另一种广为人知的方法可以从普通树中生成二叉树,并且没有“连接节点”。
这种方法可以这样理解:

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

这基本上将一般树的子节点列表表示为链表,并且每个节点都具有对其子节点的链表的引用。正如您所看到的,这在结构上相当于二叉树。

因此,在伪代码中(为了简单起见,省略了边缘情况)

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

当然,如果您需要具有您所描述的结构,那么这将是无用的,但总的来说,这是创建一个相当好的方法二叉树与普通树的区别。

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.

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