C++ 使用访问者设计模式重复访问N元树中的子节点值

C++ 使用访问者设计模式重复访问N元树中的子节点值,c++,tree,C++,Tree,这棵树是什么样子的: A / \ \ AA AB AC / \ \ AAA ABA ABB 目前,使用阵列,我能够获得以下输出: A AA AAA AAA AAA AA AA A AB ABA ABA ABA AB ABB ABB ABB AB A AC AC AC A AA AAA AB ABA ABB 输出应该是什么样子的: A AA AAA AB ABA AC ABB 使用

这棵树是什么样子的:

          A
       /   \    \
     AA   AB    AC
     /     \     \
   AAA    ABA     ABB
目前,使用阵列,我能够获得以下输出:

A AA AAA AAA AAA AA AA A AB ABA ABA ABA AB ABB ABB ABB AB A AC AC AC
A AA AAA AB ABA ABB
输出应该是什么样子的:

A AA AAA AB ABA AC ABB
使用硬代码,我只能获得以下输出:

A AA AAA AAA AAA AA AA A AB ABA ABA ABA AB ABB ABB ABB AB A AC AC AC
A AA AAA AB ABA ABB
关于这个问题有什么建议吗?如何停止重复已读取的子节点?任何帮助都将不胜感激

如果您想运行和测试,下面是代码文件

main.cpp

#include "pch.h"
#include <iostream>
#include "NTree.h"
#include <string>
using namespace std;
int main()
{
    string A("A");
    string A1("AA");
    string A2("AB");
    string A3("AC");
    string AA1("AAA");
    string AB1("ABA");
    string AB2("ABB");
    typedef NTree<string, 3> NS3Tree;

    NS3Tree root(A);

    NS3Tree nodeA1(A1);
    NS3Tree nodeA2(A2);
    NS3Tree nodeA3(A3);
    NS3Tree nodeAA1(AA1);
    NS3Tree nodeAB1(AB1);
    NS3Tree nodeAB2(AB2);
    root.attachNTree(0, &nodeA1);
    root.attachNTree(1, &nodeA2);
    root.attachNTree(2, &nodeA3);
    root[0].attachNTree(0, &nodeAA1);
    root[1].attachNTree(0, &nodeAB1);
    root[1].attachNTree(1, &nodeAB2);

    cout << "root: " << root.key() << endl;
    cout << "root[0]: " << root[0].key() << endl;
    cout << "root[1]: " << root[1].key() << endl;
    cout << "root[2]: " << root[2].key() << endl;
    cout << "root[0][0]: " << root[0][0].key() << endl;
    cout << "root[1][0]: " << root[1][0].key() << endl;
    cout << "root[1][1]: " << root[1][1].key() << endl;


    //test traversal
    PreOrderVisitor<string> v1;
    PostOrderVisitor<string> v2;
    cout << "Pre-order traversal:" << endl;
    root.transverseDepthFirst(v1);
    cout << endl;
    cout << "Post-order traversal:" << endl;
    root.transverseDepthFirst(v2);
    return 0;
}
#包括“pch.h”
#包括
#包括“NTree.h”
#包括
使用名称空间std;
int main()
{
字符串A(“A”);
字符串A1(“AA”);
字符串A2(“AB”);
字符串A3(“AC”);
字符串AA1(“AAA”);
字符串AB1(“ABA”);
字符串AB2(“ABB”);
类型定义NTree NS3树;
NS3树根(A);
NS3树节点A1(A1);
NS3树节点A2(A2);
NS3树节点A3(A3);
NS3树节点A1(AA1);
NS3树节点1(AB1);
NS3树节点2(AB2);
根.attachNTree(0和nodeA1);
根。附子树(1,&nodeA2);
附子树(2,和nodeA3);
根[0]。附件树(0,&nodeAA1);
根目录[1]。附件树(0,&NodeB1);
根[1]。attachNTree(1,&NodeB2);
cout
模板void NTree::transverseDepthFirst(const TreeVisitor&aVisitor)

必须是:

template<class T,int N>
void NTree<T, N>::transverseDepthFirst(const TreeVisitor<T>& aVisitor)const {
    if (!isEmpty()) {
      aVisitor.preVisit(key());
      for (int i = 0; i < N ; i++) {
         fNodes[i]->transverseDepthFirst(aVisitor);
      }
      aVisitor.postVisit(key());
    }
}
警告:您的树不是

          A
       /   \    \
     AA   AB    AC
     /     \     \
   AAA    ABA     ABB
但是


因为ABB是附属于AB而不是AC的:
root[1][1]:ABB
而不是
root[2][0]:ABB

你有一个调试器来检查代码是如何工作的(或不是),你不知道吗`?如果“ABB”是“AC”的孩子,命名约定似乎很奇怪。@KennyOstrom不是,树画错了;-)(如果你想看我的答案)这就是我所暗示的。我们可以在代码中看到它附在我们期望从它的名字,但张贴的“期望值”“这也是错误的。@KennyOstrom绝对的,这就是为什么我在我的回答中对此提出警告:-)谢谢!也谢谢你提醒我树连接错误。你能解释一下for循环部分吗?@cswannabe transferencedepthfirst是错误的,因为这一切都取决于访问者,最好叫它transfers。我刚刚发现了这一点“横向”似乎有类似的意思。(在我还不知道这个词之前。)然而,它不是通常被称为“横向”吗?@Scheff yes对我来说可以是横向(就像法语一样),只是“深度优先”取决于访客
          A
       /   \    \
     AA   AB    AC
     /     \     \
   AAA    ABA     ABB
          A
       /  |  \
     AA   AB  AC
    /    /  \
   AAA  ABA ABB