Data structure-----Binary sorting tree

Table of contents

Preface

1.What is a binary sorting tree?

2. How to build a binary sorting tree

3. Operation of binary sorting tree

3.1 Define node storage method

3.2 Insert node operation

3.2 Create a binary sorting tree

3.4 Traversal output (in-order traversal)

3.5 Data search operation

3.6 Get the maximum and minimum values

3.7 Delete node operation

3.8 Destroy the binary sort tree

4. Complete code


Preface

        Today we continue to learn new knowledge points - sorting binary trees. Before that, we learned related sorting algorithms. We give you an array and then sort the array. Then in the same way, we can also build a binary sorting tree and sort it during the process of creating the tree. The sorting effect can also be achieved. Let’s take a look below!

1.What is a binary sorting tree?

        Binary Sort Tree, also known as Binary Search Tree, also known as Binary Search Tree . It is a type of data structure . In general, the query efficiency is higher  than the linked list structure.

Given a binary tree, if the following conditions are met, it is a binary sorted tree

  • If its left subtree is not empty, the values ​​of all nodes on the left subtree are less than the value of its root node.
  • If its right subtree is not empty, then the values ​​of all nodes on the right subtree are greater than the value of its root node.
  • Its left and right subtrees are both binary sorting trees.

The biggest advantage of a binary sort tree is its high search efficiency. Compared with searching one by one in a linked list, a binary sort tree can search according to the sorting rules of the data.

Binary sorting tree diagram:

2. How to build a binary sorting tree

For example, given an array [62,88,58,47,35,73,51,99,37,93]  , first get the first number, use this number as the root node (standard), and construct it. If If the number is larger than this, it is placed in the right subtree, and if it is smaller than this, it is placed in the left subtree, as shown in the following figure:

 Here we can see that these nodes are inserted one by one, then we can create them through recursive insertion, traverse down in order, find the appropriate position and then perform the insertion operation.

3. Operation of binary sorting tree

3.1 Define node storage method

#include<stdio.h>
#include<stdlib.h>

//二叉排序树节点存储方式
typedef int DataType;
typedef struct binarytreenode {
	DataType data;	//数据域
	struct binarytreenode* left;	//左指针 
	struct binarytreenode* right;	//右指针
}BTnode;

3.2 Insert node operation

To insert a node, you must first find the position where the node should be inserted. Starting from the following node, if it is smaller than the following node, go to the left, if it is larger, go to the right, until the position of the leaf node is inserted.

Code: 

//插入数据
void Insert_node(BTnode** root, DataType data) {
	if (*root == NULL) {
		*root = (BTnode*)malloc(sizeof(BTnode));
		if (!*root) {
			printf("ERROR\n");
			exit(-1);
		}
		(*root)->data = data;
		(*root)->left = NULL;
		(*root)->right = NULL;
	}

	else if ((*root)->data <= data)
		Insert_node(&(*root)->right, data);
	else if ((*root)->data > data)
		Insert_node(&(*root)->left, data);
}

3.2 Create a binary sorting tree

To create a binary sorted tree, you only need to insert nodes one by one and finally return the root node. code show as below:

//创建排序二叉树
BTnode* Create_sortBtree(DataType* arr, int size) {
	if (!arr)
		return NULL;
	else {
		BTnode* T = NULL;
		for (int i = 0; i < size; i++) {
			Insert_node(&T, arr[i]);
		}
		return T;
	}
}

3.4 Traversal output (in-order traversal)

//中序遍历排序二叉树
void mid_travel(BTnode* T)
{
	if (!T)
		return;
	mid_travel(T->left);
	printf("%d ", T->data);
	mid_travel(T->right);
}

3.5 Data search operation

The operation of searching for a certain value node in a binary sorted tree and then returning this node. This can be achieved through two methods: recursive and non-recursive. The code is as follows:

Recursive implementation: 

BTnode* Btree_search(BTnode* root, DataType target) {
	if (!root)
		return NULL;
	if (target == root->data) {
		return root;
	}
	return target > root->data ? Btree_search(root->right, target) : Btree_search(root->left, target);
}

 Non-recursive implementation (iterative implementation):

//非递归查找
BTnode* Btree_search_fa(BTnode* T, DataType target) {
	BTnode* p = T;
	while (p) {
		if (p->data == target)
		{
			return p;
		}
		p = target > p->data ? p->right : p->left;
	}
	return NULL;
}

