#include "stdafx.h"
#include <iostream>
using namespace std;
const int STACK_INIT_SIZE = 20;
const int QUEUE_INIT_SIZE = 20;
const int ERROR = 0;
const int OK = 1;
const int EMPTY = 1;
typedef int Status;
template <typename T>
class SqStack
{
public:
SqStack();
~SqStack();
T GetTop() const;
Status Push(T &e);
Status Pop(T &e);
int stackLength() const;
Status IsEmpty() const;
private:
T * base;
T *top;
int stackSize;
};
template <typename T>
SqStack<T>::SqStack()
{
base = new T[STACK_INIT_SIZE];
top = base;
stackSize = STACK_INIT_SIZE;
}
template <typename T>
SqStack<T>::~SqStack()
{
delete[] base;
}
template <typename T>
T SqStack<T>::GetTop() const
{
if (top == base)
{
cerr << "当前栈为空" << endl;
}
T e = *(top - 1);
return e;
}
template <typename T>
Status SqStack<T>::Push(T &e)
{
if (top - base >= stackSize)
{
return ERROR;
}
*top++ = e;
return OK;
}
template <typename T>
Status SqStack<T>::Pop(T &e)
{
if (top == base)
{
return ERROR;
}
e = *--top;
return OK;
}
template <typename T>
int SqStack<T>::stackLength() const
{
return (top - base);
}
template <typename T>
Status SqStack<T>::IsEmpty() const
{
if (top == base)
{
return EMPTY;
}
else
{
return !EMPTY;
}
}
template <typename T>
class CircularQueue
{
public:
CircularQueue();
~CircularQueue();
Status EnQueue(T e);
Status DeQueue(T &e);
int QueueLength() const;
Status IsEmpty() const;
private:
T * base;
int front;
int rear;
int queueSize;
};
template <typename T>
CircularQueue<T>::CircularQueue()
{
base = new T[QUEUE_INIT_SIZE];
if (base == nullptr)
{
cerr << "内存分配错误!!" << endl;
}
rear = front = 0;
queueSize = QUEUE_INIT_SIZE;
}
template <typename T>
CircularQueue<T>::~CircularQueue()
{
delete[] base;
}
template <typename T>
Status CircularQueue<T>::EnQueue(T e)
{
if ((rear + 1) % QUEUE_INIT_SIZE == front)
{
return ERROR;
}
base[rear] = e;
rear = (rear + 1) % QUEUE_INIT_SIZE;
return OK;
}
template <typename T>
Status CircularQueue<T>::DeQueue(T &e)
{
if (rear == front)
{
return ERROR;
}
e = base[front];
front = (front + 1) % QUEUE_INIT_SIZE;
return OK;
}
template <typename T>
int CircularQueue<T>::QueueLength() const
{
return (rear - front + QUEUE_INIT_SIZE) % QUEUE_INIT_SIZE;
}
template <typename T>
Status CircularQueue<T>::IsEmpty() const
{
return (rear == front) ? EMPTY : !EMPTY;
}
template <class T>
class BTNode
{
public:
T data;
BTNode *lChild;
BTNode *rChild;
};
template <typename T>
class BTree
{
public:
BTree();
~BTree();
void PreOrderDispTree(BTNode<T> *const &t) const;
void PreOrderDispTreeRE(BTNode<T> *t) const;
void InOrderDispTree(BTNode<T> *t) const;
void InOrderDispTreeRE(BTNode<T> *t) const;
void PostOrderDispTree(BTNode<T> *t) const;
void PostOrderDispTreeRE(BTNode<T> *t) const;
void levelDispTree(BTNode<T> *t) const;
void createBTree(char *str);
int getBTreeHeight(BTNode<T> *t) const;
void DispBTree(BTNode<T> *t) const;
BTNode<T>* getRoot() { return root; }
void destory(BTNode<T> *&t);
private:
BTNode<T> *root;
};
template <class T>
BTree<T>::BTree()
{
root = nullptr;
}
template <class T>
BTree<T>::~BTree()
{
destory(root);
}
template <class T>
void BTree<T>::destory(BTNode<T> *&t)
{
if (t != NULL)
{
destory(t->lChild);
destory(t->rChild);
delete t;
}
}
template <typename T>
void BTree<T>::PreOrderDispTree(BTNode<T> *const &t) const
{
if (t != nullptr)
{
cout << t->data;
PreOrderDispTree(t->lChild);
PreOrderDispTree(t->rChild);
}
}
template <typename T>
void BTree<T>::PreOrderDispTreeRE(BTNode<T> *t) const
{
SqStack<BTNode<T>*> s;
BTNode<T> *p = t;
while (p != nullptr || !s.IsEmpty())
{
while (p != nullptr)
{
cout << p->data;
s.Push(p);
p = p->lChild;
}
if (!s.IsEmpty())
{
s.Pop(p);
p = p->rChild;
}
}
cout << endl;
}
template <typename T>
void BTree<T>::InOrderDispTree(BTNode<T> *t) const
{
if (t != nullptr)
{
InOrderDispTree(t->lChild);
cout << t->data;
InOrderDispTree(t->rChild);
}
}
template <typename T>
void BTree<T>::InOrderDispTreeRE(BTNode<T> *t) const
{
SqStack<BTNode<T>*> s;
BTNode<T> *p = t;
while (p != nullptr || !s.IsEmpty())
{
while (p != nullptr)
{
s.Push(p);
p = p->lChild;
}
if (!s.IsEmpty())
{
s.Pop(p);
cout << p->data;
p = p->rChild;
}
}
cout << endl;
}
template <typename T>
void BTree<T>::PostOrderDispTree(BTNode<T> *t) const
{
if (t != nullptr)
{
PostOrderDispTree(t->lChild);
PostOrderDispTree(t->rChild);
cout << t->data;
}
}
template <typename T>
void BTree<T>::PostOrderDispTreeRE(BTNode<T> *t) const
{
if (t != nullptr)
{
SqStack<BTNode<T>*> s1;
SqStack<BTNode<T>*> s2;
BTNode<T> *p = nullptr;
s1.Push(t);
while (!s1.IsEmpty())
{
s1.Pop(p);
s2.Push(p);
if (p->lChild != nullptr)
{
s1.Push(p->lChild);
}
if (p->rChild != nullptr)
{
s1.Push(p->rChild);
}
}
while (!s2.IsEmpty())
{
s2.Pop(p);
cout << p->data;
}
}
cout << endl;
}
template <typename T>
void BTree<T>::levelDispTree(BTNode<T> *t) const
{
CircularQueue<BTNode<T>*> queue1;
BTNode<T> *p = t;
BTNode<T> *q = nullptr;
if (t != nullptr)
{
queue1.EnQueue(p);
while (!queue1.IsEmpty())
{
queue1.DeQueue(q);
cout << q->data;
if (q->lChild != nullptr)
{
queue1.EnQueue(q->lChild);
}
if (q->rChild != nullptr)
{
queue1.EnQueue(q->rChild);
}
}
}
cout << endl;
}
template <typename T>
void BTree<T>::createBTree(char *str)
{
SqStack<BTNode<T>*> s;
BTNode<T> *p = nullptr;
int j = 0;
int k = -1;
char ch;
ch = str[j];
while (ch != '\0')
{
switch (ch)
{
case '(':
s.Push(p);
k = 1;
break;
case ')':
s.Pop(p);
break;
case ',':
k = 2;
break;
default:
p = new BTNode<T>;
p->data = ch;
p->lChild = nullptr;
p->rChild = nullptr;
if (root == nullptr)
{
root = p;
}
else
{
if (k == 1)
{
s.GetTop()->lChild = p;
}
if (k == 2)
{
s.GetTop()->rChild = p;
}
}
}
ch = str[++j];
}
}
template <typename T>
int BTree<T>::getBTreeHeight(BTNode<T> *t) const
{
int lChildHeight = -1;
int rChildHeight = -1;
if (t == nullptr)
{
return 0;
}
else
{
lChildHeight = getBTreeHeight(t->lChild);
rChildHeight = getBTreeHeight(t->rChild);
return (lChildHeight > rChildHeight ? lChildHeight + 1 : rChildHeight + 1);
}
}
template <typename T>
void BTree<T>::DispBTree(BTNode<T> *t) const
{
if (t != nullptr)
{
cout << t->data;
if ((t->lChild != nullptr) || (t->rChild != nullptr))
{
cout << '(';
DispBTree(t->lChild);
if (t->rChild != nullptr)
{
cout << ',';
}
DispBTree(t->rChild);
cout << ')';
}
}
}
int main()
{
BTree<char> tree;
char str[] = "A(B(D(,G)),C(E,F))";
cout << "-----二叉树功能测试-----" << endl;
tree.createBTree(str);
BTNode<char> *root = tree.getRoot();
cout << "这棵树的结构为:";
tree.DispBTree(root);
cout << endl;
cout << "先序遍历递归实现:";
tree.PreOrderDispTree(root);
cout << endl;
cout << "先序遍历非递归实现:";
tree.PreOrderDispTreeRE(root);
cout << "中序遍历递归实现:";
tree.InOrderDispTree(root);
cout << endl;
cout << "中序遍历非递归实现:";
tree.InOrderDispTreeRE(root);
cout << "后序遍历递归实现:";
tree.PostOrderDispTree(root);
cout << endl;
cout << "后序遍历非递归实现:";
tree.PostOrderDispTreeRE(root);
cout << "层序遍历结果为:";
tree.levelDispTree(root);
cout << "树的高度为:";
cout << tree.getBTreeHeight(root) << endl;
return 0;
}
