《数据结构》-二叉树(三叉链表实现)

静态数组实现二叉树

二叉链表实现二叉树

三叉链表实现二叉树

线索二叉树

三叉链表存储结构

数据结构

//二叉树的三叉链表存储表示
typedef struct BiTPNode
{
    TElemType data;
    BiTPNode *parent, *lchild, *rchild; // 双亲、左右孩子指针
} BiTPNode, *BiPTree

存储示例

在这里插入图片描述

代码实现

main.c

/*
 * Change Logs:
 * Date           Author       Notes
 * 2021-07-22     tyustli      first version
 */

#include "tree.h"

void visitT(BiPTree T)
{
    if (T) // T非空
        printf("%d ", T->data);
}

int main(int argc, char *argv[])
{
    printf("this bitree\r\n");
    int i;
    BiPTree T, c, q;
    TElemType e1, e2;
    InitBiTree(&T);
    printf("构造空二叉树后,树空否?%d(1:是 0:否)树的深度=%d\n", BiTreeEmpty(T), BiTreeDepth(T));
    e1 = Root(T);
    if (e1)
        printf("二叉树的根为: %d \n", e1);
    else
        printf("树空,无根\n");

    printf("请按先序输入二叉树(如:1 2 0 0 0表示1为根结点,2为左子树的二叉树)\n");
    CreateBiTree(&T);
    printf("建立二叉树后,树空否?%d(1:是 0:否) 树的深度=%d\n", BiTreeEmpty(T), BiTreeDepth(T));
    e1 = Root(T);
    if (e1)
        printf("二叉树的根为: %d \n", e1);
    else
        printf("树空,无根\n");

    printf("\n先序递归遍历二叉树:\n");
    PreOrderTraverse(T, visitT);
    printf("\n中序递归遍历二叉树:\n");
    InOrderTraverse(T, visitT);
    printf("\n后序递归遍历二叉树:\n");
    PostOrderTraverse(T, visitT);
    printf("\n层序遍历二叉树:\n");
    LevelOrderTraverse(T, visitT);
}

/***************** end of file ******************/

tree.c

/*
 * Change Logs:
 * Date           Author       Notes
 * 2021-07-22     tyustli      first version
 */

#include "tree.h"

// 二叉树的三叉链表存储的基本操作

#define ClearBiTree DestroyBiTree // 清空二叉树和销毁二叉树的操作一样

TElemType Nil = 0; // 设整型以0为空

// 操作结果:构造空二叉树T
void InitBiTree(BiPTree *T)
{
    *T = NULL;
}

// 初始条件:二叉树T存在。
// 操作结果:销毁二叉树T
void DestroyBiTree(BiPTree *T)
{
    if (*T) // 非空树
    {
        if ((*T)->lchild)                 // 有左孩子
            DestroyBiTree(&(*T)->lchild); // 销毁左孩子子树
        if (&(*T)->rchild)                // 有右孩子
            DestroyBiTree(&(*T)->rchild); // 销毁右孩子子树
        free(*T);                         // 释放根结点
        *T = NULL;                        // 空指针赋0
    }
}

// 按先序次序输入二叉树中结点的值(可为字符型或整型,在主程中定义),
// 构造三叉链表表示的二叉树T
void CreateBiTree(BiPTree *T)
{
    TElemType ch;
    scanf("%d ", &ch);
    if (ch == Nil) // 空
    {
        *T = NULL;
    }
    else
    {
        *T = (BiPTree)malloc(sizeof(BiTPNode)); // 动态生成根结点
        if (!*T)
            exit(-1);
        (*T)->data = ch;               // 给根结点赋值
        (*T)->parent = NULL;           // 根结点无双亲
        CreateBiTree(&(*T)->lchild);   // 构造左子树
        if ((*T)->lchild)              // 有左孩子
            (*T)->lchild->parent = *T; // 给左孩子的双亲域赋值
        CreateBiTree(&(*T)->rchild);   // 构造右子树
        if ((*T)->rchild)              // 有右孩子
            (*T)->rchild->parent = *T; // 给右孩子的双亲域赋值
    }
}

