数据结构—二叉搜索树

原理:参考趣学数据结构

代码:

队列代码:

#pragma once
#define N 100
#define elemType bstTree*
#include<stdlib.h>
typedef struct bstTree {int data;struct bstTree* lchild, *rchild;
}bstTree;
typedef struct dQueue {elemType data;struct dQueue* next;
}dQueue;
typedef struct queue {dQueue *front, *rear;
}queue;
bool initQueue(queue &Queue) {//初始化队列//Queue.front = new dQueue;Queue.front = Queue.rear = (dQueue*)malloc(sizeof(dQueue));if (!Queue.front) {return false;}Queue.front->next = NULL;//头结点return true;
}
elemType getQueueTopElem(queue &Queue) {//获取队列队头的元素elemType u=NULL ;if (Queue.front != Queue.rear) {dQueue* p = Queue.front->next;u = p->data;}return u;
}
bool enQueue(queue &Queue, elemType e) {//入队dQueue* p = Queue.rear;dQueue* s = (dQueue*)malloc(sizeof(dQueue));s->data = e;s->next = NULL;p->next = s;Queue.rear = s;return true;
}
bool deQueue(queue &Queue, elemType &e) {//出队if (Queue.front == Queue.rear) {return false;//空队}dQueue* p = Queue.front->next;e = p->data;Queue.front->next = p->next;if (p == Queue.rear) {//队尾只有一个元素的时候Queue.rear = Queue.front;}delete p;return true;
}
bool emptyQueue(queue Queue) {//队列为空的判断if (Queue.front == Queue.rear) {return true;}return false;
}

删除结点和四种(前序、中序、后序、层次)树的结点遍历:

#include<stdio.h>
#include<stdlib.h>
#include"queue.h"
void createBSTTree(bstTree* & T,int data) {//创建二叉排序树bstTree *p = NULL;if (!T) {p = (bstTree*)malloc(sizeof(bstTree));p->data = data;p->lchild = p->rchild = NULL;T = p;return;}if (data < T->data) {//左子树插入createBSTTree(T->lchild,data);}else {//右子树插入createBSTTree(T->rchild,data);}
}
void prePrint(bstTree*  BSTTree) {//前序遍历二叉排序树if (BSTTree) {printf("%d ", BSTTree->data);prePrint(BSTTree->lchild);prePrint(BSTTree->rchild);}
}
void midPrint(bstTree*  BSTTree) {//中序遍历二叉排序树if (BSTTree) {midPrint(BSTTree->lchild);printf("%d ", BSTTree->data);midPrint(BSTTree->rchild);}
}
void lastPrint(bstTree*  BSTTree) {//后序遍历二叉排序树if (BSTTree) {lastPrint(BSTTree->lchild);lastPrint(BSTTree->rchild);printf("%d ", BSTTree->data);}
}
void layerPrint(bstTree*  BSTTree) {//层次遍历二叉排序树printf("\n层次遍历二叉排序树:");queue Queue;initQueue(Queue);if (!BSTTree) {return;}enQueue(Queue, BSTTree);//入队while(!emptyQueue(Queue)) {//队列不为空时bstTree* e1 = getQueueTopElem(Queue),*e;deQueue(Queue, e);printf("%d ", e1->data);if (e1->lchild) {enQueue(Queue, e1->lchild);}if (e1->rchild) {enQueue(Queue, e1->rchild);}}printf("\n");
}
void deleteElemBSTTree(bstTree* &T, int e) {//删除元素ebstTree *f=NULL, *p=T, *q,*s;//f指向p当前指向结点的父节点if (!T) {return;}//查找元素ewhile (p) {if (p->data == e) {break;}f = p;if (p->data < e) {p = p->lchild;//左查找}else {p = p->rchild;//右查找}}if (!p) {//查找失败printf("\n没有要删除的元素%d\n", e);return;}if ((p->lchild)&&(p->rchild)) {//如果左右子树都不为空q = p->lchild;s = q;while (q->rchild) {s = q;q = q->rchild;}p->data = q->data;if (s != q) {s->rchild = q->lchild;}else {p->lchild=q->lchild;p = q;}delete q;}else {//其他情况,左孩子或右孩子为空,或者左右孩子都为空q = p;if (!p->lchild) {//左孩子为空p = p->rchild;}else if (!p->rchild) {//右孩子为空p = p->lchild;}if (!f) {//根节点的左孩子为空或者右孩子为空,或者左右孩子都为空T=p;}else if (f->lchild == q) {f->lchild = p;//f父节点的作用来了,起连接跳跃要删除结点的功能}else {f->rchild = p;}delete q;//一定要记得删除}
}
int main() {bstTree* T=NULL;//一定要初始化为空树int count,data;printf("开始构造二叉排序树:\n输入二叉排序树结点的数目:");scanf_s("%d", &count);while (count--) {//构造二叉排序树printf("输入二叉排序树的第%d个结点:",count+1);scanf_s("%d", &data);createBSTTree(T,data);}printf("前序遍历二叉排序树\n");prePrint(T);//前序遍历二叉排序树printf("\n");printf("中序遍历二叉排序树\n");midPrint(T);//中序遍历二叉排序树printf("\n");printf("后序遍历二叉排序树\n");lastPrint(T);//后序遍历二叉排序树printf("\n");layerPrint(T);//层次遍历二叉排序树printf("\n");int delElem;int counts = 2;while (counts--) {printf("删除元素:");scanf_s("%d", &delElem);deleteElemBSTTree(T, delElem);}printf("删除元素后的遍历如下:\n");printf("前序遍历二叉排序树\n");prePrint(T);//前序遍历二叉排序树printf("\n");printf("中序遍历二叉排序树\n");midPrint(T);//中序遍历二叉排序树printf("\n");printf("后序遍历二叉排序树\n");lastPrint(T);//后序遍历二叉排序树printf("\n");layerPrint(T);//层次遍历二叉排序树printf("\n");system("pause");return 0;
}

