#pragma once
#include <iostream>
#include <assert.h>
#include <stack>
using namespace std;
template <typename T>
struct BinaryTreeNode
{
T _value;
BinaryTreeNode* _leftchild;
BinaryTreeNode* _rightchild;
BinaryTreeNode(const T& x)
: _value(x)
, _leftchild(NULL)
, _rightchild(NULL)
{}
};
template <typename T>
class BinaryTree
{
typedef BinaryTreeNode<T> Node;
public:
BinaryTree()
:_root(NULL)
{}
BinaryTree(const T* arr, size_t size, const T& invalid=T())
{
assert(arr);
size_t index = 0;
_root = _CreatTree(arr, size, index, invalid);
}
BinaryTree(const BinaryTree<T>& bt)
{
_root = _Copy(bt._root);
}
BinaryTree<T>& operator=(BinaryTree bt)
{
swap(_root, bt._root);
return *this;
}
~BinaryTree()
{
_Destroy(_root);
}
void PrevOrder() //先序遍历
{
_PrevOrder(_root);
cout << endl;
}
void PrevOrderNonR() //先序非递归
{
stack<Node*> s;
Node* cur = _root;
while (!s.empty() || cur)
{
while (cur) //吧该数的左字数入栈
{
cout << cur->_value << " "; //访问根节点
s.push(cur);
cur = cur->_leftchild;
}
Node* top = s.top(); //到这,top的左子树和自己已经访问
s.pop();
cur = top->_rightchild; //转化为子问题来解决访问以top节点的右子树
}
cout << endl;
}
void InOrder() //中序遍历
{
_InOrder(_root);
cout << endl;
}
void InOrderNonR() //中序非递归
{
stack<Node*> s;
Node* cur = _root;
while (!s.empty() || cur)
{
while (cur)
{
s.push(cur);
cur = cur->_leftchild;
}
Node* top = s.top();
cout << top->_value << " ";
s.pop();
cur = top->_rightchild;
}
cout << endl;
}
void PostOrder() //后序遍历
{
_PostOrder(_root);
cout << endl;
}
void PostOrderNonR() //后序非递归
{
stack<Node*> s;
Node* cur = _root;
Node* prev = NULL;
while (!s.empty() || cur)
{
while (cur)
{
s.push(cur);
cur = cur->_leftchild;
}
Node* top = s.top();
if (top->_rightchild == NULL || top->_rightchild == prev)
{
cout << top->_value << " ";
s.pop();
prev = top;
}
else
{
cur = top->_rightchild;
}
}
cout << endl;
}
void LevelOrder() //层序遍历
{
queue<Node*> q;
Node* tmp = _root;
if (tmp == NULL)
{
return;
}
q.push(tmp);
while (!q.empty())
{
tmp = q.front();
cout << tmp->_value << " ";
q.pop();
if (tmp->_leftchild != NULL)
{
q.push(tmp->_leftchild);
}
if (tmp->_rightchild != NULL)
{
q.push(tmp->_rightchild);
}
}
cout << endl;
}
size_t Size()
{
return _Size(_root);
}
size_t Depth()
{
return _Depth(_root);
}
size_t GetKLevelSize(size_t k) //求第k层节点个数(根节点为第1层)
{
return _GetKLevelSize(_root,k);
}
size_t GetLeafSize() //求叶子节点的个数
{
return _GetLeafSize(_root);
}
Node* Find(const T& x)
{
return _Find(_root, x);
}
protected:
Node* _CreatTree(const T* arr, size_t size, size_t& index, const T& invalid = T())
{
Node* tmp = NULL;
if (index<size && arr[index] != invalid)
{
tmp = new Node(arr[index]); //int arr[15] = { 1, 2, '#', 3, '#', '#', 4, 5, '#', 6, '#', 7, '#', '#', 8 };
tmp->_leftchild = _CreatTree(arr, size, ++index, invalid);
tmp->_rightchild = _CreatTree(arr, size, ++index, invalid);
}
return tmp;
}
Node* _Copy(Node* root)
{
if (root == NULL)
{
return NULL;
}
Node* newroot = new Node(root->_value);
newroot->_leftchild = _Copy(root->_leftchild);
newroot->_rightchild = _Copy(root->_rightchild);
return newroot;
}
void _Destroy(Node* root)
{
if (root != NULL)
{
_Destroy(root->_leftchild);
_Destroy(root->_rightchild);
delete root;
root = NULL;
}
}
void _PrevOrder(Node* root)
{
if (root == NULL)
{
return;
}
cout << root->_value<<" ";
_PrevOrder(root->_leftchild);
_PrevOrder(root->_rightchild);
}
void _InOrder(Node* root)
{
if (root == NULL)
{
return;
}
_InOrder(root->_leftchild);
cout << root->_value << " ";
_InOrder(root->_rightchild);
}
void _PostOrder(Node* root)
{
if (root == NULL)
{
return;
}
_PostOrder(root->_leftchild);
_PostOrder(root->_rightchild);
cout << root->_value << " ";
}
size_t _Size(Node* root)
{
if (root == NULL)
{
return 0;
}
return _Size(root->_leftchild) + _Size(root->_rightchild) + 1;
}
size_t _Depth(Node* root)
{
if (root == NULL)
{
return 0;
}
size_t left = _Depth(root->_leftchild);
size_t right = _Depth(root->_rightchild);
return (left > right ? left : right)+1;
}
size_t _GetKLevelSize(Node* root, size_t k)
{
if (root == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
return _GetKLevelSize(root->_leftchild,k-1) + _GetKLevelSize(root->_rightchild,k-1);
}
size_t _GetLeafSize(Node* root)
{
if (root == NULL)
{
return 0;
}
if ((root->_leftchild == root->_rightchild) && root->_leftchild == NULL)
{
return 1;
}
return _GetLeafSize(root->_leftchild) + _GetLeafSize(root->_rightchild);
}
Node* _Find(Node* root, const T& x)
{
if (root == NULL)
{
return NULL;
}
if (root->_value == x)
{
return root;
}
Node* tmp = _Find(root->_leftchild, x);
if (tmp == NULL)
{
tmp = _Find(root->_rightchild, x);
}
return tmp;
}
protected:
Node* _root; //根
};数据结构-二叉树(包含递归和非递归版本)
最新推荐文章于 2020-04-29 23:16:23 发布
本文介绍了一种通用的二叉树模板类实现,包括递归与非递归的先序、中序、后序遍历方法,以及层序遍历等。此外,还提供了创建二叉树、深拷贝、计算节点数量、树深度等功能。
708

被折叠的 条评论
为什么被折叠?



