c实现二叉树

C实现二叉树

简单说明

实现了先序遍历、中序遍历、后序遍历、搜索

本来想着和平衡二叉树一起放上来的,但是花了一个下午也只是把平衡二叉树原理弄懂和左右旋代码实现,最难的平衡左/右旋还没弄,就不显摆了,就分开来写吧。

代码实现

利用了堆栈来存储每一个左节点,利用左节点把所有点的信息全部记录下来,因为左节点可以记录其子节点的地址,然后,按照树的存储规则将堆栈中的信息分配到二叉树中。

#include <stdio.h>
#include <stdlib.h>

typedef struct treenode{
    char str;
    struct treenode *left;
    struct treenode *right;
}*btree,treenode;

// x(a(b,c),d(e(g,h),f))
//       x
//    a      d
//  b  c   e   f
//        g h 
void createtree(btree btre, char *str, int num){
    int lr = 0; // left 0, right 1
    int top = 0;
    btree p;
    btree pstack[num];

    for(int i=0; i < num; i++){
        switch(str[i]){
            case '(':
                {
                    printf("(");
                    lr = 0;
                    top ++;
                    pstack[top] = p;
                    break;
                }
            case ')':
                {
                    printf(")");
                    if(top < 1){
                        printf("stack is empty\n");
                        exit(0);
                    }
                    top --;
                    break;
                }
            case ',':
                {
                    printf(",");
                    lr = 1;
                    break;
                }
            default:
                {
                    printf("d");
                    p = (btree)malloc(sizeof(treenode));
                    p->left = p->right = NULL;
                    p->str = str[i];
                    if(top == 0){
                        btre->str = p->str;
                        break;
                    }
                    if(lr == 0){
                        pstack[top]->left = p;
                    }
                    else
                        pstack[top]->right = p;
                }
        }
    }
    btre->right = pstack[1]->right;
    btre->left = pstack[1]->left;
}

void preorder(btree btre){
    btree p = btre;
    
    if(p != NULL){
        printf("%c->",p->str);
        preorder(p->left);
        preorder(p->right);
    }
}

void inorder(btree btre){
    btree p = btre;

    if(p != NULL){
        inorder(p->left);
        printf("%c->",p->str);
        inorder(p->right);
    }
}

void postorder(btree btre){
    btree p = btre;

    if(p != NULL){
        postorder(p->left);
        postorder(p->right);
        printf("%c->",p->str);
    }
}

void cleartree(btree btre){
    if(btre != NULL){
        cleartree(btre->left);
        cleartree(btre->right);
        free(btre);
        btre = NULL;
        printf(".");
    }
}

char search(btree btre,char x){
    if(btre == NULL){
        return 'N';
    }else{
        if(x == btre->str){
            return btre->str;
        }else{
            if(x == search(btre->left,x)){
                return x;
            }
            if(x == search(btre->right,x)){
                return x;
            }
            return 'N';
        }
    }
}

int main(){
    char *str = "x(a(b,c),d(e(g,h),f))";
    printf("%s\n",str);
    btree btre = (btree)malloc(sizeof(treenode));
    createtree(btre, str, 21);
    printf("\npreorder:\n");
    preorder(btre);
    printf("\ninorder:\n");
    inorder(btre);
    printf("\npostorder:\n");
    postorder(btre);
    
    char c = search(btre,'d');
    printf("\nsearch result:%c",c);

    printf("\nclear");
    cleartree(btre);
    printf("\n");
}

转载于:https://www.cnblogs.com/wangha/p/11107796.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值