二叉树算法

#include<iostream>
#include<string>
#include<queue>
using namespace std;
const int MaxSize = 100;
template <class T>
struct BiNode
{
    T data;
    BiNode<T> *lchild,*rchild;
};
template <class T>
class BiTree
{
  public:
       BiTree(){root=Creat(root);}
        ~BiTree(){Release(root);}
        void PreOrder(){PreOrder(root);} //前序
        void InOrder(){InOrder(root);}//中序
        void PostOrder(){PostOrder(root);} //后序
        void LeverOrder(){LeverOrder(root);} //层序
        void GetParentout();
        void Count(){
            number=0;
            Count(root);
            cout<<"求二叉树的结点个数: "<<number<<endl;}
        void countleaf(){
            result=0;
            countleaf(root);
            cout<<"统计叶子节点的数目:"<<result<<endl;}

        int Tree_Width()
        {
            return Tree_Width(root);
        }
        void swap_tree()
        {
            cout<<"左右子树进行交换前,层次遍历的结果:";
            LeverOrder(root);
            swap_tree(root);
            cout<<endl;
            cout<<"左右子树进行交换后,层次遍历的结果:";
            LeverOrder(root);
        }
        void Is_Wanquan()
        {
            if(Is_Wanquan(root))
            {
                cout<<"该二叉树为完全二叉树!"<<endl;
            }
            else
            {
                cout<<"该二叉树不是完全二叉树!"<<endl;
            }
        }

  private:
        BiNode<T> *root;
        int result;
        int number;
        int res;
        BiNode<T>* Creat(BiNode<T> *root);
        void PreOrder(BiNode<T> *root); //前序
        void InOrder(BiNode<T> *root); //中序
        void PostOrder(BiNode<T> *root); //后序
        void LeverOrder(BiNode<T > *root); //层序
        void Release(BiNode<T> *root);
        void Count(BiNode<T> *root);
        void countleaf(BiNode<T>* root);
        int Tree_Width(BiNode<T> *root);
        void swap_tree(BiNode<T> * root);
        bool Is_Wanquan(BiNode<T> *root);
        BiNode<T>* GetParent(BiNode<T>*root,BiNode<T>*current);//求某结点的双亲
 };
template <class T>
BiNode<T> *BiTree<T>::Creat(BiNode<T> *root)
{
    T ch;
    cin>>ch;

    if(ch=='#')
    {
        root=NULL;
    }
    else
    {
        root=new BiNode<T>;
        root->data=ch;
        root->lchild=Creat(root->lchild);
        root->rchild=Creat(root->rchild);
    }
    return root;

}
template <class T>
void BiTree<T>::Release(BiNode<T> *root)
{
    if(root==NULL){return ;}
    else
    {
        Release(root->lchild);
        Release(root->rchild);
        delete 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);
         cout<<root->data<<" ";
         InOrder(root->rchild);
     }
 }
template <class T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
     if(root==NULL)
     {
         return;
     }
     else
     {
         PostOrder(root->lchild);
         PostOrder(root->rchild);
         cout<<root->data<<" ";
     }
}
template <class T>
void BiTree<T>::LeverOrder(BiNode<T> *root)
{
    queue<BiNode<T>*> Q;
    BiNode<T>*pre=root;
    if(pre!=NULL)
    {
        Q.push(pre);
    }
    while(!Q.empty())
    {
        pre=Q.front();
        Q.pop();
        cout<<pre->data<<" ";
        if(pre->lchild)
        {
            Q.push(pre->lchild);
        }
        if(pre->rchild)
        {
            Q.push(pre->rchild);
        }
    }
}
//求某结点的双亲