测试截图:

删除元素的时间复杂度O(logn),空间复杂度为O(1)

如果存在什么问题,欢迎批评指正!谢谢!

数据结构---二叉搜索树相关推荐

  1. 二叉搜索树的删除操作可以交换吗_JavaScript数据结构 — 二叉搜索树(BST)ES6实现...

    1. 概述 最基本的数据结构是向量和链表,为了将二者的优势结合起来,我们引入了二叉树,可以认为二叉树是列表在维度上的拓展.而今天要介绍的二叉搜索树(BST)则是在形式上借鉴了二叉树,同时也巧妙借鉴了有 ...

  2. 数据结构 二叉搜索树BST的实现与应用

    概念 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 1.若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值 ...

  3. [学习][数据结构]二叉搜索树

    定义 一棵二叉搜索树是以一棵二叉树来组织的,如下图.这样一棵树可以使用一个链表数据结构来表示,其中每个节点就是一个对象.除了key和卫星数据之外,每个节点还包含属性left.right和p,他们分别指 ...

  4. 数据结构——二叉搜索树

    一.定义 二叉搜索树(binary search tree),又叫二叉查找树.二叉排序树.若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于 ...

  5. 数据结构——二叉搜索树的C语言实现

    1.什么是二叉搜索树? 2.二叉搜索树的操作 3.二叉搜索树的C语言实现 #include<stdio.h> #include<stdlib.h>#define Element ...

  6. 数据结构 二叉搜索树的删除

    文章目录 概述 待删除的结点没有子树 待删除的结点仅有一颗子树 待删除的结点有两颗子树 C代码实现 概述 这是一篇短文,专门考究一下二叉搜索树的删除. 二叉搜索树的建立非常简单,如果不熟悉的见此文 树 ...

  7. 23王道数据结构二叉搜索树(BST)算法题(6-11题)总结(伪代码)

    6.判断给定的二叉树是否是二叉排序树 算法思想:中序遍历,一棵树为二叉排序树即左右子树为二叉排序树,且当前根节点和左右子树呈递增序列,对左右子树也是如此判断,显然是个递归过程              ...

  8. 【ACM】二叉搜索树(Binary Search Tree /BS Tree) 小结

    动态管理集合的数据结构--二叉搜索树 搜索树是一种可以进行插入,搜索,删除等操作的数据结构,可以用字典或者优先队列. 二叉排序树又称为二叉查找树,他或者为空树,或者是满足如下性质的二叉树. (1)若它 ...

  9. 【LeetCode笔记】96. 不同的二叉搜索树(Java、动态规划)

    文章目录 题目描述 代码 & 思路 精简版 2.0 题目描述 这道题其实不用构造数据结构 二叉搜索树:只要利用这个结构的性质即可,即:左右两子,左小右大 然后用动态规划来做,具体如何推导见思路 ...

最新文章

  1. Windows 11 正式官宣:全新 UI、支持安卓 App、应用商店 0 抽成!
  2. svn的merge使用例子
  3. sql like 多个值_用于数据分析的8个SQL技术
  4. 卷积神经网络初探 | 数据科学家联盟 http://dataunion.org/20942.html
  5. C++ Primer 5th笔记(8)chapter8 类:IO库-流的状态
  6. 实验 6 数组1输出最大值和它所对应的下标
  7. Android App Bundle:动态功能模块
  8. NET问答: 如何避免在 EmptyEnumerable 上执行 Max() 抛出的异常 ?
  9. [蓝桥杯][算法提高VIP]夺宝奇兵-递推+记忆化搜索
  10. (47)VHDL实现8位奇偶校验电路(for loop语句)
  11. 剑指offer22-链表中倒数第k个结点
  12. java核心面试_前100多个核心Java面试问题
  13. [010]Try块和异常处理
  14. java JSONObject JSONArray对象使用小记
  15. php中面向对象静态调用,php面向对象中static静态属性和静态方法的调用_PHP
  16. HDU 2546 饭卡 动态规划01背包
  17. Awvs 12.x安装教程
  18. html grid插件,grid.html
  19. 对于寨板X99开启AIDA64传感器必须的设置
  20. 谈谈如何做到从未来看向当代的能源技术

热门文章

  1. 判断一棵树是否为平衡二叉树
  2. git之you can‘t overwrite the remote branch问题解决
  3. python3 最基本且简单的实现组合设计模式
  4. linux用户取消密码,[Linux]linux下取消用户名和密码直接登录
  5. 如果太阳系毁灭,这种神秘粒子就是真凶!
  6. 小眼睛有多惨?美颜都懒得救你......
  7. 史上最牛空姐,从飞机上掉下愣是没摔死
  8. AlphaGo背后的力量:蒙特卡洛树搜索入门指南
  9. 机器学习的最佳学习路线原来只有四步
  10. 看完此文再不懂区块链算我输:手把手教你用Python从零开始创建区块链