数据结构二叉树

#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##
*/

  

原文地址:https://www.cnblogs.com/WINDZLY/p/9978393.html