如何在C++中使用双(**)指针来处理一般的树数据结构
How do i use doubly(**) pointer in C++ for general tree data structure?
我有一个结构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管理的,因此这会有更多帮助。
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- 如何在 c++ 中处理 LinkedList 的指针
- C++在一个映射中存储不同的指针类型(并处理销毁)
- 处理从列表中删除指向对象的指针的"healthy"方法是什么?
- C 与 C++,处理 void** 指针
- 在这种情况下,我将如何继续 1 到 360 的"for loop"处理指针和地址?
- 用Cython包装C 类时处理指针
- GTK+ 如何处理指针
- 当不处理指针时,请调用子类(虚拟)函数(较晚绑定)
- 如果我确定只有一个线程一次处理指针/对象,则C/C 仍应使用同步
- 处理指针c++的分段错误(这次有更多代码)
- 用C++处理指针
- 当处理指针时,控制台中为*p1和p显示的不同值的含义
- C++地址处理(指针)
- 为什么使用mktime处理指针和非指针时difftime()会产生不同的结果?
- c++如何处理指针上的非虚函数调用
- 如何处理指针截断在ATLMFC包括文件
- 在处理指针时,vector的行为是否与数组相同?
- 处理指针数组