// 初始条件:二叉树T存在。
// 操作结果:若T为空二叉树,则返回TRUE,否则FALSE
Status BiTreeEmpty(BiPTree T)
{
    if (T)
        return FALSE;
    else
        return TRUE;
}

// 初始条件:二叉树T存在。
// 操作结果:返回T的深度
int BiTreeDepth(BiPTree T)
{
    int i, j;
    if (!T)
        return 0; // 空树深度为0
    if (T->lchild)
        i = BiTreeDepth(T->lchild); // i为左子树的深度
    else
        i = 0;
    if (T->rchild)
        j = BiTreeDepth(T->rchild); // j为右子树的深度
    else
        j = 0;
    return i > j ? i + 1 : j + 1; // T的深度为其左右子树的深度中的大者+1
}

// 初始条件:二叉树T存在。
// 操作结果:返回T的根
TElemType Root(BiPTree T)
{
    if (T)
        return T->data;
    else
        return Nil;
}

// 初始条件:二叉树T存在,p指向T中某个结点。
// 操作结果:返回p所指结点的值
TElemType Value(BiPTree p)
{
    return p->data;
}

// 给p所指结点赋值为value
void Assign(BiPTree p, TElemType value)
{
    p->data = value;
}

#if 1

typedef BiPTree QElemType; // 设队列元素为二叉树的指针类型

typedef struct QNode
{
    QElemType data;     /* 数据域 */
    struct QNode *next; /* 指针域 */
} QNode, *QueuePtr;

typedef struct LinkQueue
{
    QueuePtr front; // 队头指针
    QueuePtr rear;  // 队尾指针
} LinkQueue;

void InitQueue(LinkQueue *Q);
void DestroyQueue(LinkQueue *Q);
void ClearQueue(LinkQueue *Q);
Status QueueEmpty(LinkQueue Q);
int QueueLength(LinkQueue Q);
Status GetHead(LinkQueue Q, QElemType *e);
void EnQueue(LinkQueue *Q, QElemType e);
Status DeQueue(LinkQueue *Q, QElemType *e);
void QueueTraverse(LinkQueue Q, void (*vi)(QElemType));
void print(QElemType i);
void InitQueue(LinkQueue *Q)
{
    Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));

    if (Q->front == NULL)
        exit(-1);
    Q->front->next = NULL;
}

void DestroyQueue(LinkQueue *Q)
{
    while (Q->front)
    {
        Q->rear = Q->front->next;
        free(Q->front);
        Q->front = Q->rear;
    }
}

void ClearQueue(LinkQueue *Q)
{
    QueuePtr p, q;
    Q->rear = Q->front;
    p = Q->front->next;
    Q->front->next = NULL;
    while (p)
    {
        q = p;
        p = p->next;
        free(q);
    }
}
Status QueueEmpty(LinkQueue Q)
{
    if (Q.front->next == NULL)
        return TRUE;
    else
        return FALSE;
}
int QueueLength(LinkQueue Q)
{
    int i = 0;
    QueuePtr p;
    p = Q.front;
    while (Q.rear != p)
    {
        i++;
        p = p->next;
    }
    return i;
}
Status GetHead(LinkQueue Q, QElemType *e)
{
    QueuePtr p;
    if (Q.front == Q.rear)
        return ERROR;
    p = Q.front->next;
    *e = p->data;

    return OK;
}
void EnQueue(LinkQueue *Q, QElemType e)
{
    QueuePtr p;
    if (!(p = (QueuePtr)malloc(sizeof(QNode))))
        exit(-1);
    p->data = e;
    p->next = NULL;
    Q->rear->next = p;
    Q->rear = p;
}
Status DeQueue(LinkQueue *Q, QElemType *e)
{
    QueuePtr p;
    if (Q->front == Q->rear)
        return ERROR;
    p = Q->front->next;
    *e = p->data;
    Q->front->next = p->next;
    if (Q->rear == p)
        Q->rear = Q->front;
    free(p);

    return OK;
}
void QueueTraverse(LinkQueue Q, void (*vi)(QElemType))
{
    QueuePtr p;
    p = Q.front->next;
    while (p)
    {
        vi(p->data);
        p = p->next;
    }
    printf("\n");
}
void print(QElemType i)
{
    // printf("%s ", i);
}
#endif

