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
二叉树(C++)详解
最新推荐文章于 2024-05-26 10:39:20 发布
本文详细介绍了二叉树的概念、类型,并通过C++代码示例解析了二叉树的插入、删除、遍历等操作,帮助读者深入理解二叉树的数据结构及其算法应用。
摘要由CSDN通过智能技术生成