二叉树的基本操作


判断一个树是否为二叉树需得满足一下两个条件:
1.每个结点的度都不大于2;
2.每个结点的孩子结点次序不能任意颠倒;
即一个二叉树中的每个结点只能含有0、1或2个孩子,而且每个孩子有左右之分。

下面来讨论一下二叉树的基本操作:

头文件BTNode.h

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
#include<string.h>


#define MAX_SIZE 10
typedef char BTDataType;
typedef struct BinTreeNode
{
    struct BinTreeNode* _Pleft;
    struct BinTreeNode* _Pright;
    BTDataType _data;
} BTNode, *PBTNode;

// 构建二叉树的结点 
PBTNode BuyBinTreeNode(BTDataType data);

// 创建二叉树 
void CreateBinTree(PBTNode* PRoot, const BTDataType* array, int size, BTDataType invalid);

// 拷贝二叉树 
PBTNode CopyBinTree(PBTNode PRoot);

// 销毁二叉树 
void DestroyBinTree(PBTNode *PRoot);

// 前序遍历递归 
void PreOrder(PBTNode PRoot);

// 中序遍历递归 
void InOrder(PBTNode PRoot);

// 后续遍历递归 
void PostOrder(PBTNode PRoot);

// 二叉树的镜像递归 
void MirrorBinTree(PBTNode PRoot);

// 求二叉树中结点的个数 
int BinTreeSize(PBTNode PRoot);

// 获取二叉树中叶子结点的个数 
int GetLeafCount(PBTNode PRoot);

// 求二叉树中K层结点的个数 
int GetKLevelNode(PBTNode PRoot, int K);

// 求二叉树的高度 
int Height(PBTNode PRoot);

功能模块BTNode.c

#include"BTNode.h"

PBTNode BuyBinTreeNode(BTDataType data)  //构建二叉树的结点
{
    PBTNode PNewNode = (PBTNode)malloc(sizeof(BTNode));
    if (NULL == PNewNode)
    {
        assert(0);
        return NULL;
    }
    PNewNode->_Pleft = NULL;
    PNewNode->_Pright = NULL;
    PNewNode->_data = data;
    return PNewNode;
}


void _CreateBinTree(PBTNode* pRoot, const BTDataType* array, int size, int* index, BTDataType invalid)  // 创建二叉树 
{
    if (array[*index] == invalid)
    {

        (*pRoot) = NULL;
        return;
    }
    else if ((*index) >= size)
    {
        (*pRoot) = NULL;
        return;
    }
    else
    {
        //创建根结点
        (*pRoot) = BuyBinTreeNode(array[*index]);
        //创建根结点的左子树
        ++(*index);
        _CreateBinTree(&(*pRoot)->_Pleft, array, size, index, invalid);
        //创建根结点的右子树
        ++(*index);
        _CreateBinTree(&(*pRoot)->_Pright, array, size, index, invalid);
    }
}

void CreateBinTree(PBTNode* pRoot, const BTDataType* array, int size, BTDataType invalid)
{
    int index = 0;
    assert(pRoot);

    _CreateBinTree(pRoot, array, size, &index, invalid);
}


PBTNode CopyBinTree(PBTNode PRoot)  //拷贝二叉树
{
    PBTNode PNewRoot = NULL;
    if (PRoot)
    {
        PNewRoot = BuyBinTreeNode(PRoot->_data);
        //拷贝左子树
        if (PRoot->_Pleft)
            PNewRoot->_Pleft = CopyBinTree(PRoot->_Pleft);
        //拷贝右子树
        if (PRoot->_Pright)
            PNewRoot->_Pright = CopyBinTree(PRoot->_Pright);
    }
    return PNewRoot;
}

void DestroyBinTree(PBTNode *PRoot)  //销毁二叉树
{
    assert(PRoot);

    if (NULL == (*PRoot))
    {
        return;
    }

    DestroyBinTree(&(*PRoot)->_Pleft);
    DestroyBinTree(&(*PRoot)->_Pright);

    free(*PRoot);
    (*PRoot) = NULL;

}

void PreOrder(PBTNode PRoot)  //前序遍历递归
{
    if (PRoot)
    {
        printf("%c", PRoot->_data);
        PreOrder(PRoot->_Pleft);
        PreOrder(PRoot->_Pright);
    }
}


void InOrder(PBTNode PRoot)  //中序遍历递归
{
    if (PRoot)
    {
        PreOrder(PRoot->_Pleft);
        printf("%c", PRoot->_data);
        PreOrder(PRoot->_Pright);
    }
}

void PostOrder(PBTNode PRoot)  // 后序遍历递归
{
    if (PRoot)
    {
        PreOrder(PRoot->_Pleft);
        PreOrder(PRoot->_Pright);
        printf("%c", PRoot->_data);
    }
}



void MirrorBinTree(PBTNode PRoot)  //二叉树的镜像递归
{
    if (PRoot)
    {
        PBTNode PTemp = PRoot->_Pleft;
        PRoot->_Pleft = PRoot->_Pright;
        PRoot->_Pright = PTemp;

        MirrorBinTree(PRoot->_Pleft);
        MirrorBinTree(PRoot->_Pright);
    }
}

int BinTreeSize(PBTNode PRoot)  //求二叉树中结点的个数
{
    if (NULL == PRoot)
        return 0;

    return BinTreeSize(PRoot->_Pleft) + BinTreeSize(PRoot->_Pright) + 1;
}

int GetLeafCount(PBTNode PRoot)  //获取二叉树中叶子结点的个数
{
    if (NULL == PRoot)
        return 0;
    if (NULL == PRoot->_Pleft&&NULL == PRoot->_Pright)
        return 1;
    return GetLeafCount(PRoot->_Pleft) + GetLeafCount(PRoot->_Pright);
}

int GetKLevelNode(PBTNode PRoot, int k)  //求二叉树中K层结点的个数
{
    if (NULL == PRoot || k < 1)
        return 0;
    if (1 == k)
        return 1;
    return GetKLevelNode(PRoot->_Pleft, k - 1) + GetKLevelNode(PRoot->_Pright, k - 1);
}

int Height(PBTNode PRoot)    //求二叉树的高度
{
    if (NULL == PRoot)
        return 0;
    int leftHeight = Height(PRoot->_Pleft);
    int rightHeight = Height(PRoot->_Pright);
    return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}

测试模块 test.c

#include"BTNode.h"

void TestBinTree()
{
    const char* str = "ABD###CE##F";
    PBTNode PRoot = NULL;
    PBTNode PNewTree = NULL;
    BTDataType invalid = '#';
    CreateBinTree(&PRoot, str, strlen(str), invalid);

    printf("PreOrder:");
    PreOrder(PRoot);
    printf("\n");

    //LevelOrder(PRoot);
    printf("\n");

    PNewTree = CopyBinTree(PRoot);
    printf("PreOrder:");
    PreOrder(PNewTree);
    printf("\n");

    printf("二叉树结点的个数为:%d\n", BinTreeSize(PRoot));
    printf("二叉树叶子结点的个数为:%d\n", GetLeafCount(PRoot));
    printf("二叉树的高度为:%d\n", Height(PRoot));

    DestroyBinTree(&PRoot);
    DestroyBinTree(&PNewTree);
}

int main()
{
    TestBinTree();
    system("pause");
    return 0;
}

这是我自己的运行结果:

以上就是二叉树的一些基本操作,有建议的大佬们欢迎沟通。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值