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");
}

猜你喜欢

转载自www.cnblogs.com/wangha/p/11107796.html