最后又看了遍严蔚敏的数据结构,用C++又推 演了一下二叉树
//二叉树
#include <iostream>
#include <cassert>
#include <stack>
using namespace std;
template<typename TElemType>
class Node{
public:
Node()
{
pLeft = NULL;
pRight = NULL;
data = 0;
}
~Node(){}
void SetPLeft(Node* left){ this->pLeft = left; }
/*const对象只能访问const成员函数,而非const对象可以访问任意的成员函数,包括const成员函数;
const对象的成员是不能修改的,而通过指针维护的对象确实可以修改的;
const成员函数不可以修改对象的数据,不管对象是否具有const性质。编译时以是否修改成员数据为依据进行检查。*/
Node* GetPLeft() const { return pLeft; }
void SetPRight(Node* right){ this->pRight = right; }
Node* GetPRight() const { return pRight; }
void SetData(TElemType data){ this->data = data; }
TElemType GetData() const { return data; }
private:
Node* pLeft;
Node* pRight;
TElemType data;
};
template<typename TElemType>
class BiTree{
public:
BiTree()
{
pRoot = NULL;
}
~BiTree(){}
//初始化二叉树
void Init()
{
this->pRoot = new Node<TElemType>;
}
//创建二叉树
void Create()
{
cout << "input:";
TElemType data;
cin >> data;
assert(this->pRoot);
pRoot->SetData(data);
Node<TElemType>* pCurNode;//当前结点
Node<TElemType>* pTmpNode;
cout << "input(q,exit):";
while (cin >> data&&cin.get()!='q')
{
pCurNode = pRoot;
Node<TElemType>* pNode = new Node<TElemType>();
pNode->SetData(data);
do
{
pTmpNode = pCurNode;
if (pNode->GetData() < pCurNode->GetData()) pCurNode = pCurNode->GetPLeft();
else pCurNode = pCurNode->GetPRight();
} while (pCurNode != NULL);
if (pNode->GetData() < pTmpNode->GetData()) pTmpNode->SetPLeft(pNode);
else pTmpNode->SetPRight(pNode);
cout << "input(q,exit):";
//delete pNode;//这里是不能删除堆上的内存的
}
}
//先序遍历(根、左、右)
void PreOrder(Node<TElemType>* pNode)
{
if (pNode != NULL)
{
cout << pNode->GetData() << ",";
PreOrder(pNode->GetPLeft());
PreOrder(pNode->GetPRight());
}
}
//中序遍历(左、根、右)
void InOrder(Node<TElemType>* pNode)
{
if (pNode != NULL)
{
InOrder(pNode->GetPLeft());
cout << pNode->GetData() << ",";
InOrder(pNode->GetPRight());
}
}
//后序遍历(左、右、根)
void PostOrder(Node<TElemType>* pNode)
{
if (pNode != NULL)
{
PostOrder(pNode->GetPLeft());
PostOrder(pNode->GetPRight());
cout << pNode->GetData() << ",";
}
}
//输出
void Print()
{
cout << "先序遍历:";
PreOrder(pRoot);
cout << endl;
cout << "中序遍历:";
InOrder(pRoot);
cout << endl;
cout << "后序遍历:";
PostOrder(pRoot);
cout << endl;
}
//先序遍历,迭代
template<typename type>
void PreOrderIterator()
{
stack< Node<type>* > s;
Node<type>* pNode = pRoot;
//assert(pNode);
while (pNode != NULL || !s.empty())
{
while (pNode != NULL)
{
cout << pNode->GetData() << ",";
s.push(pNode);
pNode = pNode->GetPLeft();
}
if (!s.empty())
{
assert(s.top());
pNode = s.top();
s.pop();
pNode = pNode->GetPRight();
}
}
}
//中序遍历,迭代
void InOrderIterator()
{
stack< Node<TElemType>* > s;
Node<TElemType>* pNode = pRoot;
while (pNode != NULL || !s.empty())
{
while (pNode != NULL)
{
s.push(pNode);
pNode = pNode->GetPLeft();
}
if (!s.empty())
{
assert(s.top());
pNode = s.top();
cout << pNode->GetData() << ",";
s.pop();
pNode = pNode->GetPRight();
}
}
}
//后序遍历,迭代
void PostOrderIterator()
{
stack< Node<TElemType>* > s;
Node<TElemType>* pNode = pRoot;
Node<TElemType>* pPre = NULL;
Node<TElemType>* pTop = NULL;
while (pNode != NULL || !s.empty())
{
while (pNode != NULL)
{
s.push(pNode);
pNode = pNode->GetPLeft();
}
if (!s.empty())
{
pTop = s.top();
if (pTop->GetPRight() != NULL&&pTop->GetPRight() != pPre)
{
pNode = pTop->GetPRight();
}
else
{
cout << pTop->GetData() << " ";
pPre = pTop;
s.pop();
}
}
}
}
public:
Node<TElemType>* pRoot;
};
int main()
{
BiTree<int>* tree= new BiTree<int>;
tree->Init();
tree->Create();
tree->Print();
cout << "迭代,先序:";
tree->PreOrderIterator<int>();
cout << endl;
cout << "迭代,中序:";
tree->InOrderIterator();
cout << endl;
cout << "迭代,后序:";
tree->PostOrderIterator();
cout << endl;
delete tree;
system("pause");
return 0;
}