Class notes: Search technique of tree table: Binary Sort Tree (BST)

Linear table search is a static search. To perform a dynamic search on a linear table, there are the following problems:
dynamic search on an unordered sequence table, simple insertion operation, but high search complexity.
Dynamic search on an ordered sequence table, The time complexity of the search is good, but the time complexity of the insert operation is high
. The dynamic search is performed on the singly linked list. The insert operation is simple, but the complexity of the search operation is high.
Solution: A binary structure such as a binary tree is used to dynamically search the
binary sorting tree (Binary Search Tree)
Binary sorting tree (also called binary search tree) : either an empty binary tree or a binary tree with the following properties: (
1) if its left subtree is not empty, all nodes on the left subtree Is less than the value of the 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 the root node;
⑶ its left and right subtrees are also binary sorted tree.
A mid-order traversal of a binary sorting tree can result in a sequence ordered by key.

#include <iostream> 
using namespace std; 
template<class DataType>  
struct BiNode{    
	DataType data;     
	BiNode *lchild, *rchild;  
};
class BiSortTree { 
public:     
	BiSortTree(int a[ ], int n);      
	~ BiSortTree( ){Release(root);}  
	void InOrder( ){InOrder(root);}
	BiNode *InsertBST(int x) {return InsertBST(root, x);}
	BiNode *SearchBST(int k) {return SearchBST(root, k);}
	void DeleteBST(BiNode *p, BiNode *f );
private:
	void Release(BiNode *bt);    
	BiNode *InsertBST(BiNode *bt , int x);      
	BiNode *SearchBST(BiNode *bt, int k);     
	void InOrder(BiNode *bt);
	BiNode *root;
};

Insertion of binary sorting tree
void InsertBST (BiNode * & root, BiNode * s);
Analysis: If the binary sorting tree is an empty tree, the newly inserted node is the new root node; otherwise, the newly inserted node It must be a new leaf node whose insertion position is obtained by the search process.
Insertion algorithm of binary sorting tree : If the binary sorting tree is an empty tree, the newly inserted node is the new root node; otherwise, if the inserted value is greater than the root node value, insert in the right subtree ; Otherwise, insert in the left subtree. Recursively.

BiNode *BiSortTree::InsertBST(BiNode *bt, int x) 
{  
	if (bt == NULL){
		BiNode *s = new BiNode;    
		s->data = x;   
		s->lchild = NULL;   
		s->rchild = NULL;   
		bt = s;   
		return bt;  
	}
	else if (bt->data > x)    
		bt->lchild = InsertBST(bt->lchild, x);  
	else   
		bt->rchild = InsertBST(bt->rchild, x); 
}

Construction
of a binary sorting tree Starting from an empty binary sorting tree, one by one is inserted in sequence.

BiSortTree::BiSortTree(int a[ ], int n) 
{  
	root = NULL;  
	for (int i = 0; i < n; i++)   
		root = InsertBST(root, a[i]); 
}

Deletion
of Binary Sorting Tree After deleting a node in the binary sorting tree, the characteristics of the binary sorting tree are still maintained.
Discuss in three situations:
1. The deleted node is a leaf;
2. The deleted node only has a left subtree or only a right subtree;
3. The deleted node has both a left subtree and a right subtree .

Case 1-The deleted node is a leaf node
Operation: Change the value of the corresponding pointer field in the parent node to null.
Case 2-The deleted node has only the left subtree or only the right subtree.
Operation: Point the value of the corresponding pointer field of the parent node to the left subtree (or right subtree) of the deleted node.
Case 3-The deleted node has both a left subtree and a right subtree
Operation: Replace it with its predecessor (the largest value in the left subtree), and then delete the predecessor node.
Action: Replace it with its successor (the smallest value in the right subtree), and then delete the precursor node.
Delete algorithm of binary sorting tree——Pseudocode
1. If the node p is a leaf, delete the node p directly;
2. If the node p has only the left subtree, you only need to reconnect the left subtree of p; if If the node p has only the right subtree, you only need to reconnect the right subtree of p;
3. If the left and right subtrees of node p are not empty, then
3.1 find the leftmost lower node s on the right subtree of node p And s parent node par;
3.2 Replace the data field of node s to the data field of deleted node p;
3.3 If the right child of node p has no left subtree, connect the right subtree of s to the right of par On the subtree; otherwise, connect the right subtree of s to the left subtree of the node par;
3.4 Delete the node s;

void BiSortTree::DeleteBST(BiNode<int> *p, BiNode<int> *f ) 
{   
	if (!p->lchild && !p->rchild)  
	{                  
		if(f->child==p)        
			f->lchild= NULL;                 
		else  
			f->lchild= NULL;                
		delete p;    
	}
	else if (!p->rchild) 
	{
		if(f->child==p)   
			f->lchild=p->lchild;              
		else 
			f->rchild=p->lchild;                 
		delete p;   
	}
	else if (!p->lchild) 
	{
		if(f->child==p)  
			f->lchild=p->rchild;    
		else 
			f->rchild=p->rchild;             
		delete p;          
	}
	else 
	{
		par=p;  
		s=p->rchild;                
		while (s->lchild!=NULL)       
		{                
			par=s;                
			s=s->lchild;              
		}
		p->data=s->data;              
		if (par==p) 
			p->rchild=s->rchild;              
		else 
			par->lchild=s->rchild;          
		delete s;            
	}
}         

Searching
for a binary sorting tree The process of searching for a given value k in a binary sorting tree is: (
1) If root is an empty tree, the search fails; (
2) If k = root-> data, the search is successful; otherwise
(3) If k < root-> data, search on the left subtree of root; otherwise
search on the right subtree of root.
The above process continues until k is found or the subtree to be searched is empty. If the subtree to be searched is empty, the search fails.
The search efficiency of the binary sorting tree is that only one of the two subtrees needs to be searched.

BiNode *BiSortTree::SearchBST(BiNode<int> *root, int k) 
{     
	if (root==NULL)     
		return NULLelse if (root->data==k)                
		return root;
	else if (k<root->data)                
		return SearchBST(root->lchild, k);     
	else            
		return SearchBST(root->rchild, k); 
}

Search performance analysis of
binary sorting tree The search performance of binary sorting tree depends on the shape of the binary sorting tree, which is between O (log2n) and O (n) .

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/103359099