二叉树的C++实现代码

这篇博客详细介绍了如何使用C++实现二叉树的数据结构,包括头文件`MyBiTree.h`、源文件`MyBiTree.cpp`以及相关的`BiTree.cpp`。通过对这些代码的分析,读者可以理解二叉树的基本操作和实现细节。
摘要由CSDN通过智能技术生成

MyBiTree.h

#pragma once
#include <memory.h>

// 	IsEmpty():若树为空,返回true,否则返回false
// 	GetDepth():获得树的深度

// Destroy():销毁一颗树
// 	Clear():清空一棵树

// 	GetRoot(T):返回树的根节点到T中
// 	GetValue(T):返回指定节点T的值

// 	PreOrderTraverse():前序遍历整棵树
// 	InOrderTraverse():中序遍历整棵树
// 	PostOrderTraverse():后序遍历整棵树

// 	Insert(e): 在树中插入新节点,并将值设为e
// 	Delete(e):将值为e的节点在树中删除


class MyBiTree
{
public:
	typedef struct _NODE
	{
		_NODE* pLeft;
		_NODE* pRight;
		int    nData;
	}NODE,*PNODE;
	MyBiTree(void);
	~MyBiTree(void);
public:
	bool IsEmpty(); // 判断树是否为空
	int  GetDepth(PNODE pNode)
	{
		if (pNode==nullptr)
		{
			return 0;
		}
		int nLeftDepth = GetDepth(pNode->pLeft);

		int nRightDepth = GetDepth(pNode->pRight);
		if (nLeftDepth>nRightDepth)
		{
			return nLeftDepth+1;
		}
		else
		{
			return nRightDepth+1;
		}
// 		return nLeftDepth>nRightDepth?
// 		nLeftDepth:nRightDepth;
	}

	void PreOrderTraverse()//前序遍
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值