简体   繁体   English

C++ - 在 n 叉树上迭代

[英]C++ - iteration over a n-ary tree

This is the function with iteration algorithm over a n-ary tree, given a name , and use it to find the parent tree, return parent tree's data if found, "ZERO" if no parent is found, and "NA" if the name is not in any tree in the Tree<string>* vector.这是 function 在 n 元树上的迭代算法,给定一个name ,并name它来查找父树,如果找到则返回parent tree's data ,如果没有找到父树,则返回"ZERO" ,如果没有找到父树,则返回"NA"不在Tree<string>*向量中的任何树中。 It works most of the time, but will occasionally give wrong output that "ZERO" , which a parent was supposed to be found, mostly in the leaves .它大部分时间都有效,但偶尔会给出错误的 output 那个"ZERO" ,应该找到父母,主要是在leaves中。

string getSource(const string name) const { // no recursion
    if (existInVector(name, trees)) { // vector of Tree<string>*
        queue<Tree<string>> treesQueue;
        vector<Tree<string>*>::const_iterator it = trees.begin();
        for (; it != trees.end(); ++it) { // for each tree
            treesQueue.push(**it); // push tree
            for (int i = 0; i < (**it).root->numChildren; ++i) // push children
                treesQueue.push((**it).root->children[i]);
            while (!treesQueue.empty()) {
                Tree<string> temp = treesQueue.front(); // pop front
                treesQueue.pop();
                for (int i = 0; i < temp.root->childCount; ++i) { // check
                    if (temp.root->children[i].root->data == name)
                        return temp.root->data;
                }
            }
            if (it == trees.end()-1 && treesQueue.empty())
                return "ZERO";
        }
    }
    return "NA";
}

Here is the class template of the tree:这是树的 class 模板:

template <class T>
class Tree {
private:
    Node<T>* root;
public:
    // ... member functions ...
};

template <class T>
class Node {
private:
    T data;
    int numChildren;
    Tree<T>* children; // Tree<T> array
};

What is the possible reason to get the wrong result sometimes?有时得到错误结果的可能原因是什么?

// example with wrong result
Tree<string> tree; // below is what is inside, root is Node "G", "H" is child of "G" and so on
G
\-H
  \-I
    \-J

tree.getSource("J") == "ZERO"; // Supposed to be "I"

You should push children of current node/tree you visit.您应该推送您访问的当前节点/树的子节点。

I also remove some copies.我还删除了一些副本。

std::string getSource(const std::string& name) const {
    if (!existInVector(name, trees)) { // vector of Tree<string>*
        return "NA";
    }
    std::queue<const Tree<std::string>*> treesQueue;
    for (const auto& tree : trees) {
        treesQueue.push(&tree);
        while (!treesQueue.empty()) {
            const auto& current = *treesQueue.front();
            treesQueue.pop();
            for (int i = 0; i != current.root->childCount; ++i) {
                const auto& child = current.root->children[i];
                if (child.root->data == name)
                    return current.root->data;
                treesQueue.push(&child);
            }
        }
    }
    return "ZERO";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM