添加一个节点,并在通用树中的两个给定节点之间找到路径成本,其中c 中的儿童列表

Add a node and Find Cost of Path between two given nodes in a Generic Tree with List of Children in C++

本文关键字:节点 路径 其中 列表 之间 一个 两个 添加      更新时间:2023-10-16

我需要在列表中包含的孩子的通用树中制作一个添加节点函数。有两个结构在树上定义一个节点(具有标签,其与父亲之间的重量,以及指向其孩子名单的指针(和一个孩子列表中的孩子(该指针指向指向子节点,一个列表中的下一个元素(:

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