数据结构--二叉树

最重要的二叉树来了.

#include <stdio.h>

#define STACK_MAX_SIZE 30
#define QUEUE_MAX_SIZE 30

#ifndef elemType
    typedef char elemType;
#endif

struct BTreeNode{
    elemType data;
    struct BTreeNode *left;
    struct BTreeNode *right;
};

/* 1.初始化二叉树*/
void initBTree(struct BTreeNode **bt)
{
    *bt = NULL;
    return;    
} 

/* 2.建立二叉树(根据a所指向的二叉树广义表字符串建立)*/
void createBTree(struct BTreeNode **bt, char *str)
{
    struct BTreeNode *newP;
    struct BTreeNode *path[STACK_MAX_SIZE];     /*保存当前新节点的路径,以便返回,再在其他地方添加新节点*/ 
    int top = -1;                               /*路径位置标示*/
    int k =0;                                   /*k=1表示左子树,k=2表示右子树*/ 
    int i = 0;                                  /*遍历字符串str*/ 
    
    while(str[i] != '\0')
    {
        switch(str[i])
        {
            case ' ':
                break;
            case '(':
            case ')':
                if(top == -1) /*当前二叉树还没有节点*/
                {
                    printf("二叉树字符串表达式错误.\n");
                    system("pause");    
                }
                if(str[i] == '(')
                {
                      k=1;
                }else{
                      k=2;   
                }
                break;
            case ',':
                top--;
                break;
            default:
                if(top == STACK_MAX_SIZE -1)
                {
                    printf("栈空间被占满.\n");
                    system("pause");
                } 
                newP = malloc(sizeof(struct BTreeNode));
                if(newP == NULL)
                {
                    printf("空间申请失败!\n");
                    system("pasue");    
                } 
                
                newP->data = str[i];
                newP->left = newP->right = NULL;
                
                if(*bt == NULL) /*如果二叉树为空,newP为根节点*/ 
                {
                    *bt = newP; 
                }else{
                    if(k == 1)
                    {
                        path[top]->left = newP;
                    }else{
                        path[top]->right = newP;
                    }
                }
                path[++top] = newP;
                break; 
        }/*switch(str[i])结束*/
        i++;    
    }/*str遍历结束*/
    return;  
} 

/* 3.检查二叉树是否为空,为空返回1,否则返回0*/
int emptyBTree(struct BTreeNode *bt)
{
    if(bt == NULL)
    {
        return 1;    
    }else{
        return 0;
    } 
} 
/* 4.求二叉树深度*/
int BTreeDepth(struct BTreeNode *bt) 
{
    if(bt == NULL)
    {
        return 0;
    }else{
        int dep1 = BTreeDepth(bt->left);
        int dep2 = BTreeDepth(bt->right);
        if(dep1 > dep2)
        {   
            return dep1 + 1;
        }else{
            return dep2 + 1;
        }
    } 
} 

/* 5.从二叉树中查找值为x的结点,若存在则返回元素存储位置*/
elemType *findBTree(struct BTreeNode *bt, elemType x)
{
    if( bt == NULL)
    {
        return NULL;    
    }else{
        if(x == bt->data)
        {
            return &(bt->data);    
        }else{ 
            elemType *p;
            if((p = findBTree(bt->left , x)) != NULL) /*若p不为空标示在左子树中找到x,向上级返回p*/ 
            {
                return p;    
            }
            if((p = findBTree(bt->right, x)) != NULL)
            {
                return p;    
            }
            return NULL; 
        }
    } 
} 

/* 6.输出二叉树(前序遍历)*/
void printBTree(struct BTreeNode *bt)
{
    if(bt == NULL)
    {
        return;    
    }else{
        printf("%c",bt->data);    
        if(bt->left != NULL)
        {
            printf("(");
            printBTree(bt->left);    
        }
        if(bt->right != NULL)
        {
            printf(")");
            printBTree(bt->right);    
        }
        printf(","); 
    } 
    return; 
}
 
/* 7.清除二叉树,使之成为一颗空树*/
void clearBTree(struct BTreeNode **bt) 
{
    if(*bt != NULL)
    {
        clearBTree(&(*bt)->left);
        clearBTree(&(*bt)->right);
        free(*bt);
        *bt = NULL;    
    }
    return; 
}
 
/* 8.前序遍历*/
void preOrder(struct BTreeNode *bt)
{
    if(bt != NULL)
    {
        printf("%c ",bt->data);
        preOrder(bt->left);
        preOrder(bt->right);    
    }        
} 
/* 9.中序遍历*/
void inOrder(struct BTreeNode *bt)
{
    if(bt != NULL)
    {        
        inOrder(bt->left);
        printf("%c ",bt->data);
        inOrder(bt->right);    
    }        
}
/* 10.后序遍历*/
void postOrder(struct BTreeNode *bt)
{
    if(bt != NULL)
    {        
        postOrder(bt->left);        
        postOrder(bt->right);
        printf("%c ",bt->data);    
    }        
}

/* 11.按层遍历*/ 
void levelOrder(struct BTreeNode *bt) 
{
    struct BTreeNode *temp[QUEUE_MAX_SIZE];
    int root=0,child=0;
    
    if(bt != NULL)
    {
        temp[child++] = bt;    
    }
    while(root != child)    
    {
        if(temp[root]->left != NULL)
        {
            temp[child++] = temp[root]->left;    
        }
        if(temp[root]->right != NULL)
        {
            temp[child++] = temp[root]->right;    
        }
        printf("%c",temp[root++]);    
    } 
} 


#if 1
int main()
{
    struct BTreeNode *bt;
    char *b = "a(b(c,,)d(e(f)g,,)h()i,,,,";   /*用于存入二叉树的广义表的字符串*/ 
    elemType *temp;
     
    
    initBTree(&bt);
    printf("二叉树字符串:%s\n",b);
    createBTree(&bt,b);
    printf("创建二叉树成功!\n"); 
    
    printf("以广义表的形式输出:\n");
    printBTree(bt); 
    
    printf("\n前序:\n"); 
    preOrder(bt);
    printf("\n中序:\n"); 
    inOrder(bt);
    printf("\n后序:\n");
    postOrder(bt);
    
    temp = findBTree(bt,'e');
    if(temp)
    {
        printf("\n找到字符:%c\n",*temp);    
    }
    temp = findBTree(bt,'j');
    if(temp == NULL)
    {
        printf("\n未找到字符:j\n");    
    }
    printf("二叉树的深度为:\n");
    printf("%d\n",BTreeDepth(bt));
    
    printf("清空二叉树....\n");
    clearBTree(&bt); 
    if(emptyBTree(bt))
    {
        printf("二叉树为空.\n");    
    }
    
    system("pause"); 
    return 0;    
} 
#endif 

*************************************************************

运行结果:

*************************************************************

二叉树字符串:a(b(c,,)d(e(f)g,,)h()i,,,,
创建二叉树成功!
以广义表的形式输出:
a(b(c,,)d(e(f)g,,)h)i,,,,,
前序:
a b c d e f g h i
中序:
c b a f g e h i d
后序:
c b g f i h e d a
找到字符:e

未找到字符:j
二叉树的深度为:
5
清空二叉树....
二叉树为空.
请按任意键继续. . .

你可能感兴趣的:(数据结构)