多种二叉树基本问题的递归求解

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100;

typedef struct node {
    int e;
    node *lc, *rc;
};

node* loc[N];   // 用于构造二叉树时寻找插入位置
node* T = new node({ 1, NULL, NULL });      // 树根

int sum, max_num;       // sum用于计算度为0/1/2结点个数;max_num用于找出结点最大值
int wid[N], tree_wid = 1;       // wid[i]用于记录第i层宽度,tree_wid表示树宽,参考 https://blog.csdn.net/mcb199175mcb/article/details/12031217

node* Find(int a)
{
    return loc[a];
}

node* left_ins(int a, int v)
{
    node* t = new node({ v, NULL, NULL });
    node* p = Find(a);
    p->lc = t;
    return t;
}
node* right_ins(int a, int v)
{
    node* t = new node({ v, NULL, NULL });
    node* p = Find(a);
    p->rc = t;
    return t;
}

void initial()
{
    loc[1] = T;
    char op[2];
    int v = 1, a;
    cin >> op >> a;
    while (*op != 'x')
    {
        if (*op == 'l')
        {
            v += 1;
            loc[v] = left_ins(a, v);
        }
        else
        {
            v += 1;
            loc[v] = right_ins(a, v);
        }
        cin >> op >> a;
    }
}

void  Preordershow(node* p)
{
    if (p)
    {
        cout << p->e << ' ';
        Preordershow(p->lc);
        Preordershow(p->rc);
    }
}

void  Inordershow(node* p)
{
    if (p)
    {
        Inordershow(p->lc);
        cout << p->e << ' ';
        Inordershow(p->rc);
    }
}

/*int  Preorder(node *p)      // 找出度为0,1,2的结点个数
{
    if (p)
    {
        int i = Preorder(p->lc);
        int j = Preorder(p->rc);
        if (i + j == 0) sum += 1;
        return 0;
    }
    else
        return 1;
}*/


/*void Preorder(node* p)      // 找出值最大的结点
{
    if (p)
    {
        max_num = max(max_num, p->e);
        Preorder(p->lc);
        Preorder(p->rc);
    }
}*/


/*int  Preorder(node *p)      // 删掉叶子
{
    if (p)
    {
        int i = Preorder(p->lc);
        int j = Preorder(p->rc);
        if (i == 2)     // 说明左孩子是叶子
        {
            node* t = p->lc;
            loc[p->lc->e] = NULL;
            p->lc = NULL;
            delete t;
        }
        if (j == 2)
        {
            node* t = p->rc;
            loc[p->rc->e] = NULL;
            p->rc = NULL;
            delete t;
        }
        if (i == 1 && j == 1) return 2;
        return 0;
    }
    else
        return 1;
}*/



/*
void Preorder(node* p)      // 交换左右孩子
{
    if (p)
    {
        Preorder(p->lc);
        Preorder(p->rc);
        auto t = p->lc;
        p->lc = p->rc;
        p->rc = t; 
    }
}
*/

/*
int Preorder(node* p)      // 计算树高度
{
    if (p)
    {
        int i = Preorder(p->lc);
        int j = Preorder(p->rc);
        return max(i, j) + 1;
    }
    else
        return 0;
}
*/



/*
void Preorder(node* p, int h)      // 计算树宽度
{
    if (p)
    {
        wid[h] ++;
        tree_wid = max(tree_wid, wid[h]);
        Preorder(p->lc, h + 1);
        Preorder(p->rc, h + 1);
    }

}
*/

/*
void Preorder(node* p, int v, int h)      // 计算指定点的层次
{
    if (p)
    {
        if (p->e == v)
        {
            cout << h;
            return;
        }
        Preorder(p->lc, v, h + 1);
        Preorder(p->rc, v, h + 1);
    }

}
*/



void Preorder(node* p, int h)      // 前序遍历且将层次一并输出
{
    if (p)
    {
        printf("(value = %d, height = %d), ", p->e, h);
        Preorder(p->lc, h + 1);
        Preorder(p->rc, h + 1);
    }

}


/*
为二叉树添加结点:
    l n  或  r n:表示在结点n的左边添加一个结点 或 在结点n的右边添加一个结点
                (结点的标号是自增的,建议先把自己要构建的二叉树画出来)
    如:
    l 1                                 1
    r 1                              /     \
    l 2                             2       3
    r 2                           /   \    /  \
    l 3                         4      5  6     7
    r 3                                  / \   / \
    l 6                                 8   9 10  11
    r 6                                    / \
    l 7                                   12  13
    r 7
    l 9
    r 9 构造出的二叉树如右:

    输入  x n结束构造,n表示任意数字,如输入 x 0
*/

int main()
{
    initial();


    Preorder(T, 1);

    //cout << tree_wid;

    //Preordershow(T);
    cout << endl;
    //Inordershow(T);
    //cout << sum;
    //cout << max_num;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值