// typedef BiPTree QElemType; // 设队列元素为二叉树的指针类型

// 返回二叉树T中指向元素值为e的结点的指针
BiPTree Point(BiPTree T, TElemType e)
{
    LinkQueue q;
    QElemType a;
    if (T) // 非空树
    {
        InitQueue(&q);         // 初始化队列
        EnQueue(&q, T);        // 根结点入队
        while (!QueueEmpty(q)) // 队不空
        {
            DeQueue(&q, &a); // 出队,队列元素赋给a
            if (a->data == e)
                return a;
            if (a->lchild)              // 有左孩子
                EnQueue(&q, a->lchild); // 入队左孩子
            if (a->rchild)              // 有右孩子
                EnQueue(&q, a->rchild); // 入队右孩子
        }
    }
    return NULL;
}

// 初始条件:二叉树T存在,e是T中某个结点
// 操作结果:若e是T的非根结点,则返回它的双亲,否则返回"空"
TElemType Parent(BiPTree T, TElemType e)
{
    BiPTree a;
    if (T) // 非空树
    {
        a = Point(T, e);            // a是结点e的指针
        if (a && a != T)            // T中存在结点e且e是非根结点
            return a->parent->data; // 返回e的双亲的值
    }
    return Nil; // 其余情况返回空
}

// 初始条件:二叉树T存在,e是T中某个结点。
// 操作结果:返回e的左孩子。若e无左孩子,则返回"空"
TElemType LeftChild(BiPTree T, TElemType e)
{
    BiPTree a;
    if (T) // 非空树
    {
        a = Point(T, e);            // a是结点e的指针
        if (a && a->lchild)         // T中存在结点e且e存在左孩子
            return a->lchild->data; // 返回e的左孩子的值
    }
    return Nil; // 其余情况返回空
}

// 初始条件:二叉树T存在,e是T中某个结点。
// 操作结果:返回e的右孩子。若e无右孩子,则返回"空"
TElemType RightChild(BiPTree T, TElemType e)
{
    BiPTree a;
    if (T) // 非空树
    {
        a = Point(T, e);            // a是结点e的指针
        if (a && a->rchild)         // T中存在结点e且e存在右孩子
            return a->rchild->data; // 返回e的右孩子的值
    }
    return Nil; // 其余情况返回空
}

// 初始条件:二叉树T存在,e是T中某个结点
// 操作结果:返回e的左兄弟。若e是T的左孩子或无左兄弟,则返回"空"
TElemType LeftSibling(BiPTree T, TElemType e)
{
    BiPTree a;
    if (T) // 非空树
    {
        a = Point(T, e);                                                // a是结点e的指针
        if (a && a != T && a->parent->lchild && a->parent->lchild != a) // T中存在结点e且e存在左兄弟
            return a->parent->lchild->data;                             // 返回e的左兄弟的值
    }
    return Nil; // 其余情况返回空
}

// 初始条件:二叉树T存在,e是T中某个结点
// 操作结果:返回e的右兄弟。若e是T的右孩子或无右兄弟,则返回"空"
TElemType RightSibling(BiPTree T, TElemType e)
{
    BiPTree a;
    if (T) // 非空树
    {
        a = Point(T, e);                                                // a是结点e的指针
        if (a && a != T && a->parent->rchild && a->parent->rchild != a) // T中存在结点e且e存在右兄弟
            return a->parent->rchild->data;                             // 返回e的右兄弟的值
    }
    return Nil; // 其余情况返回空
}

