二叉树及其基本功能的实现

二叉树及其基本功能的实现

#include<bits/stdc++.h>
using namespace std;
struct BinTree
{
    int data;
    BinTree* lchild;
    BinTree* rchild;
};

//创建一棵二叉树
int create(BinTree*(&T))
{
    int num;
    cin>>num;
    if(num==-1)
    {
        T=NULL;
        return 0;
    }
    else
    {
        T=(BinTree*)malloc(sizeof(BinTree));
        T->data=num;
        cout<<"请输入"<<num<<"的左节点的值:"<<endl;
        create(T->lchild);
        cout<<"请输入"<<num<<"的右节点的值:"<<endl;
        create(T->rchild);
    }
}
//递归方法先序遍历二叉树
int Preordertraverse(BinTree* T)
{
    if(T==NULL)
    {
        return 0;
    }
    else
    {
        cout<<T->data<<" ";
        Preordertraverse(T->lchild);
        Preordertraverse(T->rchild);
    }
}
//递归方法中序遍历二叉树
int Inordertraverse(BinTree* T)
{
    if(T==NULL)
    {
        return 0;
    }
    else
    {
        Inordertraverse(T->lchild);
        cout<<T->data<<" ";
        Inordertraverse(T->rchild);
    }
}
//递归方法后序遍历二叉树
int Postordertraverse(BinTree* T)
{
    if(T==NULL)
    {
        return 0;
    }
    else
    {
        Postordertraverse(T->lchild);
        Postordertraverse(T->rchild);
        cout<<T->data<<" ";
    }
}
//销毁一棵二叉树
int Destroy(BinTree*(&T))
{
    if(T!=NULL)
    {
        Destroy(T->lchild);
        Destroy(T->rchild);
        cout<<T->data<<" ";
        free(T);
        T=NULL;
    }
}
int cnt=0;
//计算一棵二叉树的叶子数
int leafnum(BinTree* T)
{
    if(T!=NULL)
    {
        if(T->lchild==NULL&&T->rchild==NULL)cnt++;
        leafnum(T->lchild);
        leafnum(T->rchild);
    }
}
//计算一棵二叉树的叶子数
int leafcount(BinTree* T)
{
    if(T==NULL)return 0;
    else if(T->lchild==NULL&&T->rchild==NULL)return 1;
    else
    {
        return leafcount(T->lchild)+leafcount(T->rchild);
    }
}
//求一棵二叉树的高度
int height(BinTree* T)
{
    if(T==NULL)return 0;
    else if(T->lchild==NULL&&T->rchild==NULL)return 1;
    else
    {
        return height(T->lchild)+1>height(T->rchild)+1?height(T->lchild)+1:height(T->rchild)+1;
    }
}
//求一棵二叉树节点总数的算法
int Nodecount(BinTree* T)
{
    if(T==NULL)return 0;
    else return Nodecount(T->lchild)+Nodecount(T->rchild)+1;
}
//层次遍历二叉树(广度优先遍历二叉树)
int BFS(BinTree* T)
{
    queue<BinTree*>Q;
    BinTree* p;
    if(T==NULL)return 0;
    else
    {
        Q.push(T);
        while(!Q.empty())
    {
        cout<<Q.front()->data<<" ";
        if(Q.front()->lchild)Q.push(Q.front()->lchild);
        if(Q.front()->rchild)Q.push(Q.front()->rchild);
        Q.pop();
    }
    }
}
//求出所有大于x的节点总数
int largercount(BinTree* T,int x)
{
    if(T==NULL)return 0;
    else if(T->data>x)return largercount(T->lchild,x)+largercount(T->rchild,x)+1;
    else return largercount(T->lchild,x)+largercount(T->rchild,x);
}
int cnt1=0;
int Largercount(BinTree* T,int x)
{
    if(T==NULL)return 0;
    else if(T->data>x)cnt1++;
    Largercount(T->lchild,x);
    Largercount(T->rchild,x);
}
//删除每一个值为x的节点并删去它的子树
int DeleteX(BinTree*(&T),int x)
{
    if(T)
    {
        if(T->data==x)
        {
            Destroy(T);
        }
        else
        {
            DeleteX(T->lchild,x);
            DeleteX(T->rchild,x);
        }
    }
}
//复制一棵二叉树
BinTree* CopyTree(BinTree* T,BinTree*(&pt))
{
    if(T==NULL)pt=NULL;
    else
    {
        pt=new BinTree[1];
        pt->data=T->data;
        CopyTree(T->lchild,pt->lchild);
        CopyTree(T->rchild,pt->rchild);
    }
}
BinTree* copyTree(BinTree* T)
{
    BinTree* pt;
    if(T==NULL)return NULL;
    else
    {
        pt=new BinTree[1];
        pt->data=T->data;
        pt->lchild=copyTree(T->lchild);
        pt->rchild=copyTree(T->rchild);
    }
}
//判断两棵二叉树是否相等
int BinTreeEqual(BinTree* T1,BinTree* T2)
{
    if(T1==NULL&&T2==NULL)return 1;
    else if(T1==NULL||T2==NULL)return 0;
    else if(T1->data==T2->data&&BinTreeEqual(T1->lchild,T2->lchild)
            &&BinTreeEqual(T1->rchild,T2->rchild))return 1;
    else return 0;
}
//交换二叉树所有左右节点的值域
void Swap(BinTree* T)
{
    if(T!=NULL)
    {
        BinTree* p;
        p=T->lchild;
        T->lchild=T->rchild;
        T->rchild=p;
        Swap(T->lchild);
        Swap(T->rchild);
    }
}
//中序遍历非递归算法1
int Inordertraverse1(BinTree* T)
{
    if(T)
    {
        BinTree* p;
        stack<BinTree*>S;
        S.push(T);
        while(!S.empty())
    {
        while(S.top())S.push(S.top()->lchild);
        S.pop();
        if(!S.empty())
        {
            p=S.top();
            cout<<p->data<<" ";
            S.pop();
            S.push(p->rchild);
        }
    }
    }
}
//非递归算法中序遍历2
int Inordertraverse2(BinTree* T)
{
        stack<BinTree*>S;
        BinTree* p=T;
        while(p||!S.empty())
        {
            while(p){S.push(p);p=p->lchild;}
            p=S.top();
            cout<<p->data<<" ";
            S.pop();
            p=p->rchild;
        }
}
//先序遍历二叉树的非递归算法
int Preordertraverse1(BinTree* T)
{
    stack<BinTree*>S;
    BinTree* p=T;
    while(p||!S.empty())
    {
        while(p)
        {
            cout<<p->data<<" ";
            S.push(p);
            p=p->lchild;
        }
        if(!S.empty())
        {
            p=S.top();
            S.pop();
            p=p->rchild;
        }
    }
}
//后序遍历二叉树的非递归算法
int Postordertraverse1(BinTree* T)
{
    stack<BinTree*>S;
    BinTree* p=T;
    BinTree* r=NULL;
    while(p||!S.empty())
    {
        if(p)
        {
            S.push(p);
            p=p->lchild;
        }
        else
        {
            p=S.top();
            if(p->rchild!=r&&p->rchild)
            {
                p=p->rchild;
                S.push(p);
                p=p->lchild;
            }
            else
            {
                S.pop();
                cout<<p->data<<" ";
                r=p;
                p=NULL;
            }
        }
    }
}
int Postordertraverse2(BinTree* T)
{
    stack<BinTree*>S;
    vector<int>a;
    if(!T)return 0;
    S.push(T);
    while(!S.empty())
    {
        BinTree* p=S.top();
        a.push_back(p->data);
        S.pop();
        if(p->lchild)S.push(p->lchild);
        if(p->rchild)S.push(p->rchild);
    }
    reverse(a.begin(),a.end());
    for(int i=0;i<a.size();i++)cout<<a[i]<<" ";
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    BinTree* T;
    cout<<"请输入根节点的值,若输入的值为-1则表示该树为空树:"<<endl;
    create(T);
    cout<<"先序遍历:"<<endl;
    Preordertraverse1(T);
    cout<<endl;
    cout<<"中序遍历:"<<endl;
    Inordertraverse2(T);
    cout<<endl;
    cout<<"后序遍历:"<<endl;
    Postordertraverse2(T);
    cout<<endl;
    cout<<"广度优先遍历:"<<endl;
    BFS(T);
    cout<<endl;
    cout<<"叶子节点总数为:"<<leafcount(T)<<endl;
    cout<<"二叉树的高度为:"<<height(T)<<endl;
    cout<<"二叉树的节点总数为:"<<Nodecount(T)<<endl;
    cout<<"二叉树中所有大于1的节点总数为:"<< largercount(T,1)<<endl;
    cout<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值