添加一个节点,并在通用树中的两个给定节点之间找到路径成本,其中c 中的儿童列表
Add a node and Find Cost of Path between two given nodes in a Generic Tree with List of Children in C++
我需要在列表中包含的孩子的通用树中制作一个添加节点函数。有两个结构在树上定义一个节点(具有标签,其与父亲之间的重量,以及指向其孩子名单的指针(和一个孩子列表中的孩子(该指针指向指向子节点,一个列表中的下一个元素(:
struct tree::treeNode {
Label label;
Weight weight;
childrenList children;
}; //(typedef'ed as Tree)
struct tree::childrenListElem {
treeNode* child;
childrenListElem* next;
};
当树是空的(添加根(时,我已经制作的代码已经制作了作品。当我尝试将节点添加到非空树时,我正在陷入运行时错误。
Output tree::addElem(const Label labelOfNodeInTree, const Label labelOfNodeToAdd, const Weight w, Tree& t) {
if ((labelOfNodeInTree == emptyLabel) && isEmpty(t)) {
t = createNode(labelOfNodeToAdd, emptyWeight);
return OK;
}
if (((labelOfNodeInTree == emptyLabel) && !isEmpty(t)) ||
((labelOfNodeInTree != emptyLabel) && isEmpty(t)))
return FAILED;
if (member(labelOfNodeToAdd, t))
return ALREADY_PRESENT;
Tree v = getNode(labelOfNodeInTree, t); //here is where the problems begin to rise
if (v==emptyTree)
return FAILED;
else {
g = createNode(labelOfNodeToAdd, w);
v->children->next->child = g;
g = t;
return OK;
}
}
这是我实施的Createment,GetNode,会员和ISEMPTY功能:
bool tree::isEmpty(const Tree& t)
{
return (t==emptyTree);
}
Tree getNode(const Label & l, const Tree t)
{
Tree aux = t;
while (!isEmpty(aux)) {
if (aux->label == l)
return aux;
aux = aux->children->next->child;
}
return emptyTree;
}
Tree createNode(const Label l, Weight w)
{
Tree t = new treeNode;
t->label = l;
t->weight = w;
t->children = emptyChildrenList;
return t;
}
编辑:我已经按照函数建议修改了成员函数:它仅在一个单个节点上工作,一个节点是输入给出的,而不是整个树上:只要我必须搜索从输入给出的树(t节点(的儿童列表中给定节点。当我试图分析孩子的孩子时,我不工作(横向存在的其他节点上的树(。这是现在的成员函数:
bool tree::member(const Label l, const Tree& t) {
if (isEmpty(t)) return false;
if (t->label == l) return true;
for (childrenList aux = t->children; aux; aux = aux->next) {
Tree g = aux->child;
if (g->label == l)
return true;
g->children = g->children->next;
}
return false;
}
为了使函数搜索给定节点子女列表中的给定标签我该怎么办?
我认为问题可能在getNode辅助函数中,在成员中或在Addelem函数中的最后一个中,我需要找到一种方法,以便labeLofNodetoAdddd成为LabeLofNodeIntree的孩子之一。我是否正确地将其添加到列表中?getNode函数是否由于aux = aux->children->next->child;
行是否不良?我在想象孩子列表时遇到了问题,因此我不确定如何分析每个元素(检查它是否是getNode中给定标签识别的节点,或检查它是否与成员一起在He Tree中存在(。
编辑2:如果可能的话,我想介绍一个最后一个功能(以找到2个给定节点之间的路径的成本(权重(( - 因为它使用此处已经实现的功能。<<<<<<<<<<
Weight tree::pathCost(const Label from, const Label to, const Tree& t)
{
int len = 0;
Tree da = getNode(from, t);
Tree a = getNode(to, t);
if (da == emptyTree || a == emptyTree)
return notExistingPath;
if (da == a)
return 0;
for (childrenList aux = t->children; aux; aux = aux->next) {
Tree n = aux->child;
if (aux->child == a) {
return 0;
}
len += n->weight;
}
return len;
}
每当我尝试计算一条路径时,它碰巧给我问题 - 总和是一个问题吗?还是它不能正确横向?
member
的正确实现可能看起来像:
bool tree::member(const Label l, const Tree& t) {
if (isEmpty(t)) return false;
if (t->label == l) return true;
for (childrenList aux = t->children; aux; aux = aux->next) {
if (member(l, aux->child))
return true;
}
return false;
}
for循环散步节点的孩子(例如一个级别的一个级别(,递归member
呼叫负责下一个级别。
getNode
遵循相同的模式,但返回 Tree
:
Tree getNode(const Label & l, const Tree t)
{
if (isEmpty(t)) return emptyTree;
if (t->label == l) return const_cast<Tree>(t);
for (childrenList aux = t->children; aux; aux = aux->next) {
Tree candidate = getNode(l, aux->child);
if (!isEmpty(candidate))
return candidate;
}
return emptyTree;
}
那时您也可以重新进来member
为:
bool tree::member(const Label l, const Tree& t) {
return !isEmpty(getNode(l, t));
}
编辑:addElem
的最后一个分支也是错误的:
Output tree::addElem(const Label labelOfNodeInTree, const Label labelOfNodeToAdd, const Weight w, Tree& t) {
if ((labelOfNodeInTree == emptyLabel) && isEmpty(t)) {
t = createNode(labelOfNodeToAdd, emptyWeight);
return OK;
}
if (((labelOfNodeInTree == emptyLabel) && !isEmpty(t)) ||
((labelOfNodeInTree != emptyLabel) && isEmpty(t)))
return FAILED;
if (member(labelOfNodeToAdd, t))
return ALREADY_PRESENT;
Tree v = getNode(labelOfNodeInTree, t);
if (v==emptyTree)
return FAILED;
else {
// Get a reference to the first null pointer in the linked list
childrenListElem*& aux = v->children;
while (aux) {
aux = aux->next;
}
// Append a new element by overwriting the pointer
aux = new childrenListElem;
aux->child = createNode(labelOfNodeToAdd, w);
aux->next = nullptr;
return OK;
}
}
- C++A*算法并不总是在路径中具有目标节点
- 使用 Dijkstra 算法跟踪两个节点之间的最短路径
- 如何仅在 2 个节点之间获取最短路径,给定邻接列表有向图?
- A*路径查找-如何有效地更新openNodes优先级队列中的节点值
- 计算路径根到节点的总和等于一定数字的数量
- 在由邻接列表表示的树中查找节点到另一个给定节点之间的路径
- 添加一个节点,并在通用树中的两个给定节点之间找到路径成本,其中c 中的儿童列表
- 查找树(不属于任何特定类型的简单连接树)中两个节点之间的路径
- 具有最大不同节点的根到叶路径
- 沿着路径查找节点数量
- 使用 Dijkstra 算法计算两个节点之间的最短路径
- 使用路径调查访问网格中的所有节点
- 使用 ros launch 创建一个目录,然后将其路径传递给不同的节点
- 打印与BST的直径相对应的节点所包含的路径
- 在二进制搜索树中显示到节点的路径
- 使用 BFS 查找 2 个节点之间的最短路径
- 找到最大路径总和,我们可以在二叉树中的任何节点开始和结束
- 如何在C++中将文件路径作为节点添加到树或堆栈中
- 用BFS算法求两节点间的最短路径
- 显示属于二叉搜索树深度路径的节点