#include<iostream>
#include<queue>
#include<stack>
using namespace std;
//编写二叉树节点的结构体
template<class T>
struct BinaryTreeNode
{
typedef BinaryTreeNode<T> Node;
T _data;
Node* _left;//左孩子
Node* _right;//右孩子
BinaryTreeNode(const T& data) //初始化节点
:_data(data)
, _left(NULL)
, _right(NULL)
{}
};
//创建二叉树
template<class T>
class BinaryTree
{
typedef BinaryTreeNode<T> Node;
public:
BinaryTree()
:_root(NULL)
{}
BinaryTree(T* a, size_t n, const T& invalid)
{
size_t index = 0;
_root = _CreatTree(a, n, invalid, index);
}
BinaryTree(const BinaryTree<T>& t)
{
_root = _Copy(t._root);
}
Node* _Copy(Node* root)
{
if (root == NULL)
return NULL;
Node* newRoot = new Node(root->_data);
newRoot->_left = _Copy(root->_left);
newRoot->_right = _Copy(root->_right);
return newRoot;
}
BinaryTree<T>& operator=(BinaryTree<T> t)
{
swap(_root, t._root);
return *this;
}
~BinaryTree()
{
_Destory(_root);
_root = NULL;
}
void _Destory(Node* root)
{
if (root == NULL)
return;
_Destory(root->_left);
_Destory(root->_right);
delete root;
}
void PrevOrder()//前序
{
_PrevOrder(_root);
}
void InOrder()//中序
{
_InOrder(_root);
}
void PostOrder()//后序
{
_PostOrder(_root);
}
void Prevorder_NonR()//非递归前序
{
_Prevorder_NonR(_root);
}
void InOrder_NonR()//非递归中序
{
_InOrder_NonR(_root);
}
void PostOrder_NonR()//非递归后序
{
_PostOrder_NonR(_root);
}
void LevelOrder()//层序
{
_LevelOrder(_root);
}
size_t Size()//节点个数
{
return _Size(_root);
}
size_t LeafSize()//叶子节点个数
{
return _LeafSize(_root);
}
size_t Depth()//求深度
{
return _Depth(_root);
}
size_t GetKLevelSize(size_t k)//第K层节点个数
{
return _GetKLevelSize(_root, k);
}
protected:
Node* _CreatTree(T* a, size_t n, const T& invalid, size_t& index)
{
Node* root = NULL;
if (index < n&&a[index] != invalid)
{
root = new Node(a[index]);
root->_left = _CreatTree(a, n, invalid, ++index);
root->_right = _CreatTree(a, n, invalid, ++index);
}
return root;
}
void _PrevOrder(Node* root)//前序遍历
{
if (root == NULL)
return;
cout << root->_data << " ";
_PrevOrder(root->_left);
_PrevOrder(root->_right);
}
void _Prevorder_NonR(Node* root)//非递归前序遍历
{
stack<Node*> s;
Node* cur = root;
while (!s.empty() || cur)
{
while (cur)//访问左路并压栈
{
s.push(cur);
cout << cur->_data << " ";
cur = cur->_left;
}
Node* top = s.top();
s.pop();
cur = top->_right;//按照子问题访问
}
cout << endl;
}
void _InOrder(Node* root)//中序遍历
{
if (root == NULL)
return ;
_InOrder(root->_left);
cout << root->_data << " ";
_InOrder(root->_right);
}
void _InOrder_NonR(Node* root)//非递归中序遍历
{
stack<Node*> s;
Node* cur = root;
while (!s.empty() || cur)
{
while (cur)
{
s.push(cur);
cur = cur->_left;
}
Node* top = s.top();
cout << top->_data << " ";
s.pop();
cur = top->_right;
}
cout << endl;
}
void _PostOrder(Node* root)//后序遍历
{
if (root == NULL)
return ;
_PostOrder(root->_left);
_PostOrder(root->_right);
cout << root->_data << " ";
}
void _PostOrder_NonR(Node* root)//非递归后序遍历
{
stack<Node*> s;
Node* cur = root;
Node* prev = NULL;
while (!s.empty() || cur)
{
while (cur)
{
s.push(cur);
cur = cur->_left;
}
Node* front = s.top();
if (front->_right == NULL||front->_right == prev)
{
cout << front->_data << " ";
prev = front;
s.pop();
}
else
{
cur = front->_right;
}
}
cout << endl;
}
void _LevelOrder(Node* root)//层序遍历(利用队列先进先出)
{
queue<Node*> q;
if (root)//入队列
{
q.push(root);
}
while (!q.empty())//队列不为空循环
{
Node* front = q.front();
cout << front->_data << " ";
if (front->_left != NULL)
{
q.push(front->_left);
}
if (front->_right != NULL)
{
q.push(front->_right);
}
q.pop();
}
cout << endl;
}
size_t _Size(Node* root)//节点个数
{
if (root == NULL)
return 0;
return _Size(root->_left) + _Size(root->_right) + 1;
cout << endl;
}
size_t _LeafSize(Node* root)//求叶子节点的个数
{
if (root == NULL)
{
return 0;
}
if (root->_left == NULL&&root->_right == NULL)
{
return 1;
}
return _LeafSize(root->_left) + _LeafSize(root->_right);
cout << endl;
}
size_t _Depth(Node* root)//求深度
{
if (root == NULL)
{
return 0;
}
if (root->_left == NULL&&root->_right == NULL)
{
return 1;
}
size_t left = _Depth(root->_left);
size_t right = _Depth(root->_right);
return left > right ? left + 1 : right + 1;
cout << endl;
}
size_t _GetKLevelSize(Node* root, size_t k)//求第K层节点的个数
{
if (root == NULL)
return 0;
if (k == 1)
return 1;
return _GetKLevelSize(root->_left, k - 1) + _GetKLevelSize(root->_right, k - 1);
cout << endl;
}
protected:
Node* _root;
};
void TestTree1()
{
int array[] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6, '#', '#', '#' };
BinaryTree<int> tree(array, sizeof(array) / sizeof(array[0]), '#');
tree.PrevOrder();
cout << endl;
tree.Prevorder_NonR();
tree.InOrder();
cout << endl;
tree.InOrder_NonR();
tree.PostOrder();
cout << endl;
tree.PostOrder_NonR();
tree.LevelOrder();
cout<<tree.Depth()<<endl;
cout<<tree.GetKLevelSize(3)<<endl;
cout<<tree.LeafSize()<<endl;
cout<<tree.Size()<<endl;
}
int main()
{
TestTree1();
return 0;
}
二叉树基本结构
最新推荐文章于 2022-09-09 07:56:57 发布