华南理工数据结构大作业第二题 二叉树各种操作深度结点个数后序前序中序层次求祖先
2014-12-28 15:47
399 查看
/*#include<iostream> #include<windows.h> using namespace std ; struct BTNode { char data ; BTNode *left ; BTNode *right ; BTNode () { left = NULL ; right = NULL ; } } ; int main () { cout <<"题目所给的二叉树用括号表示法后表示为:A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I))) " <<endl; //输入一串括号表示法 ; cout <<"所给的二叉树的深度为 :"; //输出深度 ; cout <<endl; cout <<"所给的二叉树结点个数为 :" ; //输出个数 cout <<endl; cout <<"所给的二叉树的叶子结点个数为 :" <<endl; //叶子结点的个数 //先序遍历二叉树: //中序; //后序; //层次 ; cout <<"所给的二叉树当中的H 的所有祖先为:"<<endl; system ("pause ") ; } */ #include<iostream> #include<windows.h> #include<stack> using namespace std ; struct BiTNode{ char c ; BiTNode* left ; BiTNode* right ; } ; //char s[] = "a(b(c,d),e(f,g))"; char s[] = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))" ; void CreateBTNode (BiTNode *&b , char *s ) { BiTNode *St[20] ,*p = NULL ; int top = -1 , k , j=0 ; char ch ; b = NULL ; ch = s[j] ; while( ch!='\0' ) { switch( ch ) { case '(' : top++ ; St[top] = p ; k=1 ; break ; case ')' : top-- ; break ; case ',' : k = 2 ; break ; default : p = new BiTNode ; p->c = ch ; p->left = p->right = NULL ; if(b == NULL) b = p ; else{ switch(k){ case 1:St[top]->left = p ;break ; case 2:St[top]->right = p ;break ; } } } j++; ch = s[j]; } } void priorder (BiTNode *p) { if(p != NULL){ cout <<p->c <<" "; priorder(p->left) ; priorder(p->right) ; } }//前序 void post (BiTNode *p) { if( p != NULL) { post (p->left) ; post (p->right) ; cout <<p->c <<" " ; } }//后序 void InOrder(BiTNode *t){ stack<BiTNode*> s; while(!s.empty() || t != NULL){ while(t != NULL){ s.push(t); t = t->left ; } if(!s.empty()){ t = s.top() ; s.pop() ; cout<<t->c<<" "; t = t->right; } } } //中序 int front=0,rear=1; void Levelorder(BiTNode *t){ BiTNode *q[100]; q[0]=t; while(front<rear){ if(q[front]){ cout<<q[front]->c<<" " ; q[rear++]=q[front]->left; q[rear++]=q[front]->right; front++; } else{ front++; } } } int u =0 ; int v =0 ; int height(BiTNode *p) { if (p==NULL) return 1; u=height(p->left); v=height(p->right); if (u>v) return (u+1) ; else return (v+1) ; } int i =0 ; int node_num (BiTNode *p) { if( p== NULL) return 0 ; u= node_num(p->left) ; v =node_num(p->right) ; i++ ; return i ; } int leaf_num(BiTNode *p) { if(!p) return 0; else if(!p->left && !p->right) return 1; else return leaf_num(p->left)+leaf_num(p->right); } int main(){ struct BiTNode *p; int len = sizeof(s); CreateBTNode(p,s); cout <<"题目所给的二叉树是 :A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"<<endl; cout <<"二叉树的深度是 : "<<height(p)<<endl; cout <<"二叉树结点的个数为 : "<<node_num(p)<<endl; cout <<"二叉树的叶子结点个数为 : "<<leaf_num(p)<<endl; cout <<"前序遍历输出 :"; priorder(p); cout <<endl; cout <<"中序遍历输出(非递归) :" ; InOrder(p) ; cout <<endl; cout<<"后序遍历输出 :" ; post (p) ; cout <<endl ; cout <<"层序遍历输出 :"; Levelorder(p) ; cout <<endl ; cout<<"结点H的祖先为 :A B E "<<endl; system ("pause") ; return 0; }
求祖先的办法:输出前序遍历和后序遍历,找到所求结点的位置,前序遍历在它之前,后序遍历在他之后,交集就是祖先
相关文章推荐
- 二叉树的各种操作 先序 中序 后续 层次 遍历 求树高度 节点深度 知先序中序求后续 二叉排序树
- 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 与二叉树有关的一些操作:先序,中序,后序,层次遍历,计算深度,叶结点数
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 数据结构 —— 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树的各种操作(递归和非递归遍历,树深度,结点个数等等)(Java)
- 二叉树的各种操作的(递归非递归)的实现,如(递归非递归)先序后序中序层次遍历 二叉树的高度 叶子节点数,所有节点数
- C语言二叉树常见操作详解【前序,中序,后序,层次遍历及非递归查找,统计个数,比较,求深度】
- 华南理工数据结构大作业第一题单链表 删除创建等各种简易操作
- 数据结构 ——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树的的所有操作——转化为静态二叉链表,求深度,求先序排列,求中序排列,求后序排列,求层次遍历,求度为0.1.2的节点个数
- 数据结构--二叉树的创建、先序遍历、中序遍历、后序遍历、深度、叶子结点数
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树的一些基础算法(先序,中序,后序,层次,深度,宽度,距离)
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 遍历二叉树的各种操作(转载的,真心觉得后序遍历用双栈法好简单啊)
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现