您的位置:首页 > 理论基础 > 数据结构算法

数据结构-树形结构实例

2015-10-03 21:29 531 查看
题目介绍:

M*N的坐标方格(左下点坐标(0,0),右上点坐标(M,N),自己在图纸上画下),输出从(0,0)点到(M,N)点的所有路径(规则:只能向右和向上走)

程序源码:

// C++程序  二叉树结构

#include <IOSTREAM>

#include <MALLOC.H>

#include <VECTOR>

using namespace std;

#define M 5

#define N 5

struct nodeData

{
int x;
int y;

};

struct CTreeNode

{

    CTreeNode *lChild;
CTreeNode *rChild;

    nodeData mData;

};

CTreeNode *createTree(CTreeNode *pNode,int x,int y);

void findAllPath(CTreeNode *pRoot, vector<int> xpath,vector<int> ypath);

int main()

{
CTreeNode *rootNode;
rootNode = NULL;
rootNode = createTree(rootNode,0,0);
vector<int> xPath;
vector<int> yPath;
findAllPath(rootNode,xPath,yPath);
system("pause");  
return 0;

}

CTreeNode *createTree(CTreeNode *pNode,int x,int y)

{
if(x==M&&y==N)
{
pNode = NULL;
}
if(x<M||y<N)
{
pNode = (CTreeNode *)malloc(sizeof(CTreeNode));
pNode->lChild = NULL;
pNode->rChild = NULL;
pNode->mData.x = x;
pNode->mData.y = y;
if(x<M)
 pNode->lChild = createTree(pNode->lChild,x+1,y);
if(y<N)
 pNode->rChild = createTree(pNode->rChild,x,y+1);
}
return pNode;

}

void findAllPath(CTreeNode *pRoot, vector<int> xPath, vector<int> yPath)  

{  

    if (pRoot != NULL)  

    {  

        xPath.push_back(pRoot->mData.x);  
yPath.push_back(pRoot->mData.y);

        if (pRoot->lChild == NULL && pRoot->rChild == NULL)  

        { 
vector<int>::iterator iterx=xPath.begin();
vector<int>::iterator itery=yPath.begin();

            for (; iterx!=xPath.end(); iterx++,itery++)  

            {  

                cout<<"("<<*iterx<<","<<*itery<<") ";  

            }  

            cout << endl;

            return;  

        }  

        else  

        {  

            findAllPath(pRoot->lChild, xPath,yPath);  

            findAllPath(pRoot->rChild, xPath,yPath);  

        }  

    }  

}  

扩展:

 M*N的坐标方格,有对角线,输出从(0,0)点到(M,N)点的所有路径(规则:只能向上,向右,向斜右上方向)

程序源码

//C++ 三叉树结构

#include <IOSTREAM>

#include <MALLOC.H>

#include <VECTOR>

using namespace std;

#define M 5

#define N 5

struct nodeData

{
int x;
int y;

};

struct CTreeNode

{

    CTreeNode *lChild;
CTreeNode *mChild;
CTreeNode *rChild;

    nodeData mData;

};

CTreeNode *createTree(CTreeNode *pNode,int x,int y);

void findAllPath(CTreeNode *pRoot, vector<int> xpath,vector<int> ypath);

int main()

{
CTreeNode *rootNode;
rootNode = NULL;
rootNode = createTree(rootNode,0,0);
vector<int> xPath;
vector<int> yPath;
findAllPath(rootNode,xPath,yPath);
system("pause");  
return 0;

}

CTreeNode *createTree(CTreeNode *pNode,int x,int y)

{
if(x==M&&y==N)
{
pNode = NULL;
}
if(x<M||y<N)
{
pNode = (CTreeNode *)malloc(sizeof(CTreeNode));
pNode->lChild = NULL;
pNode->mChild = NULL;
pNode->rChild = NULL;
pNode->mData.x = x;
pNode->mData.y = y;
if(x<M)
 pNode->lChild = createTree(pNode->lChild,x+1,y);
if(x<M&&y<N)
 pNode->mChild = createTree(pNode->mChild,x+1,y+1);
if(y<N)
 pNode->rChild = createTree(pNode->rChild,x,y+1);
}
return pNode;

}

void findAllPath(CTreeNode *pRoot, vector<int> xPath, vector<int> yPath)  

{  

    if (pRoot != NULL)  

    {  

        xPath.push_back(pRoot->mData.x);  
yPath.push_back(pRoot->mData.y);

        if (pRoot->lChild == NULL && pRoot->rChild == NULL && pRoot->mChild == NULL)  

        { 
vector<int>::iterator iterx=xPath.begin();
vector<int>::iterator itery=yPath.begin();

            for (; iterx!=xPath.end(); iterx++,itery++)  

            {  

                cout<<"("<<*iterx<<","<<*itery<<") ";  

            }  

            cout << endl;

            return;  

        }  

        else  

        {  

            findAllPath(pRoot->lChild, xPath,yPath); 
findAllPa
4000
th(pRoot->mChild, xPath,yPath);

            findAllPath(pRoot->rChild, xPath,yPath);  

        }  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构