#include<stdio.h>
#include<stdlib.h>
#include<String.h>
#include<assert.h>
typedef char BTDatetype;
typedef struct BinaryTreeNode{
BTDatetype _date;
struct BinaryTreeNode* _right;
struct BinaryTreeNode* _left;
}BTNode;
//前序遍历
void PrevOrder(BTNode* root)
{
if(root==NULL)
{
printf("NULL ");
return;
}
printf("%c ",root->_date);
PrevOrder(root->_left);
PrevOrder(root->_right);
}
//中序遍历
void InOrder(BTNode* root)
{
if(root==NULL)
{
printf("NULL ");
return;
}
InOrder(root->_left);
printf("%c ",root->_date);
InOrder(root->_right);
}
//后序遍历
void PostOrder(BTNode* root)
{
if(root==NULL)
{
printf("NULL ");
return;
}
PostOrder(root->_left);
PostOrder(root->_right);
printf("%c ",root->_date);
}
//模拟树
BTNode* CreateNode(char x)
{
BTNode* Node = (BTNode*)malloc(sizeof(BTNode));
Node->_date = x;
Node->_left = NULL;
Node->_right = NULL;
return Node;
}
//计算节点个数
void TreeSize1(BTNode* root, int* psize)
{
if(root==NULL)
{
return;
}
else{
(*psize)++;
}
TreeSize1(root->_left,psize);
TreeSize1(root->_right,psize);
}
//计算节点个数(妙)
int TreeSize2(BTNode* root)
{
if(root==NULL)
{
return 0;
}
else{
return 1+TreeSize2(root->_left)+TreeSize2(root->_right);
}
}
//计算叶子节点个数(妙)
int TreeLeafSize(BTNode* root)
{
if(root==NULL)
{
return 0;
}
if(root->_left == NULL && root->_right == NULL)
{
return 1;
}
return TreeLeafSize(root->_left)+TreeLeafSize(root->_right);
}
//求第k层节点的个数(不理解)
int BinaryTreeLevelKSize(BTNode* root, int k)
{
if(root==NULL)
{
return 0;
}
if(k==1)
{
return 1;
}
return BinaryTreeLevelKSize(root->_left, k-1)+ BinaryTreeLevelKSize(root->_right, k-1);
}
//找到值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDatetype x)
{
if(root==NULL)
{
return NULL;
}
if(root->_date == x)
{
return root;
}
BTNode* node = BinaryTreeFind(root->_left, x);
if(node){
return node;
}
node = BinaryTreeFind(root->_right, x);
if(node){
return node;
}
return NULL;
}
//判断是否为单值二叉树
bool isUniverTree(BTNode* root)
{
if(root==NULL)
{
return true;
}
if(root->_left && root->_date != root->_left->_date){
return false;
}
if(root->_right && root->_date != root->_left->_date){
return false;
}
return isUniverTree(root->_left) && isUniverTree(root->_right);
}
//求树的高度
int maxDepth(BTNode* root)
{
if(root==NULL)
{
return 0;
}
int leftDepth = maxDepth(root->_left);
int rightDepth = maxDepth(root->_right);
return leftDepth > rightDepth ? leftDepth+1 : rightDepth+1;
}
//反转二叉树
BTNode* invertTree(BTNode* root){
if(root==NULL)
{
return NULL;
}
else{
BTNode* temp = root->_left;
root->_left = root->_right;
root->_right = temp;
}
invertTree(root->_left);
invertTree(root->_right);
return root;
}
//判断两个树是否相等
bool isEqualTree(BTNode* root1, BTNode* root2)
{
if(root1==NULL && root2==NULL)
{
return true;
}
if(root1==NULL && root2!=NULL)
{
return false;
}
if(root2==NULL && root1!=NULL)
{
return false;
}
if(root1->_date!=root2->_date)
{
return false;
}
return isEqualTree(root1->_left, root2->_left) && isEqualTree(root1->_right, root2->_right);
}
//销毁树
void DestoryTree(BTNode* root){
if(root == NULL)
{
return ;
}
DestoryTree(root->_left);
DestoryTree(root->_right);
free(root);
}
int main()
{
BTNode* A = CreateNode('A');
BTNode* B = CreateNode('B');
BTNode* C = CreateNode('C');
BTNode* D = CreateNode('D');
BTNode* E = CreateNode('E');
A->_left = B;
A->_right = C;
B->_left = D;
B->_right = E;
PrevOrder(A);
printf("\n");
InOrder(A);
printf("\n");
PostOrder(A);
printf("\n");
// int size=0;
// TreeSize(A, &size);
// printf("Treesize: %d \n",size);
printf("Treesize: %d",TreeLeafSize(A));
return 0;
}
08-03
578
07-08
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交