【问题标题】:Binary Representation of N-ary TreeN叉树的二进制表示
【发布时间】:2013-02-28 03:37:20
【问题描述】:

我目前正在尝试使用具有未知数量的子节点的树结构,但我还必须跟踪父节点。我查看了这个问题N-ary trees in C 并制作了类似于链接中建议的结构:

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



};

它说通过以这种方式制作树,可以使某些算法更容易编码。但是,我很难弄清楚如何为此结构创建 insert() 和 search() 方法,因为我需要跟踪父节点。有什么建议可以解决这个问题吗?

编辑:

这是我的 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

}

}

这看起来正确吗?

【问题讨论】:

    标签: c++ tree binary-tree


    【解决方案1】:

    对于任何树结构,搜索都将使用相对相同的算法(如果未排序则使用深度优先递归,如果已排序则使用简单的树搜索(如二叉搜索树))。插入时,您只需将新节点的.parent 分配给父节点。然后将新节点的.parent.child[1] 分配给新节点(从而将父节点链接到子节点)。然后,检查父节点的其他子节点以分配新节点的兄弟节点(如果有)。

    好的,这里有一些伪代码(主要像 java - 抱歉,这是我今天一直在写的),它将实现节点创建和一系列分配以将其维护在树结构中,使用第二个示例您提供的链接:

    (节点来源):

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

    (树源):

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

    当我们找到新节点应该去的位置(使用 search())时,我们需要将新节点内的父节点、子节点和兄弟节点“链接”重新分配给它的新父节点、子节点和兄弟节点。例如,我们采取这个:

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

    我们将在 F 所在的位置插入一个新节点 (X)。这只是为了说明我们如何重新分配每个新节点的链接。更精细的细节可能略有不同,但这里重要的是链接重新分配的实现示例:

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

    我们所做的是:1) 创建 X。2) 将 x.parent 分配给 c。 3) 将 c.children 重新分配给 x。 4) 将 x.siblings 分配给 f。这会插入一个新节点(请注意,插入不同于排序,如果您明确要求特定的排序,您的树可能需要重新排序)。

    【讨论】:

    • insert() 方法会因为我如何组织孩子/兄弟姐妹而有所不同吗?我的主要困惑是因为这里显示的表示:blog.mozilla.org/nnethercote/2012/03/07/n-ary-trees-in-c 其中左节点是导致其他兄弟姐妹的孩子 - 超过 2
    • 这取决于您如何组织它们。但是,该链接以一种奇怪的方式显示了非常低的儿童等级。通常,您将从左到右(或其他方式)组织它们,直到父级达到其最大子级,然后开始将下一个子级分配给该父级的兄弟姐妹。因此,在链接中,节点 2 将有子节点 5、6 和 7,节点 3 将有节点 8、9、10,节点 3 将有一个子节点:11。您应该有一个均匀分布的子节点(通常)...话虽这么说(在下一条评论中继续)
    • 如何组织孩子并不重要。即使您指定了一个奇怪的分配顺序(如链接中的绘图),插入的过程也是相同的:Object x = new node; x.parent = y; y.child = x; 等等。我在将“父”节点分配给子节点时犯了一个错误:每次创建节点时都必须这样做。
    • 我澄清了很多我的答案。但基本上,无论您在树中的哪个位置,将新子节点分配给父节点的过程都是相同的(即使在新节点是根节点的情况下)。 每次创建以下新节点时,您都​​必须完成以下动作:
    • 感谢您的图片/实施。我将试一试并实施它,如果遇到问题,我会提出更多问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 2012-01-05
    相关资源
    最近更新 更多