二叉树C++实现数据结构实验

#include <iostream>
#include <string.h>
#include <stack>
#include <queue>
using namespace std;
template<class T>
struct BiNode								//二叉数节点
{
	T data;
	BiNode<T>* lchild, *rchild;

};

template<class T>							//模板类
class BiTree
{
public:
	BiTree();								//默认构造函数
	~BiTree();								//析构函数
	BiNode<T>* GetRoot();					//返回根节点
	void PreOrder(BiNode<T>* node);			//先序遍历
	void InOrder(BiNode<T>* node);			//中序遍历
	void PostOrder(BiNode<T>* node);		//后序遍历
	void LevelOrder(BiNode<T>* node);		//层次遍历


	//选做非递归实现
	void PreOrderNonRec(BiNode<T>* node);	//先
	void InOrderNonRec(BiNode<T>* node);	//中
	void PostOrderNonRec(BiNode<T>* node);	//后


	//选做深度,节点数,叶子节点数,
	int NodesNum(BiNode<T>* node);			//节点数
	int TreeDepth(BiNode<T>* node);			//深度
	int LeafNum(BiNode<T>* node);			//叶子节点数


	//选做交换子树
	void SwapChild(BiNode<T>* node);		//交换子树


private:
	BiNode<T>* m_root;						//获取根节点
	BiNode<T>* Create();					//创建二叉树
};

template<class T>
BiTree<T>::BiTree()
{
	m_root = new BiNode<T>;
	m_root = Create();
}

template<class T>
BiTree<T>::~BiTree(){}

template<class T>
BiNode<T>* BiTree<T>::Create()				//1. 按先序序列构造一棵二叉链表表示的二叉树T;
{
	char ch=getchar();
	BiNode<T>* pnode;

	if (ch == ' ')
		pnode = NULL;
	else
	{
		pnode = new BiNode<T>;
		pnode->data = ch;
		pnode->lchild = Create();
		pnode->rchild = Create();
	}
	return pnode;
}

tem
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值