转换一般“家庭”树到二进制树(不是BST)

发布于 2025-01-29 07:32:00 字数 2592 浏览 3 评论 0原文

我目前正在考虑完成转换家庭类型的一般树(每个父母都有尽可能多的孩子)的不同方法,以使父母节点的左子女插入到左侧父母和父节点的合适孩子被插入(最古老的)子节点的右侧,依此类推。

这是从一般树转换时二进制树的结构: https://www.geeksforgeeks.org/convert-a-generic-generic-treeen-aray-aray-tree-tree-tree-tree-to-binary-tree/

我通过创建class来喂养我的数据通用树的对象GTNode添加节点并传递以下值:

//name = value1; numChildren = value2
GTNode gtn = new GTNode (String name, int numChildren);

构造函数没有问题,这纯粹是为了说明节点的主要重点,确实是家庭成员的名称。因此,这确实不能被二进制搜索树对其进行转换和分类,也不是我要创建的结果程序的重点。

我能够创建方法可以很好地穿越树,但是在转换为转换后的二进制树的转换过程中确实无济于事。

因此,我创建了一个代码的附加部分来创建数据的链接列表。我这样做的原因是要在列表中建立指针,这将指出正确的父母将孩子添加到。然后,将根据指针的指针将兄弟姐妹添加到最古老的兄弟姐妹的右侧,该指针基于我们的实时添加树中的节点的位置。到这个时候,您可以想象我在链接的列表遍历中有两个指针,沿树节点沿着三个指针。

我似乎无法在算法上取得良好的进度来从数据中创建这个二进制树。这是一个小示例代码,主要是伪代码,它基本上向您展示了我如何根据一般树数据将节点添加到转换风格的二进制树中。

p=q=first; //initialize to first node f the linked list of family data

//ex. [Tom, 2] [Marc,1] [Ron,0] [Billy, 0] where Marc and Ron are Tom's children, and Billy is Marc's child)
z=root; 

//I already established x, y = root earlier and moved pointer .next in order to add children nodes to the siblings

//here we establish x and y pointers for the tree and add our root node and first child node, which will always be to the left. 
//I left it out of the while loop, because I was running into problems with null pointers. 
{


while (p.next!=null && q.next!=null)
{
   for (i = n; i>0; i--) //int n = node.numChildren; was declared earlier and then decreased 
   // n-=1 to accommodate the first child being added to left of parent node; 
   {
   //add nodes left or right to parent node: still trying to figure out the appropriate condition 
   //statements to determine addLeft or addRight from node
   }
   //here we change the pointer of the GENERAL tree parent node based on the linked list
    // add new to binary tree to the right of the last child i.e. 
    y = new BTNode(p.value,p.numChildren);
    y = x.right;
    x = y.parent; 
    x = y; //move pointer along
    q = q.next; //q moves from Tom to Marc. 
    n = q.numChildren;
    //go to beginning of while loop and as you can see, if n>0 it iterates based on the number of children
    // if n=0 exits loop and creates a new node to the right
     }
}

我想知道这是否是可接受的可接受曲目。优化我不专注于这是一项艰巨的任务……但可以考虑到。

寻找伪代码或Java代码托运人,这些托运人将有助于沿着这些行建立算法,或者我的逻辑错误。同样,不寻求反对这样做的论点,因为我想看看它的外观。

谢谢!

I am currently considering different ways to accomplish the task of converting a family-type general tree (where each parent can have as many children as necessary) to a binary tree such that the left child of the parent node is inserted to the left of the parent and the right child of the parent node is inserted to the right of the (oldest) child node and so on.

This is the structure of how the binary tree must look like when converted from the general tree: https://www.geeksforgeeks.org/convert-a-generic-treen-array-tree-to-binary-tree/

I fed in my data by creating class object GTNode for the General Tree to add the nodes with the following values being passed:

//name = value1; numChildren = value2
GTNode gtn = new GTNode (String name, int numChildren);

No issues with constructor, this is purely to illustrated the main focus of the node is indeed the name of the family member. Thus this really can't be converted and sorted by a Binary Search Tree and is not the point of the resulting program I'm trying to create.

I was able to create methods to traverse the tree just fine, but it really doesn't help in the conversion process to the CONVERTED binary tree.

Therefore I created an additional section of code to create a linked list of the data. The reason why I did this was to establish pointers along the list which would point to the correct parent to add children to. Siblings would then be added to the right of the oldest sibling based on pointers based on where we were along real-time adding of nodes in the tree. By this time you can imagine I have two pointers going in my linked list traversal and three pointers along the tree nodes.

I can't seem to make good progress on an algorithm to create this binary tree from the data. here's a little sample code mostly pseudo code to show you basically how I'm adding nodes to the converted-style binary tree based on general tree data.

p=q=first; //initialize to first node f the linked list of family data

//ex. [Tom, 2] [Marc,1] [Ron,0] [Billy, 0] where Marc and Ron are Tom's children, and Billy is Marc's child)
z=root; 

//I already established x, y = root earlier and moved pointer .next in order to add children nodes to the siblings

//here we establish x and y pointers for the tree and add our root node and first child node, which will always be to the left. 
//I left it out of the while loop, because I was running into problems with null pointers. 
{


while (p.next!=null && q.next!=null)
{
   for (i = n; i>0; i--) //int n = node.numChildren; was declared earlier and then decreased 
   // n-=1 to accommodate the first child being added to left of parent node; 
   {
   //add nodes left or right to parent node: still trying to figure out the appropriate condition 
   //statements to determine addLeft or addRight from node
   }
   //here we change the pointer of the GENERAL tree parent node based on the linked list
    // add new to binary tree to the right of the last child i.e. 
    y = new BTNode(p.value,p.numChildren);
    y = x.right;
    x = y.parent; 
    x = y; //move pointer along
    q = q.next; //q moves from Tom to Marc. 
    n = q.numChildren;
    //go to beginning of while loop and as you can see, if n>0 it iterates based on the number of children
    // if n=0 exits loop and creates a new node to the right
     }
}

I'm wondering if this is an acceptable track to go down in terms of operability. Optimization I'm not as focused on because this is a difficult task... but could be taken into account.

Looking for pseudocode or java code shippers that would help in establishing an algorithm along these lines, or error in my logic. Again, not looking for an argument against doing a conversion like this because I want to see what it would look like as is.

Thanks!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文