42-二叉树基本运算测试代码

二叉树基本运算实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIZE 20

//二叉树结点
typedef struct BINARYNODE{
    char data;
    struct BINARYNODE* lchild;
    struct BINARYNODE* rchild;
}BinaryNode;


//1.创建二叉树CreateBTNode(*b,*str):根据二叉树括号表示法的字符串*str生成对应的链式存储结构。
void Create_BinaryNode(BinaryNode **BNode , char *str)
{
    if(BNode == NULL || str == NULL)
    {
        return;
    }

    int k;
    int i = 0;
    char ch = str[i];
    int top = -1;
    BinaryNode *temp = NULL;
    BinaryNode *st[MAXSIZE];

    while(ch != '\0')
    {
        switch(ch)
        {
            //遍历到左括号则压栈,k值为1
        case '(':
            top++;
            st[top] = temp;
            k = 1;
            break;

            //遍历到右括号则弹出栈顶元素
        case ')':
            top--;
            break;

             //遍历到逗号则设置k值为2
        case ',':
            k = 2;
            break;

        default:
            //分配新节点空间
            temp = (BinaryNode *)malloc(sizeof(BinaryNode));
            temp->data = ch;
            temp->lchild = NULL;
            temp->rchild = NULL;
            //树为空则把BNode设置为二叉树的根
            if(*BNode == NULL)
            {
                *BNode = temp;
            }
            else
            {   
                //以树(BNode)不为空则根据k值将新节点设置为左子树或右子树
                if(k == 1)
                {
                    st[top]->lchild = temp;
                }
                else if(k == 2)
                {
                    st[top]->rchild = temp;
                }
            }
        }
        i++;
        ch = str[i];
    }
}




//2. 查找节点
BinaryNode *Find_Node(BinaryNode *node , char data)
{
    //根节点为空
    if(node == NULL)
    {
        return NULL;
    }
    //说明找到了节点
    else if(node->data == data)
    {
        return node;
    }
    //按照根—左—右的顺序遍历
    else
    {
        BinaryNode *temp = Find_Node(node->lchild , data);
        //说明找到了节点
        if(temp != NULL)
        {
            return temp;
        }
        return Find_Node(node->rchild , data);
    }
}


//3.1 查找左孩子节点
BinaryNode *Find_lchild(BinaryNode *node)
{
    //空树
    if(node == NULL)
    {
        return NULL;
    }
    return node->lchild;
}

//3.2 查找右孩子节点
BinaryNode *Find_rchild(BinaryNode *node)
{
    if(node == NULL)
    {
        return NULL;
    }
    return node->rchild;
}


//4. 用括弧表示法输出二叉树
void print_node(BinaryNode *node)
{
    if(node == NULL)
    {
        return;
    }

    printf("%C" , node->data);
    if(node->lchild != NULL || node->rchild != NULL)
    {
        printf("(");
        print_node(node->lchild);
        if(node->rchild != NULL)
        {
            printf(",");
        }
        print_node(node->rchild);
        printf(")");
    }
}



//5.求树的高度
int Get_TreeDepth(BinaryNode *node)
{

    int lchild_Depth = 0;
    int rchild_Depth = 0;
    if(node == NULL)
    {
        return 0;
    }
    else
    {
        lchild_Depth = Get_TreeDepth(node->lchild);
        rchild_Depth = Get_TreeDepth(node->rchild);
        //取左右子树的最大高度 + 1
        if(lchild_Depth > rchild_Depth)
        {
            return lchild_Depth += 1;

        }else
        {
            return rchild_Depth += 1;
        }
    }
}



main.c测试文件

int main(void)
{
    char *str = "A(B(D(,G)),C(E,F))";
    BinaryNode *node1 = NULL;

    Create_BinaryNode(&node1 , str);

    printf("\n");
    print_node(node1);
    printf("\n\n");

    char data = 'D';
    BinaryNode * temp = Find_Node(node1 , data);
    printf("find D node:            %c\n" , temp->data);

    temp = Find_lchild(node1);
    printf("find A left child node:     %c\n" , temp->data);

    temp = Find_rchild(node1);
    printf("find A right child node:    %c\n" , temp->data);


    int Tree_Depth = Get_TreeDepth(node1);
    printf("Tree Depth : %d\n" , Tree_Depth);
    return 0;
}



测试结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_35733751/article/details/80942908