二叉树的基本操作 (二叉链表实现)

///以下代码都是自己根据理解写的 可能有不对的地方 麻烦路过的大神指出来 感激不尽
///以下所有操作均只能对第一个值为X的节点进行操作 至于怎么解决这个问题。。。我再想想
#include<iostream>
#include<queue>
using namespace std;
typedef struct treenode{
    char data;
    treenode *lchild;
    treenode *rchild;
}node;
node *create(node *t){
    char a;
    cin >> a;
    if(a == '0'){
        return 0;
    }
    t = new node;
    t->data = a;
    t->lchild = create(t->lchild);
    t->rchild = create(t->rchild);
    return t;
}
node *create_1(node *t)
{
    char a;
    cin >> a;
    t = new node;
    t->data = a;
    if(a == '0')
        return 0;
    else
    {
        t->lchild = create_1(t->lchild);
        t->rchild = create_1(t->rchild);
    }
    return t;
}
int leaves(node *t){
    int sum = 0, lsum, rsum;
    if(t){
        if(!(t->lchild) && !(t->rchild)){
            sum++;
        }
        lsum = leaves(t->lchild);
        sum += lsum;
        rsum = leaves(t->rchild);
        sum += rsum;
    }
    return sum;
}
void pre(node *t){
    if(t){
        cout << t->data;
        pre(t->lchild);
        pre(t->rchild);
    }
}
void in(node *t){
    if(t){
        in(t->lchild);
        cout << t->data;
        in(t->rchild);
    }
}
void post(node *t){
    if(t){
        post(t->lchild);
        post(t->rchild);
        cout << t->data;
    }
}
int depth(node *t){
    int d = 0, ld = 0, rd = 0;
    if(!t){
        d = 0;
    }
    else{
        ld += depth(t->lchild);
        rd += depth(t->rchild);
        d = 1 + max(ld, rd);
    }
    return d;
}
int IsEmpty(node *t)
{
    if(!t)
        return 1;
    return 0;
}

void destroy_tree(node *t)
{
    if(t){
        if(t->lchild)
            destroy_tree(t->lchild);
        if(t->rchild)
            destroy_tree(t->rchild);
        delete t;

    }

}

void level(node *t)
{
    queue<node *>q;
    node *a;
    if(t)
    {
        while(!q.empty())
            q.pop();
        q.push(t);
        while(!q.empty())
        {
            a = q.front();
            q.pop();
            cout << a->data;
            if(a->lchild)
                q.push(a->lchild);
            if(a->rchild)
                q.push(a->rchild);
        }
    }
}
node * parent(node *t, char e)///找某个节点的双亲
{
    queue<node *>q;
    node *a;
    if(t)
    {
        while(!q.empty())
            q.pop();
        q.push(t);
        while(!q.empty())
        {
            a = q.front();
            q.pop();
            if(a->lchild && a->lchild->data==e||a->rchild && a->rchild->data == e)
                return a;
            else
            {
                if(a->lchild)
                    q.push(a->lchild);
                if(a->rchild)
                    q.push(a->rchild);
            }
        }
    }
    return NULL;
}
node * leftchild1(node *t, char e)///遍历所有节点去找值为e的那个节点
{
    queue<node *>q;
    node *a;
    if(t)
    {
        while(!q.empty())
            q.pop();
        q.push(t);
        while(!q.empty())
        {
            a = q.front();
            q.pop();
            if(a->data == e)
                return a->lchild;
            else
            {
                if(a->lchild)
                    q.push(a->lchild);
                if(a->rchild)
                    q.push(a->rchild);
            }
        }
    }
    return NULL;
}
///返回二叉树T中指向元素值为s的结点的指针。
node *point(node *t, char s)
{
    queue<node *>q;
    node *a;
    if(t)
    {   ///队列 宽搜 bfs
        while(!q.empty())
        q.pop();
        q.push(t);
        while(!q.empty())
        {
            a = q.front();
            q.pop();
            if(a->data == s)
                return a;
            if(a->lchild)
                q.push(a->lchild);
            if(a->rchild)
                q.push(a->rchild);
        }
    }
    return NULL;
}

