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

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

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

csu1329(双向链表)

题意:给n个盒子,编号为1到n,四个操作:1、将x盒子移到y的左边;2、将x盒子移到y的右边;3、交换x和y盒子的位置;4、将所有的盒子反过来放。 思路分析:用双向链表解决。每个操作的时间复杂度为O(1),用数组来模拟链表,下面的代码是参考刘老师的标程写的。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略 1. 特权模式限制2. 宿主机资源隔离3. 用户和组管理4. 权限提升控制5. SELinux配置 💖The Begin💖点点关注,收藏不迷路💖 Kubernetes的PodSecurityPolicy(PSP)是一个关键的安全特性,它在Pod创建之前实施安全策略,确保P

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)