树的基本操作,包含树的建立,树的左右颠倒

#include <stdio.h>  
#include <stdlib.h>  
#include<stack>
typedef char ElemType;   //定义树的结点类型  


typedef struct BiTNode   
{  
     ElemType data;  
     struct BiTNode *lchild;
     struct BiTNode *rchild;     
}BiTNode,*BiTree;  

//创建空二叉树
void InitBiTree(BiTree &T)
{
	T = NULL;
}

//二叉树的创建  
void CreateBiTree(BiTree &T)  
{  
    char ch;  
    scanf("%c",&ch);  
	if(ch == ' ') 
	{
		T=NULL; //截止二叉树的建立
	}
    else 
	{  
       T = (BiTNode *) malloc(sizeof(BiTNode)); //申请结点空间   
	   if(!T)
		  exit(0);
       T->data = ch;  
	   CreateBiTree(T->lchild);  //构造左子树
	   CreateBiTree(T->rchild);  //构造右子树
     }  
 }   
    
  //二叉树的先序遍历  
void  PreOrderTraverse(BiTNode *p)  
{  
  if(p != NULL)  
  {  
      printf("%c*",p->data);  
      PreOrderTraverse(p->lchild);  
      PreOrderTraverse(p->rchild);  
   }  
 }  
 //二叉树的中序遍历  
void InOrderTraverse(BiTNode *p)  
{  
   if(p != NULL)  
   {  
      InOrderTraverse(p->lchild);  
      printf("%c*",p->data);  
      InOrderTraverse(p->rchild);  
   }  
 }  
 //二叉树的后序遍历  
void PostOrderTraverse(BiTNode *p)  
{  
    if(p != NULL)  
    {  
        PostOrderTraverse(p->lchild);  
        PostOrderTraverse(p->rchild);  
        printf("%c*",p->data);  
    }  
}   
//求二叉树的高度
int High(BiTNode *p)
{
	int lh=0;
	int rh=0;
	if(p == NULL)
	{
		return 0;
	}
	lh=High(p->lchild);
	rh=High(p->rchild);
	if(lh>rh)
	{
		return lh+1;
	}
	else
	{
		return rh+1;
	}
}
//求二叉树的结点数目
int Count(BiTree T)
{
	if(T == NULL)
	{
		return 0;
	}
	return Count(T->lchild)+Count(T->rchild)+1;
}
//实现左右子树的交换
void exchange(BiTree T)
{
	if(T == NULL)
	{
		return;
	}
	else
	{
		BiTree temp=T->lchild;
		T->lchild = T->rchild;
		T->rchild = temp;
		exchange(T->lchild);
		exchange(T->rchild);
	}
}
//前序遍历的非递归算法
void PreTraverse(BiTree T)
{
	BiTree p;
	p=T;
	stack S;
	S.IintStack();
	while(p || S.StackEmpty())
	{
		if(p)
		{
			printf("%d",p->data);
			Push(&S,p);
			p=p->lchild;
		}
		else
		{
			S.pop(p);
			p=p->rchild;
		}
	}
}
//中序遍历的非递归算法
void InTraverse(BiTree T)
{
	BiTree p;
	p=T;
	stack S;
	S.IintStack();
	while(p || S.StackEmpty())
	{
		if(p)
		{
			Push(&S,p);
			p=p->lchild;
		}
		else
		{
			S.pop(p);
			printf("%d",p->data);
			p=p->rchild;
		}
	}
}

//后序遍历的非递归算法
#define MaxSize 100
void PostTraverse(BiTree T)
{
   BiTree p;
   stack<BiTree> S;
   int intstack[MaxSize];
   int i;
   int top;
   p=T;
   //S.IintStack();
   for(int i=0;i<100;i++)
   {
	   intstack[i]=0;
   }
   top=0;
   while(p || S.empty())
   {
	   if(p)
	   {
		   Push(&S,p);
		   intstack[top]=0;
		   top++;
		   p=p->lchild;
	   }
	   else
	   {
		   if(intstack[top-1] == 0)
		   {
			   GetTop(S,&p);
			   intstack[top-1]=1;
			   p=p->rchild;
		   }
		   else
		   {
			   Pop(&S,&p);
			   top--;
			   printf("%d",p->data);
			   p=NULL;
		   }
	   }
   }
}

int main()  
{  
    BiTree T;  
    CreateBiTree(T);  
    PreOrderTraverse(T);  
    printf("\n");  
    InOrderTraverse(T);  
    printf("\n");  
    PostOrderTraverse(T);  
    printf("\n");  
	int a=High(T);
	int b=Count(T);
	exchange(T);
	return 0; 
 } 
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值