跳到正文

个人记录

树的储存方法和遍历方法

coach_zhaxi约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年7月13日
更新
2026年7月13日
篇幅
约 7 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
struct AdjMatrix{//邻接矩阵
    int n;//节点数
    bool matrix[123][123];//矩阵
    bool vis[123];//标记数组
    AdjMatrix(){//输入
        memset(vis,0,sizeof(vis));
        cin>>n;
        for(int i=1;i<=n;i++){
            int x,y;cin>>x>>y;
            matrix[x][y]= true;
            matrix[y][x]= true;
        }
    }
    void show(){//显示矩阵
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++) cout<<matrix[i][j];
            cout<<endl;
        }
    }
    void dfs(int root){//深搜遍历
        cout<<root<<' ';
        vis[root]= true;
        for(int i=1;i<=n;i++)
            if(matrix[root][i] and !vis[i]) dfs(i);
    }
    void bfs(int root){//广搜遍历
        queue<int> q;
        q.push(root);
        while(!q.empty()){
            int head=q.front();
            q.pop();
            cout<<head<<' ';
            vis[head]= true;
            for(int i=1;i<=n;i++)
                if(matrix[head][i] and !vis[i]) q.push(i);
        }
    }
};

struct AdjList_V{
    int n;
    vector<int> matrix[123];
    bool vis[123];
    AdjList_V(){
        memset(vis,0,sizeof(vis));
        cin>>n;
        for(int i=1;i<n;i++){
            int x,y;cin>>x>>y;
            matrix[x].push_back(y);
            matrix[y].push_back(x);
        }
    }
    void show(){
        for(int i=1;i<=n;i++){
            cout<<i<<":\t";
            for(int j=0;j<matrix[i].size();j++) cout<<matrix[i][j]<<'\t';
            cout<<endl;
        }
    }
    void dfs(int root){
        cout<<root<<' ';
        vis[root]= true;
        for(int i=0;i<matrix[root].size();i++){
            int t=matrix[root][i];
            if(!vis[t]) dfs(t);
        }
    }
    void bfs(int root){
        queue<int> q;
        q.push(root);
        while(!q.empty()){
            int head=q.front();
            q.pop();
            cout<<head<<' ';
            vis[head]= true;
            for(int i=0;i<matrix[head].size();i++){
                int t=matrix[head][i];
                if(!vis[t]) q.push(t);
            }
        }
    }
};
struct AdjList_S{
    int n;
    bool vis[123];
    int head[123];
    int tot;
    struct node{
        int val,next;
    }edge[123*2];
    void init(){
        tot=0;
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
    }
    void add(int x,int y){
        edge[tot].val=y;
        edge[tot].next=head[x];
        head[x]=tot++;
    }
    void link(int x,int y){
        add(x,y);
        add(y,x);
    }
    AdjList_S(){
        init();
        cin>>n;
        for(int i=1;i<n;i++){
            int x,y;cin>>x>>y;
            link(x,y);
        }
    };
    void show(){
        for(int i=1;i<=n;i++){
            cout<<i<<":\t";
            for(int j=head[i];j!=-1;j=edge[j].next) cout<<edge[j].val<<'\t';
            cout<<endl;
        }
    }
    void dfs(int root){
        cout<<root<<' ';
        vis[root]= true;
        for(int i=head[root];i!=-1;i=edge[i].next){
            int t=edge[i].val;
            if(!vis[t]) dfs(t);
        }
    }
    void bfs(int root){
        queue<int> q;
        q.push(root);
        while(!q.empty()){
            int Head=q.front();
            q.pop();
            cout<<Head<<' ';
            vis[Head]= true;
            for(int i=head[Head];i!=-1;i=edge[i].next){
                int t=edge[i].val;
                if(!vis[t]) q.push(t);
            }
        }
    }
};

int main(){
    freopen("tree.in","r",stdin);
    AdjList_S a;
    a.show();
    a.bfs(1);
    return 0;
}
原文