zoukankan      html  css  js  c++  java
  • 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
    ");
                            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
    ",str);
        btree btre = (btree)malloc(sizeof(treenode));
        createtree(btre, str, 21);
        printf("
    preorder:
    ");
        preorder(btre);
        printf("
    inorder:
    ");
        inorder(btre);
        printf("
    postorder:
    ");
        postorder(btre);
        
        char c = search(btre,'d');
        printf("
    search result:%c",c);
    
        printf("
    clear");
        cleartree(btre);
        printf("
    ");
    }
    
  • 相关阅读:
    面试题,找出每个产品的最新五个产品,还有其它方法吗 —— 游标加表变量
    SQL排序,重名和调名
    存储过程分页
    Perl/Python 感概
    Perl解析INI文件
    Perl 多进程文件锁
    Windows Sharepoint Services 版本更新
    工作、SOA、MBF...
    Windows Sharepoint Services 版本更新
    被CDOEXM折磨了一把
  • 原文地址:https://www.cnblogs.com/wangha/p/11107796.html
Copyright © 2011-2022 走看看