node *leftchild2(node *t, char e)///也是遍历的所有节点去找   不喜欢这种 还是上面的写着顺手
{
    node *a;
    if(t)
    {
        a = point(t, e);///这里已经找到了值为e的节点 之后只需返回它的左孩子(前提是他们都存在)
        if(a && a->lchild)///但是这里又有一个问题, 就是只能找按层序第一个值为e的节点,那要是想要其
            return a->lchild;///他的怎么办呢 值得思考。。。 还是先把基本功能实现再说吧~
    }
    return NULL;
}
///右孩子同理啊 先不写了
node* leftsibling(node *t, char e)///要注意一下 若e是它的双亲的左孩子, 那么e无左兄弟
{   if(t)
    {
        node *a = parent(t, e);
        node *b = point(t, e);
       /// if(a && b == a->lchild) 有的时候 第一次写往往很麻烦 所以每一句都要反复推敲啊
           /// return NULL;
        if(a && a->lchild && b!=a->lchild)
            return a->lchild;
        return NULL;
    }

}
///右兄弟也同理
///要插入了 好激动啊啊啊啊啊啊啊啊
bool insert1(node *t, int i, node *t1)///小树左右子树空不空都行
{
    node * tmp;///临时节点
    if(t)///要插的节点不空
    {
        if(i == 0)///插左子树
        {
            tmp = t1;
            while(tmp->lchild)   ///感觉t的左孩子放小树的哪里都可以吧
                tmp = tmp->lchild; ///我就放在最左边了 形容的不太具体
            tmp = t->lchild;     ///看代码应该就明白了
            t->lchild = t1;///刚开始没用临时节点 这里就错了呀
        }
        else///插右
        {
            while(tmp->rchild)
                tmp = tmp->rchild;
            tmp = t->rchild;
            t->rchild = t1;
        }
        return true;
    }
    return false;
}
bool delete1(node *t, int i)
{
    if(t)
    {
        if(i == 0)
        {
            t->lchild = NULL;
        }
        else
        {
            t->rchild = NULL;
        }
        return true;
    }
    return false;
}
int main(int argc, char const *argv[]) {
    node *tree;
    node *t1;///先建一个一会要插入的小树
    t1 = create_1(t1);///为什么要写两个建树的函数呢 呵呵哒 我只是练练手~
    tree = create(tree);///建树
    cout << "small tree level:" << endl;
    level(t1);
    cout << endl;
    cout << "pre_order_traverse:" << endl;
    pre(tree);///先序遍历
    cout << endl;
    cout << "in_order_traverse:" << endl;
    in(tree);///中
    cout << endl;
    cout << "post_order_traverse:" << endl;
    post(tree);///后
    cout << endl;
    cout << "level_order_traverse:" << endl;
    level(tree);///层
    cout << endl;
    int n = leaves(tree);///叶子数
    cout << "leaves num:" << endl;
    cout << n << endl;
    int d = depth(tree);///树深
    cout << "depth:" << endl;
    cout << d << endl;
    char s;
    cin >> s;///输入一会要插的那个节点
    node *t = point(tree, s);///找某个节点
    cout << t->data << endl;///没啥用 就是看看找着没 insert1里面有做判断
    cout << "find parent:\n";
    node * p = parent(tree, 'f');///找双亲
    cout << p->data << endl;
    cout << "find left child(way 1):\n";
    node *q = leftchild1(tree, 'e');
    if(q)
        cout << q->data << endl;
    else
        cout << "NULL" << endl;
    cout << "find leftchild(way 2):\n";
    q = leftchild2(tree, 'a');
    if(q)
        cout << q->data << endl;
    else
        cout << "NULL" << endl;
    cout << "find leftsibling:\n";
    q = leftsibling(tree,'a');
    if(q)
        cout << q->data << endl;
    else
        cout << "NULL" << endl;
    ///OK 现在可以开始今天的重头戏了~
    cout << "Insert child:\n";
    int i;
    cin >> i;
    if(insert1(t, i, t1))///t是上面我们找的值为某某的节点, 把t1插在节点t的左或右
    {
        level(tree);///子树上   0代表左 1代表右
        cout << endl;
    }
    else
    {
        cout << "插入失败"  << endl;
    }
    ///完成完成 哈哈哈哈
    ///接下来是删除
    cout << "Delete child:\n";
    ///还有二十分钟熄灯 害怕~
    cin >> s;
    t = point(tree, s);///删除节点t的左子树(0)或者右子树(1)
    cin >> i;
    if(delete1(t, i))   ///这个太简单 还是删t这个节点好玩 呵呵哒 他有两个子树
    {                    ///删了也没地儿放啊 【笑哭】 得了 就这样吧
        level(tree);
        cout << endl;
    }
    else
        cout << "删除失败\n" ;
    destroy_tree(tree);///销毁
    if(IsEmpty(tree))///判断树空
        cout << "empty!" << endl;
    return 0;
}

/*
ta00e0k00
abd00e00cf00g00
small tree level:
taek
pre_order_traverse:
abdecfg
in_order_traverse:
dbeafcg
post_order_traverse:
debfgca
level_order_traverse:
abcdefg
leaves num:
4
depth:
3

b
b
find parent:
c
find left child(way 1):
NULL
find leftchild(way 2):
b
find leftsibling:
NULL
Insert child:
0
abctefgaek
Delete child:
a
0
acfg

Process returned 0 (0x0)   execution time : 23.389 s
Press any key to continue.

*/
///熄灯之前圆满完成任务 完美

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值