3.6 Get the maximum and minimum values

To obtain the maximum or minimum value in a sorted binary tree, to put it bluntly, just find the rightmost and leftmost nodes. The two maximum values ​​of the binary sorted tree are on the two extreme sides. code show as below:

Get the maximum value

//获取最大值
int Btree_max(BTnode* T) {
	BTnode* cur = T;
	while (cur->right) {
		cur = cur->right;
	}
	return cur->data;
}

 Get the minimum value

//获取最小值
int Btree_min(BTnode* T) {
	BTnode* cur = T;
	while (cur->left) {
		cur = cur->left;
	}
	return cur->data;
}

3.7 Delete node operation

Deleting a node may damage the structure of the sorted binary tree, so it needs to be handled in several cases. 1. If you delete a leaf node, you can delete it directly, because the left and right nodes of the leaf node are empty, which will not affect the structure of the binary sort tree; 2. If the node to be deleted has only one left subtree or If there is a right subtree, we only need to find the left node or right node of the node, and then replace the node to be deleted; 3. If the node to be deleted has both left and right subtrees, here we will It is necessary to traverse to find a node that is one bit larger or one bit smaller than this node to replace this node. As shown below:

1. Delete leaf nodes 

 2. Delete nodes with only one left (right) subtree

3. Delete nodes with left and right subtrees 

Code:

//删除节点
void Btree_del(BTnode* T, DataType l) {
	if (!T) {
		printf("fuck no\n");
		return;
	}
	//找到这个要删除节点的父节点
	BTnode* p = T, * f = NULL;
	while (p) {
		if (p->data == l)
		{
			break;
		}
		f = p;
		p = l > p->data ? p->right : p->left;
	}
	if (!p)
	{
		printf("没有这个节点\n");
		return;
	}
	BTnode* target = p;//此时的要删除目标节点
	BTnode* par = f; //此时要删除节点的父节点

	//第一种情况 此节点只有一个子树的时候
	if (!target->left && target->right != NULL)
	{
		if (target->data > par->data) {
			par->right = target->right;
		}
		else {
			par->left = target->right;
		}
		free(target);//释放空间
		target = NULL;
	}
	else if (target->left != NULL && !target->right) {
		if (target->data > par->data) {
			par->right = target->left;
		}
		else {
			par->left = target->left;
		}
		free(target);
		target = NULL;
	}
	//第二种情况,如果删除的是叶节点,直接删除即可
	else if (!target->left && !target->right) {
		if (target->data > par->data) {
			par->right = NULL;
		}
		else {
			par->left = NULL;
		}
		free(target);
		target = NULL;
	}
	//第三种情况,如果左右子树都存在的话
	//可以用右子树的最小元素
	//或者左子树的最大元素来替代被删除的节点
	//我这里就直接去用左树的最大代替这个节点
	else
	{
		BTnode* Lchild = target->left;
		while (Lchild->right != NULL) {
			Lchild = Lchild->right;
		}
		if (target->data > par->data) {
			par->right = Lchild;
		}
		else {
			par->left = Lchild;
		}
		free(target);
		target = NULL;
	}
	printf("Deleting successfully\n");
}

3.8 Destroy the binary sort tree

//销毁
void Destory_btree(BTnode* T) {
	if (!T)
		return;
	BTnode* cur = T;
	if (cur->left)
		Destory_btree(cur->left);
	if (cur->right)
		Destory_btree(cur->right);
	free(T);
}

4. Complete code

#include<stdio.h>
#include<stdlib.h>

//二叉排序树节点存储方式
typedef int DataType;
typedef struct binarytreenode {
	DataType data;	//数据域
	struct binarytreenode* left;	//左指针 
	struct binarytreenode* right;	//右指针
}BTnode;


//插入数据
void Insert_node(BTnode** root, DataType data) {
	if (*root == NULL) {
		*root = (BTnode*)malloc(sizeof(BTnode));
		if (!*root) {
			printf("ERROR\n");
			exit(-1);
		}
		(*root)->data = data;
		(*root)->left = NULL;
		(*root)->right = NULL;
	}

	else if ((*root)->data <= data)
		Insert_node(&(*root)->right, data);
	else if ((*root)->data > data)
		Insert_node(&(*root)->left, data);
}


