C语言二叉树的递归遍历、节点数目、二叉树高度、拷贝及释放

#include <stdio.h>
#include <stdlib.h>

typedef struct BinaryTree {
    int value;
    struct BinaryTree* Left;
    struct BinaryTree* Right;  
}BinaryTrees;

void pre_trival(BinaryTrees *p_node) {
    if(p_node == NULL)
        return;
    printf("%d ", p_node -> value);
    pre_trival(p_node -> Left);
    pre_trival(p_node -> Right);
}

void mid_trival(BinaryTrees *p_node) {
    if(p_node == NULL)
        return;
    mid_trival(p_node -> Left);
    printf("%d ", p_node -> value);
    mid_trival(p_node -> Right);
}

void hid_trival(BinaryTrees *p_node) {
    if(p_node == NULL)
        return;
    hid_trival(p_node -> Left);
    hid_trival(p_node -> Right);
    printf("%d ", p_node -> value);
}

void node_tree(BinaryTrees *p_node, int &nums) {
    if(p_node == NULL)
        return;
    if(p_node -> Left == NULL && p_node -> Right == NULL)
        nums ++;
    node_tree(p_node -> Left, nums);
    node_tree(p_node -> Right, nums);
}

int height_tree(BinaryTrees *p_node) {
    if(p_node == NULL)
        return 0;
    int left_height = height_tree(p_node -> Left);
    int right_height = height_tree(p_node -> Right);
    return left_height > right_height? (left_height + 1) : (right_height + 1);
}

BinaryTrees *Node_copy(BinaryTrees *p_node) {
    if(p_node == NULL)
        return NULL;
    BinaryTrees *node_left = Node_copy(p_node -> Left);
    BinaryTrees *node_right = Node_copy(p_node -> Right);
    BinaryTrees *new_node = (BinaryTrees *)malloc(sizeof(BinaryTrees));
    new_node -> Left = node_left;
    new_node -> Right = node_right;
    new_node -> value = p_node -> value;
    return new_node;
}

void Destory_Tree(BinaryTrees *p_node) {
    if(p_node == NULL)
        return;
    Destory_Tree(p_node -> Left);
    Destory_Tree(p_node -> Right);
    free(p_node);
}

int main() {
    BinaryTrees n1 = {1, NULL, NULL};
    BinaryTrees n2 = {2, NULL, NULL};
    BinaryTrees n3 = {3, NULL, NULL};
    BinaryTrees n4 = {4, NULL, NULL};
    BinaryTrees n5 = {5, NULL, NULL};
    BinaryTrees n6 = {6, NULL, NULL};
    BinaryTrees n7 = {7, NULL, NULL};
    BinaryTrees n8 = {8, NULL, NULL};
    n1.Left = &n2;
    n1.Right = &n3;
    n2.Left = &n4;
    n2.Right = &n5;
    n3.Left = &n6;
    n3.Right = &n7;
    n4.Left = &n8;
    printf("---开始前序遍历---\n");
    pre_trival(&n1);printf("\n");
    printf("---开始中序遍历---\n");
    mid_trival(&n1);printf("\n");
    printf("---开始后序遍历---\n");
    hid_trival(&n1);printf("\n");

    int num_help = 0;
    node_tree(&n1, num_help);
    printf("---二叉树的叶子数量:%d---\n", num_help);
    printf("---二叉树的高度:%d---\n", height_tree(&n1));

    printf("---开始拷贝二叉树---\n");
    BinaryTrees *new_tree = Node_copy(&n1);
    printf("---拷贝二叉树完成---\n");
    printf("---开始前序遍历---\n");
    pre_trival(new_tree);printf("\n");

    printf("---开始销毁二叉树---\n");
    Destory_Tree(new_tree);
    printf("---销毁二叉树完成---\n");

    system("pause");
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值