简体   繁体   English

遍历n元树

[英]Traverse a n-ary tree

I am trying to understand pre-order traversal for an n-ary tree. 我试图了解n元树的预遍历遍历。 I have been reading and all the examples that i found use a Left Subtree and a right subtree but, in an n-ary tree, what is the left and what is the right subtree? 我一直在阅读,发现的所有示例都使用Left子树和Right子树,但是在n元树中,左边是什么,右边子树是什么? Can someone give a good explanation or a pseudo code? 有人可以给出很好的解释或伪代码吗?

Instead of thinking in specifics of left and right , as in: 而不是像leftright那样思考:

def preorder_traversal(tree)
   preorder_traversal(tree->left)
   preorder_traversal(tree->right)
end

What if instead, you thought of it as branches: 如果相反,您将其视为分支:

def preorder_traversal(tree)
   branches = tree->branches // e.g. [left, middle, next-to-right, right]
   branches.each do |branch|
     preorder_traversal(branch)
   end
end

Does that help you? 这对您有帮助吗?

Here is my C++ Implementation for level order traversal of n dimensional tree.You can tweak a bit for pre-order traversal.It assumes that one node can have at most 50 branches but you can always modify that part.Hope this helps and Please Let me know if you found any bug. 这是我对n维树的级别顺序遍历的C ++实现,您可以进行一些微调以进行预遍历,它假设一个节点最多可以有50个分支,但您始终可以修改该部分。希望这会有所帮助,请让我知道您是否发现任何错误。 Thanks! 谢谢!

#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define MAXSIZE 50

typedef struct node{
    int data;
    node** bArray;
} Node;


Node* newNode(int data){
    Node* newnode = new Node;
    newnode->data = data;
    newnode->bArray = new Node* [MAXSIZE];
    for(int i=0;i<50;i++){
        newnode->bArray[i] = NULL;
    }
    return newnode;
}

void mapFun(Node* root,int level,map<int,vector<int> >&m){
    if(root == NULL)return;
    m[level].push_back(root->data);

    for(int i=0;i<MAXSIZE;i++)
        mapFun(root->bArray[i],level+1,m);

    return;
}

void print_level(map<int,vector<int> >&m, int level){
    cout<<level<<"th level traversal is........";
    vector<int> v = m[level];
    vector<int>::iterator it;
    for(it=v.begin();it!=v.end();it++){
        cout<<*it<<" ";
    }

}

void levelOrder(Node* root, map<int,vector<int> >&m){
    mapFun(root,1,m);
    int mapsize = m.size();
    for(int i=1;i<=mapsize;i++){
        print_level(m,i);
        cout<<endl;
    }
}

int main(){
    int i;
    map<int,vector<int> >mymap;
    map<int,vector<int> > :: iterator it;
    Node *root = newNode(1);
    root->bArray[0] = newNode(2);
    root->bArray[1] = newNode(3);
    root->bArray[2] = newNode(4);
    Node* first = root->bArray[1];
    first->bArray[0] = newNode(8);
    first->bArray[1] = newNode(9);
    first->bArray[2] = newNode(10);
    Node *second = first->bArray[1];
    second->bArray[0] = newNode(55);
    second->bArray[1] = newNode(65);
    second->bArray[2] = newNode(75);
    cout << "level order traversal is \n";
    levelOrder(root,mymap);
    return 0;
}

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

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