数据结构-树形结构实例
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);
}
}
}
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);
}
}
}
相关文章推荐
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- 数据结构之Treap详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)
- java数据结构和算法学习之汉诺塔示例
- Java数据结构及算法实例:三角数字
- Java数据结构之简单链表的定义与实现方法示例
- 数据结构之AVL树详解
- qqwry.dat的数据结构图文解释第1/2页
- JavaScript中数据结构与算法(五):经典KMP算法