CPP:Construct and visit the forest

Posted xinyueliu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CPP:Construct and visit the forest相关的知识,希望对你有一定的参考价值。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <list>
#include <queue>
#include <stack>

typedef struct forest {
    std::string nodeData;
    std::string parentNodeData;
    std::list<std::string> *forestNodelist;
} forest_t, *lpforest_t;

/*
bool operator == (const forest &f1, const forest &f2) {
    bool flag = false;
    if (f1.nodeData == f2.nodeData && f1.parentNodeData == f2.parentNodeData) {
        flag = true;
    }
    return flag;
}
void printDataList(std::list<struct forest*> forestList) {
    std::list<struct forest*>::iterator iter;
    std::list<std::string>::iterator iterNodeList;
    struct forest* pForestNode = NULL;
    std::list<std::string> *forestNodelist;
    for (iter = forestList.begin(); iter != forestList.end(); iter++) {
        pForestNode = (*iter);
        std::cout << "nodeData:" << pForestNode->nodeData << ",parentNodeData" << pForestNode->parentNodeData << std::endl;
        forestNodelist = pForestNode->forestNodelist;
        if (NULL == forestNodelist) {
            continue;
        }

        for (iterNodeList = (*forestNodelist).begin(); iterNodeList != (*forestNodelist).end(); ++iterNodeList) {
            std::cout << "	" << "nodeList:" << *iterNodeList << std::endl;
        }
    }
}
*/

void initDataMap(std::map<std::string, struct forest*> &forestMap) {
    forestMap.insert(std::pair<std::string, lpforest_t>("103121", new forest_t({ "103121", "10312", NULL }) ));
    forestMap.insert(std::pair<std::string, lpforest_t>("103122", new forest_t({ "103122", "10312", NULL }) ));

    forestMap.insert(std::pair<std::string, lpforest_t>("10311", new forest_t({ "10311", "1031", NULL }) ));
    forestMap.insert(std::pair<std::string, lpforest_t>("10312", new forest_t({ "10312", "1031", NULL }) ));

    forestMap.insert(std::pair<std::string, lpforest_t>("1011", new forest_t({ "1011", "101", NULL }) ));
    forestMap.insert(std::pair<std::string, lpforest_t>("1012", new forest_t({ "1012", "101", NULL }) ));
    forestMap.insert(std::pair<std::string, lpforest_t>("1031", new forest_t({ "1031", "103", NULL }) ));
    forestMap.insert(std::pair<std::string, lpforest_t>("1033", new forest_t({ "1033", "103", NULL }) ));

    forestMap.insert(std::pair<std::string, lpforest_t>("101", new forest_t({ "101", "10", NULL }) ));
    forestMap.insert(std::pair<std::string, lpforest_t>("102", new forest_t({ "102", "10", NULL }) ));
    forestMap.insert(std::pair<std::string, lpforest_t>("103", new forest_t({ "103", "10", NULL }) ));
    forestMap.insert(std::pair<std::string, lpforest_t>("104", new forest_t({ "104", "10", NULL }) ));

    forestMap.insert(std::pair<std::string, lpforest_t>("10", new forest_t({ "10", "", NULL })));
}


void initSubNodeList(std::map<std::string, struct forest*> &forestMap, std::string node, std::list<std::string> * &forestNodelist) {
    std::map<std::string, struct forest*>::iterator iter;
    struct forest* pForestNode = NULL;
    for (iter = forestMap.begin(); iter != forestMap.end(); iter++) {
        pForestNode = iter->second;
        std::cout << "nodeData:" << pForestNode->nodeData << ",parentNodeData" << pForestNode->parentNodeData << std::endl;
        if (node == pForestNode->parentNodeData) {
            if (NULL == forestNodelist) {
                forestNodelist = new std::list<std::string>();
            }
            forestNodelist->push_back(std::string(pForestNode->nodeData));
        }
    }
}

void initForestNodelist(std::map<std::string, struct forest*> &forestMap){
    std::map<std::string, struct forest*>::iterator iter;
    std::list<std::string>::iterator iterNodeList;
    struct forest* pForestNode = NULL;
    std::list<std::string> *forestNodelist;
    for (iter = forestMap.begin(); iter != forestMap.end(); iter++) {
        pForestNode = iter->second;
        std::cout << "nodeData:" << pForestNode->nodeData << ",parentNodeData" << pForestNode->parentNodeData << std::endl;
        initSubNodeList(forestMap, pForestNode->nodeData, pForestNode->forestNodelist);
    }
}


