树的简单应用
来源:互联网 发布:网页数据采集器 开源 编辑:程序博客网 时间:2024/06/05 05:13
#include<iostream>#include<cstdio>#include<vector>#include<cstring>using namespace std;typedef struct BNode{ char data; //数据域 struct BNode *l; //左孩子指针 struct BNode *r; //右孩子指针}BTNode;typedef BTNode *Tree;void CreatexxTree(Tree *root)//前序建树{ char ch; cin>>ch; if(ch=='#') *root=NULL; else{ *root=new BTNode; (*root)->data=ch; CreatexxTree(&((*root)->l)); CreatexxTree(&((*root)->r)); }}void Preorder(Tree root)//前序遍历{ if(root!=NULL) { cout<<root->data; Preorder(root->l); Preorder(root->r); }}void Inorder(Tree root)//中序建树{ if(root!=NULL) { Inorder(root->l); cout<<root->data; Inorder(root->r); }}void Postorder(Tree root)//后序建树{ if(root!=NULL) { Postorder(root->l); Postorder(root->r); cout<<root->data; }}bool DeletePostTree(Tree &root)//后序毁树{ if(root==NULL) return true; else {DeletePostTree(root->l); DeletePostTree(root->r); delete root; return true; } return false;}int PostTreeDepth(Tree root)//{ int hl,hr,max1; if(root!=NULL) { hl=PostTreeDepth(root->l); hr=PostTreeDepth(root->r); max1=hl>hr?hl:hr; return (max1+1); } else return 0;}void levelTree(Tree T){ vector<BTNode*> vec; vec.push_back(T); int cur=0; int en=1; while(cur < vec.size()) { en=vec.size(); while(cur<en) { cout<<vec[cur]->data<<' '; if(vec[cur]->l) vec.push_back(vec[cur]->l); if(vec[cur]->r) vec.push_back(vec[cur]->r); cur++; } }}char Prestr[20]="ABCDEFGHIJKLM",Instr[20]="CBEFDGAJIHLKM",Postr[20]="CFEGDBJILMKHA";void PreInCreateTree(Tree &root,int Pre,int In,int TreeLen){ if(TreeLen<=0) { root=NULL; return ; } else { root=new BTNode; root->data=Prestr[Pre]; int index=strchr(Instr,Prestr[Pre])-Instr; int Lenf=index-In; PreInCreateTree(root->l,Pre+1,In,Lenf); int Lenr=TreeLen-1-Lenf; PreInCreateTree(root->r,Pre+Lenf+1,index+1,Lenr); }}void PostInCreateTree(Tree &root,int In,int Post,int TreeLen){ if(TreeLen <= 0){ root = NULL; return; } else{ root = new BTNode; root->data = Postr[Post]; int index = strchr(Instr,Postr[Post]) - Instr; int Lenf = index - In; PostInCreateTree(root->l,In,Post - (TreeLen - 1 - Lenf) - 1,Lenf); int Lenr = TreeLen - 1 - Lenf; PostInCreateTree(root->r,index + 1,Post-1,Lenr); }}int main(){ //freopen("a.txt","r",stdin); // cin.getline(Prestr,20); //cin.getline(Instr,20); // cin.getline(Postr,20); Tree root; //PreInCreateTree(root,0,0,strlen(Instr)); // PostInCreateTree(root,0,strlen(Postr)-1,strlen(Instr));//CreatexxTree(&root);PostInCreateTree(root,0,strlen(Postr)-1,strlen(Instr)); Preorder(root); cout<<endl; Inorder(root); cout<<endl; Postorder(root); cout<<endl; if(DeletePostTree(root)) cout<<"内存已回收\n"; return 0;}
0 0
- 树的简单应用
- 线段树的简单应用
- Trie树的简单应用
- zTree树的简单应用
- poj 3140 简单的树的应用
- 二叉树的简单应用--表达式树
- TreeView树型控件的简单应用
- 关于树面板TreePanel的简单应用
- 字典树的简单应用 NYoj 290
- hdu 1251 字典树的简单应用
- 二叉树的创建及其简单应用
- Trie树的建立与简单应用
- 完全二叉树的简单应用
- Hibernate的简单应用
- HTA的简单应用
- Hibernate的简单应用
- PHOTSHOP 简单的应用
- ajax的简单应用
- 排序
- CentOS中redis以及python中redis模块的安装
- vimrc常用配置以及vim常用操作
- 第十三周项目3-形状类族的中的纯虚函数
- python数据保存
- 树的简单应用
- 开源学习地址
- Python 3.4.3 使用threading模块进行多线程编码实现
- Logos语法解析
- 程序员装逼利器之impress.js
- 利用JUnit4进行单元测试
- STL--set
- AI不与人为敌
- LoadRunner 使用过程中问题及解决办法记录