二叉树的简单实现
#include <iostream>
#include <queue>
#include<stack>
#include <cstdlib>
using namespace std;
template<class T>
struct BinaryTreeNode
{
BinaryTreeNode<T>* _left;
BinaryTreeNode<T>* _right;
T _date;
BinaryTreeNode(const T& x)
:_date(x)
, _left(NULL)
, _right(NULL)
{}
};
template<class T>
class BinaryTree
{
typedef BinaryTreeNode<T> Node;
public:
BinaryTree()
:_root(NULL)
{
cout << "BinaryTree()" << endl;
}
BinaryTree(const T* a, size_t size, const T& invalid)
{
size_t index = 0;
_root = _BinaryTree(a, size, index, invalid);
cout << "BinaryTree(const T* a, size_t size, const T& invalid)" << endl;
}
BinaryTree(const BinaryTree<T>& t)
{
_root = _Copy(t._root);
cout << "BinaryTree(const BinaryTree<T>& t)" << endl;
}
BinaryTree<T>& operator=(const BinaryTree<T>& t)
{
if (this != &t)
{
Node* tmp = _Copy(t._root);
_root = _Destory(_root);
_root = tmp;
}
return *this;
}
~BinaryTree()
{
_root = _Destory(_root);
cout << "~BinaryTree()" << endl;
}
void PrevOrder()//前序遍历
{
_PrevOrder(_root);
cout << endl;
}
void InOrder()//中序遍历
{
_InOrder(_root);
cout << endl;
}
void PostOrder()//后序遍历
{
_PostOrder(_root);
cout << endl;
}
void LevelOrder()//层遍历
{
_LevelOrder(_root);
cout << endl;
}
size_t Size()//求节点个数
{
return _Size(_root);
}
size_t Depth()//求树的深度
{
return _Depth(_root);
}
size_t LeafSize()//求叶子节点个数
{
return _LeafSize(_root);
}
size_t GetKLevel(size_t k)//求第K层叶子节点个数
{
return _GetKLevel(_root,k);
}
protected:
Node* _BinaryTree(const T* a, size_t size, size_t& index, const T& invalid)
{
Node* NewNode = NULL;
if (index < size && a[index] != invalid)
{
NewNode = new Node(a[index]);
NewNode->_left = _BinaryTree(a, size, ++index, invalid);
NewNode->_right = _BinaryTree(a, size, ++index, invalid);
}
return NewNode;
}
Node* _Copy(Node* root)
{
Node* NewNode = NULL;
if (root != NULL)
{
NewNode = new Node(root->_date);
NewNode->_left = _Copy(root->_left);
NewNode->_right = _Copy(root->_right);
}
return NewNode;
}
Node* _Destory(Node* root)
{
if (root != NULL)
{
_Destory(root->_left);
_Destory(root->_right);
delete root;
root = NULL;
}
return root;
}
void _PrevOrder(Node* root)
{
if (root != NULL)
{
cout << root->_date << " " ;
_PrevOrder(root->_left);
_PrevOrder(root->_right);
}
}
void _InOrder(Node* root)
{
if (root != NULL)
{
_InOrder(root->_left);
cout << root->_date << " ";
_InOrder(root->_right);
}
}
void _PostOrder(Node* root)
{
if (root != NULL)
{
_PostOrder(root->_left);
_PostOrder(root->_right);
cout << root->_date << " ";
}
}
void _LevelOrder(Node* root)
{
queue<Node*> q;
q.push(root);
while (!q.empty())
{
Node* tmp = q.front();
cout << tmp->_date << " ";
q.pop();
if (tmp->_left != NULL)
{
q.push(tmp->_left);
}
if (tmp->_right != NULL)
{
q.push(tmp->_right);
}
}
}
size_t _Size(Node* root)
{
size_t size = 0;
if (root != NULL)
{
size += 1;
size += _Size(root->_left);
size += _Size(root->_right);
}
return size;
}
size_t _Depth(Node* root)
{
if (root == NULL)
return 0;
int left = _Depth(root->_left);
int right = _Depth(root->_right);
return left > right ? left + 1 : right + 1;
}
size_t _LeafSize(Node* root)
{
size_t size = 0;
if (root != NULL)
{
if (root->_left == NULL && root->_right == NULL)
{
size += 1;
}
size += _LeafSize(root->_left);
size += _LeafSize(root->_right);
}
return size;
}
size_t _GetKLevel(Node* root,size_t k)
{
if (root == NULL)
return 0;
if (k == 1)
return 1;
return _GetKLevel(root->_left, k - 1) + _GetKLevel(root->_right, k - 1);
}
protected:
Node* _root;
};
void TestBinaryTree()
{
int a[10] = {1,2,3,'#','#',4,'#','#',5,6};
int a2[15] = { 1, 2, '#', 3, '#', '#', 4, 5, '#', 6, '#', 7, '#', '#', 8 };
BinaryTree<int> b(a, 10, '#');
BinaryTree<int> e(a2, 15, '#');
b.PostOrder();
/*b.PrevOrder();
b.InOrder();
/*e.PrevOrder();
e.InOrder();
/*b.InOrder();
b.PostOrder();
b.LevelOrder();
BinaryTree<int> c(b);
c.PrevOrder();
c.InOrder();
c.PostOrder();
c.LevelOrder();
BinaryTree<int> d;
d = c;
d.PrevOrder();
d.InOrder();
d.PostOrder();
d.LevelOrder();
cout<<"size:"<<b.Size()<<endl;*/
/*BinaryTree<int> e(a2, 15, '#');
e.PrevOrder();
cout <<"b.Depth(): "<< b.Depth() << endl;
cout << "e.Depth(): "<<e.Depth() << endl;
cout << "b.LeafSize(): "<<b.LeafSize()<<endl;
cout << "e.LeafSize(): "<<e.LeafSize()<<endl;
cout <<"b.GetKLevel(2): "<< b.GetKLevel(2) << endl;
cout <<"e.GetKLevel(4): "<< e.GetKLevel(4) << endl;*/
}
int main()
{
TestBinaryTree();
return 0;
}