C语言创建二叉树、遍历二叉树、计算二叉树深度、计算二叉树双孩子结点数

// ConsoleApplication_bin_tree.cpp : Defines the entry point for the console application.

//Created by Xueshang Nai

//We use two methods to create bin tree link and then use preorder , inorder, postorder to transvers the tree respectively

//Email:xsnai@163.com

//---------------------------------------------------------------------------------------

#include "stdafx.h"// if you use VC 6.0 ,this line is not needed, but for visual studio 2013 or later version it is needed

#include<stdio.h>

#include<malloc.h>

#include<stdlib.h>

typedef char DataType; //Define node type

typedef struct node

{

DataType data;

struct node *lchild, *rchild;

}BinTNode;

typedef BinTNode *BinTree;

char PRO2[] = { 'A','B','D',' ', 'G',' ',' ','E','H',' ',' ',' ','C','F',' ', ' ', ' '};//This sequence is for bin tree to created with virtual node in pre sequence

void InOrder(BinTree T)

{

if (T)

{

InOrder(T->lchild);

printf("%c",T->data);

InOrder(T->rchild);

}

}

void PreOrder(BinTree T)

{

if (T)

{

printf("%c",T->data);

PreOrder(T->lchild);

PreOrder(T->rchild);

}

}

void PostOrder(BinTree T)

{

if (T)

{

PostOrder(T->lchild);

PostOrder(T->rchild);

printf("%c",T->data);

}

}

void CreateBinTreePI(BinTree *T, char *PRO, char *INO, int n)

{//created the bin tree link with pre order and in order sequence

int m = 0;

if (n == 0)

*T = NULL;

else

{

*T = (BinTNode*)malloc(sizeof(BinTNode));//created a node with alloc function

(*T)->data = PRO[0];

while (INO[m] != PRO[0])

m++;

CreateBinTreePI(&(*T)->lchild, PRO + 1, INO, m);//create the left child in the inorder sequence

CreateBinTreePI(&(*T)->rchild, PRO + m + 1, INO + m + 1, n - m - 1);//create the ritht child in the inorder sequence

}

}

void CreateBinTreeP(BinTree *T,int index)//create the bin tree with virtual node pre order sequence

{

if (*PRO2 == ' ')

{

*T = NULL;

index++;

}

else

{

*T = (BinTNode*)malloc(sizeof(BinTNode));

(*T)->data = PRO2[index++];

CreateBinTreeP(&(*T)->lchild,index);

CreateBinTreeP(&(*T)->rchild,index);

}

}

 

int BinTreeDepth(BinTNode *BT)// caulculate the depth of the bin tree

{

int leftdep, rightdep;

if (BT == NULL)

return 0;

else

{

leftdep = BinTreeDepth(BT->lchild);

rightdep = BinTreeDepth(BT->rchild);

}

return leftdep>rightdep ? leftdep + 1 : rightdep + 1;

}

int TwoSonCount(BinTNode *BT)

{

if (BT == NULL)

return 0;

else if (BT->lchild == NULL || BT->rchild == NULL)

return(TwoSonCount(BT->lchild) + TwoSonCount(BT->rchild));

else

return(TwoSonCount(BT->lchild) + TwoSonCount(BT->rchild) + 1);

}

 

int main()

{

char PRO[] = {'A','B','D','G','E','H','C','F'};//Pre order sequence

char INO[] = { 'D','G','B','H','E','A','F','C'};//In order sequence

BinTree tree1;//tree 1

BinTree tree2;//tree 2

printf("\nThe tree created with pre order and in order sequence is following:\n");

CreateBinTreePI(&tree1, PRO, INO, 8);

printf("\nThe pre order is:\n");

PreOrder(tree1);

printf("\nThe in order is:\n");

InOrder(tree1);

printf("\nThe post order is:\n");

PostOrder(tree1);

printf("\npress any key to contunue!");

getchar();

/*printf("\nThe tree created with virtual node pre order sequence is following:\n");

CreateBinTreeP(&tree2,0);

printf("The pre order is:\n");

PreOrder(tree2);

printf("\nThe in order is:\n");

InOrder(tree2);

printf("\nThe post order is:\n");

PostOrder(tree2);*/

printf("the depth of the tree is: %d\n", BinTreeDepth(tree1));

printf("the two sons node number of the tree is: %d\n", TwoSonCount(tree1));

getchar();

return 1;

}

程序运行结果如下:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值