template<class T>
BiNode<T>* BiTree<T>::GetParent(BiNode<T>*root,BiNode<T>*current)
{
    BiNode<T>*temp;
    if(root==NULL)
    {
        return NULL;
    }
    if(root->lchild==current||root->rchild==current)
    {
        return root;
    }
    if((temp=GetParent(root->lchild,current))!=NULL)
    {
        return temp;
    }
    else
        return GetParent(root->rchild,current);
}
template<class T>
void BiTree<T>::GetParentout()
{
    BiNode<T>* cur;
    BiNode<T>* pre;
    T data;
    cout<<"\n例如要查找的结点为D:";
    cur=root->lchild->rchild;
    pre=GetParent(root,cur);
    if(pre!=NULL)
    {
        cout<<"则双亲结点值为:"<<pre->data<<endl;
    }
    else
    {
        cout<<"查找失败!"<<endl;
    }

}
template<class T>
void BiTree<T>::Count(BiNode<T> *root)
{
    if (root) {
         Count(root->lchild);
         number++;  //number为数据成员
         Count(root->rchild);
   }
}
template <class T>
void BiTree<T>::countleaf(BiNode<T>* root)
{
    if(root==NULL)
    {
        return;
    }
    if(root->lchild==NULL&&root->rchild==NULL)
    {
        result=result+1;
    }
    countleaf(root->lchild);
    countleaf(root->rchild);
}
template <class T>
int BiTree<T>::Tree_Width(BiNode<T> *root)
{
	int front = -1;
	int rear = -1;  //采用顺序队列,并假定不会发生上溢
	BiNode<T>* Q[MaxSize];
    BiNode<T>* q;
	int level_tail=0;//记录每层最右边结点在顺序队列中的位置,
	int count[MaxSize];//记录每层结点的个数
	int width=0;// 记录二叉树的宽度
	int level_num=0;//记录层数
    if (root==NULL) return 0;
    else
        {
		Q[++rear] = root;
		level_tail=rear;
		count[level_num]=0;
		while (front != rear)
        {
		    q = Q[++front];//队首元素出队  	cout<<q->data<<" ";
    		if (q->lchild != NULL)
    		{
                Q[++rear] = q->lchild;	count[level_num]++;
            }
		    if (q->rchild != NULL)
            {
		        Q[++rear] = q->rchild;count[level_num]++;
            }
		    if(front==level_tail)
            {
			    if (width<count[level_num])
				    {width=count[level_num];}
			    level_num++;
			    level_tail=rear;
			    count[level_num]=0;
			}
		}
	}
	return width;
}
template<typename T>
void  BiTree<T>::swap_tree(BiNode<T> * root)
{
    BiNode<T> * temp;
	if (root==NULL)
	{
	    return;
	}
	else
    {
        temp=root->rchild;
        root->rchild=root->lchild;
        root->lchild=temp;
        swap_tree(root->lchild);
        swap_tree(root->rchild);
    }
}
template<class T>
bool BiTree<T>::Is_Wanquan(BiNode<T> *root)
{
	queue<BiNode<T>*> q;
	BiNode <T>* pointer;
	bool is_leaf=false;
	if(!root)
		return false;
	q.push(root);
    while(!q.empty())
	{
		pointer=q.front();
		q.pop();
		if(pointer->rchild!=NULL && pointer->lchild==NULL)
			return false;
		else if(pointer->rchild==NULL && pointer->lchild!=NULL )
        {
            if(is_leaf)
				return false;
			else
				is_leaf=true;
        }
		else if(pointer->rchild==NULL && pointer->lchild==NULL )
			is_leaf=true;
		else if(is_leaf)
			return false;
		if(pointer->lchild!=NULL)
			q.push(pointer->lchild);
		if(pointer->rchild!=NULL)
			q.push(pointer->rchild);
	}
	return true;
}


int main()
{
    cout<<"请按前序遍历序列依次输入:";
    BiTree<char> E;
    cout << "该二叉树的前序遍历序列是:";
	E.PreOrder();
	cout << "\n该二叉树的中序遍历序列是:";
	E.InOrder();
	cout << "\n该二叉树的后序遍历序列是:";
	E.PostOrder();
	cout << "\n该二叉树的层序遍历序列是:";
    E.LeverOrder();
    E.GetParentout();
    E.Count();
    E.countleaf();
    cout<<"计算二叉树的宽度:"<<E.Tree_Width()<<endl;
    E.swap_tree();
    E.Is_Wanquan();
	return 0;

}


  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值