数据结构-树形结构实例

来源:互联网 发布:知党史短句 编辑:程序博客网 时间:2024/05/10 18:48

题目介绍:

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); 
findAllPath(pRoot->mChild, xPath,yPath);
            findAllPath(pRoot->rChild, xPath,yPath);  
        }  
    }  
}  




0 0
原创粉丝点击