版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010095372/article/details/83419946
今天不开森,自行车在楼下被偷了
文章目录
二叉树的创建
//先序创建二叉树
void CreatBinarTreeNode(BiThrTree *T){
char c;
scanf("%c", &c);
//输出空格就是没有了
if ( ' ' == c ){
*T = NULL;
}else {
*T = (BiThrNode *)malloc(sizeof(BiThrNode));
(*T)->data = c;
//创建完跟节点创建下面左右子树
CreatBinarTreeNode(&(*T)->LeftTreeNode);
CreatBinarTreeNode(&(*T)->RightTreeNode);
}
}
二叉树的先序遍历
//先序遍历,完全是按照递归的方式来走,很C
void putout1(BiThrTree T){
if(T)
{
printf("%c ", T->data);
putout1(T->LeftTreeNode);
putout1(T->RightTreeNode);
}
}
二叉树的中序遍历
//中序遍历
void putout2(BiThrTree T){
if(T)
{
putout2(T->LeftTreeNode);
printf("%c ", T->data);
putout2(T->RightTreeNode);
}
}
二叉树的后序遍历
//后序遍历
void putout3(BiThrTree T){
if(T)
{
putout3(T->LeftTreeNode);
putout3(T->RightTreeNode);
printf("%c ", T->data);
}
}
看看有多少个叶子节点(度为0的节点)
//叶子节点
int Leafnum(BiThrTree T){
//节点啥也没有就返回0
if(!T)
return 0;
//叶子节点就是最下面的,就是左右节点均无的
else if (!T->LeftTreeNode && !T->RightTreeNode)
return 1;
//按照递归来找就可以了
else
return Leafnum(T->LeftTreeNode) + Leafnum(T->RightTreeNode);
return -1;
}
总共有多少个节点
//总节点
int nodeNum(BiThrTree T){
if(!T)
return 0;
//只要走过有东西那就加1
else
return 1 + nodeNum(T->LeftTreeNode)+nodeNum(T->RightTreeNode);
return -1;
}
二叉树的高度
//二叉树的高度
int heightBitree(BiThrTree T){
//空节点肯定是0
if(!T)
return 0;
//因为二叉树的高度就是左右树那个最长的那个
//所以逆向来的话就是比较子节点的高度,哪个高要哪个
else
{
int Lheight = heightBitree(T->LeftTreeNode);
int Rheight = heightBitree(T->RightTreeNode);
return Lheight > Rheight ? Lheight+1 : Rheight+1;
}
return -1;
}
度为1的节点个数
//度为1的节点个数
int nodeOnenume(BiThrTree T){
if(!T)
return 0;
//只要符合条件就加一,并且看看下面还有木有
else if((T->LeftTreeNode && !T->RightTreeNode) || (!T->LeftTreeNode && T->RightTreeNode))
return 1 + nodeOnenume(T->LeftTreeNode) + nodeOnenume(T->RightTreeNode);
//继续递归
else
return nodeOnenume(T->LeftTreeNode) + nodeOnenume(T->RightTreeNode);
return 0;
}
度为2的节点个数
//度为2的节点个数
int nodeTwonume(BiThrTree T){
if(!T)
return 0;
//符合条件接着往下找
else if(T->LeftTreeNode && T->RightTreeNode)
return 1 + nodeTwonume(T->LeftTreeNode) + nodeTwonume(T->RightTreeNode);
//度为1的结点的话,就看看它的子节点里有木有
else
return nodeOnenume(T->LeftTreeNode) + nodeOnenume(T->RightTreeNode);
return -1;
}
交换左右子树
//交换左右子树
void exchangeTree(BiThrTree *T){
if(*T){
//这里就将两个大的子树就换了
BiThrTree temp = (*T)->LeftTreeNode;
(*T)->LeftTreeNode = (*T)->RightTreeNode;
(*T)->RightTreeNode = temp;
//还是递归,子树的规模会缩小,也会越细节
exchangeTree(&(*T)->LeftTreeNode);
exchangeTree(&(*T)->RightTreeNode);
}
}
完整代码
//
// main.c
// 二叉树的若干操作
//
// Created by 赫凯 on 2018/10/26.
// Copyright © 2018 赫凯. All rights reserved.
//
#include <stdlib.h>
#include <stdio.h>
typedef int ElementType;
typedef struct BinaryTreeNode{
ElementType data;
struct BinaryTreeNode *LeftTreeNode;
struct BinaryTreeNode *RightTreeNode;
}BiThrNode, *BiThrTree;
//先序创建二叉树
void CreatBinarTreeNode(BiThrTree *T){
char c;
scanf("%c", &c);
if ( ' ' == c ){
*T = NULL;
}else {
*T = (BiThrNode *)malloc(sizeof(BiThrNode));
(*T)->data = c;
CreatBinarTreeNode(&(*T)->LeftTreeNode);
CreatBinarTreeNode(&(*T)->RightTreeNode);
}
}
//先序遍历
void putout1(BiThrTree T){
if(T)
{
printf("%c ", T->data);
putout1(T->LeftTreeNode);
putout1(T->RightTreeNode);
}
}
//中序遍历
void putout2(BiThrTree T){
if(T)
{
putout2(T->LeftTreeNode);
printf("%c ", T->data);
putout2(T->RightTreeNode);
}
}
//后序遍历
void putout3(BiThrTree T){
if(T)
{
putout3(T->LeftTreeNode);
putout3(T->RightTreeNode);
printf("%c ", T->data);
}
}
void putout(BiThrTree T){
printf("\n先序:");
putout1(T);
printf("\n中序:");
putout2(T);
printf("\n后序:");
putout3(T);
}
//叶子节点
int Leafnum(BiThrTree T){
if(!T)
return 0;
else if (!T->LeftTreeNode && !T->RightTreeNode)
return 1;
else
return Leafnum(T->LeftTreeNode)+Leafnum(T->RightTreeNode);
return -1;
}
//总节点
int nodeNum(BiThrTree T){
if(!T)
return 0;
else
return 1+nodeNum(T->LeftTreeNode)+nodeNum(T->RightTreeNode);
return -1;
}
//二叉树的高度
int heightBitree(BiThrTree T){
if(!T)
return 0;
else
return heightBitree(T->LeftTreeNode)>heightBitree(T->RightTreeNode)?heightBitree(T->LeftTreeNode)+1:heightBitree(T->RightTreeNode)+1;
return -1;
}
//度为1的节点个数
int nodeOnenume(BiThrTree T){
if(!T)
return 0;
else if((T->LeftTreeNode && !T->RightTreeNode) || (!T->LeftTreeNode && T->RightTreeNode))
return 1 + nodeOnenume(T->LeftTreeNode) + nodeOnenume(T->RightTreeNode);
else
return nodeOnenume(T->LeftTreeNode) + nodeOnenume(T->RightTreeNode);
return 0;
}
//度为2的节点个数
int nodeTwonume(BiThrTree T){
if(!T)
return 0;
else if(T->LeftTreeNode && T->RightTreeNode)
return 1+nodeTwonume(T->LeftTreeNode) + nodeTwonume(T->RightTreeNode);
else
return nodeTwonume(T->LeftTreeNode) + nodeTwonume(T->RightTreeNode);
return 0;
}
//交换左右子树
void exchangeTree(BiThrTree *T){
if(*T){
BiThrTree temp = (*T)->LeftTreeNode;
(*T)->LeftTreeNode = (*T)->RightTreeNode;
(*T)->RightTreeNode = temp;
exchangeTree(&(*T)->LeftTreeNode);
exchangeTree(&(*T)->RightTreeNode);
}
}
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
//abc d ef g
BiThrTree Tree = NULL;
CreatBinarTreeNode(&Tree);
putout(Tree);
printf("\n");
printf("叶子点的个数%d",Leafnum(Tree));
printf("\n");
printf("节点的个数%d",nodeNum(Tree));
printf("\n");
printf("高度%d",heightBitree(Tree));
printf("\n");
printf("度为1的个数:%d",nodeOnenume(Tree));
printf("\n");
printf("度为2的个数:%d",nodeTwonume(Tree));
exchangeTree(&Tree);
putout(Tree);
return 0;
}