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

    #include <bits/stdc++.h>
    #define null NULL
    #define maxn 500005
    
    typedef long long ll;
    
    using namespace std;
    
    typedef struct tree//二叉树的定义
    {
        char data;
        tree *left;
        tree *right;
    }tree,*netree;
    
    //创建一个先序二叉树
    netree createtree()
    {
        char c;
        cin >> c;
        netree root;
        if(c == '#') root = null;
        else
        {
            root = new tree;
            root -> data = c;
            root -> left = createtree();
            root -> right = createtree();
        }
        return root;
    }
    
    //输出递归先序二叉树
    void pretree(netree root)
    {
        if(root)
        {
            cout << root -> data << " ";
            pretree(root -> left);
            pretree(root -> right);
        }
    }
    
    //输出递归中序二叉树
    void midtree(netree root)
    {
        if(root)
        {
            midtree(root -> left);
            cout << root -> data << " ";
            midtree(root -> right);
        }
    }
    
    //输出递归后序二叉树
    void endtree(netree root)
    {
        if(root)
        {
            endtree(root -> left);
            endtree(root -> right);
            cout << root -> data << " ";
        }
    }
    
    //层次遍历
    void every(netree root)
    {
        queue<netree> q;
        if(root)
        {
            q.push(root);
            while(!q.empty())
            {
                netree p;
                p = q.front();
                q.pop();
                cout << p -> data << " ";
                if(p -> left)q.push(p -> left);
                if(p -> right)q.push(p -> right);
            }
        }
    }
    
    void mid_tree(netree root)//非递归中序遍历
    {
        stack<netree>s;
        while(!s.empty() || root)
        {
            if(root)
            {
                s.push(root);
                root = root -> left;
            }
            else
            {
                netree p;
                p = s.top();
                s.pop();
                cout << p -> data << " ";
                root = p -> right;
            }
        }
    
    }
    
    void leaves(netree root)//叶子的数量
    {
        ll ans = 0;
        queue<netree> q;
        if(root)
        {
            q.push(root);
            while(!q.empty())
            {
                netree p;
                p = q.front();
                q.pop();
                if(p -> left == null && p -> right == null)ans++;
                cout << p -> data << " ";
                if(p -> left)q.push(p -> left);
                if(p -> right)q.push(p -> right);
            }
        }
        cout << ans << endl;
    }
    
    ll detree(netree root)//树的深度
    {
        if(root == null)return 0;
        else
        {
            ll m = detree(root -> left);
            ll n = detree(root -> right);
            if(m > n)return (m + 1);
            else return (n + 1);
        }
    }
    
    ll treecount(netree root)//树结点数
    {
        if(root == null)return 0;
        else return treecount(root -> left) + treecount(root -> right) + 1;
    }
    
    void changetree(netree &root)//交换左右子树
    {
        netree p;
        if(root)
        {
            p = root -> left;
            root -> left = root -> right;
            root ->right = p;
            changetree(root -> left);
            changetree(root -> right);
        }
    }
    int main()
    {
        netree root;
    
        cout << "请输入一个先序的二叉树(以#为空)来建立二叉树" << endl;
        root = createtree();
    
        cout << "二叉树的递归先序遍历是" <<endl;
        pretree(root);
        cout << endl;
    
        cout << "二叉树的递归中序遍历是" <<endl;
        midtree(root);
        cout << endl;
    
        cout << "二叉树的递归后序遍历是" <<endl;
        endtree(root);
        cout << endl;
    
        cout << "二叉树的层次遍历是" <<endl;
        every(root);
        cout << endl;
    
        cout << "二叉树的非递归中序遍历是" <<endl;
        mid_tree(root);
        cout << endl;
    
        cout << "叶子的数量" << endl;
        leaves(root);
    
        cout << "树的深度" << endl;
        cout << detree(root) << endl;
    
        cout << "树的结点数" << endl;
        cout << treecount(root) << endl;
    
        cout << "交换左右子树" << endl;
        changetree(root);
        cout << "交换成功" << endl;
    
        cout << "交换后二叉树的递归先序遍历是" <<endl;
        pretree(root);
        cout << endl;
    
        cout << "交换后二叉树的递归中序遍历是" <<endl;
        midtree(root);
        cout << endl;
    
        cout << "交换后二叉树的递归后序遍历是" <<endl;
        endtree(root);
        cout << endl;
    
        cout << "交换后二叉树的层次遍历是" <<endl;
        every(root);
        cout << endl;
    
        cout << "交换后二叉树的非递归中序遍历是" <<endl;
        mid_tree(root);
        cout << endl;
    
    
    
    }
    
    /*
    A##
    ABC####
    AB##C##
    ABCD###EF##G##H##
    A##B##
    */
    

      

  • 相关阅读:
    Faster-RCNN Pytorch实现的minibatch包装
    RuntimeError: cuda runtime error (10) : invalid device ordinal
    pip使用技巧
    Pytorch model saving and loading 模型保存和读取
    vs 附加包含目录不起作用
    微服务杂谈
    spring 学习笔记
    java 发送 http 请求
    PHP7 错误及异常机制
    Mac OS 下定制终端颜色
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/9978393.html
Copyright © 2011-2022 走看看