二叉树的基本操作

时间:2021-04-07 17:30:37

二叉树的基本操作

多文件组织:

main.cpp:

#include <iostream>
#include<malloc.h>
#include<cstdio>
#include"btree.h"
using namespace std;

int main()
{
    BTNode *b,*p,*lp,*rp;
    printf("创建二叉树:\n");
    CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
    printf("输出二叉树:");
    DispBTNode(b);
    printf("\n");
    printf("查找H节点:");
    p=FindNode(b,'H');
    if(p!=NULL)
    {
        lp=LchildNode(p);
        if(lp!=NULL)
            printf("左孩子为%c ",lp->data);
        else
            printf("无左孩子");
        rp=RchildNode(p);
        if(rp!=NULL)
            printf("右孩子为%c",rp->data);
        else
            printf("无右孩子");
    }
    else
        printf("未找到!");
    printf("\n");
    printf("二叉树的深度:%d",BTNodeHeight(b));
     printf("\n");
    printf("释放二叉树\n");
    DestroyBTNode(b);
    return 0;
}

btree.cpp

#include "btree.h"
#include <iostream>
#include<malloc.h>
#include<cstdio>
void CreateBTNode(BTNode *&b,char *str)
{
    BTNode *St[MaxSize],*p;
    int top=-1,k,j=0;
    char ch;
    ch=str[j];
    b=NULL;
    while(ch!='\0')
    {
        switch(ch)
        {
        case '(':
            top++;
            St[top]=p;
            k=1;
            break;
        case ')':
            top--;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            p->data=ch;
            p->lchild=p->rchild=NULL;
            if(b==NULL)
                b=p;
            else
            {
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
            }
        }
        j++;
        ch=str[j];

    }
}
BTNode *FindNode(BTNode *b,ElemType x)
{
    BTNode *p;
    if(b==NULL)
        return NULL;
    else if(b->data==x)
        return b;
    else
    {
        p=FindNode(b->lchild,x);
        if(p!=NULL)
            return p;
        else
            return FindNode(b->rchild,x);
    }
}
BTNode *LchildNode(BTNode *p)
{
    return p->lchild;
}
BTNode *RchildNode(BTNode *p)
{
    return p->rchild;
}
int BTNodeHeight(BTNode *b)
{
    int lchild,rchild;
    if(b==NULL)
        return 0;
    else
    {
        lchild=BTNodeHeight(b->lchild);
        rchild=BTNodeHeight(b->rchild);
        return (lchild>rchild)?lchild+1:rchild+1;
    }
}
void DispBTNode(BTNode *b)
{
    if(b!=NULL)
    {
        printf("%c",b->data);
        if(b->lchild!=NULL||b->rchild!=NULL)
        {
            printf("(");
            DispBTNode(b->lchild);
            if(b->rchild!=NULL)
                printf(",");
            DispBTNode(b->rchild);
            printf(")");
        }
    }
}
void DestroyBTNode(BTNode *&b)
{
    if(b==NULL)
    {
        DestroyBTNode(b->lchild);
        DestroyBTNode(b->rchild);
        free(b);
    }
}

btree.h

1.头文件:btree.h,包含定义顺序表数据结构的代码、宏定义、要实现算法的函数的声明;

#ifndef BTREE_H_INCLUDED
#define BTREE_H_INCLUDED

typedef char ElemType;
#define MaxSize 101
typedef struct node
{
    ElemType data;
    struct node *lchild;
    struct node *rchild;
} BTNode;
<pre code_snippet_id="1707984" snippet_file_name="blog_20160605_1_1425990" name="code" class="cpp">void CreateBTNode(BTNode *&b,char *str);
BTNode *FindNode(BTNode *b,ElemType x);
BTNode *LchildNode(BTNode *p);
BTNode *RchildNode(BTNode *p);
int BTNodeHeight(BTNode *b);
void DispBTNode(BTNode *b);
void DestroyBTNode(BTNode *&b);
#endif // BTREE_H_INCLUDED