二叉树(C++)详解

本文详细介绍了二叉树的概念、类型,并通过C++代码示例解析了二叉树的插入、删除、遍历等操作,帮助读者深入理解二叉树的数据结构及其算法应用。
摘要由CSDN通过智能技术生成
pragma once
#include<iostream>
#include<stack>
#include<queue>
using namespace std;

template<class T>
 struct Node
{
	struct Node<T>* _left;
	struct Node<T>* _right;
	T _data;
	Node(T data = T())
		:_left(NULL)
		,_right(NULL)
		,_data(data)
	{}
};


template<class T>
class Tree
{
	typedef Node<T> Node;
	typedef Node* pNode;
public:
	Tree()
		:_root(NULL)
	{}

	Tree(const T* a, size_t size, int pi)
	{
		_root = BTCreate(a, size, pi);
	}

	Tree(const Tree<T>& t)
	{
		_root = CopyTree(t._root);
	}

	~Tree()
	{
		BTDestory(_root);
	}

	pNode CopyTree(const pNode _root)
	{
		if (_root == NULL)
			return NULL;
		pNode root = BTBuyNode(_root->_data);
		root->_left = CopyTree(_root->_left);
		root->_right = CopyTree(_root->_right);
		return root;
	}
	pNode BTCreate(const T* a, int n, int& pi)
	{
		pNode root = NULL;
		if (pi < n && a[pi] != '#')
		{
			root = BTBuyNode(a[pi]);
			root->_left = BTCreate(a, n, ++pi);
			root->_right = BTCreate(a, n, ++pi);
		}
		return root;
	}

	pNode BTBuyNode(T x)
	{
		pNode node = new Node;
		node->_left = NULL;
		node->_right = NULL;
		node->_data = x;
		return node;
	}
	void
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值