// 二叉树2.cpp : Defines the entry point for the console application.
//
#include<iostream>
#include<vector>
#include<string>
using namespace std;
template<class T>
struct BiNode
{
T data;
BiNode <T>* lchild;
BiNode <T>* rchild;//建立二叉链表,用4种方法便利,求二叉树的高度,节点数,求父节点
};
const int Maxsize = 100;
class LinkQueue
{
private:
int front;
int rear;
BiNode <char>* data[Maxsize];
public:
LinkQueue();//构造函数
BiNode<char>* DeQueue();
bool Empty();//判断队列是否为空
void EnQueue(BiNode<char>* x);
};
template<class T>
class BiTree
{
private:
BiNode <T>* root;
public:
BiTree()
{
root = NULL;
}
BiTree(vector<T>& pre);
BiNode<T>* CreatByPre(vector<T>& pre, int& i);
~BiTree();//析构函数
void Free(BiNode<T>* p);
void PreOrder1();//先序遍历
void PreOrder1(BiNode <T>* p);
void PreOrder2();//中序遍历
void PreOrder2(BiNode <T>* p);
void PreOrder3();//后序遍历
void PreOrder3(BiNode <T>* p);
void LevelOrder();//层序遍历
int Count();//计算节点个数
int Count(BiNode<T>* p);
int Hight();//计算树的高度
int Hight(BiNode<T>* p);
BiNode<T>* Search(BiNode<T>* p, T e);//查找父节点
BiNode<T>* Search(T e);
BiNode<T>* SearchParent(BiNode<T>* p, BiNode<T>* child);
BiNode<T>* SearchParent(BiNode<T>* child);
BiNode<T>* operator=(BiNode<char>* node);
};
template<class T>
BiNode<T>* BiTree<T>::operator=(BiNode<char>* node)
{
BiNode<T>* a;
a.data = node.data;
a.lchild = node.lchild;
a.rchild = node.rchild;
return a;
}
//先序遍历
template <class T>
void BiTree<T>::PreOrder1(BiNode <T>* p)
{
if (p == NULL)
return;
cout << p->data;
PreOrder1(p->lchild);
PreOrder1(p->rchild);
}
template<class T>
void BiTree<T>::PreOrder1()
{
PreOrder1(root);
cout << " 先序遍历" << endl;
}
//----------------------------------------------------------------
//中序遍历
template <class T>
void BiTree<T>::PreOrder2(BiNode <T>* p)
{
if (p == NULL)
return;
PreOrder2(p->lchild);
cout << p->data;
PreOrder2(p->rchild);
}
template<class T>
void BiTree<T>::PreOrder2()
{
PreOrder2(root);
cout << " 中序遍历" << endl;
}
//---------------------------------------------------------------
//后序遍历
template <class T>
void BiTree<T>::PreOrder3(BiNode <T>* p)
{
if (p == NULL)
return;
PreOrder3(p->lchild);
PreOrder3(p->rchild);
cout << p->data;
}
template<class T>
void BiTree<T>::PreOrder3()
{
PreOrder3(root);
cout << " 后序遍历" << endl;
}
//-----------------------------------------------------------------
//层次遍历
template<class T>
void BiTree<T>::LevelOrder()
{
if (root == NULL)
return;
LinkQueue Q;
Q.EnQueue(root);
while (!Q.Empty())
{
BiNode<T>* p = Q.DeQueue();
cout << p->data;
if (p->lchild != NULL)
Q.EnQueue(p->lchild);
if (p->rchild != NULL)
Q.EnQueue(p->rchild);
}
}
BiNode<char>* LinkQueue::DeQueue()
{
if (rear == front)
{
cout << "上溢" << endl;
exit(0);
}
front = (front + 1) % Maxsize;
return data[front];
}
LinkQueue::LinkQueue()
{
front = rear = 0;
}
void LinkQueue::EnQueue(BiNode<char>* x)
{
if ((rear + 1) % Maxsize == front)
{
cout << "上溢" << endl;
exit(1);
}
rear = (rear + 1) % Maxsize;
data[rear] = x;
}
bool LinkQueue::Empty()
{
return rear == front;
}
//-----------------------------------------------------------------
template<class T>
BiNode<T>* BiTree<T>::CreatByPre(vector<T>& pre, int& i)//先序构造构造函数
{
T e = pre[i];
i++;
if (e == '*')
return NULL;
BiNode<T>* p = new BiNode<T>;
p->data = e;
p->lchild = CreatByPre(pre, i);
p->rchild = CreatByPre(pre, i);
return p;
}
template<class T>
BiTree<T>::BiTree(vector<T>& pre)
{
int i = 0;
root = CreatByPre(pre, i);
}
//----------------------------------------------------------------
template <class T>
void BiTree<T>::Free(BiNode<T>* p)//析构函数
{
if (p == NULL)
return;
Free(p->lchild);
Free(p->rchild);
delete p;
}
template <class T>
BiTree<T>::~BiTree()
{
Free(root);
}
//-----------------------------------------------------------
template<class T>
int BiTree<T>::Count(BiNode<char>* p)//计算节点数
{
if (p == NULL)
return 0;
int left = Count(p->lchild);
int right = Count(p->rchild);
return left + right + 1;
}
template <class T>
int BiTree<T>::Count()
{
return Count(root);
}
//------------------------------------------------------------
template<class T>
int BiTree<T>::Hight(BiNode<T>* p)//计算高度
{
if (p == NULL)
return 0;
int left = Hight(p->lchild);
int right = Hight(p->rchild);
if (left > right)
return left + 1;
else
return right + 1;
}
template<class T>
int BiTree<T>::Hight()
{
return Hight(root);
}
//-----------------------------------------------------------------
template<class T>
BiNode<T>* BiTree<T>::SearchParent(BiNode<T>* p, BiNode<T>* child)//查找父节点
{
if (p == NULL)
return NULL;
if (p->lchild == child || p->rchild == child)
return p;
BiNode<T>* q = SearchParent(p->lchild, child);
if (q != NULL)
return q;
return SearchParent(p->rchild, child);
}
template<class T>
BiNode<T>* BiTree<T>::SearchParent(BiNode<T>* child)
{
return SearchParent(root, child);
}
template<class T>
BiNode<T>* BiTree<T>::Search(BiNode<T>* p, T e)
{
if (p == NULL)
return NULL;
if (p->data == e)
return p;
BiNode<T>* q = Search(p->lchild, e);
if (q != NULL)
return q;
return Search(p->rchild, e);
}
template<class T>
BiNode<T>* BiTree<T>::Search(T e)
{
return Search(root, e);
}
void menu()
{
cout << "请选择对二叉树进行的操作" << endl;
cout << "1、先序遍历输出二叉树" << endl;
cout << "2、中序遍历输出二叉树" << endl;
cout << "3、后序遍历输出二叉树" << endl;
cout << "4、层次遍历输出二叉树" << endl;
cout << "5、计算二叉树的高度" << endl;
cout << "6、计算二叉树的节点数" << endl;
cout << "7、查找节点的父节点" << endl;
cout << "8、退出系统" << endl;
}
template<class T>
void Menu(BiTree<T> a)
{
int i = 0, h = 0;
BiNode<char>* fnode;
BiNode<char>* child;
char node;
while (1)
{
menu();
cin >> i;
switch (i)
{
case 1:
a.PreOrder1();
break;
case 2:
a.PreOrder2();
break;
case 3:
a.PreOrder3();
break;
case 4:
a.LevelOrder();
cout << " 层次遍历" << endl;
break;
case 5:
h = a.Hight();
cout << "二叉树高度为:" << h << endl;
break;
case 6:
i = a.Count();
cout << "二叉树的节点数为:" << i << endl;
break;
case 7:
cout << "输入要查找父节点的节点" << endl;
cin >> node;
child = a.Search(node);
fnode = a.SearchParent(child);
if (fnode == NULL)
cout << "查找失败" << endl;
else
cout << "该节点的父节点为:" << fnode->data << endl;
break;
case 8:
cout << "已退出" << endl;
system("pause");
exit(0);
}
}
}
void main()
{
cout << "欢迎进入二叉树处理系统" << endl;
cout << "请选择操作" << endl;
vector<char>num;
char c;
//建立二叉树
cout << "请输入带有空指针的先序序列(以@结束):" << endl;
for (int j = 0; c != '@'; j++)
{
cin >> c;
num.push_back(c);
}
BiTree<char> tree(num);
Menu(tree);
}
CH7(1) 二叉树
最新推荐文章于 2024-05-20 19:43:04 发布