void printDataMap(std::map<std::string, struct forest*> &forestMap) {
    std::map<std::string, struct forest*>::iterator iter;
    std::list<std::string>::iterator iterNodeList;
    struct forest* pForestNode = NULL;
    std::list<std::string> *forestNodelist;
    for (iter = forestMap.begin(); iter != forestMap.end(); iter++) {
        pForestNode = iter->second;
        std::cout << "nodeData:" << pForestNode->nodeData << ",parentNodeData:" << pForestNode->parentNodeData << std::endl;
        forestNodelist = pForestNode->forestNodelist;
        if (NULL == forestNodelist) {
            continue;
        }

        for (iterNodeList = (*forestNodelist).begin(); iterNodeList != (*forestNodelist).end(); ++iterNodeList) {
            std::cout << "	" << "nodeList:" << *iterNodeList << std::endl;
        }
    }
}


void printParentNode(std::map<std::string, struct forest*> &forestMap, std::string node) {
    std::map<std::string, struct forest*>::iterator iterFound;
    struct forest* pForestNode = NULL;
    std::cout << "currentNode:" << node << std::endl;
    while (forestMap.end() != (iterFound = forestMap.find(node))) {
        pForestNode = iterFound->second;
        std::cout << "	" <<pForestNode->parentNodeData << std::endl;
        node = pForestNode->parentNodeData;
    }
}


void printSubNodeByBreadth(std::map<std::string, struct forest*> &forestMap, std::string node) {
    std::map<std::string, struct forest*>::iterator iterFound = forestMap.find(node);
    std::queue<std::string> q;
    q.push(iterFound->first);

    std::cout << "currentNode:" << node << std::endl;

    std::list<std::string>::iterator iterNodeList;
    std::list<std::string> *forestNodelist;
    while (!q.empty()) {
        node = q.front();
        q.pop();
        std::cout << "	" << node << std::endl;

        iterFound = forestMap.find(node);
        forestNodelist = (iterFound->second)->forestNodelist;
        if (NULL == forestNodelist) {
            continue;
        }

        for (iterNodeList = (*forestNodelist).begin(); iterNodeList != (*forestNodelist).end(); ++iterNodeList) {
            q.push(*iterNodeList);
        }
    }
}

void printSubNodeByDeep(std::map<std::string, struct forest*> &forestMap, std::string node) {
    std::map<std::string, struct forest*>::iterator iterFound = forestMap.find(node);
    std::stack<std::string> s;
    s.push(iterFound->first);

    std::cout << "currentNode:" << node << std::endl;

    std::list<std::string>::reverse_iterator iterNodeList;
    std::list<std::string> *forestNodelist;
    while (!s.empty()) {
        node = s.top();
        s.pop();
        std::cout << "	" << node << std::endl;

        iterFound = forestMap.find(node);
        forestNodelist = (iterFound->second)->forestNodelist;
        if (NULL == forestNodelist) {
            continue;
        }

        for (iterNodeList = (*forestNodelist).rbegin(); iterNodeList != (*forestNodelist).rend(); ++iterNodeList) {
            s.push(*iterNodeList);
        }
    }
}


int _tmain(int argc, _TCHAR* argv[]) {
    std::map<std::string, struct forest*> forestMap;
    initDataMap(forestMap);
    printDataMap(forestMap);

    initForestNodelist(forestMap);
    printDataMap(forestMap);

    //printParentNode(forestMap, "103121");
    printSubNodeByBreadth(forestMap, "10");
    printSubNodeByDeep(forestMap, "10");

    return 0;
}

 

以上是关于CPP:Construct and visit the forest的主要内容,如果未能解决你的问题,请参考以下文章

How to solve the problem that Github can't visit in China?

841. Keys and Rooms —— weekly contest 86

二叉树先序中序后序的递归算法---《数据结构》

二叉树遍历的操作与实现

MySQL:优化格式化日期的左连接

visit(卢卡斯定理&&中国剩余定理)