// 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;
}
程序运行结果如下: