zoukankan      html  css  js  c++  java
  • 有根树的遍历和无根树的遍历

    有根树的遍历

    考察树的思想和dfs、bfs的实现,做不出来说明 dfs、bfs没有掌握 且 树的思想没有领悟。

    //有根树的遍历 
    #include <bits/stdc++.h>
    using namespace std;
    vector<int> to[200005];
    int n, a, vis[200005], q[200005];
    int cmp(int b, int c) { return b > c; }
    void ad(int u, int v) { to[u].push_back(v); }
    void dfs(int k) 
    {
        cout << k << ' ';
        for (int i = 0; i < to[k].size(); i++) {
            if (vis[to[k][i]] == 0) {
                vis[to[k][i]] = 1;
                dfs(to[k][i]);
            }
        }
    }
    void bfs(int k) 
    {
        int head = 1, tail = 1;
        q[tail++] = k;while (head < tail) {
            int now = q[head++];
            cout << now << " ";for (int i = 0; i < to[now].size(); i++) {
                if (vis[to[now][i]] == 0) {
                    vis[to[now][i]] = 1;
                    q[tail++] = to[now][i];
                }
            }
        }
    }
    int main() {
        cin >> n;
        for (int i = 1; i <= n - 1; i++) {
            cin >> a;
            ad(a, i);
            ad(i, a);
        }
        for (int i = 0; i < n; i++) sort(to[i].begin(), to[i].end(), cmp);
        vis[0] = 1;
        dfs(0);
        memset(vis, 0, sizeof(vis));
        cout << endl;
        vis[0] = 1;
        bfs(0);
        return 0;
    }

    无根树的遍历

     这个题和上一个有什么区别?没区别 =.=

    #include <bits/stdc++.h>
    using namespace std;
    vector<int> to[200005];
    int n, a, vis[200005], q[200005], start;
    int cmp(int b, int c) { return b > c; }
    void ad(int u, int v) { to[u].push_back(v); }
    void dfs(int k) 
    {
        cout << k << ' ';
        for (int i = 0; i < to[k].size(); i++) {
            if (vis[to[k][i]] == 0) {
                vis[to[k][i]] = 1;
                dfs(to[k][i]);
            }
        }
    }
    void bfs(int k) {
        int head = 1, tail = 1;
        q[tail++] = k;
        while (head < tail) {
            int now = q[head++];
            cout << now << " ";
            for (int i = 0; i < to[now].size(); i++) {
                if (vis[to[now][i]] == 0) {
                    vis[to[now][i]] = 1;
                    q[tail++] = to[now][i];
                }
            }
        }
    }
    int main() 
    {
        cin >> n;
        for (int i = 1; i <= n - 1; i++) {
            cin >> a;
            ad(a, i);
            ad(i, a);
        }
        cin >> start;
        for (int i = 0; i < n; i++) sort(to[i].begin(), to[i].end(), cmp);
        vis[start] = 1;
        dfs(start);
        memset(vis, 0, sizeof(vis));
        cout << endl;
        vis[start] = 1;
        bfs(start);
        return 0;
    }
    View Code
    满堂花醉三千客,一剑霜寒十四州
  • 相关阅读:
    JavaScript使用方法和技巧大全
    PHP JSON 数据解析代码
    效率较高的php下读取文本文件的代码
    PHP操作MongoDB 数据库
    安装mongo php拓展
    MongoDB与MySQL的插入、查询性能测试
    java字符数组char[]和字符串String之间的转换
    python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
    python操作txt文件中数据教程[2]-python提取txt文件
    python操作txt文件中数据教程[1]-使用python读写txt文件
  • 原文地址:https://www.cnblogs.com/phemiku/p/11426569.html
Copyright © 2011-2022 走看看