#include<iostream>
#include<stdlib.h>
using namespace std;
#include<stack>
#include<queue>
template<class T>
struct BinaryTreeNode
{
BinaryTreeNode(const T& data)//结点
: _data(data)
, _pLeft(NULL)
, _pRight(NULL)
{}
T _data;
BinaryTreeNode<T>* _pLeft; // 左孩子
BinaryTreeNode<T>* _pRight; // 右孩子
};
template<class T>
class BinaryTree
{
typedef BinaryTreeNode<T> Node;//起了一个别名
public:
BinaryTree()
: _data(0)
, _pLeft(NULL)
, _pRight(NULL)
{}
BinaryTree(T array[], size_t size, const T invalid)
:_pRoot(NULL)
{
// 创建树
size_t index = 0;
_pRoot = _CreateTree(_pRoot, array, size, index, invalid);
}
BinaryTree(const BinaryTree<T>& t) //拷贝构造
{
_pRoot = _CopyBinaryTree(t._pRoot);
}
BinaryTree<T>& operator=(const BinaryTree<T>& t)//赋值运算符的重载
{
if (this != &t)
{
_pRoot = _CopyBinaryTree(t._pRoot);
_pLeft = _CopyBinaryTree(t._pLeft);
_pRight = _CopyBinaryTree(t._pRight);
}
return *this;
}
Node* _CopyBinaryTree()
{
Node *_root = NULL;
if (NULL != _pRoot)
{
_root = new Node(pRoot->_data);
_root->_pLeft = _CopyBinaryTree(pRoot->_pLeft);
_root->_pRight = _CopyBinaryTree(pRoot->_pRight);
}
return _root;
}
#include<stdlib.h>
using namespace std;
#include<stack>
#include<queue>
template<class T>
struct BinaryTreeNode
{
BinaryTreeNode(const T& data)//结点
: _data(data)
, _pLeft(NULL)
, _pRight(NULL)
{}
T _data;
BinaryTreeNode<T>* _pLeft; // 左孩子
BinaryTreeNode<T>* _pRight; // 右孩子
};
template<class T>
class BinaryTree
{
typedef BinaryTreeNode<T> Node;//起了一个别名
public:
BinaryTree()
: _data(0)
, _pLeft(NULL)
, _pRight(NULL)
{}
BinaryTree(T array[], size_t size, const T invalid)
:_pRoot(NULL)
{
// 创建树
size_t index = 0;
_pRoot = _CreateTree(_pRoot, array, size, index, invalid);
}
BinaryTree(const BinaryTree<T>& t) //拷贝构造
{
_pRoot = _CopyBinaryTree(t._pRoot);
}
BinaryTree<T>& operator=(const BinaryTree<T>& t)//赋值运算符的重载
{
if (this != &t)
{
_pRoot = _CopyBinaryTree(t._pRoot);
_pLeft = _CopyBinaryTree(t._pLeft);
_pRight = _CopyBinaryTree(t._pRight);
}
return *this;
}
Node* _CopyBinaryTree()
{
Node *_root = NULL;
if (NULL != _pRoot)
{
_root = new Node(pRoot->_data);
_root->_pLeft = _CopyBinaryTree(pRoot->_pLeft);
_root->_pRight = _CopyBinaryTree(pRoot->_pRight);
}
return _root;
}
void DestroyTree()
{
_DestroyTree(Node* &pRoot)
}
// 先序遍历
void PreOrder()
{
_PreOrder(_pRoot);
}
void PreOrder_pre()
{
stack<Node*> s;
Node *p = _pRoot;
while (p != NULL || !s.empty())
{
while (p != NULL)
{
cout << p->_data;
s.push(p);
p = p->_pLeft;
}
if (!s.empty())
{
p = s.top();
s.pop();
p = p->_pRight;
}
}
}
// 中序遍历:访问左子树-->根-->右子树
void InOrder()
{
_InOrder(_pRoot);
}
void InOrder_pre()
{
stack<Node*> s;
Node *p = _pRoot;
while (p != NULL || !s.empty())
{
while (p != NULL)
{
s.push(p);
p = p->_pLeft;
}
if (!s.empty())
{
p = s.top();
cout << p->_data;
s.pop();
p = p->_pRight;
}
}
}
// 后续遍历:访问左子树-->访问右子树-->访问根节点
void PostOrder()
{
_PostOrder(_pRoot);
}
// 层序遍历
void PostOrder_pre()
{
_PostOrder_pre();
}
void LevelOrder()
{
_LevelOrder();
}
~BinaryTree()
{
_DestroyTree(_pRoot);
}
void mirror()
{
_mirror(_pRoot);
PreOrder();
}
void mirror_pre() //镜像
{
_mirror_pre(_pRoot);
PreOrder();
}
Node* Find(const T& value)
{
return _Find(_pRoot, value);
}
size_t Height()
{
return _Height(_pRoot);
}
size_t GetLeefNode()
{
return _GetLeefNode(_pRoot);
}
size_t GetKLevelNode(size_t k)
{
return _GetKLevelNode(_pRoot, k);
}
private:
Node* _Find(Node* pRoot, const T& value)
{
if (pRoot == NULL)
{
return 0;
}
if (pRoot->_data == value)
return pRoot->_data;
return value > pRoot->_data ? _Find(pRoot->_pRight) : _Find(pRoot->_pLeft);
}
{
_DestroyTree(Node* &pRoot)
}
// 先序遍历
void PreOrder()
{
_PreOrder(_pRoot);
}
void PreOrder_pre()
{
stack<Node*> s;
Node *p = _pRoot;
while (p != NULL || !s.empty())
{
while (p != NULL)
{
cout << p->_data;
s.push(p);
p = p->_pLeft;
}
if (!s.empty())
{
p = s.top();
s.pop();
p = p->_pRight;
}
}
}
// 中序遍历:访问左子树-->根-->右子树
void InOrder()
{
_InOrder(_pRoot);
}
void InOrder_pre()
{
stack<Node*> s;
Node *p = _pRoot;
while (p != NULL || !s.empty())
{
while (p != NULL)
{
s.push(p);
p = p->_pLeft;
}
if (!s.empty())
{
p = s.top();
cout << p->_data;
s.pop();
p = p->_pRight;
}
}
}
// 后续遍历:访问左子树-->访问右子树-->访问根节点
void PostOrder()
{
_PostOrder(_pRoot);
}
// 层序遍历
void PostOrder_pre()
{
_PostOrder_pre();
}
void LevelOrder()
{
_LevelOrder();
}
~BinaryTree()
{
_DestroyTree(_pRoot);
}
void mirror()
{
_mirror(_pRoot);
PreOrder();
}
void mirror_pre() //镜像
{
_mirror_pre(_pRoot);
PreOrder();
}
Node* Find(const T& value)
{
return _Find(_pRoot, value);
}
size_t Height()
{
return _Height(_pRoot);
}
size_t GetLeefNode()
{
return _GetLeefNode(_pRoot);
}
size_t GetKLevelNode(size_t k)
{
return _GetKLevelNode(_pRoot, k);
}
private:
Node* _Find(Node* pRoot, const T& value)
{
if (pRoot == NULL)
{
return 0;
}
if (pRoot->_data == value)
return pRoot->_data;
return value > pRoot->_data ? _Find(pRoot->_pRight) : _Find(pRoot->_pLeft);
}
size_t _Height(Node* pRoot)
{
if (pRoot == NULL)
{
return 0;
}
size_t leftHigh = _Height(pRoot->_pLeft);
size_t rightHigh = _Height(pRoot->_pRight);
return leftHigh > rightHigh ? leftHigh + 1 : rightHigh + 1;
}
size_t _GetLeefNode(Node* pRoot)
{
if (pRoot == NULL)
{
return 0;
}
if (pRoot->_pLeft == NULL&&pRoot->_pRight == NULL)
{
return 1;
}
return _GetLeefNode(pRoot->_pLeft) + _GetLeefNode(pRoot->_pRight) + 1;
}
size_t _GetKLevelNode(Node* pRoot, size_t k)
{
if (pRoot == NULL)
{
return 0;
}
if (k==1)
{
return 1;
}
size_t s1 = _GetKLevelNode( pRoot->_pLeft,k-1);
size_t s2 = _GetKLevelNode(pRoot->_pRight, k - 1);
return s1 + s2;
}
Node* _CreateTree(Node*& _pRoot, const T array[], size_t size, size_t& index, const T& invalid)
{
{
if (pRoot == NULL)
{
return 0;
}
size_t leftHigh = _Height(pRoot->_pLeft);
size_t rightHigh = _Height(pRoot->_pRight);
return leftHigh > rightHigh ? leftHigh + 1 : rightHigh + 1;
}
size_t _GetLeefNode(Node* pRoot)
{
if (pRoot == NULL)
{
return 0;
}
if (pRoot->_pLeft == NULL&&pRoot->_pRight == NULL)
{
return 1;
}
return _GetLeefNode(pRoot->_pLeft) + _GetLeefNode(pRoot->_pRight) + 1;
}
size_t _GetKLevelNode(Node* pRoot, size_t k)
{
if (pRoot == NULL)
{
return 0;
}
if (k==1)
{
return 1;
}
size_t s1 = _GetKLevelNode( pRoot->_pLeft,k-1);
size_t s2 = _GetKLevelNode(pRoot->_pRight, k - 1);
return s1 + s2;
}
Node* _CreateTree(Node*& _pRoot, const T array[], size_t size, size_t& index, const T& invalid)
{
if (index < size&&array[index] != invalid)
{
_pRoot = new Node(array[index]);
_pRoot->_pLeft = _CreateTree(_pRoot->_pLeft, array, size, ++index, invalid);
_pRoot->_pRight = _CreateTree(_pRoot->_pRight, array, size, ++index, invalid);
}
return _pRoot;
}
Node* _CopyBinaryTree(Node* pRoot);
void _DestroyTree(Node* &_pRoot)
{
if (_pRoot == NULL)
{
return;
}
if (_pRoot->_pLeft)
{
_DestroyTree(_pRoot->_pLeft);
}
if (_pRoot->_pRight)
{
_DestroyTree(_pRoot->_pRight);
}
delete _pRoot;
_pRoot = NULL;
}
void _PreOrder(Node*_pRoot)
{
if (_pRoot == NULL)
{
return;
}
cout << _pRoot->_data << "";
_PreOrder(_pRoot->_pLeft);
_PreOrder(_pRoot->_pRight);
}
void _InOrder(Node*_pRoot)
{
if (_pRoot == NULL)
{
return;
}
_InOrder(_pRoot->_pLeft);
cout << _pRoot->_data << "";
_InOrder(_pRoot->_pRight);
}
{
_pRoot = new Node(array[index]);
_pRoot->_pLeft = _CreateTree(_pRoot->_pLeft, array, size, ++index, invalid);
_pRoot->_pRight = _CreateTree(_pRoot->_pRight, array, size, ++index, invalid);
}
return _pRoot;
}
Node* _CopyBinaryTree(Node* pRoot);
void _DestroyTree(Node* &_pRoot)
{
if (_pRoot == NULL)
{
return;
}
if (_pRoot->_pLeft)
{
_DestroyTree(_pRoot->_pLeft);
}
if (_pRoot->_pRight)
{
_DestroyTree(_pRoot->_pRight);
}
delete _pRoot;
_pRoot = NULL;
}
void _PreOrder(Node*_pRoot)
{
if (_pRoot == NULL)
{
return;
}
cout << _pRoot->_data << "";
_PreOrder(_pRoot->_pLeft);
_PreOrder(_pRoot->_pRight);
}
void _InOrder(Node*_pRoot)
{
if (_pRoot == NULL)
{
return;
}
_InOrder(_pRoot->_pLeft);
cout << _pRoot->_data << "";
_InOrder(_pRoot->_pRight);
}
void _PostOrder(Node*_pRoot)
{
if (_pRoot == NULL)
{
return;
}
_PostOrder(_pRoot->_pLeft);
_PostOrder(_pRoot->_pRight);
cout << _pRoot->_data << "";
}
{
if (_pRoot == NULL)
{
return;
}
_PostOrder(_pRoot->_pLeft);
_PostOrder(_pRoot->_pRight);
cout << _pRoot->_data << "";
}
void _PostOrder_pre()
{
if (_pRoot == NULL)
return;
stack<Node*>s;
Node*cur = _pRoot;
Node*cet = NULL;
while (cur || !s.empty())
{
while (cur) //找到最左边的那个结点
{
s.push(cur);
cur = cur->_pLeft;
}
Node*ps = s.top();
if (ps->_pRight == NULL || ps->_pRight == cet)
{
cout << ps->_data<<"";
s.pop();
cet = ps;
}
else
{
cur = ps->_pRight;
}
}
}
void _LevelOrder()
{
queue <Node*>p;
if (_pRoot != NULL)
{
p.push(_pRoot);
}
while (!p.empty())
{
Node*pcur = p.front();
cout << pcur->_data <<"";
p.pop();
if (pcur->_pLeft!=NULL)
p.push(pcur->_pLeft);
if (pcur->_pRight != NULL)
p.push(pcur->_pRight);
}
}
void _mirror(Node *_pRoot)//递归实现镜像
{
if (NULL == _pRoot)
{
return;
}
if (NULL == _pRoot->_pLeft || NULL == _pRoot->_pRight)
{
return;
}
Node* temp = _pRoot->_pLeft;
_pRoot->_pLeft = _pRoot->_pRight;
_pRoot->_pRight = temp;
if (_pRoot->_pLeft)
{
_mirror(_pRoot->_pLeft);
}
if (_pRoot->_pRight)
{
_mirror(_pRoot->_pRight);
}
}
void _mirror_pre(Node *_pRoot)//非递归实现镜像
{
stack<Node*>s;
if (NULL == _pRoot)
{
return;
}
Node* temp = _pRoot->_pLeft;
_pRoot->_pLeft = _pRoot->_pRight;
_pRoot->_pRight = temp;
while (s.size())
{
_pRoot = s.top();
s.pop();
if (NULL != _pRoot->_pLeft || NULL != _pRoot->_pRight)
{
Node*temp = _pRoot->_pLeft;
_pRoot->_pLeft = _pRoot->_pRight;
_pRoot->_pRight = temp;
}
if (NULL != _pRoot->_pLeft)
s.push(_pRoot->_pLeft);
if (NULL != _pRoot->_pRight)
s.push(_pRoot->_pRight);
}
}
private:
BinaryTreeNode<T>* _pRoot;
};
int main()
{
int ret = 0;
int cet = 0;
char *str = "124###35##6";
BinaryTree<char>t(str, strlen(str), '#');
t.InOrder();
cout << endl;
t.InOrder_pre();
cout << endl;
t.PreOrder();
cout << endl;
t.PreOrder_pre();
cout << endl;
t.PostOrder();
cout << endl;
t.PostOrder_pre();
cout << endl;
t.LevelOrder();//层序
{
if (_pRoot == NULL)
return;
stack<Node*>s;
Node*cur = _pRoot;
Node*cet = NULL;
while (cur || !s.empty())
{
while (cur) //找到最左边的那个结点
{
s.push(cur);
cur = cur->_pLeft;
}
Node*ps = s.top();
if (ps->_pRight == NULL || ps->_pRight == cet)
{
cout << ps->_data<<"";
s.pop();
cet = ps;
}
else
{
cur = ps->_pRight;
}
}
}
void _LevelOrder()
{
queue <Node*>p;
if (_pRoot != NULL)
{
p.push(_pRoot);
}
while (!p.empty())
{
Node*pcur = p.front();
cout << pcur->_data <<"";
p.pop();
if (pcur->_pLeft!=NULL)
p.push(pcur->_pLeft);
if (pcur->_pRight != NULL)
p.push(pcur->_pRight);
}
}
void _mirror(Node *_pRoot)//递归实现镜像
{
if (NULL == _pRoot)
{
return;
}
if (NULL == _pRoot->_pLeft || NULL == _pRoot->_pRight)
{
return;
}
Node* temp = _pRoot->_pLeft;
_pRoot->_pLeft = _pRoot->_pRight;
_pRoot->_pRight = temp;
if (_pRoot->_pLeft)
{
_mirror(_pRoot->_pLeft);
}
if (_pRoot->_pRight)
{
_mirror(_pRoot->_pRight);
}
}
void _mirror_pre(Node *_pRoot)//非递归实现镜像
{
stack<Node*>s;
if (NULL == _pRoot)
{
return;
}
Node* temp = _pRoot->_pLeft;
_pRoot->_pLeft = _pRoot->_pRight;
_pRoot->_pRight = temp;
while (s.size())
{
_pRoot = s.top();
s.pop();
if (NULL != _pRoot->_pLeft || NULL != _pRoot->_pRight)
{
Node*temp = _pRoot->_pLeft;
_pRoot->_pLeft = _pRoot->_pRight;
_pRoot->_pRight = temp;
}
if (NULL != _pRoot->_pLeft)
s.push(_pRoot->_pLeft);
if (NULL != _pRoot->_pRight)
s.push(_pRoot->_pRight);
}
}
private:
BinaryTreeNode<T>* _pRoot;
};
int main()
{
int ret = 0;
int cet = 0;
char *str = "124###35##6";
BinaryTree<char>t(str, strlen(str), '#');
t.InOrder();
cout << endl;
t.InOrder_pre();
cout << endl;
t.PreOrder();
cout << endl;
t.PreOrder_pre();
cout << endl;
t.PostOrder();
cout << endl;
t.PostOrder_pre();
cout << endl;
t.LevelOrder();//层序
cout << endl;
t.mirror();//镜像
t.mirror();//镜像
cout << endl;
t.mirror_pre();
/*ret = t.Height();
cout << ret << "";*/
cet= t.GetLeefNode();
cout << cet <<endl;
ret = t.GetKLevelNode(2);
cout << ret << endl;
system("pause");
return 0;
}
t.mirror_pre();
/*ret = t.Height();
cout << ret << "";*/
cet= t.GetLeefNode();
cout << cet <<endl;
ret = t.GetKLevelNode(2);
cout << ret << endl;
system("pause");
return 0;
}
运行结果: