二叉搜索树可以按照关键字的顺序保存结点,查找和其他操作可以使用二叉搜索原理:当在树(或者寻找新的结点的地方)中查找节点是,它从根接点遍历叶结点,与每个结点的关键字进行比较,然后基于比较结果,决定在左子树或右子树中进行搜索。
每次比较将跳过树的大约一半的元素,这使得每次查找,插入或删除一个结点所花费的时间与树的结点个树的对数(树的高度)成正比。
二叉搜索树又叫二叉查找树或者二叉排序树。它支持快速的查找,插入删除一个数据。它的每个结点是一个对象,包括 key,卫星数据,还不快一些为了维护树结构所需要的信息:left、right、parent,分别指向左孩子、右孩子、父结点。如果孩子节点或者父结点不存在时,用NULL表示。
二叉搜索树是一颗二叉树,具有搜索树的特征:
二叉查找树中的查找逻辑:
先取根结点,如果它等于我们要查找的数据,就返回;
如果要查找的数据比根结点小,就在左子树中递归查找;
如果要查找的数据比根结点大,就在右子树中递归查找
二叉查找树中的插入操作:
从根结点开始,一次比较要插入的数据和结点的大小关系;
如果要插入的数据比结点数据大,并且右子树为空,就将新数据直接插到右子树的位置;如果不为空,再递归遍历右子树,查找插入的位置
二叉查找树中的删除操作:
如果要删除的结点没有子结点,只需要将被删除结点的父结点只想被删除结点的指针位置为null;
如果要删除的结点只有一个子结点(左结点或右结点),只需要更新父结点中指向要删除结点的指针,让他指向要删除结点的子结点;
如果要删除结点有两个子结点,我们需要找到这个结点的右子树中的最小结点,把它替换到要删除的结点上,然后在删除这个最小结点,因为最小结点肯定没有左子节点(如果有左子结点,就不是最小结点了)
给定一串数字,这串数字按照二叉搜索树的规则插入之后,这个二叉数的中序遍历是从小到大的排序。例如给定的数字是:8,3,10,1,6,14,4,7,13
其二叉搜索树是:

求出中序遍历是:1,3,4,6,7,8,10,13,14 。
定义一个结构体作为结点,并且初始化
- //二叉搜索树的节点结构体
- struct node {
- struct node* left;
- struct node* right;
- int data;
- }*root;
- void init(int data) {
- root = (struct node*)malloc(sizeof(struct node));
- root->data = data;
- root->left = NULL;
- root->right = NULL;
- }
只要左子树为空,就把小于父节点的数插入作为左子树
只要右子树为空,就把大于父节点的数插入作为右子树
如果不为空,就一直往下去搜索,直到找到合适的插入位置
- //插入元素
- void insert(int key) {
- struct node* temp=root;
- struct node* prev=NULL;
- //先循环遍历,找到符合要求的叶子结点
- while (temp != NULL) {
- prev = temp;
- if (key < temp->data) {
- temp = temp->left;
- }
- else if (key > temp->data) {
- temp = temp->right;
- }
- else {
- return;
- }
- }
- //根据key值与当前叶子结点的比较,决定将元素存放在右结点或者是左结点
- //当元素与当前叶子节点是重复的情况,就不用插入
- if (key < prev->data) {
- prev->left = (struct node*)malloc(sizeof(struct node));
- prev->left->data = key;
- prev->left->left = NULL;
- prev->left->right = NULL;
- }
- else if (key > prev->data) {
- prev->right = (struct node*)malloc(sizeof(struct node));
- prev->right->data = key;
- prev->right->left = NULL;
- prev->right->right = NULL;
- }
- }
- //对二叉搜索树进行中序遍历,可以得到从小到大的排序
- void show(struct node *root) {
- if (root == NULL) {
- return;
- }
- show(root->left);
- printf("%d ", root->data);
- show(root->right);
- }
- //判断该元素是否存在
- bool search(int key) {
- struct node* temp=root;
- struct node* prev;
- while (temp != NULL) {
- prev = root;
- if (key < temp->data) {
- temp = temp->left;
- }
- else if (key > temp->data) {
- temp = temp->right;
- }
- else
- return true;
- }
- return false;
- }
分为三种情况
第一种:删掉的结点没有子树
可以直接从二叉查找树中删除,不会影响其他结点
第二种:删掉的结点只有左子树
把要删除结点的左孩子,连接到要删除结点的父亲结点(父结点的左子树),然后删除D结点
第三种:删掉的结点只有右子树
将要删除结点的右孩子,连接到要删除结点D的父亲结点(父结点的右子树),然后删除D结点。
第四种:删掉的结点有左子树和右子树
删除这个结点之后,它的位置怎么替补呢?
首先是要选择和它相邻的结点(一个是左子树的“最右边”,另一个是右子树的“最左边”),可以根据中序遍历来理解。
然后用找到的这个相邻的结点来替换被删除的结点。
- //删除结点
- node* deleteNode(node* root, int key) {
- if (root == NULL) {
- return root;
- }
- if (key < root->data) {
- root->left = deleteNode(root->left, key);
- }
- else if (key > root->data) {
- root->right = deleteNode(root->right, key);
- }
- else {
- if (root->left == NULL) {
- node* temp = root->right;
- free(root);
- return temp;
- }
- else if (root->right == NULL) {
- node* temp = root->left;
- free(root);
- return temp;
- }
- node* temp = root->right;
- while (temp->left != NULL) {
- temp = temp->left;
- }
- root->data = temp->data;
- root->right = deleteNode(root->right, temp->data);
- }
- return root;
- }
总代码
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- struct node {
- struct node* left;
- struct node* right;
- int data;
- }*root;
- //初始化
- void init(int key) {
- root = (struct node*)malloc(sizeof(struct node));
- root->data = key;
- root->left = NULL;
- root->right = NULL;
- }
- //插入元素
- void insert(int key) {
- struct node* temp = root;
- struct node* prev = NULL;
- while (temp != NULL) {
- prev = temp;
- if (key < temp->data) {
- temp = temp->left;
- }
- else if (key > temp->data) {
- temp = temp->right;
- }
- else {
- return;
- }
- }
- if (key < prev->data) {
- prev->left = (struct node*)malloc(sizeof(struct node));
- prev->left->data = key;
- prev->left->left = NULL;
- prev->left->right = NULL;
- }
- else if (key > prev->data) {
- prev->right = (struct node*)malloc(sizeof(struct node));
- prev->right->data = key;
- prev->right->left = NULL;
- prev->right->right = NULL;
- }
- }
- //对二叉搜索树进行中序遍历,可以得到从小到大的排序
- void show(struct node* root) {
- if (root == NULL) {
- return;
- }
- show(root->left);
- printf("%d ", root->data);
- show(root->right);
- }
- //判断该元素是否存在
- bool search(int key) {
- struct node* temp = root;
- struct node* prev;
- while (temp != NULL) {
- prev = root;
- if (key < temp->data) {
- temp = temp->left;
- }
- else if (key > temp->data) {
- temp = temp->right;
- }
- else
- return true;
- }
- return false;
- }
- node* deleteNode(node* root, int key) {
- if (root == NULL) {
- return root;
- }
- if (key < root->data) {
- root->left = deleteNode(root->left, key);
- }
- else if (key > root->data) {
- root->right = deleteNode(root->right, key);
- }
- else {
- if (root->left == NULL) {
- node* temp = root->right;
- free(root);
- return temp;
- }
- else if (root->right == NULL) {
- node* temp = root->left;
- free(root);
- return temp;
- }
- node* temp = root->right;
- while (temp->left != NULL) {
- temp = temp->left;
- }
- root->data = temp->data;
- root->right = deleteNode(root->right, temp->data);
- }
- return root;
- }
- int main() {
- //定义一个结构体二叉搜索树
- init(8);
- //插入元素
- insert(3);
- insert(10);
- insert(1);
- insert(6);
- insert(14);
- insert(4);
- insert(7);
- insert(13);
-
- //查找是否含有元素6
- if (search(6))
- printf("存在元素——6\n");
- else {
- printf("不存在元素——6\n");
- }
- //查找是否含有元素9
- if (search(9))
- printf("存在元素——9\n");
- else {
- printf("不存在元素——9\n");
- }
-
- //二叉搜索树中的元素(中序遍历)
- printf("该二叉搜索树中的元素为:");
- show(root);
- printf("\n");
-
- //删除元素
- root=deleteNode(root, 8);
- printf("该二叉搜索树中的元素为:");
- show(root);
- printf("\n");
-
- return 0;
-
- }