#include <iostream>
#include <queue>
#include <stack>
using namespace std;
template<
typename T>
struct BinaryNode
{
BinaryNode()
: _pData(
0)
, _pLeftChild(NULL)
, _pRightChild(NULL)
{}
T _pData;
BinaryNode<T>* _pLeftChild;
BinaryNode<T>* _pRightChild;
typedef BinaryNode<T> Node;
};
template<
typename T>
class BinartyTree
{
typedef BinaryNode<T> Node;
public:
BinartyTree()
{}
BinartyTree(
const T* arr,
int size, T& invalid)
:_invalid(invalid)
{
int index =
0;
CreatBinTree(_pRoot, arr, size, _invalid, index);
}
BinartyTree(
const BinartyTree<T>& bt)
{
Destroy(_pRoot);
_invalid = bt._invalid;
Copy(_pRoot, bt._pRoot, _invalid);
}
~BinartyTree()
{
Destroy(_pRoot);
}
void FrontOrder()
{
cout <<
"FrontOrder:" <<
" ";
_FrontOrder(_pRoot);
cout << endl << endl;
}
void FrontOrder_Nor()
{
if (NULL == _pRoot)
return;
cout <<
"FrontOrder_Nor: ";
stack<Node*> s;
s.push(_pRoot);
while (!s.empty())
{
Node* tmp = s.top();
cout << tmp->_pData <<
" ";
s.pop();
if (tmp->_pRightChild)
s.push(tmp->_pRightChild);
if (tmp->_pLeftChild)
s.push(tmp->_pLeftChild);
}
cout << endl << endl;
}
void InOrder()
{
cout <<
"InOrder:" <<
" ";
_InOrder(_pRoot);
cout << endl << endl;
}
void InOrder_Nor()
{
if (NULL == _pRoot)
return;
cout <<
"InOrder_Nor: ";
stack<Node*> s;
Node* temp = _pRoot;
while (temp || !s.empty())
{
while (temp)
{
s.push(temp);
temp = temp->_pLeftChild;
}
temp = s.top();
s.pop();
cout << temp->_pData <<
" ";
temp = temp->_pRightChild;
}
cout << endl << endl;
}
void PostOrder()
{
cout <<
"PostOrder:" <<
" ";
_PostOrder(_pRoot);
cout << endl << endl;
}
void PostOrder_Nor()
{
if (NULL == _pRoot)
return;
cout <<
"PostOrder_Nor: ";
struct Post
{
Node* BNode;
bool flag;
};
stack<Post*> s;
Node* temp = _pRoot;
while (temp || !s.empty())
{
while (temp)
{
Post* p = (Post*)
malloc(
sizeof(Post));
p->BNode = temp;
p->flag =
true;
s.push(p);
temp = temp->_pLeftChild;
}
if (!s.empty())
{
Post* p = s.top();
s.pop();
if (p->flag)
{
p->flag =
false;
s.push(p);
if (p->BNode->_pRightChild)
temp = p->BNode->_pRightChild;
}
else
{
cout << p->BNode->_pData <<
" ";
}
}
}
cout << endl << endl;
}
void LevelOrder()
{
cout <<
"LevelOrder: ";
_LevelOrder(_pRoot);
cout << endl << endl;
}
Node* GetParents(Node* child)
{
cout << child->_pData <<
"'s Parents is: ";
cout << _GetParents(_pRoot, child)->_pData << endl << endl;
return _GetParents(_pRoot, child);
}
Node* GetLeftChild(Node* parent)
{
cout << parent->_pData <<
"'s LeftChild is: " << _GetLeftChild(_pRoot, parent)->_pData << endl << endl;
return _GetLeftChild(_pRoot, parent);
}
Node* GetRightChild(Node* parent)
{
cout << parent->_pData <<
"'s RightChild is: " << _GetRightChild(_pRoot, parent)->_pData << endl << endl;
return _GetRightChild(_pRoot, parent);
}
size_t SumLeafNode()
{
int count =
0;
cout <<
"The Sum Of LeafNode Is: " << _SumLeafNode(_pRoot) << endl << endl;
return _SumLeafNode(_pRoot);
}
Node* FindNode(T value)
{
cout <<
"The Address Of " << value <<
" Is: " << _FindNode(_pRoot, value) <<
"H" << endl << endl;
return _FindNode(_pRoot, value);
}
size_t HightOfBinTree()
{
cout <<
"The Hight Of This Binary Tree Is: " << _HightOfBinTree(_pRoot) << endl << endl;
return _HightOfBinTree(_pRoot);
}
size_t GetKLevelCount(size_t k)
{
cout <<
"The Hight Of "<< k <<
" has " << _GetKLevelCount(_pRoot, k) <<
" Node "<< endl << endl;
return _GetKLevelCount(_pRoot, k);
}
void BinaryMirror()
{
_BinaryMirror(_pRoot);
}
void BinaryMirror_Nor()
{
if (NULL == _pRoot)
return;
queue<Node*> q;
q.push(_pRoot);
while (!q.empty())
{
Node* temp = q.front();
q.pop();
if(temp->_pLeftChild)
q. push(temp->_pLeftChild);
if(temp->_pRightChild)
q.push(temp->_pRightChild);
std::swap(temp->_pLeftChild, temp->_pRightChild);
}
}
private:
void CreatBinTree(Node*& pRoot,
const T* arr,
int size, T& invalid,
int& index)
{
if ((index < size) && (arr[index] != invalid))
{
pRoot =
new Node;
pRoot->_pData = arr[index];
CreatBinTree(pRoot->_pLeftChild, arr, size, invalid, ++index);
CreatBinTree(pRoot->_pRightChild, arr, size, invalid, ++index);
}
}
void Copy(Node*& pRoot, Node* SrcNode, T& invalid)
{
if (SrcNode != NULL)
{
pRoot =
new Node;
pRoot->_pData = SrcNode->_pData;
Copy(pRoot->_pLeftChild, SrcNode->_pLeftChild, invalid);
Copy(pRoot->_pRightChild, SrcNode->_pRightChild, invalid);
}
}
void Destroy(Node*& pRoot)
{
if (pRoot != NULL)
{
Destroy(pRoot->_pLeftChild);
Destroy(pRoot->_pRightChild);
delete pRoot;
pRoot = NULL;
}
}
void _FrontOrder(Node* pRoot)
{
if (pRoot)
{
cout << pRoot->_pData <<
" ";
_FrontOrder(pRoot->_pLeftChild);
_FrontOrder(pRoot->_pRightChild);
}
}
void _InOrder(Node* pRoot)
{
if (pRoot)
{
_InOrder(pRoot->_pLeftChild);
cout << pRoot->_pData <<
" ";
_InOrder(pRoot->_pRightChild);
}
}
void _PostOrder(Node* pRoot)
{
if (pRoot)
{
_PostOrder(pRoot->_pLeftChild);
_PostOrder(pRoot->_pRightChild);
cout << pRoot->_pData <<
" ";
}
}
void _LevelOrder(Node* pRoot)
{
if (NULL == pRoot)
return;
queue<Node*> q;
q.push(pRoot);
while (!q.empty())
{
Node* temp = q.front();
if (temp->_pLeftChild)
q.push(temp->_pLeftChild);
if (temp->_pRightChild)
q.push(temp->_pRightChild);
cout << temp->_pData <<
" ";
q.pop();
}
}
int _SumLeafNode(Node* pRoot)
{
if (NULL == pRoot)
return 0;
if (NULL == pRoot->_pLeftChild && NULL == pRoot->_pRightChild)
return 1;
return _SumLeafNode(pRoot->_pLeftChild) + _SumLeafNode(pRoot->_pRightChild);
}
Node* _FindNode(Node* pRoot, T& value)
{
if (pRoot)
{
if (pRoot->_pData == value)
return pRoot;
Node* temp = _FindNode(pRoot->_pLeftChild, value);
if (temp)
return temp;
Node* tmp = _FindNode(pRoot->_pRightChild, value);
if (tmp)
return tmp;
}
else
return NULL;
}
size_t _HightOfBinTree(Node* pRoot)
{
if (NULL == pRoot)
return 0;
if (NULL == pRoot->_pLeftChild && NULL == pRoot->_pRightChild)
return 1;
int left = _Height(pRoot->_pLeftChild);
int right = _Height(pRoot->_pRightChild);
return (left > right ? left : right) +
1;
}
Node* _GetParents(Node* pRoot, Node* child)
{
if (NULL == pRoot)
return NULL;
if (pRoot == child)
return NULL;
if (pRoot->_pLeftChild == child || pRoot->_pRightChild == child)
return pRoot;
Node* temp = _GetParents(pRoot->_pLeftChild, child);
if (temp)
return temp;
temp = _GetParents(pRoot->_pRightChild, child);
if (temp)
return temp;
}
Node* _GetLeftChild(Node* pRoot, Node* parent)
{
if (NULL == pRoot)
return NULL;
if (parent->_pLeftChild)
return parent->_pLeftChild;
}
Node* _GetRightChild(Node* pRoot, Node* parent)
{
if (NULL == pRoot)
return NULL;
if (parent->_pRightChild)
return parent->_pRightChild;
}
size_t _GetKLevelCount(Node* pRoot, size_t k)
{
if (NULL == pRoot || k<
1)
return 0;
if (k ==
1)
return 1;
return _GetKLevelCount(pRoot->_pLeftChild, k -
1) + _GetKLevelCount(pRoot->_pRightChild, k -
1);
}
void _BinaryMirror(Node* pRoot)
{
if (NULL == pRoot)
return;
std::swap(pRoot->_pLeftChild, pRoot->_pRightChild);
_BinaryMirror(pRoot->_pLeftChild);
_BinaryMirror(pRoot->_pRightChild);
}
Node* _pRoot;
T _invalid;
};
void FunTest()
{
char* arr =
"124###35##6";
char invalid =
'#';
BinartyTree<
char> bt(arr,
strlen(arr), invalid);
bt.GetKLevelCount(
2);
bt.PostOrder_Nor();
bt.FrontOrder();
bt.InOrder();
bt.PostOrder();
bt.LevelOrder();
bt.SumLeafNode();
bt.FindNode(
'5');
bt.HightOfBinTree();
bt.FrontOrder_Nor();
bt.InOrder_Nor();
BinaryNode<
char>* temp = bt.FindNode(
'5');
bt.GetParents(temp);
temp = bt.FindNode(
'3');
bt.GetLeftChild(temp);
bt.GetRightChild(temp);
bt.GetKLevelCount(
5);
BinartyTree<
char> bt1(bt);
bt1.BinaryMirror();
bt1.BinaryMirror_Nor();
bt1.FrontOrder();
bt1.InOrder();
bt1.PostOrder();
bt1.LevelOrder();
char* brr =
"124##78##9##35##6##";
BinartyTree<
char> bt2(brr,
strlen(brr), invalid);
bt2.PostOrder_Nor();
}
int main()
{
FunTest();
system(
"pause");
return 0;
}