其他分享
首页 > 其他分享> > 数据结构复习代码——递归实现二叉树的创建、前中后序遍历、层次遍历、求节点个数、求树高

数据结构复习代码——递归实现二叉树的创建、前中后序遍历、层次遍历、求节点个数、求树高

作者:互联网

1、递归实现二叉树的创建、前中后序遍历、层次遍历、求节点个数、求树高等操作

#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include"LinkQueue.h"
#define ElemType char

typedef struct BinTreeNode
{
    ElemType data;
    struct BinTreeNode *leftchild;
    struct BinTreeNode *rightchild;
}BinTreeNode;

typedef struct BinTree
{
    BinTreeNode *root;
    ElemType    refvalue;
}BinTree;
//初始化
void InitBinTree(BinTree *bt,ElemType ref);
//创建二叉树----引用指针方式
void CreateBinTree_1(BinTree *bt);
void CreateBinTree_1(BinTree *bt, BinTreeNode **t);
//创建二叉树----引用方式
void CreateBinTree_2(BinTree *bt);
void CreateBinTree_2(BinTree *bt, BinTreeNode *&t);
//创建二叉树----创建节点方式
void CreateBinTree_3(BinTree *bt);
BinTreeNode* CreateBinTree_3x(BinTree *bt);
//创建二叉树----非输入方式   创建方式可自行选择
void CreateBinTree_4(BinTree *bt,char *str);
void CreateBinTree_4(BinTree *bt,BinTreeNode *&t,char *&str);
//前序遍历
void preOrder(BinTree *bt);
void preOrder(BinTreeNode *t);
//中序遍历
void inOrder(BinTree *bt);
void inOrder(BinTreeNode *t);
//后续遍历
void postOrder(BinTree *bt);
void postOrder(BinTreeNode *t);
//层序遍历
void LevelOrder(BinTree *bt);
void LevelOrder(BinTreeNode *t);
//求二叉树节点个数
int Size(BinTree *bt);
int Size(BinTreeNode *t);
//求二叉树高度
int Height(BinTree *bt);
int Height(BinTreeNode *t);
//查找指定节点
BinTreeNode* Search(BinTree *bt, ElemType key);
BinTreeNode* Search(BinTreeNode *t, ElemType key);
//查找指定节点的父节点
BinTreeNode* Parent(BinTree *bt, BinTreeNode *p);
BinTreeNode* Parent(BinTreeNode *t, BinTreeNode *p);
//求指定节点的左孩子节点
BinTreeNode* LeftChild(BinTreeNode *p);

//求指定节点的右孩子节点
BinTreeNode* RightChild(BinTreeNode *p);
//判空
bool BinTreeEmpty(BinTree *bt);


void CopyBinTree(BinTree *bt1,BinTree *bt2);
void CopyBinTree(BinTreeNode *&t1,BinTreeNode *t2);
void BinTreeClear(BinTree *bt);



void InitBinTree(BinTree *bt,ElemType ref)
{
    bt->root = NULL;
    bt->refvalue = ref;
}

void CopyBinTree(BinTree *bt1,BinTree *bt2)
{
    CopyBinTree(bt1->root,bt2->root);
}
void CopyBinTree(BinTreeNode *&t1,BinTreeNode *t2)
{
    if(t2 == NULL)
        t1 = NULL;
    else{

        t1 = (BinTreeNode*)malloc(sizeof(BinTreeNode));
        assert(t1 != NULL);
        t1->data = t2->data;
        CopyBinTree(t1->leftchild,t2->leftchild);
        CopyBinTree(t1->rightchild,t2->rightchild);
    }
}

BinTreeNode* LeftChild(BinTreeNode *p)
{
    if(p != NULL)
        return p->leftchild;
    return NULL;
}

BinTreeNode* RightChild(BinTreeNode *p)
{
    if(p != NULL)
        return p->rightchild;
    return NULL;
}

bool BinTreeEmpty(BinTree *bt)
{
    return (bt->root == NULL);
}

BinTreeNode* Parent(BinTree *bt, BinTreeNode *p)
{
    return Parent(bt->root,p);
}
BinTreeNode* Parent(BinTreeNode *t, BinTreeNode *p)
{
    if(t == NULL || p == NULL)
        return NULL;
    if(t->leftchild == p || t->rightchild == p)
        return t;
    BinTreeNode *q = Parent(t->leftchild,p);
    if(q != NULL)
        return q;
    return Parent(t->rightchild,p);

}

BinTreeNode* Search(BinTree *bt, ElemType key)
{
    return Search(bt->root,key);
}
BinTreeNode* Search(BinTreeNode *t, ElemType key)
{
    if(t == NULL)
        return NULL;
    if(t->data == key)
    {
        return t;
    }
    BinTreeNode *p = Search(t->leftchild,key);
    if(p != NULL)
        return p;
    return Search(t->rightchild,key);
}

