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

2023-12-10 20:30

本文主要是介绍《数据结构》-二叉树(三叉链表实现),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

静态数组实现二叉树

二叉链表实现二叉树

三叉链表实现二叉树

线索二叉树

三叉链表存储结构

数据结构

//二叉树的三叉链表存储表示
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);elseprintf("树空,无根\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);elseprintf("树空,无根\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;elsereturn TRUE;
}// 初始条件:二叉树T存在。
// 操作结果:返回T的深度
int BiTreeDepth(BiPTree T)
{int i, j;if (!T)return 0; // 空树深度为0if (T->lchild)i = BiTreeDepth(T->lchild); // i为左子树的深度elsei = 0;if (T->rchild)j = BiTreeDepth(T->rchild); // j为右子树的深度elsej = 0;return i > j ? i + 1 : j + 1; // T的深度为其左右子树的深度中的大者+1
}// 初始条件:二叉树T存在。
// 操作结果:返回T的根
TElemType Root(BiPTree T)
{if (T)return T->data;elsereturn Nil;
}// 初始条件:二叉树T存在,p指向T中某个结点。
// 操作结果:返回p所指结点的值
TElemType Value(BiPTree p)
{return p->data;
}// 给p所指结点赋值为value
void Assign(BiPTree p, TElemType value)
{p->data = value;
}#if 1typedef 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;elsereturn 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); // 出队,队列元素赋给aif (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或FALSEtypedef 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) -lmmain.o : tree.h
tree.o : tree.h.PHONY : clean
clean :-rm obj $(objects)

这篇关于《数据结构》-二叉树(三叉链表实现)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/478281

相关文章

Redis实现分布式锁全过程

《Redis实现分布式锁全过程》文章介绍Redis实现分布式锁的方法,包括使用SETNX和EXPIRE命令确保互斥性与防死锁,Redisson客户端提供的便捷接口,以及Redlock算法通过多节点共识... 目录Redis实现分布式锁1. 分布式锁的基本原理2. 使用 Redis 实现分布式锁2.1 获取锁

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

使用Python实现一个简易计算器的新手指南

《使用Python实现一个简易计算器的新手指南》计算器是编程入门的经典项目,它涵盖了变量、输入输出、条件判断等核心编程概念,通过这个小项目,可以快速掌握Python的基础语法,并为后续更复杂的项目打下... 目录准备工作基础概念解析分步实现计算器第一步:获取用户输入第二步:实现基本运算第三步:显示计算结果进

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

Python利用PySpark和Kafka实现流处理引擎构建指南

《Python利用PySpark和Kafka实现流处理引擎构建指南》本文将深入解剖基于Python的实时处理黄金组合:Kafka(分布式消息队列)与PySpark(分布式计算引擎)的化学反应,并构建一... 目录引言:数据洪流时代的生存法则第一章 Kafka:数据世界的中央神经系统消息引擎核心设计哲学高吞吐

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

Java调用Python脚本实现HelloWorld的示例详解

《Java调用Python脚本实现HelloWorld的示例详解》作为程序员,我们经常会遇到需要在Java项目中调用Python脚本的场景,下面我们来看看如何从基础到进阶,一步步实现Java与Pyth... 目录一、环境准备二、基础调用:使用 Runtime.exec()2.1 实现步骤2.2 代码解析三、