// 初始条件:二叉树T存在,p指向T中某个结点,LR为0或1,非空二叉树c与T不相交且右子树为空
// 操作结果:根据LR为0或1,插入c为T中p所指结点的左或右子树。p所指结点
Status InsertChild(BiPTree p, int LR, BiPTree c) // 形参T无用
{
    //           的原有左或右子树则成为c的右子树
    if (p) // p不空
    {
        if (LR == 0)
        {
            c->rchild = p->lchild;
            if (c->rchild) // c有右孩子(p原有左孩子)
                c->rchild->parent = c;
            p->lchild = c;
            c->parent = p;
        }
        else // LR==1
        {
            c->rchild = p->rchild;
            if (c->rchild) // c有右孩子(p原有右孩子)
                c->rchild->parent = c;
            p->rchild = c;
            c->parent = p;
        }
        return OK;
    }
    return ERROR; // p空
}

// 初始条件:二叉树T存在,p指向T中某个结点,LR为0或1
// 操作结果:根据LR为0或1,删除T中p所指结点的左或右子树
Status DeleteChild(BiPTree p, int LR) // 形参T无用
{
    if (p) // p不空
    {
        if (LR == 0) // 删除左子树
            ClearBiTree(&p->lchild);
        else // 删除右子树
            ClearBiTree(&p->rchild);
        return OK;
    }
    return ERROR; // p空
}

// 先序递归遍历二叉树T
void PreOrderTraverse(BiPTree T, void (*Visit)(BiPTree))
{
    if (T)
    {
        Visit(T);                           // 先访问根结点
        PreOrderTraverse(T->lchild, Visit); // 再先序遍历左子树
        PreOrderTraverse(T->rchild, Visit); // 最后先序遍历右子树
    }
}

// 中序递归遍历二叉树T
void InOrderTraverse(BiPTree T, void (*Visit)(BiPTree))
{
    if (T)
    {
        InOrderTraverse(T->lchild, Visit); // 中序遍历左子树
        Visit(T);                          // 再访问根结点
        InOrderTraverse(T->rchild, Visit); // 最后中序遍历右子树
    }
}

// 后序递归遍历二叉树T
void PostOrderTraverse(BiPTree T, void (*Visit)(BiPTree))
{
    if (T)
    {
        PostOrderTraverse(T->lchild, Visit); // 后序遍历左子树
        PostOrderTraverse(T->rchild, Visit); // 后序遍历右子树
        Visit(T);                            // 最后访问根结点
    }
}

// 层序遍历二叉树T(利用队列)
void LevelOrderTraverse(BiPTree T, void (*Visit)(BiPTree))
{
    LinkQueue q;
    QElemType a;
    if (T)
    {
        InitQueue(&q);
        EnQueue(&q, T);
        while (!QueueEmpty(q))
        {
            DeQueue(&q, &a);
            Visit(a);
            if (a->lchild != NULL)
                EnQueue(&q, a->lchild);
            if (a->rchild != NULL)
                EnQueue(&q, a->rchild);
        }
    }
}

/***************** end of file ******************/

tree.h

/*
 * Change Logs:
 * Date           Author       Notes
 * 2021-07-22     tyustli      first version
 */

#include <string.h>
#include <ctype.h>
#include <malloc.h> // malloc()等
#include <limits.h> // INT_MAX等
#include <stdio.h>  // EOF(=^Z或F6),NULL
#include <stdlib.h> // atoi()
#include <math.h>   // floor(),ceil(),abs()

// 函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
// #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行
typedef int Status;  // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int Boolean; // Boolean是布尔类型,其值是TRUE或FALSE

typedef int TElemType;

//二叉树的三叉链表存储表示
typedef struct BiTPNode
{
    TElemType data;
    struct BiTPNode *parent, *lchild, *rchild; // 双亲、左右孩子指针
} BiTPNode, *BiPTree;

