您的位置:首页 > 理论基础 > 数据结构算法

华南理工数据结构大作业第二题 二叉树各种操作深度结点个数后序前序中序层次求祖先

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;
}

求祖先的办法:输出前序遍历和后序遍历,找到所求结点的位置,前序遍历在它之前,后序遍历在他之后,交集就是祖先
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