二叉链表 C++实现

#include<iostream>
#include<string>
#include<queue>
using namespace std;
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);} //层序
        int BiTreeDepth( ){cout<<BiTreeDepth(root);}
        void GetParentout();
        void Count(){
            number=0;
            Count(root);
            cout<<"求二叉树的结点个数: "<<number<<endl;}
        void countleaf(){
            result=0;
            countleaf(root);
            cout<<"统计叶子节点的数目:"<<result<<endl;}
  private:
        BiNode<T> *root;
        int result;
        int number;
        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 BiTreeDepth( 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++;
    }
    countleaf(root->lchild);
    countleaf(root->rchild);
}
template <class T>
int BiTree<T>::BiTreeDepth( BiNode<T>* root )
{
	int dep1 = 0, dep2 = 0;
	if ( root == NULL )
		return 0;
	else
	{
		dep1 = BiTreeDepth( root->lchild );
		dep2 = BiTreeDepth( root->rchild );
		if ( dep1 > dep2 )
			return dep1 + 1;
		else
			return dep2 + 1;
	}
}
int main()
{
    //例子:AB#D##C##
    cout<<"请按前序遍历序列依次输入:";
    BiTree<char> E;
    cout << "该二叉树的前序遍历序列是:";
	E.PreOrder();
	cout << "\n该二叉树的中序遍历序列是:";
	E.InOrder();
	cout << "\n该二叉树的后序遍历序列是:";
	E.PostOrder();
	cout << "\n该二叉树的层序遍历序列是:";
    E.LeverOrder();
    cout <<endl;
//    E.GetParentout();
//    E.Count();
    E.countleaf();
    cout<<"树的深度:";
    E.BiTreeDepth();
	return 0;

}


输入:AB#D##C##

运行结果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值