#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
template<class T>
struct BinTreeNode
{
BinTreeNode(const T& data):_data(data)
,pLchild(NULL)
, pRchild(NULL)
{}
T _data;
BinTreeNode<T>* pLchild;
BinTreeNode<T>* pRchild;
};
template <class T>
class BinTree
{
typedef BinTreeNode<T> Node;
typedef BinTreeNode<T>* PNode;
public:
BinTree() :
_pRoot(NULL)
{}
BinTree(const T* array, size_t size, const T& invalid) //构造函数
{
size_t index = 0;
_CreateBinTree(_pRoot, array, size, index, invalid);
}
BinTree(const BinTree<T>& bt) //拷贝构造
{
_pRoot = _CopyBinTree(bt._pRoot);
}
BinTree& operator=(const BinTree<T>& bt)
{
_DestoryBinTree(_pRoot);
_pRoot = _CopyBinTree(bt._pRoot);
return *this;
}
~BinTree()
{
_DestoryBinTree(_pRoot); //销毁二叉树
}
void PreOrder() //递归前序遍历
{
_PreOrder(_pRoot);
cout << endl;
}
void PreOrder_Nor() //非递归前序遍历
{
if (NULL == _pRoot)
return;
stack<PNode> s;
s.push(_pRoot);
while (!s.empty())
{
PNode cur = s.top();
cout << cur->_data << " ";
s.pop();
if (cur->pRchild) //右孩子入栈
s.push(cur->pRchild);
if (cur->pLchild)
s.push(cur->pLchild); //左孩子入栈
}
cout << endl;
}
void PreOrder_Nor_L() //非递归前序遍历
{
if (NULL == _pRoot)
return;
stack<PNode> s;
s.push(_pRoot);
while (!s.empty())
{
PNode cur = s.top();
s.pop();
while (cur) //访问左子树
{
cout << cur->_data << " ";
if (cur->pRchild)
s.push(cur->pRchild);
cur = cur->pLchild;
}
}
cout << endl;
}
void InOrder() //中序遍历
{
_InOrder(_pRoot);
cout << endl;
}
void InOrder_NIn() //非递归中序遍历
{
if (NULL == _pRoot)
{
return;
}
PNode cur = _pRoot;
stack<PNode> s;
while (cur || !s.empty())
{
while (cur) //1、找到以当前结点为根左侧路径的所有结点
{
s.push(cur);
cur = cur->pLchild;
}
cur = s.top();
cout << cur->_data << " "; // 2、取栈顶并访问
s.pop();
cur = cur->pRchild; //3、cur 左支树 不存在 或 不存在
while (cur == NULL && !s.empty())
{
cur = s.top();
cout << cur->_data << " ";
s.pop();
cur = cur->pRchild;
}
}
}
void PostOrder() //后序遍历
{
_PostOrder(_pRoot);
cout << endl;
}
void PostOrder_NPo() //非递归后序遍历
{
if (NULL == _pRoot)
return;
PNode pCur = _pRoot;
PNode prev = NULL;
stack<PNode> s;
while (pCur || !s.empty())
{
while ( pCur != NULL && pCur!= prev)
{
s.push(pCur);
pCur = pCur->pLchild;
}
if (s.empty())
return;
pCur = s.top();
if (NULL == pCur->pRchild || pCur->pRchild == prev)
{
cout << pCur->_data << " ";
prev = pCur;
s.pop();
}
else
{
pCur = pCur->pRchild;
}
}
cout << endl;
}
void PostOrder_NPo1() //非递归后序遍历
{
if (NULL == _pRoot)
return;
PNode pCur = _pRoot;
PNode pTop = NULL;
PNode prev = NULL;
stack<PNode> s;
while (pCur || !s.empty())
{
while (pCur != NULL) //左子树入栈
{
s.push(pCur);
pCur = pCur->pLchild;
}
if (s.empty())
return;
pTop = s.top();
if (NULL == pTop->pRchild || pTop->pRchild == prev)
{
cout << pTop->_data << " ";
prev = pTop;
s.pop();
}
else
{
pCur = pTop->pRchild;
}
}
cout << endl;
}
bool CompleteBinTree() // 是否为完全二叉树
{
PNode pCur = _pRoot;
queue<PNode> q;
q.push(pCur);
bool flag = false;
while (!q.empty())
{
pCur = q.front();
if (flag)
{
if (pCur->pLchild != NULL && pCur->pRchild != NULL)
return false;
}
if (pCur->pLchild != NULL && pCur->pRchild != NULL)
{
q.push(pCur->pLchild);
q.push(pCur->pRchild);
}
else if (pCur->pLchild == NULL && pCur->pRchild != NULL)
{
return false;
}
else if (pCur->pLchild != NULL && pCur->pRchild == NULL)
{
q.push(pCur->pLchild);
flag = true;
}
else
{
flag = true;
}
q.pop();
}
return true;
}
void LevelNode() // 按层遍历
{
PNode cur = _pRoot;
queue<PNode> q;
q.push(cur);
while (!q.empty())
{
cur = q.front();
if (cur)
{
cout << cur->_data << " ";
q.push(cur->pLchild);
q.push(cur->pRchild);
}
q.pop();
}
}
size_t Size() //结点个数
{
return _Size(_pRoot);
}
size_t GetLeafCount() //叶子结点个数
{
return _GetLeafCount(_pRoot);
}
size_t GetKLevelCount(size_t k) //第k层结点
{
return _GetKLevelCount(_pRoot, k);
}
size_t Height() //求高度
{
return _Height(_pRoot);
}
PNode Find(const T& data) //查找结点
{
return _Find(_pRoot, data);
}
PNode Parent(const PNode pnode) //查找双亲结点
{
return _Parent(_pRoot, pnode);
}
PNode LelfChild(const PNode pnode) //查找左孩子结点
{
return Find(pnode->_data)->pLchild;
}
PNode RightChild(const PNode pnode) //查找右孩子结点
{
return Find(pnode->_data)->pRchild;
}
void Mrrior() // 二叉树的镜像
{
_Mrrior(_pRoot);
}
void Mrrior_N() //非递归二叉树的镜像
{
PNode pCur = _pRoot;
queue<PNode> q;
q.push(pCur);
while (!q.empty())
{
pCur = q.front();
swap(pCur->pLchild, pCur->pRchild);
if (pCur->pLchild)
q.push(pCur->pLchild);
if (pCur->pRchild)
q.push(pCur->pRchild);
q.pop();
}
}
private:
void _CreateBinTree(PNode& _pRoot, const T* array, size_t size, size_t& index, const T& invalid) //创建二叉树
{
if (index < size && _pRoot == NULL)
{
if (array[index] != invalid)
{
_pRoot = new Node(array[index]);
_CreateBinTree(_pRoot->pLchild, array, size, ++index, invalid);
_CreateBinTree(_pRoot->pRchild, array, size, ++index, invalid);
}
}
}
PNode _CopyBinTree(const PNode& _pRoot) //拷贝二叉树
{
PNode temp = NULL;
if (_pRoot != NULL)
{
temp = new Node(_pRoot->_data);
temp->pLchild = _CopyBinTree(_pRoot->pLchild);
temp->pRchild = _CopyBinTree(_pRoot->pRchild);
}
return temp;
}
void _DestoryBinTree(PNode& root) //销毁二叉树
{
if (root)
{
_DestoryBinTree(root->pLchild);
_DestoryBinTree(root->pRchild);
delete root;
root = NULL;
}
}
void _PreOrder(const PNode& root) //前序遍历
{
if (root)
{
cout << root->_data << " ";
_PreOrder(root->pLchild);
_PreOrder(root->pRchild);
}
}
void _InOrder(const PNode& root) //中序遍历
{
if (root)
{
_InOrder(root->pLchild);
cout << root->_data << " ";
_InOrder(root->pRchild);
}
}
void _PostOrder(const PNode& root) //后序遍历
{
if (root)
{
_PostOrder(root->pLchild);
_PostOrder(root->pRchild);
cout << root->_data << " ";
}
}
size_t _Size(const PNode root) //求结点数
{
if (root == NULL)
{
return 0;
}
if (root->pLchild == NULL && root->pRchild == NULL)
return 1;
return _Size(root->pLchild) + _Size(root->pRchild) + 1;
}
size_t _GetLeafCount(const PNode root) //求叶子结点数
{
if (NULL == root)
return 0;
if (NULL == root->pLchild && NULL == root->pRchild)
return 1;
return _GetLeafCount(root->pLchild) + _GetLeafCount(root->pRchild);
}
size_t _GetKLevelCount(PNode _pRoot, size_t k) //第k层结点个数
{
if (NULL == _pRoot || 0 == k)
return 0;
if (1 == k)
{
return 1;
}
k--;
return _GetKLevelCount(_pRoot->pLchild, k) + _GetKLevelCount(_pRoot->pRchild, k);
}
size_t _Height(PNode root) //求高度
{
if (NULL == root)
{
return 0;
}
if (NULL == root->pLchild && NULL == root->pRchild)
{
return 1;
}
return _Height(root->pLchild)>_Height(root->pRchild)? _Height(root->pLchild) + 1: _Height(root->pRchild) + 1;
}
PNode _Find( PNode root, const T data) //查找结点
{
PNode temp = NULL;
if (root)
{
if (root->_data == data)
return root;
if (!temp)
temp = _Find(root->pLchild, data);
if (!temp)
temp = _Find(root->pRchild, data);
}
return temp;
}
PNode _Parent(PNode _proot,const PNode& pNode) // 找双亲结点
{
PNode temp = NULL;
if (_proot)
{
if (_proot->pLchild == pNode || _proot->pRchild == pNode)
return _proot;
if (!temp)
temp = _Parent(_proot->pLchild, pNode);
if (!temp)
temp = _Parent(_proot->pRchild, pNode);
}
return temp;
}
void _Mrrior(PNode proot) // 二叉树的镜像
{
if (proot)
{
swap(proot->pLchild, proot->pRchild);
_Mrrior(proot->pLchild);
_Mrrior(proot->pRchild);
}
}
PNode _pRoot;
};
void Funtest()
{
char* p = "ABD#G###CE##F";
char* s = "ABD###CE##F";
BinTree<char> a(p, strlen(p), '#');
BinTree<char> b(s, strlen(s), '#');
/*b = a;*/
a.PreOrder();
a.InOrder();
a.PostOrder();
a.LevelNode();
/*cout << a.Size() << endl;*/
/*cout << a.GetLeafCount()<< endl;*/
cout << a.GetKLevelCount(3) << endl;
/*cout << a.Height() << endl;*/
/*a.PreOrder_Nor_L();*/
/*a.PreOrder_Nor();
a.InOrder_NIn();*/
/*a.PostOrder_NPo();*/
/*b.PostOrder_NPo();*/
/*cout << a.Find('E') << endl;
cout << a.Parent(a.Find('E'))->pLchild<< endl;
cout << a.LelfChild(a.Find('A'))->_data << endl;
cout << a.RightChild(a.Find('A'))->_data << endl;*/
/*b.PostOrder_NPo();*/
/*b.PostOrder_NPo1();*/
//b.PreOrder();
//b.Mrrior();
//b.Mrrior_N();
//b.PreOrder();
cout << b.CompleteBinTree() << endl;
}
int main()
{
Funtest();
return 0;
}
二叉树的相关操作
最新推荐文章于 2022-10-25 17:24:40 发布