int Height(BinTree *bt)
{
    return Height(bt->root);
}
int Height(BinTreeNode *t)
{
    if(t == NULL)
        return 0;
    else{
        int left_height = Height(t->leftchild);
        int right_height = Height(t->rightchild);
        return (left_height>right_height ? left_height:right_height)+1;
    }

}
int Size(BinTree *bt)
{
    return Size(bt->root);
}
int Size(BinTreeNode *t)
{
    if(t == NULL)
        return 0;
    else{
        return Size(t->leftchild)+Size(t->rightchild)+1;
    }
}

void CreateBinTree_1(BinTree *bt)
{
    CreateBinTree_1(bt,&(bt->root));
}

//´´½¨¶þ²æÊ÷
void CreateBinTree_1(BinTree *bt, BinTreeNode **t)
{
    ElemType item;
    scanf("%c",&item);
    if(item == bt->refvalue)
        (*t) = NULL;
    else{
        (*t) = (BinTreeNode*)malloc(sizeof(BinTreeNode));
        assert((*t) != NULL);
        (*t)->data = item;
        CreateBinTree_1(bt,&((*t)->leftchild));
        CreateBinTree_1(bt,&((*t)->rightchild));
    }
}


void CreateBinTree_2(BinTree *bt)
{
    CreateBinTree_2(bt,bt->root);
}

void CreateBinTree_2(BinTree *bt, BinTreeNode *&t)
{
    ElemType item;
    scanf("%c",&item);
    if(item == bt->refvalue)
        t = NULL;
    else{
        t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
        assert(t != NULL);
        t->data = item;
        CreateBinTree_2(bt,t->leftchild);
        CreateBinTree_2(bt,t->rightchild);
    }
}


void CreateBinTree_3(BinTree *bt)
{
    bt->root = CreateBinTree_3x(bt);
}

BinTreeNode* CreateBinTree_3x(BinTree *bt)
{
    ElemType item;
    scanf("%c" , &item);
    if(item == bt->refvalue)
        return NULL;
    else
    {
        BinTreeNode *t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
        assert(t != NULL);
        t->leftchild = CreateBinTree_3x(bt);
        t->rightchild = CreateBinTree_3x(bt);
        return t;
    }
}


void CreateBinTree_4(BinTree *bt,char *str)
{
    CreateBinTree_4(bt,bt->root,str);
}
void CreateBinTree_4(BinTree *bt,BinTreeNode *&t,char *&str)
{
    if(*str == bt->refvalue)
        t = NULL;
    else
    {
        t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
        assert(t != NULL);
        t->data = *str;
        CreateBinTree_4(bt,t->leftchild,++str);
        CreateBinTree_4(bt,t->rightchild,++str);
    }
}

void preOrder(BinTree *bt)
{
    preOrder(bt->root);
}
void preOrder(BinTreeNode *t)
{
    if(t != NULL)
    {
        printf("%c ",t->data);
        preOrder(t->leftchild);
        preOrder(t->rightchild);
    }
}

void inOrder(BinTree *bt)
{
    inOrder(bt->root);
}
void inOrder(BinTreeNode *t)
{
    if(t != NULL)
    {
        inOrder(t->leftchild);
        printf("%c ",t->data);
        inOrder(t->rightchild);
    }
}

void postOrder(BinTree *bt)
{
    postOrder(bt->root);
}
void postOrder(BinTreeNode *t)
{
    if(t != NULL)
    {
        postOrder(t->leftchild);
        postOrder(t->rightchild);
        printf("%c ",t->data);
    }
}

void LevelOrder(BinTree *bt)
{
    LevelOrder(bt->root);
}
void LevelOrder(BinTreeNode *t)
{
    if(t != NULL)
    {
        BinTreeNode *v;
        LinkQueue Q;
        InitQueue(&Q);
        EnQueue(&Q,t);
        while(!QueueIsEmpty(&Q))
        {
            v = GetHead(&Q);
            DeQueue(&Q);
            printf("%c ",v->data);
            if(v->leftchild != NULL)
                EnQueue(&Q,v->leftchild);
            if(v->rightchild != NULL)
                EnQueue(&Q,v->rightchild);
        }
    }
}



