#include<iostream>
#include<string>
#include <stack>
#include <queue>
using namespace std;
template <class T>
struct BiNode //二叉树的结点结构
{
T data;
BiNode<T> *lchild, *rchild;
};
template <class T>
class BiTree
{
public:
BiTree(); //构造函数,初始化一棵二叉树,其前序序列由键盘输入
~BiTree(void); //析构函数,释放二叉链表中各结点的存储空间
BiNode<T>* Getroot(); //获得指向根结点的指针
void PreOrder(BiNode<T> *root); //前序遍历二叉树
void InOrder(BiNode<T> *root); //中序遍历二叉树
void PostOrder(BiNode<T> *root); //后序遍历二叉树
void LeverOrder(BiNode<T> *root); //层序遍历二叉树
void DFS(BiNode<T> *root);
void BFS(BiNode<T> *root);
// 递归版本
BiNode<T> *search(BiNode<T> *root, T key) const;
// 非递归版本
BiNode<T> *search_feidigui(BiNode<T>* root, T key)const;
private:
BiNode<T> *root; //指向根结点的头指针
BiNode<T> *Creat(); //有参构造函数调用
void Release(BiNode<T> *root); //析构函数调用
};
#endif
/*
*前置条件:二叉树不存在
*输 入:无
*功 能:构造一棵二叉树
*输 出:无
*后置条件:产生一棵二叉树
*/
template<class T>
BiTree<T>::BiTree()
{
this->root = Creat();
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:释放二叉链表中各结点的存储空间
*输 出:无
*后置条件:二叉树不存在
*/
template<class T>
BiTree<T>::~BiTree(void)
{
Release(root);
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:获取指向二叉树根结点的指针
*输 出:指向二叉树根结点的指针
*后置条件:二叉树不变
*/
template<class T>
BiNode<T>* BiTree<T>::Getroot()
{
return root;
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:前序遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template<class T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
if (root == NULL) return;
else {
cout << root->data << " ";
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:中序遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <class T>
void BiTree<T>::InOrder(BiNode<T> *root)
{
if (root == NULL) return; //递归调用的结束条件
else {
InOrder(root->lchild); //中序递归遍历root的左子树
cout << root->data << " "; //访问根结点的数据域
InOrder(root->rchild); //中序递归遍历root的右子树
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:后序遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <class T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
if (root == NULL) return; //递归调用的结束条件
else
{
PostOrder(root->lchild); //后序递归遍历root的左子树
PostOrder(root->rchild); //后序递归遍历root的右子树
cout << root->data << " "; //访问根结点的数据域
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:层序遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <class T>
void BiTree<T>::LeverOrder(BiNode<T> *root)
{
const int MaxSize = 100;
int front = 0;
int rear = 0; //采用顺序队列,并假定不会发生上溢
BiNode<T>* Q[MaxSize];
BiNode<T>* q;
if (root == NULL) return;
else {
Q[rear++] = root;
while (front != rear)
{
q = Q[front++];
cout << q->data << " ";
if (q->lchild != NULL) Q[rear++] = q->lchild;
if (q->rchild != NULL) Q[rear++] = q->rchild;
}
}
}
template<class T>
void BiTree<T>::DFS(BiNode<T>* root)
{
BiNode<T> *p = root;
if (p != NULL)
{
std::stack<BiNode<T> *> trav_stack;
trav_stack.push(p);
std::cout << "DFS:\n";
while (!trav_stack.empty())
{
p = trav_stack.top();
trav_stack.pop();
if (p->lchild != 0)
{
trav_stack.push(p->lchild);
}
if (p->rchild != 0)
{
trav_stack.push(p->rchild);
}
std::cout << p->data << "\t";
}
}
}
template<class T>
void BiTree<T>::BFS(BiNode<T>* root)
{
BiNode<T> *p = root;
if (p != NULL)
{
std::queue<BiNode<T> *> trav_queue;
trav_queue.push(p);
std::cout << "BFS:";
while (!trav_queue.empty())
{
p = trav_queue.front();
trav_queue.pop();
if (p->lchild != 0)
{
trav_queue.push(p->lchild);
}
if (p->rchild != 0)
{
trav_queue.push(p->rchild);
}
std::cout << p->data << "\t";
}
}
}
// 递归版本
template<class T>
BiNode<T>* BiTree<T>::search(BiNode<T>* root, T key) const
{
if (root == NULL || root->data == key)
{
return root;
}
if (root->data < key)
{
return search(root->rchild, key);
}
else
{
return search(root->lchild, key);
}
return NULL;
}
template<class T>
BiNode<T>* BiTree<T>::search_feidigui(BiNode<T>* root, T key) const
{
while (root != NULL)
{
if (root->data == key)
{
return root;
}
else
{
if (root->data > key)
{
root = root->lchild;
}
else
{
root = root->rchild;
}
}
}
return NULL;
}
/*
*前置条件:空二叉树
*输 入:数据ch;
*功 能:初始化一棵二叉树,构造函数调用
*输 出:无
*后置条件:产生一棵二叉树
*/
template <class T>
BiNode<T>* BiTree<T>::Creat()
{
BiNode<T>* root;
T ch;
cout << "请输入创建一棵二叉树的结点数据" << endl;
cin >> ch;
if (ch == 0) root = NULL;
else
{
root = new BiNode<T>; //生成一个结点
root->data = ch;
root->lchild = Creat(); //递归建立左子树
root->rchild = Creat(); //递归建立右子树
}
return root;
}
/*
*前置条件:二叉树已经存在
*输 入:无
*功 能:释放二叉树的存储空间,析构函数调用
*输 出:无
*后置条件:二叉树不存在
*/
template<class T>
void BiTree<T>::Release(BiNode<T>* root)
{
if (root != NULL) {
Release(root->lchild); //释放左子树
Release(root->rchild); //释放右子树
delete root;
}
}
void main()
{
BiTree<int> bt;
BiNode<int> *root = bt.Getroot();
bt.DFS(root);
bt.BFS(root);
cout << endl;
std::cout << "输入要查找的数:\n";
int search_val;
BiNode<int> *search_node;
while (true)
{
std::cin >> search_val;
search_node = bt.search(root, search_val);
if (search_node)
{
std::cout << "查找到:" << search_node->data << std::endl;
}
else
{
std::cout << "没有查找到:" << search_val<< std::endl;
}
}
std::system("pause");
}
二叉树
最新推荐文章于 2024-07-29 08:47:18 发布