剑指offer刷题笔记七:重建二叉树(前导:之 一文搞懂二叉树)

学习的过程是一个由浅入深,由表及里的过程,今天来浅谈以下数据结构之二叉树,究竟是怎么写出来的。

代码如下:
BinaryTreeNode.h文件

#pragma once
#include<iostream>
struct BinaryTreeNode {
	int m_nValue;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;
};

BinaryTreeNode* CreateBinaryTreeNode(int value);
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);
void PrintTreeNode(const BinaryTreeNode* pNode);
void PrintTree(const BinaryTreeNode* pRoot);
void DestroyTree(BinaryTreeNode* pRoot);

在头文件中,首先定义一个二叉树结点的定义。其中,二叉树主要有数据域和指针域两个域构成,指针指向两个其余的子节点,数据存放当前结点的数据。然后,包含一些辅助函数的声明。

BinaryTreeNode.cpp文件

#include"BinaryTree.h"
BinaryTreeNode* CreateBinaryTreeNode(int value) {
	BinaryTreeNode* pNode = new BinaryTreeNode();
	pNode->m_nValue = value;
	pNode->m_pLeft = nullptr;
	pNode->m_pRight = nullptr;

    return pNode;
}

void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight) {
	if (pParent != nullptr) {
		pParent->m_pLeft = pLeft;
		pParent->m_pRight = pRight;
	}
}

void PrintTreeNode(const BinaryTreeNode* pNode) {
    if (pNode != nullptr)
    {
        printf("value of this node is: %d\n", pNode->m_nValue);

        if (pNode->m_pLeft != nullptr)
            printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);
        else
            printf("left child is nullptr.\n");

        if (pNode->m_pRight != nullptr)
            printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);
        else
            printf("right child is nullptr.\n");
    }
    else
    {
        printf("this node is nullptr.\n");
    }

    printf("\n");
}

void PrintTree(const BinaryTreeNode* pRoot) {
    PrintTreeNode(pRoot);

    if (pRoot != nullptr)
    {
        if (pRoot->m_pLeft != nullptr)
            PrintTree(pRoot->m_pLeft);

        if (pRoot->m_pRight != nullptr)
            PrintTree(pRoot->m_pRight);
    }
}

void DestroyTree(BinaryTreeNode* pRoot) {
    if (pRoot != nullptr)
    {
        BinaryTreeNode* pLeft = pRoot->m_pLeft;
        BinaryTreeNode* pRight = pRoot->m_pRight;

        delete pRoot;
        pRoot = nullptr;

        DestroyTree(pLeft);
        DestroyTree(pRight);
    }
}

以上为相关辅助函数的实现。
main.cpp文件

#include"BinaryTree.h"

int main() {
	BinaryTreeNode* btNode1 = CreateBinaryTreeNode(1);
	BinaryTreeNode* btNode2 = CreateBinaryTreeNode(2);
	BinaryTreeNode* btNode3 = CreateBinaryTreeNode(3);
	BinaryTreeNode* btNode4 = CreateBinaryTreeNode(4);

	ConnectTreeNodes(btNode1, btNode2, btNode3);
	ConnectTreeNodes(btNode3, btNode4, nullptr);

	PrintTree(btNode1);

	DestroyTree(btNode1);
	return 0;
}

主函数中定义了四个结点,结构如下

        1
       / \
      2   3
         /
        4 

先创建结点,利用参数初始化数据域,然后将指针初始化问nullptr;
其次,利用辅助函数将各结点连接起来。
然后,将二叉树整体打印输出,其中利用了递归结构
最后,将在堆区开辟的内存释放。
程序运行结束。

运行结果:
在这里插入图片描述
一步步开始,阶梯式进步,欢迎各位评论点赞,一起交流,共同进步。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cherries Man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值