中序和后序确定树结构 树最长路径 叶节点个数
2010-01-17 22:04
585 查看
]#include <iostream> #include <stack> #include <string> #include <list> using namespace std; #define MAX_SIZE 30 //树的结构体 struct TreeNode { char data; TreeNode *lChild; TreeNode *rChild; public: TreeNode(char c):data(c),lChild(0),rChild(0){} }; //辅助 struct Helper{ TreeNode *node; int index; public: Helper(TreeNode *pNode,int idx):node(pNode),index(idx){} }; int leafCount=0;//叶节点个数 int twoCount=0; //二度节点个数 int record=0; //记录到页节点长度 char Max_Record[MAX_SIZE]; char last_Record[MAX_SIZE];//路径 int maxlength=0; //还原 void Mid_Post_Restore(string post,string mid,TreeNode *&result); //前序遍历 void PreorderTraversal(TreeNode *pTree); int main() { string Postorder1;// = "HIDJKEBMNFOPGCA" ; string Midorder1;// = "HDIBJEKAMFNCOGP" ; cout<<"请输入后序序列!"; cin>>Postorder1; cout<<"请输入中序序列!"; cin>>Midorder1; TreeNode *res=0; Mid_Post_Restore(Postorder1, Midorder1, res); //前序序列 cout<<"前序序列为:"; PreorderTraversal(res); cout<<endl<<"叶节点个数:"; cout<<leafCount; cout<<endl<<"二度节点个数:"; cout<<twoCount; cout<<endl<<"一条最长路径各结点:"; for(int i=0;i<=maxlength;++i) cout<<last_Record[i]<<" "; return 0; } //还原 void Mid_Post_Restore(string post,string mid,TreeNode *&result) { int pi=post.size()-1; //后序遍历的字符串下标 int mi=0; //中序遍历的下标 char pc; result = new TreeNode(post[pi]); //后序遍历第一个字符是根 TreeNode *pNode=0; mi = mid.find(post[pi]); //在中序遍历中查找 list<Helper> helper; helper.push_back(Helper(0,-1)); helper.push_back(Helper(result,mi)); helper.push_back(Helper(0,mid.size())); list<Helper>::iterator cur=helper.begin(); cur++; for(pi=post.size()-2;pi>=0;pi--) { pc=post[pi]; mi=mid.find(pc); while(true) { if(mi>(*cur).index) { pNode =new TreeNode(pc); (*cur).node->rChild=pNode; cur++; cur=helper.insert(cur,Helper(pNode,mi)); break; }else { list<Helper>::iterator nxt=cur; cur--; if((*cur).index<mi && mi<(*nxt).index) { pNode=new TreeNode(pc); (*nxt).node->lChild=pNode; helper.erase(nxt); cur++; cur=helper.insert(cur,Helper(pNode,mi)); break; }else { helper.erase(nxt); continue; } } } } } //前序遍历 void PreorderTraversal(TreeNode *pTree) { if(pTree!=NULL) { Max_Record[record]=pTree->data; if(pTree->lChild==NULL && pTree->rChild==NULL) { if(record>maxlength) { for(int i=0;i<=record;++i) last_Record[i]=Max_Record[i]; maxlength=record; } leafCount++; } if(pTree->lChild!=NULL && pTree->rChild!=NULL) twoCount++; cout<<pTree->data; record++; PreorderTraversal(pTree->lChild); PreorderTraversal(pTree->rChild); record--; } }
以上是具体代码,希望对某些计算机学习者有帮助。
相关文章推荐
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
- 二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
- 树结构的动态建立和前序、中序、后序遍历
- 数据结构基础 后序遍历和中序遍历还原二叉树
- 求解一颗二叉树中两个叶子节点最长的路径
- 采用二叉链表结构实现二叉树,并以递归遍历思想实现二叉树的创建、二叉树的遍历(先序、中序、后序和层次遍历)
- 【数据结构】求节点的哈夫曼的带权路径长度
- 数据结构(一)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树中节点的最大距离(树的最长路径)——递归解法
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- (编程训练)再回首,数据结构——二叉树的前序、中序、后序遍历(递归)
- 数据结构——树(4):二叉树前序,中序,后序遍历互求
- 有向图的最长路径及是否存在环路结构
- js中树结构根据条件查找节点返回节点路径的一些思路
- 算法竞赛入门经典:第六章 数据结构基础 6.9 根据二叉树的后序和中序确定前序序列
- 树和二叉树的应用举例(相似二叉树,由先序和中序、中序和后序确定二叉树)
- 二叉树的先序 中序 后序 层次遍历 求叶节点个数、深度、最大元、最小元
- 二叉树系列(建树,前序,中序,后序,中序非递归,深度,叶子数,节点数)