int main()
{
    char *str = "ABC##DE##F##G#H##";
    BinTree mytree;
    InitBinTree(&mytree,'#');
    //CreateBinTree_3(&mytree);
    CreateBinTree_4(&mytree,str);
    preOrder(&mytree);
    printf("\n");
    inOrder(&mytree);
    printf("\n");
    postOrder(&mytree);
    printf("\n");
    LevelOrder(&mytree);
    printf("\n");
    printf("Size = %d",Size(&mytree));
    printf("\n");
    printf("Height = %d",Height(&mytree));
    printf("\n");
    BinTreeNode *p = Search(&mytree,'D');
    BinTreeNode *parent = Parent(&mytree,p);
    printf("p = %c",p->data);
    printf("\n");
    printf("parent = %c",parent->data);
    printf("\n");

    BinTree youtree;
    InitBinTree(&youtree,'#');
    CopyBinTree(&youtree,&mytree);
    preOrder(&youtree);
    printf("\n");

    return  0;
}

 

 

2、其中引用队列相关代码如下

#include<stdio.h>
#include<assert.h>
#include<malloc.h>

struct BinTreeNode;
#define EType BinTreeNode*
typedef struct QueueNode        //队列节点结构体
{
    EType data;              //节点数据域
    struct QueueNode *next;     //节点指针域
}QueueNode;

typedef struct LinkQueue{       //队列结构体
    QueueNode *front;           //队列头指针
    QueueNode *tail;            //队列尾指针
}LinkQueue;

void InitQueue(LinkQueue *Q);                   //队列初始化
void EnQueue(LinkQueue *Q,EType x);          //入队操作
void ShowQueue(LinkQueue *Q);                   //输出队列---简单遍历队列
void DeQueue(LinkQueue *Q);                     //出队操作--头结点出队
EType GetHead(LinkQueue *Q);                 //获取队列首元素
int Length(LinkQueue *Q);                       //获取当前队列长度
void ClearQueue(LinkQueue *Q);                  //逐个清除队列节点
void DestroyQueue(LinkQueue *Q);                //摧毁队列
bool QueueIsEmpty(LinkQueue *Q);                //判空


void InitQueue(LinkQueue *Q)    //队列初始化
{
    QueueNode *s = (QueueNode*)malloc(sizeof(QueueNode));       //为头结点分配内存空间
    assert(s!=NULL);
    Q->front = Q->tail = s;     //初始化头尾指针
    s->next = NULL;             //设置头结点指针
}

void EnQueue(LinkQueue *Q,EType x)       //入队操作
{
    QueueNode *s = (QueueNode*)malloc(sizeof(QueueNode));       //为新结点申请内存空间
    assert(s!=NULL);
    s->data = x;                            //数据域赋值
    s->next = NULL;                         //设置尾节点指向

    Q->tail->next = s;                      //连接当前队列尾部节点,进行入队操作
    Q->tail = s;                            //更新尾指针位置
}

void ShowQueue(LinkQueue *Q)                //输出队列---简单遍历队列
{
    QueueNode *p = Q->front->next;          //获取队列头结点
    printf("Front>");
    while(p!=NULL)                          //遍历队列、获取队列各节点数据域
    {
        printf("%d",p->data);
        p = p->next;
    }
    printf("<Tail");
    printf("\n");
}

void DeQueue(LinkQueue *Q)                  //出队操作--头结点出队
{
    if(Q->front == Q->tail)                 //判空
        return;
    QueueNode *p = Q->front->next;          //获取队列头结点
    Q->front->next = p->next;               //更新队列头结点
    free(p);                                //释放队列头结点
    if(p == Q->tail)                        //判断释放节点是否为当前队列的尾节点、更新队尾指针
        Q->tail = Q->front;
    /*
    while(p!=NULL)
    {
        Q->front->next = p->next;
        free(p);
        p = Q->front->next;
    }*/
}

EType GetHead(LinkQueue *Q)              //获取队列首元素
{
    if(Q->front == Q->tail)
        return NULL;
    QueueNode *q = Q->front->next;
    EType p = q->data;
    return p;
}

int Length(LinkQueue *Q)                    //获取当前队列长度
{
    int len = 0;
    QueueNode *p = Q->front->next;
    while(p!=NULL)                          //简单遍历队列
    {
        len++;                              //计数
        p = p->next;
    }
    return len;
}

void ClearQueue(LinkQueue *Q)               //逐个清除队列节点
{
    if(Q->front == Q->tail)
        return;
    QueueNode *p = Q->front->next;
    while(p!=NULL)
    {
        Q->front->next = p->next;
        free(p);                            //释放该节点
        p = Q->front->next;
    }
}

void DestroyQueue(LinkQueue *Q)             //摧毁队列
{
    ClearQueue(Q);
    free(Q->front);
    Q->front = Q->tail = NULL;
}

bool QueueIsEmpty(LinkQueue *Q)
{
    return (Q->front==Q->tail);
}

 

标签:遍历,return,前中,void,bt,二叉树,BinTreeNode,NULL,BinTree
来源: https://www.cnblogs.com/lx06/p/16448502.html