//创建排序二叉树
BTnode* Create_sortBtree(DataType* arr, int size) {
	if (!arr)
		return NULL;
	else {
		BTnode* T = NULL;
		for (int i = 0; i < size; i++) {
			Insert_node(&T, arr[i]);
		}
		return T;
	}
}

//中序遍历排序二叉树
void mid_travel(BTnode* T)
{
	if (!T)
		return;
	mid_travel(T->left);
	printf("%d ", T->data);
	mid_travel(T->right);
}

//递归查找数据
BTnode* Btree_search(BTnode* root, DataType target) {
	if (!root)
		return NULL;
	if (target == root->data) {
		return root;
	}
	return target > root->data ? Btree_search(root->right, target) : Btree_search(root->left, target);
}
//非递归查找
BTnode* Btree_search_fa(BTnode* T, DataType target) {
	BTnode* p = T, * f = NULL;
	while (p) {
		if (p->data == target)
		{
			return f;
		}
		f = p;
		p = target > p->data ? p->right : p->left;
	}
	return NULL;
}

//获取最大值
int Btree_max(BTnode* T) {
	BTnode* cur = T;
	while (cur->right) {
		cur = cur->right;
	}
	return cur->data;
}
//获取最小值
int Btree_min(BTnode* T) {
	BTnode* cur = T;
	while (cur->left) {
		cur = cur->left;
	}
	return cur->data;
}


//删除节点
void Btree_del(BTnode* T, DataType l) {
	if (!T) {
		printf("fuck no\n");
		return;
	}
	//找到这个要删除节点的父节点
	BTnode* p = T, * f = NULL;
	while (p) {
		if (p->data == l)
		{
			break;
		}
		f = p;
		p = l > p->data ? p->right : p->left;
	}
	if (!p)
	{
		printf("没有这个节点\n");
		return;
	}
	BTnode* target = p;//此时的要删除目标节点
	BTnode* par = f; //此时要删除节点的父节点

	//第一种情况 此节点只有一个子树的时候
	if (!target->left && target->right != NULL)
	{
		if (target->data > par->data) {
			par->right = target->right;
		}
		else {
			par->left = target->right;
		}
		free(target);//释放空间
		target = NULL;
	}
	else if (target->left != NULL && !target->right) {
		if (target->data > par->data) {
			par->right = target->left;
		}
		else {
			par->left = target->left;
		}
		free(target);
		target = NULL;
	}
	//第二种情况,如果删除的是叶节点,直接删除即可
	else if (!target->left && !target->right) {
		if (target->data > par->data) {
			par->right = NULL;
		}
		else {
			par->left = NULL;
		}
		free(target);
		target = NULL;
	}
	//第三种情况,如果左右子树都存在的话
	//可以用右子树的最小元素
	//或者左子树的最大元素来替代被删除的节点
	//我这里就直接去用左树的最大代替这个节点
	else
	{
		BTnode* Lchild = target->left;
		while (Lchild->right != NULL) {
			Lchild = Lchild->right;
		}
		if (target->data > par->data) {
			par->right = Lchild;
		}
		else {
			par->left = Lchild;
		}
		free(target);
		target = NULL;
	}
	printf("Deleting successfully\n");
}

//销毁
void Destory_btree(BTnode* T) {
	if (!T)
		return;
	BTnode* cur = T;
	if (cur->left)
		Destory_btree(cur->left);
	if (cur->right)
		Destory_btree(cur->right);
	free(T);
}

int main()
{
	int a[] = { 53,17,78,9,45,65,87,23 };
	//创建二叉排序树
	BTnode* T = Create_sortBtree(a, sizeof(a) / sizeof(int));
	mid_travel(T);//遍历输出
	puts("");
	//删除最大最小值
	printf("max:%d  min:%d\n", Btree_max(T), Btree_min(T));
	//查找
	BTnode* find = Btree_search(T, 23);
	printf("查找结果%d\n", find->data);

	//删除节点
	Btree_del(T, 45);
	mid_travel(T);
	puts("");
	//销毁操作
	Destory_btree(T);
}
//输出结果:
//9 17 23 45 53 65 78 87
//max:87  min : 9
//查找结果23
//Deleting successfully
//9 17 23 53 65 78 87


The above is all about the binary sorting tree. See you next time! Happy National Day to everyone!

 Share a wallpaper:

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/133443742