数据结构-树形结构实例
来源:互联网 发布:知党史短句 编辑:程序博客网 时间: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);
}
}
}
- 数据结构-树形结构实例
- 数据结构之树形结构
- 数据结构---树形结构
- 数据结构--树形结构
- 数据结构之【树形结构】复习题
- js实现树形结构实例
- 数据结构——树形结构的应用
- 【数据结构】树形结构:二叉树&树
- 数据结构_树形结构_二叉树
- 数据结构中的是树形的结构
- 数据结构——树形结构概念
- JAVA 树形结构程序事件实例
- autoit实现树形结构的实例
- Java创建树形结构算法实例
- 哈工大数据结构实验二 树形结构及其应用
- [转]数据结构--树形结构(1)--作者:深挖三尺--抓狂
- js 把线性的数据结构改成树形结构
- 数据结构实验: 树形结构及其应用(C语言)
- 封装button
- 求二叉树的深度
- java 事件处理
- 寻找旋转排序数组中的最小值
- HDU 4056 Draw a Mess(并查集)
- 数据结构-树形结构实例
- 学习常量中指针变量和它指向的实体的个人理解
- Android中dpi,dp,dip,px.sp等各种单位详解
- Cocos2D中的纹理大小计算
- 第一阶段知识总结及问题
- UITableView常用属性和一些小知识点
- Android数据库高手秘籍(八)——使用LitePal的聚合函数
- 得到屏幕宽高
- Spring3.2新注解@ControllerAdvice