pragma once
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
template<class T>
struct Node
{
struct Node<T>* _left;
struct Node<T>* _right;
T _data;
Node(T data = T())
:_left(NULL)
,_right(NULL)
,_data(data)
{}
};
template<class T>
class Tree
{
typedef Node<T> Node;
typedef Node* pNode;
public:
Tree()
:_root(NULL)
{}
Tree(const T* a, size_t size, int pi)
{
_root = BTCreate(a, size, pi);
}
Tree(const Tree<T>& t)
{
_root = CopyTree(t._root);
}
~Tree()
{
BTDestory(_root);
}
pNode CopyTree(const pNode _root)
{
if (_root == NULL)
return NULL;
pNode root = BTBuyNode(_root->_data);
root->_left = CopyTree(_root->_left);
root->_right = CopyTree(_root->_right);
return root;
}
pNode BTCreate(const T* a, int n, int& pi)
{
pNode root = NULL;
if (pi < n && a[pi] != '#')
{
root = BTBuyNode(a[pi]);
root->_left = BTCreate(a, n, ++pi);
root->_right = BTCreate(a, n, ++pi);
}
return root;
}
pNode BTBuyNode(T x)
{
pNode node = new Node;
node->_left = NULL;
node->_right = NULL;
node->_data = x;
return node;
}
void BTDestory(pNode& root)
{
if (root)
{
BTDestory(root->_left);
BTDestory(root->_right);
delete root;
root = NULL;
}
}
int BTSize(pNode root)//二叉树大小
{
if (root == NULL)//无节点
return 0;
else
return BTSize(root->_left) + BTSize(root->_right) + 1;//此时左右孩子节点为空,自己为一个节点
}
int BTLeafSize(pNode root)//叶结点大小
{
if (root == NULL)
return 0;
else if (root->_left == NULL && root->_right == NULL)
return 1;
else
return BTLeafSize(root->_left) + BTLeafSize(root->_right);
}
int BTLevelKSize(pNode root, int k)// 第k行结点大小
{
if (root == NULL)
return 0;
else if (k == 1)
return 1;
else
return BTLevelKSize(root->_left, k - 1) + BTLevelKSize(root->_right, k - 1);
}
int BTHeight(pNode root)//二叉树高度
{
int left;
int right;
if (root == NULL)
return 0;
left = BTHeight(root->_left);
right = BTHeight(root->_right);
return left > right ? left + 1 : right + 1;
}
pNode BTFind(pNode root, T x)//查找
{
pNode ret = NULL;
if (root == NULL || root->_data == x)
return root;
if (ret = BTFind(root->_left, x))
return ret;
if (ret = BTFind(root->_right, x))
return ret;
return NULL;
}
// 遍历 递归&非递归
void BTPrevOrder(pNode root)//前序遍历递归
{
if (root == NULL)
//cout << "#" << " ";
return;
cout << root->_data << " ";
BTPrevOrder(root->_left);
BTPrevOrder(root->_right);
}
void BTInOrder(pNode root)//中序遍历递归
{
if (root == NULL)
return;
BTInOrder(root->_left);
cout << root->_data << " ";
BTInOrder(root->_right);
}
void BTPostOrder(pNode root)//后序遍历递归
{
if (root == NULL)
return;
BTPostOrder(root->_left);
BTPostOrder(root->_right);
cout << root->_data << " ";
}
void BTLevelOrder(pNode root)//层序遍历
{
queue<pNode> q;
pNode front;
q.push(root);
while (q.empty() != 1)
{
front = q.front();
cout << front->_data << " ";
q.pop();
if(front->_left != NULL)
q.push(front->_left);
if(front->_right != NULL)
q.push(front->_right);
}
}
int BTComplete(pNode root)//是否完全二叉树
{
queue<pNode> q;
pNode front;
if (root)
q.push(root);
while (q.empty() != 1)
{
front = q.front();
q.pop();
if (front)
{
q.push(front->_left);
q.push(front->_right);
}
else
break; //此时找到第一个为空的子树
}
while (q.empty() != 1)//判断之后的结点后有无不为空的结点
{
front = q.front();
if (front)
return -1;
else
q.pop();
}
return 0;
}
void BTPrevOrderNonR(pNode root)//前序遍历非递归
{
stack<pNode> s;
pNode top;
while (root || s.empty() != 1)
{
while (root)//从根节点开始左子树一直入栈
{
cout << root->_data << " ";
s.push(root);
root = root->_left;
}
top = s.top();
s.pop();
root = top->_right;
}
}
void BTInOrderNonR(pNode root)//中序遍历非递归
{
stack<pNode> s;
pNode top;
while (root || s.empty() != 1)
{
while (root)
{
s.push(root);
root = root->_left;
}
top = s.top();
s.pop();
cout << top->_data << " ";
root = top->_right;
}
}
void BTPostOrderNonR(pNode root)//后序遍历非递归
{
stack<pNode> s;
pNode top;
pNode sign = NULL;
while (root || s.empty() != 1)
{
while (root)
{
s.push(root);
root = root->_left;
}
top = s.top();
if (top->_right == NULL || top->_right == sign)
{
cout << top->_data << " ";
sign = top;
s.pop();
}
else
root = top->_right;
}
}
pNode BTMirror(pNode root)//镜像
{
if (!root)
return root;
pNode left = root->_left;
pNode right = root->_right;
root->_left = right;
root->_right = left;
BTMirror(left);
BTMirror(right);
return root;
}
public:
pNode _root;
};
#include"BinaryTree.h"
int main()
{
int i = 0;
char arr[] = { 'A','B','D','#','#','E','#','#','C','F','#','#','G','#','#' };
Tree<char> t(arr, sizeof(arr) / sizeof(arr[0]), i);
//t.BTPrevOrder(t._root);
//t.BTInOrder(t._root);
t.BTPostOrder(t._root);
//t.BTLevelOrder(t._root);
//cout << "大小是:" << t.BTSize(t._root) << endl;
//cout << "大小是:" << t.BTLeafSize(t._root) << endl;
//cout << "第k行节点是:" << t.BTLevelKSize(t._root,1) << endl;
//cout << "树的高度是:" << t.BTHeight(t._root) << endl;
//cout << t.BTFind(t._root, 'G')->_data << endl;
//cout << t.BTComplete(t._root) << endl;
//t.BTMirror(t._root);
//t.BTLevelOrder(t._root);
//t.BTPrevOrderNonR(t._root);
//t.BTInOrderNonR(t._root);
t.BTPostOrderNonR(t._root);
return 0;
}