The concept and common operation of binary search tree

Keywords: less

What is a binary search tree?

We divide the general search operations into two categories: static search and dynamic search. There is a fast searching method in static searching (the time complexity of binary searching is O (log (n)). The reason why it can reduce the time complexity is that it sorts the data in order before searching. When searching, the order of searching is fixed, and it is a decision tree like structure. A linear search process is transformed into a tree like search process. Search efficiency is the height of the tree. From then on, we get the Enlightenment: why can't we store the data directly in the structure of binary tree when searching. Compared with the linear structure tree, it is more dynamic and convenient to insert and delete. In this way, a binary search tree is formed.
Binary search tree definition:
A binary tree can be empty. If it is not empty, it satisfies the following properties:
(1) The values of all nodes in the non empty left subtree are smaller than the values of the root node.
(2) The values of all nodes in a non empty right subtree are greater than the values of the root nodes.
(3) The left and right subtrees are binary search trees.
In this way, when searching, you can find from the root node, if it is smaller than the left subtree, if it is larger than the right subtree.

Common operations of binary search tree:

//Find the element data from the binary search tree and return the address of the node. If not, return NULL;
Binary_Tree* Find(Binary_Tree* BST,Elementype data)//From the BST in the search binary tree, find the minimum value data and return the address of the node (null if the tree is empty)
Binary_Tree* Find_Min(Binary_Tree* BST)//From the BST in the search binary tree, find the maximum value data and return the address of the node (null if the tree is empty)
Binary_Tree* Find_Max(Binary_Tree* BST)
//Insert a node from a binary search tree
Binary_Tree* Insert( Binary_Tree* BST, Elementype data )//Delete a node from the binary search tree
Binary_Tree* Delete( Binary_Tree* BST, Elementype data )

Specific operation:

#include<stdio.h>
#include<stdlib.h>
#define Elementype char
typedef struct Binary_Tree{
	Elementype data;
	struct Binary_Tree* L_child;
	struct Binary_Tree* R_child;
}Binary_Tree;
//Find the element data from the binary search tree and return the address of the node. If not, return NULL;
Binary_Tree* Find(Binary_Tree* BST,Elementype data){
	if(BST == NULL){//Return NULL not found
		return NULL;
	}else if(BST ->data == data){
		return BST;
	}else if(data < BST ->data){
		Find(BST ->L_child,data);
	}else if(data > BST ->data){
		Find(BST ->R_child,data);
	}
}
//From the BST in the search binary tree, find the minimum value data and return the address of the node (null if the tree is empty)
Binary_Tree* Find_Min(Binary_Tree* BST){
	if(BST == NULL){//The tree is empty.
		return NULL;
	}else if(BST ->L_child){
		Find_Min(BST ->L_child);
	}else{
		return BST;
	}
}
//From the BST in the search binary tree, find the maximum value data and return the address of the node (null if the tree is empty)
Binary_Tree* Find_Max(Binary_Tree* BST){
	if(BST == NULL){//The tree is empty.
		return NULL;
	}else if(BST ->R_child){
		Find_Max(BST ->R_child);
	}else{
		return BST;
	}
}
Binary_Tree* Insert( Binary_Tree* BST, Elementype data ){//Insert a node from a binary search tree
	if(BST){//Trees are not empty.
		if(data < BST ->data){//The value to insert is less than the value of the node
			BST ->L_child = Insert(BST ->L_child,data);
		}else if(data > BST ->data){//The value to insert is greater than the value of the node
			BST ->R_child = Insert(BST ->R_child ,data);
		}
	}else{//The tree is empty.
		BST = (Binary_Tree*)malloc(sizeof(Binary_Tree));
		BST ->data = data;
		BST ->L_child = BST ->R_child = NULL;
	}
	return BST;
}
Binary_Tree* Delete( Binary_Tree* BST, Elementype data ){//Delete a node in binary search tree
	Binary_Tree* tem = NULL;
	if(BST == NULL){
		printf("Deleted node is not in tree");
	}else{
		if(data < BST ->data){
			BST ->L_child = Delete(BST ->L_child,data);
		}else if(data > BST ->data){
			BST ->R_child = Delete(BST ->R_child,data);
		}else{
			if(BST ->L_child && BST ->R_child){//Both left and right subtrees exist
				tem = Find_Max(BST ->R_child);//Right subtree finds the address of the maximum node
				BST ->data = tem ->data;
				Delete(BST ->R_child,tem ->data);//Delete the maximum node of the right subtree;
			}else{
				if(BST ->L_child){
					tem = BST ->L_child;
				}else{
					tem = BST ->R_child;
				}
				free(BST);
				BST = tem;
			}
		}
	}
	return BST;
}
Published 12 original articles, won praise 2, visited 248
Private letter follow

Posted by nanobots on Sat, 15 Feb 2020 23:21:03 -0800