void InitBiTree(BiPTree *T);
void CreateBiTree(BiPTree *T);
void DestroyBiTree(BiPTree *T);
void ClearBiTree(BiPTree *T);
Status BiTreeEmpty(BiPTree T);
int BiTreeDepth(BiPTree T);
TElemType Root(BiPTree T);
TElemType Value(BiPTree p);
void Assign(BiPTree p, TElemType value);
BiPTree Point(BiPTree T, TElemType e);
TElemType Parent(BiPTree T, TElemType e);
TElemType LeftChild(BiPTree T, TElemType e);
TElemType RightChild(BiPTree T, TElemType e);
TElemType LeftSibling(BiPTree T, TElemType e);
TElemType RightSibling(BiPTree T, TElemType e);
Status InsertChild(BiPTree p, int LR, BiPTree c);
Status DeleteChild(BiPTree p, int LR);
void PreOrderTraverse(BiPTree T, void (*Visit)(BiPTree));
void InOrderTraverse(BiPTree T, void (*Visit)(BiPTree));
void PostOrderTraverse(BiPTree T, void (*Visit)(BiPTree));
void LevelOrderTraverse(BiPTree T, void (*Visit)(BiPTree));

/***************** end of file ******************/

makefile

objects  = main.o tree.o
obj: $(objects)
	cc -o obj $(objects) -lm

main.o : tree.h
tree.o : tree.h

.PHONY : clean
clean :
	-rm obj $(objects)

  • 6
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构源码C语言描述续,本篇描述了二叉树三叉链表结构及其操作,以及测试程序: //创建二叉树结点 TriTreeNode *CreateTriTreeNode(char data); //给二叉树添加结点,用于创建二叉树 int AddTriTreeNode(char data, TriTreeNode *newTriNode); //创建二叉树 TriTreeNode *CreateTriTree(); //计算二叉树的高度 int GetTriTreeDepth(TriTreeNode *triTree); //插入结点(连接两棵二叉树),这个结点(二叉树)和root不相交; //newTriNode可以是一个结点也可以是一棵二叉树 int InsertChildTriNode(TriTreeNode *newTriNode, TriTreeNode *root); //获取根结点 TriTreeNode *GetTriTreeRoot(TriTreeNode *triTree); //判断二叉树是否为空 int IsTriTreeEmpty(TriTreeNode *triTree); //获取二叉树中某一个结点的左孩子结点 void GetLeftChildFromTriTree(TriTreeNode *triTree, TriTreeNode *triNode, TriTreeNode *lChild); //获取二叉树中某一个结点的右孩子结点 void GetRightChildFromTriTree(TriTreeNode *triTree, TriTreeNode *triNode, TriTreeNode *rChild); //获取二叉树某一个指定结点父节点 int GetTriTreeParent(TriTreeNode *root, TriTreeNode *triNode, TriTreeNode *parentNode); //删除二叉树,某一个指定结点的左或右子树 void DeleteChildFromTriTree(TriTreeNode *root, TriTreeNode *triNode, int flag); //销毁二叉树 void DestroyTriTree(TriTreeNode *TriTree); //先序遍历(DRL), 先序遍历按照既定算法遍历出来将是一个无序列表 void PreOrderTraversTriTreeDRL(TriTreeNode *TriTree); //先序遍历(DLR), 先序遍历按照既定算法遍历出来将是一个无序列表 void PreOrderTraversTriTreeDLR(TriTreeNode *TriTree); //中序遍历(LDR),遍历结果应该是一个从小到大的有序排列 void InOrderTraversTriTreeLDR(TriTreeNode *TriTree); //中序遍历(RDL),遍历结果是一个从大到小的有序排列 void InOrderTraversTriTreeRDL(TriTreeNode *triTree); //后续遍历(LRD) void PostOrderTraversTriTreeLRD(TriTreeNode *TriTree); //后续遍历(RLD), void PostOrderTraversTriTreeRLD(TriTreeNode *TriTree);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值