如何在C++中使用双(**)指针来处理一般的树数据结构

How do i use doubly(**) pointer in C++ for general tree data structure?

本文关键字:处理 指针 数据结构 C++      更新时间:2023-10-16

我有一个结构s:

struct s{
   int x;
  /********************************************************************
   *  NoOfchild doesn't represent maximum no of children for node s . 
   *  This represent no of children node s have at any given instance . 
   *********************************************************************/      
   int NoOfChild; 
   s  **child;
}

我想使用**来动态地声明指针数组。节点s被逐个添加到数组中。有任何方法可以实现这一点。这棵树将用于FpGrowth算法。

                                         *(0) 
                                           |
          _____________________________________________________________
          |                                |                          | 
       [* (1)                             *(2)                      *(3)]
          |                                |                          |
_______________                    _________________        __________________________
|    |    |   |                   |        |       |        |    |    |     |    |   |
[*   *    *   *]                  [*       *       *]      [*    *    *     *    *   *]

**表示节点s。我不想declare all children of a node at the same time,即当它是required时,我想添加子节点one by one。则节点1被添加为根的子节点,如果需要则节点2被添加,以此类推。[****]表示节点x的子节点。

编辑:
对于给定的节点,人们假设NoOfChild为maximum no of a child,但这不是真的,这里NoOfChild表示一个节点在给定实例下有多少个子节点,它可能会根据要求或时间而变化
解释:
最初,节点0已初始化,因此它没有(0)个子节点
则节点1被添加为节点0的子节点,使得o->NoOfChild=1并且1->NoOfChild=0
则节点[*]被添加为节点1的子节点,使得0->NoOfChild=1并且1->NoOfChild=1
则2被添加为节点0的子节点,使得0->NoOfChild=2并且1->NoOfChild=1
等等

编辑:
最后使用CCD_ 5。

对于通用树数据结构,您可以使用:-

 struct tree{
 int element;
 struct tree *firstchild;
 struct tree *nextsibling;
 };

元素包含要在节点处插入的数据。

FirstChild包含节点的第一个子节点。

nextbling包含同一父节点的另一个子节点。示例:-

   A
 B  C  D
EF  G     H

那么A->firstchild = B; B->nextsibling=C; C->nextsibling=D; B->firstchild=E; E->nextsibling=F; C->firstchild=g; D->firstchild=H;

其他未指定的值可以取为NULL;

第一个答案是:你没有。像注释中的每个人一样使用容器类。

第二个答案是:动态分配:

void addNewChild(s *into, s* new_child)
{
   s **tmp_s = new s*[into->NoOfChild+1];    ///< will throw if allocation fails
   for(int i=0; i<into->NoOfChild; i++) tmps[i] = into->child[i];  ///< use a memcpy instead
   tmps[into->NoOfChild++] = new_child;
   s **del_s = into->child;
   into->child = tmp_s;
   delete[] del_s;
}

最后:不要这样做。根据孩子的父母数量,使用std::vector<s>std::vector<s*>

纯c版本:

struct node
{
    int key;
    int noOfChild;
    struct node** childrenArray;
};
struct node* newNode(int key, int noOfChild)
{
    int i;
    struct node* node = (struct node*) malloc(sizeof(struct node));
    node->key = key;
    node->noOfChild = noOfChild;
    node->childrenArray = (struct node**) malloc(noOfChild * sizeof(struct node*));
    for(i=0;i<noOfChild;i++)
    {
        node->childrenArray[i] = NULL;
    }
    return(node);
}

由于您标记了c++:

#include <vector>
#include <memory>
#include <algorithm>
#include <iostream>
template <class ValueType>
class VariadicTree
{
public:
    VariadicTree(const ValueType& value) : m_value(value), m_size(0)
    {
    }
    VariadicTree<ValueType>& addNode(const ValueType& value)
    {
        m_children.emplace_back(new VariadicTree<ValueType>(value));
        ++m_size;
        return *m_children.back();
    }
    bool leaf()
    {
        return std::all_of(m_children.begin(), m_children.end(),
                           [&](const std::unique_ptr<VariadicTree<ValueType>>& ptr)
                              {return ptr == nullptr;});
    }
    size_t size()
    {
        return m_size;
    }
    const ValueType& value()
    {
        return m_value;
    }
private:
    size_t m_size;
    const ValueType& m_value;
    std::vector<std::unique_ptr<VariadicTree<ValueType>>> m_children;
};
int main()
{
    VariadicTree<int> root(5);
    auto& c1 = root.addNode(4);
    auto& c2 = root.addNode(6);
    auto& c3 = root.addNode(2);
    auto& c11 = c1.addNode(2);
    std::cout << root.leaf() << "n";
    std::cout << c11.leaf() << "n";
    std::cout << root.size() << "n";
    std::cout << c1.size() << "n";
    std::cout << c11.size() << "n";
    return 0;
}

所有权可能可以更优雅地处理,但这应该用于演示目的。

我使用解决了问题

struct s{
   int x;
   vector<s*> child ;
}

由于所有指针/引用都是由STL管理的,因此这会有更多帮助。