C语言中对二叉树的操作(递归算法)

感觉递归算法看了几遍,还是半生不熟的,所以就写到这,以后用的时候也算有点参考。下面分别是头文件和函数文件,英语单词通俗易懂我就不多做注释了。

<btree.h>

#ifndef BTREE_H_INCLUDED
#define BTREE_H_INCLUDED
#define MAXSIZE 100


typedef struct node{
    char data;
    struct node *lchild;
    struct node *rchild;
}BTNode;
BTNode* creatnode(BTNode *b,char *c);//创建二叉树
BTNode* findnode(BTNode *b,char c);//递归查找字符返回所找字符的地址
BTNode* lchildnode(BTNode *b);//返回左孩子节点地址
BTNode* rchildnode(BTNode *b);//返回右孩子节点地址
int BTNodedepth(BTNode *b);//求树的深度
int countnodepoint(BTNode *b);//求二叉树的节点数
int nodewidth(BTNode *b);//二叉树宽度
void displaynode(BTNode *b);//以括号形式输出二叉树
void destroynode(BTNode *b);//销毁二叉树
void levelorder(BTNode *b);//前序遍历二叉树
void outputleafnode(BTNode *b);//Output leaf nodes AND output the number of leaf node




#endif // BTREE_H_INCLUDED

<btree.c>

#include <stdio.h>
#include <stdlib.h>
#include "btree.h"
extern int i;
BTNode* creatnode(BTNode *b,char *str)
{
    char ch;
    BTNode *st[MAXSIZE],*p;
    int top=-1,j=0,k=0;
    ch=str[j];
    b=NULL;
    while(ch!='\0')
    {
        switch (ch)
        {
        case '(':
            k=1;
            top++;
            st[top]=p;
            break;
        case ',':
            k=2;
            break;
        case ')':
            top--;
            break;
        default :
            p=(BTNode *)malloc(sizeof(BTNode));
            p->data=ch;p->rchild=p->lchild=NULL;

            if (b==NULL)
            {
                b=p;
            }
            else
            {
                switch (k)
                {
                case 1:
                    st[top]->lchild=p;
                    break;
                case 2:
                    st[top]->rchild=p;
                    break;
                }
            }
        }
        ch=str[++j];
    }
    return b;
}

BTNode* findnode(BTNode *b,char c)
{
    BTNode *p=NULL;
    if(b==NULL)
    {
        return b;
    }
    else if(b->data==c)
    {
        return b;
    }
    else
    {
        p=findnode(b->lchild,c);
        if(p!=NULL)
        {
            return p;
        }
        else
        {
            return findnode(b->rchild,c);
        }
    }
}

BTNode* lchildnode(BTNode *b)
{
    return b->lchild;
}

BTNode* rchildnode(BTNode *b)
{
    return b->rchild;
}

int BTNodedepth(BTNode *b)
{
    int lchilddep,rchilddep;
    if(b==NULL)
    {
        return 0;
    }
    else
    {
        lchilddep=BTNodedepth(b->lchild);
        rchilddep=BTNodedepth(b->rchild);
        return (lchilddep>=rchilddep)?(lchilddep+1):(rchilddep+1);
    }
}

void displaynode(BTNode* b)
{
    if(b!=NULL)
    {
        printf("%c",b->data);
        if(b->lchild!=NULL || b->rchild!=NULL)
        {
            printf("(");
            displaynode(b->lchild);
            if (b->rchild!=NULL) printf(",");
            displaynode(b->rchild);
            printf(")");
        }
    }
}

void destroynode(BTNode *b)
{
    if(b->lchild==NULL && b->rchild==NULL)
    {
        free(b);
    }
    else
    {
        destroynode(b->lchild);
        destroynode(b->rchild);
    }
}
/*销毁二叉树的递归算法二
void DestroyBTNode(BTNode *&b)   //销毁二叉树
{
    if (b!=NULL)
    {
        DestroyBTNode(b->lchild);
        DestroyBTNode(b->rchild);
        free(b);
    }
}
*/

void levelorder(BTNode *b)
{
    BTNode *p;
    BTNode *qu[MAXSIZE];
    int rear=-1,front=-1;
    rear++;
    qu[rear]=b;
    while(rear!=front)
    {
        front=(front+1)%MAXSIZE;
        p=qu[front];
        printf("%c ",p->data);
        if(p->lchild != NULL)
        {
            rear=(rear+1)%MAXSIZE;
            qu[rear]=p->lchild;
        }
        if(p->rchild != NULL )
        {
            rear=(rear+1)%MAXSIZE;
            qu[rear]=p->rchild;
        }
    }
}

int countnodepoint(BTNode *b)
{
    if(b==NULL)
    {
        return 1;
    }
    else
    {
        return (countnodepoint(b->lchild)+countnodepoint(b->rchild));
    }
}

void outputleafnode(BTNode *b)
{

    if(b->lchild!=NULL )
    {
        outputleafnode(b->lchild);
    }
    if(b->rchild!=NULL)
    {
        outputleafnode(b->rchild);
    }
    if(b->lchild ==NULL && b->rchild ==NULL)
    {
        printf("%c ",b->data);
        i++;
    }
}
int nodewidth(BTNode *b)
{
    if(b!=NULL)
    {
        if(b->lchild==b->rchild && b->lchild==NULL)
        {
            return 1;
        }
        else
        {
            return nodewidth(b->lchild) + nodewidth(b->rchild);
        }
    }
    else
        return 0;

}

    原文作者:递归算法
    原文地址: https://blog.csdn.net/qq_36278071/article/details/72593618
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