二叉树的实现


typedef struct node{
    char data;
    struct node *lchild,*rchild;
}BinTNode,*BinTree;            //自定义二叉树的结点类型

int NodeNum,leaf;            //NodeNum为结点数,leaf为叶子数


//基于先序遍历算法创建二叉树
//要求输入先序序列,其中加入虚结点"#"以示空指针的位置
BinTree CreatBinTree()
{
    BinTree T;
    char ch;
    if((ch=getchar())=='#')
       return(NULL);       //读入#,返回空指针
    else{             
       T=(BinTNode *)malloc(sizeof(BinTNode));   //生成结点
       T->data=ch;
       T->lchild=CreatBinTree();        //构造左子树
       T->rchild=CreatBinTree();        //构造右子树
       return(T);
    }
}
//DLR 先序遍历
void Preorder(BinTree T)
{
    if(T) {       //T非空
              printf("%c",T->data);    //访问结点
              Preorder(T->lchild);    //先序遍历左子树
              Preorder(T->rchild);    //先序遍历右子树
	}
}

//LDR 中序遍历
void Inorder(BinTree T)

{
       if(T){
              Inorder(T->lchild);
              printf("%c",T->data);
              Inorder(T->rchild);
       }
}
//LRD 后序遍历
void Postorder(BinTree T)
{
       if(T){
              Postorder(T->lchild);
              Postorder(T->rchild);
        printf("%c",T->data);
       }
}

//采用后序遍历求二叉树的深度、结点数及叶子数的递归算法
int TreeDepth(BinTree T)
{
	int hl,hr,max;
    if(T){
       hl=TreeDepth(T->lchild);     //求左深度
       hr=TreeDepth(T->rchild);    //求右深度
       max=hl>hr? hl:hr;           //取左右深度的最大值

              NodeNum=NodeNum+1;      //求结点数
       if(hl==0&&hr==0) leaf=leaf+1;  //若左右深度为0,即为叶子。
              return(max+1);
    }
   else return(0);
}

//利用队列,按层次遍历二叉树
void Levelorder(BinTree T)
{
    int front=0,rear=1;
    BinTNode *cq[Max],*p;   //定义结点的指针数组cq
    cq[1]=T;                //根入队
    while(front!=rear)     
    {
              front=(front+1)%NodeNum;
              p=cq[front];            //出队
              printf("%c",p->data);     //出队,输出结点的值
              if(p->lchild!=NULL){
           rear=(rear+1)%NodeNum;
                  cq[rear]=p->lchild;     //左子树入队
              }
             if(p->rchild!=NULL){
           rear=(rear+1)%NodeNum;
                  cq[rear]=p->rchild;     //右子树入队
       }
    }
}


bool Copy_Tree(BinTree T,BinTree &S)
{
	if (T == NULL)  S = NULL; 
	else
	{
		S = (BinTree)malloc(sizeof(BinTNode));
		 if (!S) exit(ERROR);
		S->data=T->data;
		Copy_Tree(T->lchild,S->lchild);
		Copy_Tree(T->rchild,S->rchild);
	}
	return 0;
}


void A(){printf(".");}
void yemianzairu()
{
	for(int i=1;i<=3;i++)
	{
	system("cls");printf("\n\n\n\n\n\n\n\n\n                                 页面载入中");
	Sleep(100);
	A();
		Sleep(500);
	A();
		Sleep(500);
	A();
		Sleep(500);
	A();
	}
	printf("\n");
	system("cls");
}

//主函数
void main()
{
	yemianzairu();
	system("color F4");
    BinTree root,S;
    int i,depth,depth_S;
	printf("输入二叉树(满二叉树):");//输入完全二叉树的先序序列,
                         // 用#代表虚结点,如ABD###CE##F##
    root=CreatBinTree();       //创建二叉树,返回根结点

              printf("\t**********选择功能 ************\n");
			  printf("\t1: 显示先序遍历的结果\n");   
              printf("\t2: 显示中序遍历的结果\n");
			  printf("\t3: 显示后序遍历的结果\n");
              printf("\t4: 求二叉树的深度、结点数及叶子数\n");
			  printf("\t5: 显示层序遍历的结果\n"); //按层次遍历之前,先选择4,求出该树的结点数。
			  printf("\t6: 复制二叉树;\n");
              printf("\t0: 退出\n");
              printf("\t*******************************\n");
	   do {                    //从菜单中选择遍历方式,输入序号。
		   printf("请输入你的选择:");
       scanf("%d",&i);    //输入菜单序号(0-5)
       switch (i){
       case 1: printf("先序遍历的结果: ");
              Preorder(root);      //先序遍历
                            break;
       case 2: printf("中序遍历的结果: ");
                            Inorder(root);      //中序遍历
              break;
       case 3: printf("后序遍历的结果: ");
                            Postorder(root);    //后序遍历
              break;
       case 4: depth=TreeDepth(root);     //求树的深度及叶子数
               printf("二叉树的深度=%d;结点数=%d;叶子数=%d",depth,NodeNum,leaf);
			break;
       case 5: printf("层序遍历的结果: ");
              Levelorder(root);     //按层次遍历
             break;
	   case 6:Copy_Tree(root,S);printf("二叉树复制成功!新的二叉树命名为S;\n"); 
              printf("S的先序遍历的结果: ");Preorder(S);printf("\n");
			  printf("S的中序遍历的结果: ");Inorder(root);printf("\n");
			  printf("S的后序遍历的结果: ");Postorder(root);
             break;
        default: exit(1);
       }
              printf("\n");
    } while(i!=0);
	   free(S);free(root);    //释放申请的内存;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值