二叉树的查找:
#include<iostream>
#include"DoubleNode.h" //双链表结点类
#include"SeqStack.h" //顺序栈
#include"LinkedStack.h" //链式栈
#include"SeqQueue.h" //顺序循环队列
using namespace std;
template <class T>
class BinaryTree // 二叉树类
{
public:
DoubleNode<T> *root; //指向根结点
BinaryTree(); //构造空二叉树
BinaryTree(T prelist [],int n); //标明空子树的先根序列构造一棵二叉树
BinaryTree(T prelist [],T inlist [],int n); //先根和中根序列构造二叉树
~BinaryTree();
bool IsEmpty(); //判断是否是空二叉树
int Count(); //返回结点个数
int Height(); //返回二叉树的高度
DoubleNode<T> *Search(T value); //查找首次出现值为value的结点
DoubleNode<T> *GetParent(DoubleNode<T> *node); //返回node结点的双亲结点
void preRoot(); //先根次序遍历二叉树
void midRoot(); //中根次序遍历二叉树
void postRoot(); //后根次序遍历二叉树
DoubleNode<T> *Insert(DoubleNode<T> *p, T value,bool priorChild = true);//插入value作为P结点的孩子
void Remove(DoubleNode<T> * p,bool priorChild = true); //删除p结点的左或右子树
void PrintGList(); //以广义表输出二叉树
void preRootTraverse(); // 先根次序遍历二叉树的非递归算法
void midRootTraverse(); // 中根次序遍历二叉树的非递归算法
void LevelOrder(); //按层次遍历二叉树
private:
void Destroy (DoubleNode<T> *p); //撤销二叉树
int Count(DoubleNode<T> *p); //返回以P结点为根的子树结点个数
int Height(DoubleNode<T> *p); //返回以P结点为根的子树结点的高度
DoubleNode<T>*Search (DoubleNode<T> *p, T value); //在以p为根的子树中查找首次出现的值为value的结点
DoublenNode<T> *GetParent (DoubleNode<T> *p, DoubleNode<T> *node); // p为根的子树中查找并返回首次出现的值为value的结点
void preRoot(DoubleNode<T> *p); //先跟次序遍历以p结点为根的子树
void midtRoot(DoubleNode<T> *p); //中根次序遍历以p结点为根的子树
void postRoot(DoubleNode<T> *p); //后根次序遍历以P结点为根的子树
DoubleNode<T> *creat(T prelist[],int n,int &i); //以标明空子树的先根遍历序列创建子树
DoubleNode<T> *creat(T prelist[],T inlist[],int preStart, int inStart,int n); //以先根和中根序列创建一棵子树
void PrintGList (DoubleNode<T> *p); //以广义表表示输出以p结点为根的子树
};
template <class T>
BinaryTree<T>::BinaryTree() //构造空二叉树
{
this->root =NULL;
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
cout<<"撤销二叉树:";
Destroy(this->root);
cout<<endl;
}
template<class T>
void BinaryTree<T>::Destroy(DoubleNode<T> *p)
{
if(p!=NULL)
{
Destroy (p->prior);
Destroy (p->next);
cout<<p->data<<" "; // 显示撤销结点的次序
delete p;
}
}
template<class T>
bool BinaryTree<T>::IsEmpty()
{
return this->root==NULL;
}
template <class T>
int BinaryTree<T>::Count()
{
return Count(this->root);
}
template<class T>
int BinaryTree<T>::Count(DoubleNode<T> *p)
{
if(p==NULL)
return 0;
else
return 1+Count(p->prior)+Count(p->next);
}
template <class T>
void BinaryTree<T>::preRoot()
{
cout<<"先根次序遍历二叉树";
preRoot(this->root);
cout<<endl;
}
template<class T>
void BinaryTree<T>::preRoot(DoubleNode<T> *p)
{
if(p!=NULL)
{
cout<<p->data<<" ";
preRoot(p->prior);
preRoot(p->next);
}
}
template <class T>
void BinaryTree<T>::midRoot()
{
cout<<"中根次序遍历二叉树";
midRoot(this->root);
cout<<endl;
}
template<class T>
void BinaryTree<T>::midtRoot(DoubleNode<T> *p)
{
if(p!=NULL)
{
midRoot(p->prior);
cout<<p->data<<" ";
midRoot(p->next);
}
}
template<class T>
void BinaryTree<T>::postRoot()
{
cout<<"后根次序遍历二叉树";
postRoot(this->root);
cout<<endl;
}
template<class T>
void BinaryTree<T>::postRoot(DoubleNode<T> *p)
{
if(p!=NULL)
{
postRoot(p->prior);
postRoot(p->next);
cout<<p->data<<" ";
}
}
计算二叉树高度:
二叉树的高是一棵较高的一棵子树的高度加1,因此计算二叉树高度必须采用后根次序遍历,首先分别计算出左右子树高度,再计算当前结点为根的子树的高度
template <class T>
int BinaryTree<T> ::Height()//返回二叉树的高度
{
return Height(this->root);
}
template<class T>
int BinaryTree<T>::Height(DoubleNode<T> *p)
{
if(p!=NULL)
{
int lh = Height(p->prior);
int rh = Height(p->next);
return (lh>=rh)?lh+1:rh+1;
}
return 0;
}
查找:
template <class T>
DoubleNode<T> *BinaryTree<T>::Search(T value)
{
return BinaryTree(this->root,value);
}
template <class T>
DoubleNode <T> *BinaryTree<T>::Search(DoubleNode<T> *p, T value) //先根次序遍历查找值为value的结点,返回首次出现结点的指针,若未找到返回NULL
{
DoubleNode<T> *find = NULL;
if(p!=NULL)
{
if(p->data == value)
return p;
find = Search(p->prior,value);
if(find == NULL)
find = Search(p->next,value);
}
return find;
}
二叉树的双亲结点:
template<class T>
DoubleNode<T> *BinaryTree<T>::GetParent(DoubleNode<T> *node)
{
if(this->root == NULL||node==NULL||node==this->root)
return NULL;
return GetParent(this->root,node);
}
template<class T>
DoubleNode<T> * BinaryTree<T>::GetParent(DoubleNode<T> *p, DoubleNode<T> *node)
{
DoubleNode<T> *find =NULL;
if(p!=NULL)
{
if(p->prior == node|| p->next == node)
return p;
find = GetParent(p->prior,node);
if(find ==NULL)
find =GetParent(p->next,node);
}
return find;
}
构造二叉树:
template <class T>
BinaryTree<T>::BinaryTree(T prelist[], T inlist[], int n)
{
this->root = creat(parlist,inlist,0,0,n);
}
template <class T>
DoubleNode<T> *BinaryTree<T>::creat(T prelist[], T inlist[], int preStart, int inStart, int n)
//以先根和中根序列创建一颗子树,子树根结点是prelist[i],返回根结点指
{
DoubleNode<T> *p = NULL;
if(n>0)
{
T elem = prelist[preStart]; //根结点值
p = new DoubleNode<T> (elem); //创建结点
int i= 0;
while(i<n && elem !=inlist[inStart+i]) //在中根序列中查找根值所在的位置
i++;
p->prior = create(prelist,inlist,preStart+1,inStart,i) //创建左子树
p->next = create(prelist,inlist,preStart+i+1,inStart+i+1,n-1-i); //创建右子树
}
return p;
}
template <class T>
BinaryTre<T>::BinaryTree(T prelist [],int n) //标明空子树的先根序列构造二叉树
{
int i = 0;
this->root = create(prelist,n,i);
}
template <class T>
DoubleNode<T> * BinaryTree<T>::create(T prelist[], int n, int &i) //以标明空子树的先根次序遍历序列创建一颗子树,子树根结点是prelist[i],返回根结点指针
{
DoubleNode<T> *p =NULL;
if(i<n)
{
T elem = prelist[i];
i++;
if(elem!=NULL)
{
p = new DoubleNode<T> (elem); //创建结点
p->prior = create(prelist, n , i); // 创建左子树
p->next = create(prelist,n,i); //创建右子树
}
}
return p;
}
二叉树的广义表表示法:
template <class T>
void BinaryTree<T>::PrintGList() //以广义表输出二叉树
{
PrintGlist(this -> root);
cout<<endl;
}
template<class T>
void BinaryTree<T>::PrintGList(DoubleNode<T> *p)
{
if(p==NULL)
cout<<"^";
else
{
cout<<p->data;
if(p->prior!=NULL||p->next != NULL)
{
cout<<"(";
PrintGlist(p->prior);
cout<<",";
PrintGlist(p->next);
cout<<")";
}
}
}
插入:
template <class T>
DoubleNode<T> *BinaryTree<T>::Insert(DoubleNode<T> *p, T value, bool priorChild ) //插入value作为P结点的孩子
{
DoubleNode<T> *q = NULL;
if(p!=NULL)
if(priorChild)
{
q = new DoubleNode<T> (value,p->prior,NULL);
p->prior = q;
}
else
{
q = new DoubleNode<T> (value,NULL,p->next);
p->next = q;
}
return q;
}
删除:
template <class T>
void BinaryTree<T>::Remove(DoubleNode<T> *p, bool priorChild)
{
if(p!=NULL)
if(priorChild)
{
Destroy(p->prior); //撤销左子树
p->prior = NULL;
}
else
{
Destroy (p->next); // 撤销右子树
p->next = NULL;
}
}
二叉树的非递归实现:
template <class T>
void BinaryTree<T>::preRootTraverse() //先根次序遍历二叉树的非递归算法
{
cout<<"先根次序遍历的非递归";
SeqStack<DoubleNode<T> *> stack; // 创建一个空栈
DoubleNode<T> * p = this ->root;
while(p!=NULL||!stack.isEmpty()) //p非空或栈非空时
if(p!=NULL)
{
cout<<p->data<<" "; // 访问结点
stack.push(p); // p结点入栈
p = p->prior;// 进入左子树
}
else
{
p=stack.pop();
p = p->next;
}
cout<<endl;
}
template <class T>
void BinaryTree<T>::midRootTraverse()
{
cout<<"中根次序遍历的非递归";
LinkedStack<DoubleNode<T> *> stack; //创建一个空栈
DoubleNode<T> *p =this->root;
while(p!=NULL||!stack.isEmpty()) //p非空或栈非空树
if(p!=NULL)
{
stack.push(p);
p = p->prior;
}
else
{
p = stack.pop();
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
template<class T>
void BinaryTree<T>::LevelOrder() //按层次遍历二叉树
{
cout<<"层次遍历: ";
SeqQueue<DoubleNode<T> *>que;
DoubleNode<T> *p = this->root;
while(p!=NULL)
{
cout<<p->data<<" ";
if(p->prior!=NULL)
que.enqueue(p->prior);
if(p->next!=NULL)
que.enqueue(p->next);
if(!que.isEmpty())
p = que.dequeue();
else
p = NULL;
}
cout<<endl;
}