数据结构-实验四 二叉树


数据结构- 二叉树递归:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<utility> 

using namespace std;

typedef struct BiTNode{
	char date;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

void Pre_CreateBiTree(BiTree &T)
{
	char ch;
	scanf("%c",&ch);
    if(ch=='#') T=NULL;
	else {
		T=new BiTNode;
		T->date=ch;
		Pre_CreateBiTree(T->lchild);
		Pre_CreateBiTree(T->rchild); 
	}	
} 

void Pre_Traverse(BiTree T)
{
	if(T) {
		printf("%c",T->date);
		Pre_Traverse(T->lchild);
		Pre_Traverse(T->rchild); 
	}
}

void In_Traverse(BiTree T)
{
	if(T) {
		In_Traverse(T->lchild);
		printf("%c",T->date);
		In_Traverse(T->rchild);
	}
}
 
void Post_Traverse(BiTree T)
{
	if(T) {
		Post_Traverse(T->lchild);
		Post_Traverse(T->rchild);
		printf("%c",T->date);
	}
}

void Lever_Traverse(BiTree T)
{
    queue<BiTNode*> Q;
	if(T) Q.push(T);
	while(!Q.empty()) {
		BiTree tem=Q.front(); Q.pop();
		printf("%c",tem->date);
		if(tem->lchild) Q.push(tem->lchild);
		if(tem->rchild) Q.push(tem->rchild); 
	}
} 

void PreorderTraverse(BiTree root)
{
     stack<BiTNode*> S;
     BiTree top=root;
     BiTNode *tem=new BiTNode;
     while(top||!S.empty())
     {
     	if(top) {
     		printf("%c",top->date);
     		S.push(top);   
     		top=top->lchild;
		 }
		 else {
		 	tem=S.top(); S.pop();
		    top=tem->rchild;
		 } 
	 }
}

void InorderTraverse(BiTree root) 
{
	stack<BiTNode*> S;
	BiTree p=root;
	BiTNode* q=new BiTNode;
	while(p||!S.empty()) {
		if(p) {
			S.push(p); p=p->lchild;
		}
		else {
			q=S.top(); S.pop();
			printf("%c",q->date);
			p=q->rchild; 
		}
	} 
}

void PostoderTraverse(BiTree root) 
{
	stack<pair<BiTNode*,bool> > S;
	BiTree to=root;
	BiTNode *tem=new BiTNode;
	while(to||!S.empty()) {
		if(to) {
		     pair<BiTNode*,bool> P(to,false);
		     S.push(P);
		}
		else {
			pair<BiTNode*,bool> P2=S.top(); 	S.pop();
			if(!P2.second) {
				P2.second=true;	S.push(P2);
				to=P2.first->rchild;
			} 
		    else {
		    	printf("%c",P2.first->date); 
			}
		}
	}  
}
int Count_Leaf(BiTree T)
{
     if(!T) return 0;
	 else if(T->lchild||T->rchild) return Count_Leaf(T->lchild)+Count_Leaf(T->rchild);
	 else return 1;	
} 
void Exchange_LR(BiTree &T)
{
	if(T) {
	//	swap(T->lchild,T->rchild);
		Exchange_LR(T->lchild); 
		Exchange_LR(T->rchild);
		swap(T->lchild,T->rchild);
	}
}
void Exchange_LR_Mid(BiTree &T)
{
	if(T) {
		Exchange_LR(T->lchild); 
		swap(T->lchild,T->rchild);
		Exchange_LR(T->lchild);
	}
}
int main()
{
	freopen("stdin.txt","r",stdin);
	freopen("stdout.txt","w",stdout); 
    BiTree root=NULL;
	
	printf("请输入创建的二叉树(先序序列): ");
    Pre_CreateBiTree(root); 

    printf("\n二叉树叶子结点数目: %d\n", Count_Leaf(root));
    
    printf("\n原二叉树: ");
    printf("\n先序遍历: "); Pre_Traverse(root); 
    printf("\n中序遍历: "); In_Traverse(root);
    printf("\n后序遍历: "); Post_Traverse(root);
    printf("\n按层遍历: "); Lever_Traverse(root);
    
    Exchange_LR_Mid(root);
    
    printf("\n左右子树交换之后的二叉树:\n");
    printf("\n先序遍历: "); Pre_Traverse(root); 
    printf("\n中序遍历: "); In_Traverse(root);
    printf("\n后序遍历: "); Post_Traverse(root);
    printf("\n按层遍历: "); Lever_Traverse(root);
	 
	return 0;
} 



非递归:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<utility> 

using namespace std;

typedef struct BiTNode{
	char date;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

void Pre_CreateBiTree(BiTree &T)
{
	char ch;
	scanf("%c",&ch);
    if(ch=='#') T=NULL;
	else {
		T=new BiTNode;
		T->date=ch;
		Pre_CreateBiTree(T->lchild);
		Pre_CreateBiTree(T->rchild); 
	}	
} 
void PreorderTraverse(BiTree root) 
{
     stack<BiTNode*> S;
     BiTree top=root;
     BiTNode *tem=new BiTNode;
     while(top||!S.empty())
     {
     	if(top) {
     		printf("%c",top->date);
     		S.push(top);   
     		top=top->lchild;
		 }
		 else {
		 	tem=S.top(); S.pop();
		    top=tem->rchild;
		 } 
	 }
}

void InorderTraverse(BiTree root) 
{
	stack<BiTNode*> S;
	BiTree p=root;
	BiTNode* q=new BiTNode;
	while(p||!S.empty()) {
		if(p) {
			S.push(p); p=p->lchild;
		}
		else {
			q=S.top(); S.pop();
			printf("%c",q->date);
			p=q->rchild; 
		}
	} 
}

void PostorderTraverse(BiTree root) 
{
	stack<pair<BiTNode*,bool> > S;
	BiTree top=root;
	BiTNode *tem=new BiTNode;
	while(top||!S.empty()) {
		if(top) {
		     pair<BiTNode*,bool> P(top,false);
		     S.push(P); 
		     top=top->lchild; 
		}
		else {
			pair<BiTNode*,bool> P2=S.top(); 	S.pop();
			if(!P2.second) {
				P2.second=true;	S.push(P2);
				top=P2.first->rchild;
			} 
		    else {
		    	printf("%c",P2.first->date); 
		    }
		}
	}  
}
//
//void PostorderTraverse(BiTree root) 
//{
//	stack<BiTNode*> S;
//	BiTree top=root,pre=NULL;
//    S.push(top);
//	while(!S.empty()) {
//		top=S.top();
//		if( (!top->lchild&&!top->rchild) || (pre&&(pre==top->lchild||pre==top->rchild)) ) 
//		{
//			S.pop(); printf("%c",top->date); pre=top;
//		 } 
//		 else {
//		 	if(top->rchild) S.push(top->rchild);
//		 	if(top->lchild) S.push(top->lchild);
//		 }
//	}
//}
int main()
{
	freopen("stdin.txt","r",stdin);
	freopen("stdout.txt","w",stdout);
    BiTree root=NULL;
	
	printf("请输入创建的二叉树(先序序列): ");
    Pre_CreateBiTree(root); 
    
    printf("\n非递归先序遍历二叉树序列: ");
    PreorderTraverse(root);
    
    printf("\n非递归中序遍历二叉树序列: ");
    InorderTraverse(root);
    
    printf("\n非递归后序遍历二叉树序列: ");
    PostorderTraverse(root);
	 
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值