二叉树的各种操作汇总

#include <bits/stdc++.h>

using namespace std;

struct node
{
    char data;
    node *lchild, *rchild;
};

char a[100], b[100];
int top = 0;
//dbgeafc
//dgebfca
node *creatTree() //一行建立二叉树
{
    node *root;
    top++;
    if (a[top] == ',')
        return NULL;
    else
    {
        root = new node;
        root->data = a[top];
        root->lchild = creatTree();
        root->rchild = creatTree();
    }
    return root;
}
node *createTreexz(char a[], int n, char b[]) //已知先序和中序建立二叉树
{
    if (n <= 0)
        return NULL;
    node *root = new node;
    root->data = a[0];
    int i;
    for (i = 0; i < n; i++)
        if (a[0] == b[i])
            break;
    root->lchild = createTreexz(a + 1, i, b);
    root->rchild = createTreexz(a + i + 1, n - i - 1, b + i + 1);
    return root;
}
node *createTree(char a[], int n, char b[]) //已知中序和后序建立二叉树
{
    if (n <= 0)
        return NULL;
    node *root;
    root = new node;
    root->data = b[n - 1];
    int i;
    for (i = 0; i < n; i++)
        if (a[i] == b[n - 1])
            break;
    root->lchild = createTree(a, i, b);
    root->rchild = createTree(a + i + 1, n - i - 1, b + i);
    return root;
}
void xb(node *head) //先序遍历
{
    if (head == NULL)
        return;
    else
    {
        cout << head->data;
        xb(head->lchild);
        xb(head->rchild);
    }
}
int depth(node *head) //求二叉树深度
{
    int m, n;
    //若定义为全局变量会WR
    if (head == NULL) return 0;
    else
    {
        m = depth(head->lchild);
        n = depth(head->rchild);
        if (m > n)
            return m + 1;
        else
            return n + 1;
    }
}
int leaves(node *head) //叶子数
{
    if (head == NULL)
        return 0;
    else if (head->lchild == NULL && head->rchild == NULL)
        return 1;
    else
        return leaves(head->lchild) + leaves(head->rchild);
}
void cengcibianli(node *t) //层次遍历
{
    if (t == NULL)
        return;
    queue<node *> q;
    q.push(t);
    while (q.empty() == false) //如果队列不是空,继续执行
    {
        node *x = q.front(); //返回第一个元素
        cout << x->data;     //输出
        q.pop();             //删除第一个元素
        if (x->lchild != NULL)
            q.push(x->lchild); //左孩子入队
        if (x->rchild != NULL)
            q.push(x->rchild); //右孩子入队
    }
}
void cengcibianli(node *t) //层次遍历
{
    node *a[10000];
    int in, out;
    in = out = 0;
    a[in++] = t; //将根节点导入
    while (in > out)
    {
        if (a[out])
        {
            cout << a[out]->data;
            a[in++] = a[out]->lchild;
            a[in++] = a[out]->rchild;
        }
        out++;
    }
}
int main()
{
    node *root;
    int n;
    cin >> n;
    while (n--)
    {
        cin >> a;
        cin >> b;
        int len = strlen(a);
        root = createTree(a, len, b);
        xb(root);
        cout << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JdiLfc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值