zoukankan      html  css  js  c++  java
  • 数据结构作业-二叉树

    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<queue> 
    using namespace std;
    typedef struct binode{
        char date;
        binode *lchild,*rchild;
    }binode,*bitree;
    void creatBT(bitree &T) //创建二叉树 
    {
        char ch;
        T=new binode;
        cin>>ch;
        if(ch=='#')
        {
            T=NULL;
            return;
        }
        else
        {
            T->date=ch;
            creatBT(T->lchild);
            creatBT(T->rchild);
        }
    }
    int emptyBT(bitree &T)
    {
        if(!T)
        return 0;
        else
        return 1;
    }
    int depthBT(bitree &T)    //求深度 
    {
        if(!T)
        return 0; 
        else if(T->lchild==0&&T->rchild==0)
        return 1;
        else
        return max(depthBT(T->lchild),depthBT(T->rchild))+1;
    }
    int findBT(bitree &T,char e)//查找元素 
    {
        if(!T)
        return 0;
        if(T->date==e)
        return 1;
        else if(findBT(T->lchild,e)||findBT(T->rchild,e))
        return 1;
        else
        return 0;
    }
    void preorder(bitree &T)//先序遍历 
    {
        if(!T)
        {
            cout<<'#'<<' ';
            return;
        }
        cout<<T->date<<' ';
        preorder(T->lchild);
        preorder(T->rchild);
    }
    void inorder(bitree &T)//中序遍历 
    {
        if(!T)
        {
            cout<<'#'<<' ';
            return;
        }
        inorder(T->lchild);
        cout<<T->date<<' ';
        inorder(T->rchild);
    }
    void postorder(bitree &T)//后序遍历 
    {
        if(!T)
        {
            cout<<'#'<<' ';
            return;
        }
        postorder(T->lchild);
        postorder(T->rchild);
        cout<<T->date<<' ';
    }
    void levelorder(bitree &T)//层次遍历,用bfs 
    {
        queue<bitree>q;
        while(!q.empty())
        q.pop();
        q.push(T);
        while(!q.empty())
        {
            bitree p=q.front();
            q.pop();
            cout<<p->date<<' ';
            if(p->lchild!=NULL)
            q.push(p->lchild);
            if(p->rchild!=NULL)
            q.push(p->rchild);
        }
    }
    int leafcount(bitree &T)//求叶子节点个数 
    {
        if(!T)
        return 0;
        if(T->lchild==0&&T->rchild==0)
        return 1;
        else
        return leafcount(T->lchild)+leafcount(T->rchild);
    }
    int nodecount(bitree &T)//求总结点的个数 
    {
        if(!T)
        return 0;
        else
        return nodecount(T->lchild)+nodecount(T->rchild)+1; 
    }
    void deleteBT(bitree &T)//删除 
    {
        if(!T)
        return;
        else
        {
            deleteBT(T->lchild);
            deleteBT(T->rchild);
            delete T;
        }
    }
    int main()
    {
        bitree T=NULL;//输入数据abd###ce###
        creatBT(T);
        cout<<"先序遍历为:";
        preorder(T);
        cout<<endl;
        cout<<"中序遍历为:";
        inorder(T);
        cout<<endl;
        cout<<"后序遍历为:";
        postorder(T);
        cout<<endl;
        cout<<"层次遍历为:";
        levelorder(T);
        cout<<endl;
        cout<<"深度为:";
        cout<<depthBT(T)<<endl;
        cout<<"节点个数为:";
        cout<<nodecount(T)<<endl;
        cout<<"叶子节点个数为:";
        cout<<leafcount(T)<<endl;
        cout<<"请输入要查找的字符:"<<endl;
        char e;
        cin>>e;
        if(findBT(T,e))
        cout<<"存在"<<endl;
        else
        cout<<"不存在"<<endl;
        deleteBT(T);
        return 0;
     } 
  • 相关阅读:
    LeetCode "Palindrome Partition II"
    LeetCode "Longest Substring Without Repeating Characters"
    LeetCode "Wildcard Matching"
    LeetCode "Best Time to Buy and Sell Stock II"
    LeetCodeEPI "Best Time to Buy and Sell Stock"
    LeetCode "Substring with Concatenation of All Words"
    LeetCode "Word Break II"
    LeetCode "Word Break"
    Some thoughts..
    LeetCode "Longest Valid Parentheses"
  • 原文地址:https://www.cnblogs.com/6262369sss/p/9119474.html
Copyright © 2011-2022 走看看