创建二叉链表、前、中、后序遍历、层次遍历、计算深度,结点个数
# 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" ) ;
}
}
}