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;
    }
  • 相关阅读:
    关于管理单元初始化失败的解决方法
    如何快速在两台电脑之间传输大文件
    拿到商标受理通知书就可以打上“TM”就可以使用吗?
    山里王土蜂蜜
    我的博客今天1岁344天了,我领取了新锐博主徽章
    Winxp Stop c0000218 unknown hard error
    设置网易博客、新浪博客、博客园的windows live writer帐户支持
    邮件变成了Winmail.dat
    outlook 2007 .pst文件过大,提示:“磁盘空间已满,无法删除邮件”
    git 本地给远程仓库创建分支 三步法
  • 原文地址:https://www.cnblogs.com/jingmoxukong/p/3789529.html
Copyright © 2011-2022 走看看