简体   繁体   English

N元树的二进制表示

[英]Binary Representation of N-ary Tree

I am currently trying to make a tree structure with Nodes having an unknown amount of children but I also have to keep track of parent Nodes. 我目前正在尝试使用具有未知数量子节点的节点来制作树形结构,但我还必须跟踪父节点。 I looked at this question N-ary trees in C and made a structure similar to that advised in the link: 我查看了C语言中的N元树,并构造了与链接中建议的结构类似的结构:

template <class T>
struct treeNode {

public: 

T  * data;
treeNode *parent;
treeNode *kids; //left 
treeNode *siblings; //right


treeNode();
~treeNode();
treeNode(T data);
void treeInsert(T newItem);



};

It says that by making the tree in this way, it makes certain algorithms easier to code. 它说通过以这种方式制作树,可以使某些算法更易于编码。 However, I am having a difficult time figuring out how I would create insert() and search() methods for this structure, seeing that I need to keep track of parent nodes. 但是,我很难确定如何为该结构创建insert()和search()方法,因为我需要跟踪父节点。 Are there any suggestions as to how I may go about this? 关于我该如何处理,有什么建议吗?

EDIT: 编辑:

Here is my insert() method: 这是我的insert()方法:

template<class T>
bool NewTree<T>::insert( T *data, treeNode<T> * parent)
{
if(this->root == NULL)
{
    this->root = new treeNode<T>();
    this->root->data = data;
    this->root->parent = NULL;
    this->root->children = NULL;
}
else
{
    treeNode temp = new treenode<T>();
    temp.data = data;
    temp.parent = parent;
    parent->children->siblings = temp; // add node to siblings of parent's   children
    parent->children = temp; // add node to children of parent

}

}

Does this look correct? 这看起来正确吗?

With any tree structure, searching is going to use relatively the same algorithm (depth-first recursion if unsorted, or simple tree-searching if sorted (like binary search tree)). 对于任何树结构,搜索将使用相对相同的算法(如果未排序,则使用深度优先递归;如果排序,则使用简单的树搜索(例如二进制搜索树))。 When you insert, all you need to do is assign the new node's .parent to the parent. 插入时,您要做的就是将新节点的.parent分配给父节点。 Then assign the new node's .parent.child[1] to the new node (thus linking the parent to the child). 然后将新节点的.parent.child[1]分配给新节点(从而将父节点链接到子节点)。 Then, check the other children of the parent node to assign your new node's siblings (if any). 然后,检查父节点的其他子节点以分配新节点的兄弟姐妹(如果有)。

Okay, so here's some pseudocode (mostly like java -- sorry, it's what i've been writing today) that will implement node creation and the series of assignments to maintain it in a tree structure, using the second example in the link you provided: 好的,所以这里有一些伪代码(大多数情况下像java一样-抱歉,这是我今天写的),将使用您提供的链接中的第二个示例来实现节点创建和一系列分配以将其维护在树结构中:

(node source): (节点源):

class Node {
  // generic data store
  public int data;
  public Node parent;
  public Node siblings;
  public Node children;
}

(tree source): (树源):

class NewTree {
  // current node
  public Node current;
  // pointer to root node
  public Node root;

  // constructor here
  // create new node
  public boolean insert(int data) {
    // search for the node which is immediately BEFORE where the new node should go
    // remember that with duplicate data values, we can just put the new node in
    // front of the chain of siblings
    Node neighbor = this.search(data);
    // if we've found the node our new node should follow, create it with the 
    // found node as parent, otherwise we are creating the root node
    // so if we're creating the root node, we simply create it and then set its
    // children, siblings, and parent to null
    // i think this is the part you're having trouble with, so look below for a 
    // diagram for visual aid
  }

  public Node search(int target) {
    // basically we just iterate through the chain of siblings and/or children 
    // until this.current.children is null or this.current.siblings is null
    // we need to make certain that we also search the children of 
    // this.index.sibling that may exist (depth-first recursive search)
  }
}

When we find the spot (using search()) where our new node should go, we need to reassign the parent, children, and siblings "links" inside the new node to its new parent, children, and siblings. 当我们找到要使用新节点的位置(使用search())时,我们需要将新节点内的父代,子代和同级兄弟“链接”重新分配给新的父代,子代和同级兄弟。 For example, we take this: 例如,我们这样做:

A-|
|
B-C-|
| |
| F-G-|  
| |
| -
|
D-E-|
| |
- H-|
  |
  -

And we will insert a new node (X) where F is. 然后,我们将在F处插入一个新节点(X)。 This is just to illustrate how we reassign each of the new node's links. 这只是为了说明我们如何重新分配每个新节点的链接。 The finer details may be slightly different, but what's important here is the implementation example of the link reassignment: 较详细的信息可能会稍有不同,但是这里重要的是链接重新分配的实现示例:

A-|
|
B-C-|
| |
| X-F-G-|  
| | |
| - -
|
D-E-|
| |
- H-|
  |
  -

What we do is: 1) Create X. 2) Assign x.parent to c. 我们要做的是:1)创建X。2)将x.parent分配给c。 3) Reassign c.children to x. 3)将c.children重新分配给x。 4) Assign x.siblings to f. 4)将x.siblings分配给f。 This inserts a new node (be mindful that an insertion is different than a sort and your tree may need resorting if you explicitly require a specific ordering). 这将插入一个新节点(请注意,插入不同于排序,如果您明确要求特定的顺序,则树可能需要重新排序)。

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

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