zoukankan      html  css  js  c++  java
  • 二叉树

    二叉树基本操作代码

    #include "stdafx.h"
    #include "stdlib.h"
    #include "string.h"
    
    #define MAX 100
    typedef char Elemtype;
    
    typedef struct BTNODE
    {
        Elemtype data;
        BTNODE *left;
        BTNODE *right;
    } BTNode;
    
    void CreateBTNode(BTNode *&root, char *str)
    {
        BTNode *p = NULL;
        BTNode *st[MAX] = {NULL};
        int top = -1;
        int i = 0;
        int k = 0;
        char ch = str[i];
    
        while ('' != ch)
        {
            switch (ch)
            {
            case '(':
                {
                    top++;
                    st[top] = p;
                    k = 1;
                    break;
                }
            case ')':
                {
                    top--;
                    break;
                }
            case ',':
                {
                    k = 2;
                    break;
                }
            default:
                {
                    p = (BTNode*)malloc(sizeof(BTNode));
                    if (NULL == p)
                    {
                        return;
                    }
                    p->data = ch;
                    p->left = p->right = NULL;
    
                    if (!root)
                    {
                        root = p;
                    } 
                    else
                    {
                        switch (k)
                        {
                        case 1:
                            st[top]->left = p;
                            break;
                        case 2:
                            st[top]->right = p;
                            break;
                        }
                    }
                    break;
                }
            }
            ch = str[++i];
        }
    }
    
    void DispBTNode(BTNode *&root)
    {
        if (root)
        {
            printf("%c", root->data);
            if (root->left || root->right)
            {
                printf("(");
                DispBTNode(root->left);
                if (root->right)
                {
                    printf(",");
                    DispBTNode(root->right);
                }
                printf(")");
            }
        }
    }
    
    int GetBTNodeDepth(BTNode *&root)
    {
        int iLeftDepth  = 0;
        int iRightDepth = 0;
    
        if (!root)
        {
            return 0;
        }
    
        iLeftDepth  = GetBTNodeDepth(root->left);
        iRightDepth = GetBTNodeDepth(root->right);
        return (iLeftDepth > iRightDepth ? (iLeftDepth+1):(iRightDepth+1));
    }
    
    void PreOrder(BTNode *&root)
    {
        if (root)
        {
            printf("%c	", root->data);
            PreOrder(root->left);
            PreOrder(root->right);
        }
    }
    
    void PreOrder1(BTNode *&root)
    {
        int top = -1;
        BTNode *p = NULL;
        BTNode *st[MAX] = {NULL};
    
        if (root)
        {
            top++;
            st[top] = root;
    
            while (top > -1)
            {
                p = st[top];
                top--;
                printf("%c	", p->data);
                if (p->right)
                {
                    top++;
                    st[top] = p->right;
                }
                if (p->left)
                {
                    top++;
                    st[top] = p->left;
                }
            }
        }
    }
    
    void InOrder(BTNode *&root)
    {
        if (root)
        {        
            PreOrder(root->left);
            printf("%c	", root->data);
            PreOrder(root->right);
        }
    }
    
    void PostOrder(BTNode *&root)
    {
        if (root)
        {        
            PreOrder(root->left);        
            PreOrder(root->right);
            printf("%c	", root->data);
        }
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        BTNode *root = NULL;
        char *str = "A(B(D(,G)),C(E,F))";
        CreateBTNode(root, str);
        DispBTNode(root);
        printf("
    ");
        printf("The BTree's Depth = %d
    ", GetBTNodeDepth(root));
    
        printf("PreOrder:
    ");
        PreOrder(root);
        printf("
    ");
    
        printf("InOrder:
    ");
        InOrder(root);
        printf("
    ");
    
        printf("PostOrder:
    ");
        PostOrder(root);
        printf("
    ");
        return 0;
    }
  • 相关阅读:
    Quick Find
    并查集
    树形问题和更多树
    二叉搜索树的局限性
    Oracle Auto Increment Column
    测试机器性能
    EXP/IMP version
    python getaddrinfo 函数
    open cursor too much error
    要看的一些链接
  • 原文地址:https://www.cnblogs.com/jingmoxukong/p/3789529.html
Copyright © 2011-2022 走看看