码迷,mamicode.com
首页 > 其他好文 > 详细

CPP:Construct and visit the forest

时间:2020-05-04 19:35:37      阅读:47      评论:0      收藏:0      [点我收藏+]

标签:tchar   deb   tin   front   lag   iter   ++   while   false   

#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 << "\t" << "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 << "\t" << "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 << "\t" <<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 << "\t" << 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 << "\t" << 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

标签:tchar   deb   tin   front   lag   iter   ++   while   false   

原文地址:https://www.cnblogs.com/xinyueliu/p/12827683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!