2021-11-07

创建二叉链表、前、中、后序遍历、层次遍历、计算深度,结点个数

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<iostream>
#define TElemType int
using namespace std;
//二叉链表二叉树 ,数据是整形 
typedef struct BiTNode{
	TElemType data;
	struct BiTNode *lchild;
	struct BiTNode *rchild;
}BiTNode,*BiTree; 


//按先序遍历输入二叉树中结点的值 ,递归 
void CreateBiTree(BiTree &T){
	TElemType e;
	scanf("%d",&e);
	if(-1 == e) T = NULL;
	else{
    T = new BiTNode;
    T->data = e;
    CreateBiTree(T->lchild);
    CreateBiTree(T->rchild);
}
} 
void PreBiTree(BiTree &T){
	if(T != NULL){
         printf("%d ",T->data);
	     PreBiTree(T->lchild);
	     PreBiTree(T->rchild);
}
} 
void InBiTree(BiTree &T){
		if(T != NULL){
         PreBiTree(T->lchild);
	     printf("%d ",T->data);
	     PreBiTree(T->rchild);
}
}

void PostBiTree(BiTree &T){
		if(T != NULL){
         PreBiTree(T->lchild);
	     PreBiTree(T->rchild);
         printf("%d ",T->data);
}
}
void InitBiTree(BiTree &T){
	printf("请输入先序遍历系列(-1为空数据):\n");
	CreateBiTree(T);
	printf("先序遍历的结果为:\n");
	PreBiTree(T);
	printf("\n");
	
}
void LevelBiTree(BiTree &T){
	queue<BiTNode> q;
	if(T != NULL){
		BiTNode temp;       //暂存出队的数据 
		q.push(*T);
        while(!q.empty()){
             temp = q.front();
		     q.pop();
		     printf("%d ",temp.data);
	         if(temp.lchild != NULL) q.push(*temp.lchild);
		     if(temp.rchild != NULL) q.push(*temp.rchild);
 		}
    }
}
int Depth(BiTree &T){
	int m;
	int n;
	if(T==NULL) {
	return 0;
}else{
		m = Depth(T->lchild);
		n = Depth(T->rchild);
		if(m>n) return(m+1);
		else return (n+1);
	} 
}  
void DepthNum(BiTree &T){
	int num;
	num = Depth(T);
	printf("深度为%d\n",num);
}
void Traverse(BiTree &T){
	int i;
	while(1){
		printf("***********************\n"); 
		printf("1.二叉树前序遍历:\n");
		printf("2.二叉树中序遍历:\n");
		printf("3.二叉树后序遍历:\n");
		printf("4.二叉树层次遍历:\n");
		printf("请输入你的选择:\n");
		scanf("%d",&i);
		if(5 == i) break;
		switch(i){
		case 1:printf("二叉树先序遍历结果为:\n");PreBiTree(T);printf("\n"); break;
		case 2:printf("二叉树中序遍历结果为:\n");InBiTree(T);printf("\n");break;
		case 3:printf("二叉树后序遍历结果为:\n");PostBiTree(T);printf("\n");break; 
		case 4:printf("二叉树层次遍历结果为:\n");LevelBiTree(T);printf("\n");break; 
		default:printf("输入错误!\n"); break;
		}
		
	}
}
int NodeCount(BiTree &T){
	if(T==NULL) return 0;
	else return (NodeCount(T->lchild)+NodeCount(T->rchild)+1);
}
void NodeNumber(BiTree &T){
	int count;
	count = NodeCount(T);
	if(count == 0){
		printf("二叉树为空!");
	}
	else{
		printf("二叉树结点个数为%d\n",count);
	}
}
void menu(){
	printf("************************\n");
	printf("*****1.创建  2.遍历*****\n");
	printf("*****3.计算二叉树深度**\n");  
	printf("*****4.计算二叉树结点个数**\n"); 
}
int main(){
	BiTree T;
	int choice;
	while(1){
		menu();
		printf("请输入你的选择:\n");
		scanf("%d",&choice);
		if(5 == choice) break;
		switch(choice){
			case 1:InitBiTree(T); break;
			case 2:Traverse(T); break;
			case 3:DepthNum(T); break;
   			case 4:NodeNumber(T); break;
			default:printf("输入错误!!\n");
		} 
	}
} 

在这里插入图片描述在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值