// AVL.cpp : 定义控制台应用程序的入口点。
//
#include
#include
#include
using namespace std;
template
class BinaryTreeNode {
public:
BinaryTreeNode
BinaryTreeNode
int LTag, RTag;
T m_tData;
};
template
class BinaryTree {
public:
BinaryTree();
~BinaryTree() {
DestoryBinTree(m_pRoot);
}
BinaryTreeNode
void PreOrder() { PreOrder(m_pRoot); }
void InOrder() { InOrder(m_pRoot); }
void PostOrder() { PostOrder(m_pRoot); }
void LevelOrder() { LevelOrder(m_pRoot); }
int GetHigh() { return GetHigh(m_pRoot); }
int GetLeafNum() { return GetLeafNum(m_pRoot); }
void InOrderThreading() {
pre = NULL;
InOrderThreading(m_pRoot);
if (pre->m_pRChild == NULL || pre->RTag)
pre->m_pRChild = NULL;
}
void PreOrderThreading() {
pre = NULL;
PreOrderThreading(m_pRoot);
if (pre->m_pRChild == NULL || pre->RTag)
pre->m_pRChild = NULL;
}
void PostOrderThreading() {
pre = NULL;
PostOrderThreading(m_pRoot);
if (pre->m_pRChild == NULL || pre->RTag)
pre->m_pRChild = NULL;
}
private:
BinaryTreeNode
BinaryTreeNode
void DestoryBinTree(BinaryTreeNode
void PreOrder(BinaryTreeNode
void InOrder(BinaryTreeNode
void PostOrder(BinaryTreeNode
void LevelOrder(BinaryTreeNode
int GetHigh(BinaryTreeNode
int GetLeafNum(BinaryTreeNode
void InOrderThreading(BinaryTreeNode
void PreOrderThreading(BinaryTreeNode
void PostOrderThreading(BinaryTreeNode
};
/*template
struct BinThrNode {
DataType data;
BinThrNode
int LTag, RTag;
};
template
class BinThrTree{
public:
void PreBTTree() { PreBTTree(m_TRoot); }
void InOrder() { InOrder(m_TRoot); }
void PostOrder() { PostOrder(m_TRoot); }
BinThrTree();
~BinThrTre(){}
private:
BinThrNode m_TRoot , pre;
void PreBTTree(BinThrNode
void InOrder(BinThrNode
void PostOrder(BinThrNode
};*/ //线索二叉树模板
template
BinaryTree
{
m_pRoot = new BinaryTreeNode
freopen("C:\\Users\\孙啸峰\\Desktop\\test.txt", "r", stdin);
m_pRoot = CreateBinTree();
}
template
BinaryTreeNode
{
char ch;
BinaryTreeNode
cin >> ch;
if (ch == '#') {
pBinaryTreeNode = NULL;
}
else {
pBinaryTreeNode = new BinaryTreeNode
pBinaryTreeNode->m_tData = ch;
pBinaryTreeNode->LTag = 0;
pBinaryTreeNode->RTag = 0;
pBinaryTreeNode->m_pLChild = CreateBinTree();
pBinaryTreeNode->m_pRChild = CreateBinTree();
}
return pBinaryTreeNode;
}
template
void BinaryTree
{
if (pBinTreeNode != NULL) {
DestoryBinTree(pBinTreeNode->m_pLChild);
DestoryBinTree(pBinTreeNode->m_pRChild);
delete pBinTreeNode;
}
}
template
void BinaryTree
{
if (pBinTreeNode == NULL) return;
else {
cout << "数据 :" << pBinTreeNode->m_tData << endl;
if (pBinTreeNode->m_pLChild)
cout << "左孩子数据 :" << pBinTreeNode->m_pLChild->m_tData << endl;
else
cout << "左孩子为空;" << endl;
if (pBinTreeNode->m_pRChild)
cout << "右孩子数据: " << pBinTreeNode->m_pRChild->m_tData << endl;
else
cout << "右孩子为空 " << endl;
if(!pBinTreeNode->LTag)
PreOrder(pBinTreeNode->m_pLChild);
if(!pBinTreeNode->RTag)
PreOrder(pBinTreeNode->m_pRChild);
}
}
template
void BinaryTree
if (pBinTreeNode == NULL) return;
else {
if(!pBinTreeNode->LTag)
InOrder(pBinTreeNode->m_pLChild);
cout << "数据 :" << pBinTreeNode->m_tData << endl;
if (pBinTreeNode->m_pLChild)
cout << "左孩子数据 :" << pBinTreeNode->m_pLChild->m_tData << endl;
else
cout << "左孩子为空;" << endl;
if (pBinTreeNode->m_pRChild)
cout << "右孩子数据: " << pBinTreeNode->m_pRChild->m_tData << endl;
else
cout << "右孩子为空 " << endl;
if( !pBinTreeNode->RTag)
InOrder(pBinTreeNode->m_pRChild );
}
}
template
void BinaryTree
{
if (pBinTreeNode == NULL) return;
else {
if(!pBinTreeNode->LTag)
PostOrder(pBinTreeNode->m_pLChild);
if(!pBinTreeNode->RTag)
PostOrder(pBinTreeNode->m_pRChild);
cout << "数据 :" << pBinTreeNode->m_tData << endl;
if (pBinTreeNode->m_pLChild)
cout << "左孩子数据 :" << pBinTreeNode->m_pLChild->m_tData << endl;
else
cout << "左孩子为空;" << endl;
if (pBinTreeNode->m_pRChild)
cout << "右孩子数据: " << pBinTreeNode->m_pRChild->m_tData << endl;
else
cout << "右孩子为空 " << endl;
}
}
template
void BinaryTree
queue
BinaryTreeNode
if (pBinTreeNode == NULL) return;
queueTreeNode.push(pBinTreeNode);
while (!queueTreeNode.empty()) {
pTemp = queueTreeNode.front();
queueTreeNode.pop();
cout << pTemp->m_tData;
if (pTemp->m_pLChild)
queueTreeNode.push(pTemp->m_pLChild);
if (pTemp->m_pRChild)
queueTreeNode.push(pTemp->m_pRChild);
}
}
template
int BinaryTree
{
if (pBinTreeNode == NULL)
return 0;
else
return max(GetHigh(pBinTreeNode->m_pLChild), GetHigh(pBinTreeNode->m_pRChild))+1;
}
template
int BinaryTree
{
if( pBinTreeNode->m_pLChild==NULL && pBinTreeNode->m_pRChild==NULL)
return 1;
else {
BinaryTreeNode
pLChild = pBinTreeNode->m_pLChild;
pRChild = pBinTreeNode->m_pRChild;
if (!pLChild)
return GetLeafNum(pRChild);
if (!pRChild)
return GetLeafNum(pLChild);
return GetLeafNum(pLChild) + GetLeafNum(pRChild);
}
}
template
void BinaryTree
{
if (p) {
if (!p->LTag)
InOrderThreading(p->m_pLChild);
if (p->m_pLChild == NULL || p->LTag) {
p->LTag = 1; p->m_pLChild = pre;
}
if (pre)
if (pre->m_pRChild == NULL || pre->RTag) {
pre->RTag = 1; pre->m_pRChild = p;
}
pre = p;
if (!p->RTag)
InOrderThreading(p->m_pRChild);
}
}
template
void BinaryTree
{
if (p) {
if (p->m_pLChild == NULL ||p->LTag) {
p->LTag = 1; p->m_pLChild = pre;
}
if (pre)
if (pre->m_pRChild == NULL || pre->RTag) {
pre->RTag = 1; pre->m_pRChild = p;
}
pre = p;
if (!p->LTag)
PreOrderThreading(p->m_pLChild);
if (!p->RTag)
PreOrderThreading(p->m_pRChild);
}
}
template
void BinaryTree
{
if (p) {
if (!p->LTag)
PostOrderThreading(p->m_pLChild);
if (!p->RTag)
PostOrderThreading(p->m_pRChild);
if (p->m_pLChild == NULL || p->LTag) {
p->LTag = 1; p->m_pLChild = pre;
}
if (pre)
if (pre->m_pRChild == NULL || pre->RTag) {
pre->RTag = 1; pre->m_pRChild = p;
}
pre = p;
}
}
void main()
{
BinaryTree
cout << "前序遍历:" << endl;
tempBinaryTree.PreOrder();
cout << endl;
cout << "中序遍历:" << endl;
tempBinaryTree.InOrder();
cout << endl;
cout << "后序遍历:" << endl;
tempBinaryTree.PostOrder();
cout << endl;
cout << "层序遍历:" << endl;
tempBinaryTree.LevelOrder();
cout << endl;
cout << "二叉树的高度:" << endl;
cout << tempBinaryTree.GetHigh()<< endl;
cout << "二叉树的叶子结点数:" << endl;
cout << tempBinaryTree.GetLeafNum() << endl;
cout << "前序线索化后显示:" << endl;
tempBinaryTree.PreOrderThreading();
tempBinaryTree.PreOrder();
cout << endl;
cout << "中序线索化后显示:" << endl;
tempBinaryTree.InOrderThreading();
tempBinaryTree.InOrder();
cout << endl;
cout << "后序线索化后显示:" << endl;
tempBinaryTree.PostOrderThreading();
tempBinaryTree.PostOrder();
cout << endl;
freopen("CON", "r", stdin);
system("pause");
}