C Primer Plus--高级数据结构之二叉树
C Primer Plus--高级数据结构表示之二叉树
二叉搜索树 Binary Search Tree
二叉树是一种高级数据结构。树中的每个节点都包含一个项目和两个指向其他节点的指针。
每个节点都有两个子节点:左节点、右节点。在左节点中的项目是父节点中项目的前序向,而在右节点中的项目是父节点项目的后序向。
二叉树中每一个节点本身是其后代节点的根,此节点与其后代节点构成一个子树,子树有左右之分。
用C构建二叉树ADT
首先明确二叉树结构:
二叉树或者是一个空的节点集合(空树),或者是一个指定某个节点为根的节点集合。每个节点有两个作为其后代的树,称为左子树和右子树。
每个子树本身又是一个二叉树,也包含它是个空树的可能性。
二叉搜索树是有序的二叉树,它的每个节点包含一个项目,它的所有左子树的项目排在根项目的前面,而根项目排在所有右子树项目的前面。
而且二叉树的类型操作有:
- 树初始化为空树
- 查询树是否为空
- 查询树是否已满
- 查询树中项目个数
- 向树中添加项目
- 从树中删除项目
- 从树中搜索一个项目
- 遍历树中所有项目
- 清空树
树结构的定义
假设一个项目中包含一部电影的名字,上映年份,我们定义项目为Item:定义节点Node结构,包含一部电影,节点的左子节点,节点的右子节点指针;定义结构Tree包含根节点指针、树的项目个数。
#define TITLE_MAX_CHARS 40
typedef struct movie {
char title[TITLE_MAX_CHARS];
int year;
} Item;
typedef struct node {
Item movie;
struct movie * left;
struct movie * right;
} Node;
typedef struct tree {
Node * root;
int size;
} Tree;
定义好了数据结构,下面进行树操作的定义:
//初始化树
void InitializeTree(Tree * ptoTree);
//树是空的吗?
bool TreeIsEmpty(const Tree * ptoTree);
//树满了吗?假定我们队树的最大项目树有要求
bool TreeIsFull(const Tree * ptoTree);
//查询树的项目数
bool TreeSize(const Tree * ptoTree);
//向树添加项目
bool AddMovieToTree(const Item * ptoItem,Tree * ptoTree);
//从树删除项目
bool DleteMovieFromTree(const Item * ptoItem,Tree * ptoTree);
//项目是否重复?
bool IsInTree(const Item * ptoItem, Tree * ptoTree);
//遍历树的项目
void TraverseTree(const Tree * ptoTree,void (* ptoFunc) (Item item));
完整程序如下:
binarySearchTree.h:
//
// Created by bob on 2018/11/14.
//
#ifndef LEARNINGC_BINARYSEARCHTREE_H
#define LEARNINGC_BINARYSEARCHTREE_H
#include <stdbool.h>
#define MAX_ITEMS 40
#define TITLE_MAX_CHARS 40
typedef struct movie {
char title[TITLE_MAX_CHARS];
int year;
} Item;
typedef struct node {
Item movie;
struct node * left;
struct node * right;
} Node;
typedef struct tree {
Node * root;
int size;
} Tree;
typedef struct pair {
Node * parent;
Node * child;
} Pair;
void InitializeTree(Tree * ptoTree);
bool TreeIsEmpty(const Tree * ptoTree);
bool TreeIsFull(const Tree * ptoTree);
int TreeSize(const Tree * ptoTree);
bool AddMovieToTree(const Item * ptoItem,Tree * ptoTree);
bool DleteMovieFromTree(const Item * ptoItem,Tree * ptoTree);
bool IsInTree(const Item * ptoItem, Tree * ptoTree);
void TraverseTree(const Tree * ptoTree,void (* ptoFunc) (Item item));
void ClearTree(Tree * ptoTree);
#endif //LEARNINGC_BINARYSEARCHTREE_H
binarySearchTree.c:
//
// Created by bob on 2018/11/14.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "binarySearchTree.h"
static Pair SeekItem(const Item *, const Tree *);
static bool ToLeft(const Item * p1, const Item * p2);
static bool ToRight(const Item * p1, const Item * p2);
static Node * MakeNode(const Item * ptoItem);
static bool AddNodeToTree(Node * new_node, Node * root);
static bool DeleteNode(Node ** p);
static void Traverse(const Tree * ptoTree,void (*pfunc) (Item movie));
static void InOrder(const Node * parent, void (*pfunc) (Item movie));
static void DeleteAllNodes(Node * parent);
Pair SeekItem(const Item * ptoItem, const Tree * ptoTree) {
Pair scan;
scan.parent = NULL;
scan.child = ptoTree->root;
if(scan.child == NULL)
return scan;
while (scan.child != NULL){
if(ToLeft(ptoItem,&(scan.child->movie))){
scan.parent = scan.child;
scan.child = scan.child->left;
} else if(ToRight(ptoItem,&(scan.child->movie))){
scan.parent = scan.child;
scan.child = scan.child->right;
} else{
break;
}
}
return scan;
}
bool ToLeft(const Item *p1, const Item *p2) {
int compl;
if((compl = strcmp(p1->title,p2->title)) < 0)
return true;
else if((compl = strcmp(p1->title,p2->title)) == 0 && p1->year < p2->year)
return true;
else
return false;
}
bool ToRight(const Item *p1, const Item *p2) {
int compl;
if((compl = strcmp(p1->title,p2->title)) > 0)
return true;
else if((compl = strcmp(p1->title,p2->title)) == 0 && p1->year > p2->year)
return true;
else
return false;
}
Node * MakeNode(const Item *ptoItem) {
Node * new_node;
new_node = (Node *) malloc(sizeof(Node));
if(new_node == NULL){
fprintf(stderr,"Can not allocate memory to create a node.\n");
return NULL;
}
if(ptoItem != NULL){
new_node->movie = * ptoItem;
new_node->left = NULL;
new_node->right = NULL;
}
return new_node;
}
bool AddNodeToTree(Node *new_node, Node * root) {
if(ToLeft(&new_node->movie,&root->movie)){
if(root->left == NULL)
root->left = new_node;
else
AddNodeToTree(new_node,root->left);
}else if(ToRight(&new_node->movie,&root->movie)){
if(root->right == NULL)
root->right = new_node;
else
AddNodeToTree(new_node,root->right);
}else{
fprintf(stderr,"Error in locating the inserting index of this node.\n");
exit(1);
}
return true;
}
bool DeleteNode(Node ** p) {
Node * p_temp;
puts("Deleting the movie:");
puts((*p)->movie.title);
if((*p)->left == NULL){
p_temp = *p;
*p = (*p)->right;
free(p_temp);
} else if((*p)->right == NULL){
p_temp = *p;
*p = (*p)->left;
free(p_temp);
}else{
for (p_temp = (*p)->left;p_temp->right != NULL;p_temp = p_temp->right)
continue;
p_temp->right = (*p)->right;
p_temp = *p;
*p = (*p)->left;
free(p_temp);
}
}
void Traverse(const Tree *ptoTree, void (*pfunc)(Item)) {
if(ptoTree != NULL)
InOrder(ptoTree->root,pfunc);
}
void InOrder(const Node *parent, void (*pfunc)(Item)) {
if(parent != NULL){
InOrder(parent->left,pfunc);
(*pfunc)(parent->movie);
InOrder(parent->right,pfunc);
}
}
void DeleteAllNodes(Node *parent) {
Node * ptoRight;
if(parent != NULL){
ptoRight = parent->right;
DeleteAllNodes(parent->left);
free(parent);
DeleteAllNodes(ptoRight);
}
}
void InitializeTree(Tree *ptoTree) {
ptoTree -> root = NULL;
ptoTree->size=0;
}
bool TreeIsEmpty(const Tree *ptoTree) {
if(ptoTree->root == NULL)
return 1;
else
return 0;
}
bool TreeIsFull(const Tree *ptoTree) {
if(ptoTree->size >= MAX_ITEMS)
return true;
else
return false;
}
int TreeSize(const Tree *ptoTree) {
return ptoTree->size;
}
bool AddMovieToTree(const Item * ptoItem, Tree * ptoTree) {
if(ptoItem == NULL | strlen(ptoItem->title) == 0 | ptoItem->year < 1800){
fprintf(stderr,"The movie you are adding has something wrong.");
return false;
}
if(TreeIsFull(ptoTree)){
fprintf(stderr,"The tree is full. You can not add a movie to a full tree");
return false;
}
if(SeekItem(ptoItem,ptoTree).child != NULL){
fprintf(stderr,"Trying to add duplicate movie.\n");
}
Node * new_node;
new_node = MakeNode(ptoItem);
// if(new_node == NULL){
//
// }//无需判断new_node是否为空指针,MakeNode函数里已经做过了
ptoTree->size++;
if(ptoTree->root == NULL)
ptoTree->root = new_node;
else
AddNodeToTree(new_node,ptoTree->root);
return true;
}
bool DleteMovieFromTree(const Item *ptoItem, Tree *ptoTree) {
Pair scan;
scan = SeekItem(ptoItem,ptoTree);
if(scan.child == NULL)
return false;
if(scan.parent == NULL)
DeleteNode(&ptoTree->root);
else if(scan.parent->left == scan.child)
//这里不能传scan.child,虽染这两个指向的是同一个node,但我们必须得传父节点持有的指针的指针
DeleteNode(&scan.parent->left);
else
DeleteNode(&scan.parent->right);
ptoTree->size--;
return true;
}
bool IsInTree(const Item *ptoItem, Tree *ptoTree) {
return SeekItem(ptoItem,ptoTree).child != NULL;
}
void TraverseTree(const Tree *ptoTree, void (*ptoFunc)(Item)) {
Traverse(ptoTree,ptoFunc);
}
void ClearTree(Tree *ptoTree) {
if(ptoTree == NULL)
return;
else
DeleteAllNodes(ptoTree->root);
ptoTree->root = NULL;
ptoTree->size = 0;
}
好乱,日后再改。
C Primer Plus--高级数据结构之二叉树的更多相关文章
- Python中的高级数据结构
数据结构 数据结构的概念很好理解,就是用来将数据组织在一起的结构.换句话说,数据结构是用来存储一系列关联数据的东西.在Python中有四种内建的数据结构,分别是List.Tuple.Dictionar ...
- Python中的高级数据结构详解
这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...
- Python中的高级数据结构(转)
add by zhj: Python中的高级数据结构 数据结构 数据结构的概念很好理解,就是用来将数据组织在一起的结构.换句话说,数据结构是用来存储一系列关联数据的东西.在Python中有四种内建的数 ...
- 数据结构(5) 第五天 快速排序、归并排序、堆排序、高级数据结构介绍:平衡二叉树、红黑树、B/B+树
01 上次课程回顾 希尔排序 又叫减少增量排序 increasement = increasement / 3 + 1 02 快速排序思想 思想: 分治法 + 挖坑填数 分治法: 大问题分解成各个小问 ...
- GO语言的进阶之路-Golang高级数据结构定义
GO语言的进阶之路-Golang高级数据结构定义 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们之前学习过Golang的基本数据类型,字符串和byte,以及rune也有所了解, ...
- 20181022 考试记录&高级数据结构
题目 W神爷的题解 高级数据结构 T1: 其实是一道easy题,$O(n^3log n)$ 也是能卡过去的,本着要的70分的心态,最后尽然A了. 如果是正解则是$O(n^3)$,当确定你要选择的列时, ...
- python数据结构之二叉树的统计与转换实例
python数据结构之二叉树的统计与转换实例 这篇文章主要介绍了python数据结构之二叉树的统计与转换实例,例如统计二叉树的叶子.分支节点,以及二叉树的左右两树互换等,需要的朋友可以参考下 一.获取 ...
- 高级数据结构之 BloomFilter
高级数据结构之 BloomFilter 布隆过滤器 https://en.wikipedia.org/wiki/Bloom_filter A Bloom filter is a space-effic ...
- C++数据结构之二叉树
之前打算编算法类的程序,但是搞了几次英雄会后,觉得作为一个还在学习阶段的学生,实在是太浪费时间了,并不是没意义,而是我的基础还不牢固啊.所以转变了思路,这个学期打算分别用C++.Python.Java ...
随机推荐
- 使用KONG网关实现接口迁移的灰度验证
在我们对一个API站点进行微服务化的过程中,使用KONG网关可以实现以下几个效果: 1. 业务线无感知,其实内部已经被Kong转到其他站点上执行了,这对业务线特别友好. 2. 可以实现租户级/接口级灰 ...
- js运算符及数据类型转换(二)
1.一元运算符+.-[将其它类型转化为number类型,相当于调用了Number()函数]var num = +('hello') NaN typeof num->numbernum = + ...
- CSS-盒模型与文本溢出笔记
注意点: 文本居中: text-align:center:文本左右居中 line-heigh:30px; 等于容器高度时,单行文本上下居中 margin:0 auto: 浏览器居中 清除margin ...
- Linux内核同步机制之completion
内核编程中常见的一种模式是,在当前线程之外初始化某个活动,然后等待该活动的结束.这个活动可能是,创建一个新的内核线程或者新的用户空间进程.对一个已有进程的某个请求,或者某种类型的硬件动作,等等.在这种 ...
- Android 为TV端助力之解决ViewPager嵌套RecyclerView水平滑动问题
public class MyViewPager extends ViewPager { private RecyclerView recyclerView; public MyViewPager(@ ...
- 安装nginx + nginx-gridfs + mongodb
1.安装依赖包 yum -y install pcre-devel openssl-devel zlib-devel git gcc gcc-c++ git clone https://github. ...
- MySQL数据库入门到高薪培训教程(从MySQL 5.7 到 MySQL 8.0)
一.MySQL数据库入门到高薪培训视频教程(从MySQL5.7到MySQL8.0) 本套MySQL学习教程地址: https://edu.51cto.com/course/18034.html 为满足 ...
- Java开发环境之Eclipse
查看更多Java开发环境配置,请点击<Java开发环境配置大全> 拾壹章:Eclipse安装教程 1)去官网下载安装包 http://www.eclipse.org/downloads/ ...
- Oracle数据库插入过程中特殊符号
-- 问题描述:(插入数据中有特殊符号)数据插入后乱码. -- 背景:客户提供部分Excel表格数据要求导入数据库.由于考虑到数据量不大所以粗略在Excel中进行了sql处理(在数据前后添加sql及对 ...
- php审核流程详解
在公司运营中,人员的变动及请假.离职情况都很普遍,这就需要有一个管理系统来系统的做一套流程,可以提升工作效率节省时间.在流程中需要有顺序的进行提交审核,接下来我们做一套简单的新建流程以及提交审核的系统 ...