Search -- binary sort tree

Keywords: less

Binary Sort Tree, also known as binary search tree, has the following properties: if its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node; if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node; its left and right subtrees are also binary sort trees.

1. Search operation of binary sort tree

Recursively looks up whether there is a key in the binary sort tree T. the pointer f points to the parents of T. its initial call value is NULL. If the search is successful, the pointer p points to the data element node and returns TRUE. Otherwise, the pointer p points to the last node accessed in the search path and returns FALSE.

2. Delete operation of binary sort tree

Find the node of the key element and enter the Delete function to Delete the node. Three situations of deleting the node are analyzed: leaf node; only left or right subtree node, and both left and right subtrees have nodes. In practice, the deleted node p is found recursively:

  • If the right subtree is empty, just reconnect the left subtree of the node, and delete free;
  • If the left subtree is empty, just reconnect the right subtree of the node, and delete free;
  • If the left and right subtrees are not empty, find the rightmost node s of the left child of the deleted node * p, and replace the value of the deleted node * p with the value of the rightmost node s of the left child of the deleted node * p. in the first case, the left child of the deleted node * p has the right subtree, because the value of the rightmost node s of the deleted node * p replaces the value of the deleted node * p, then s - > L replaces the s node, so Q - > R = s - > L; The second case is that the left child of deleted node * p has no right subtree, q is the node to be deleted by * p, so Q - > L = s - > L, and finally free node s

3. Summary of binary sort tree

The binary sort tree is stored in the form of link, which keeps the advantage that the link storage structure does not need to move elements when performing insertion or deletion operations. As long as the appropriate insertion and deletion positions are found, only the link pointer needs to be modified. Insert delete time performance is better. For the search of binary sort tree, the path is from the root node to the last node to be searched. The number of comparisons is equal to the number of layers of the given value nodes in the binary sort tree. Therefore, the structure of the tree has a great impact on the search efficiency, so it is necessary to balance the binary tree.

#include<iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
typedef char Elemtype;
//Definition of node structure of binary list
typedef struct Node  //Node structure
{
	Elemtype data;  //Node data
	struct Node* L;  //Left child pointer
	struct Node* R;  //Right child pointer
}Node, * BTree;
//Input the values of the nodes in the binary tree according to the previous order, ාrepresents the empty tree, and construct the binary list to represent the binary tree T
void CreateBTree(BTree* T)
{
	Elemtype e;
	cin >> e;
	if (e == '#')
		*T = NULL;
	else
	{
		(*T) = (Node*)malloc(sizeof(Node));
		(*T)->data = e;
		CreateBTree(&(*T)->L);
		CreateBTree(&(*T)->R);
	}
}
//Binary sort tree search operation
int SearchBST(BTree T, int key, BTree f, BTree* p)  //T is the root node, key is the number of lookups, f points to the parent node of T (to find the parent node for easy insertion as a child node), p is the node after successful lookups. The reason for using * p is to change the value of p by reference
{
	if (T == NULL)  //Failed to find. The returned node p points to the last leaf node f
	{
		*p = f;
		return FALSE;
	}
	else if (key == T->data)
	{
		*p = T;
		return TRUE;
	}
	else if (key < T->data)
		return SearchBST(T->L, key, T, p);  //Find in left subtree
	else
		return SearchBST(T->R, key, T, p);  //Find in right subtree
}
//Binary sort tree insertion
int InsertBST(BTree* T, int key)
{
	BTree p, s;
	if (SearchBST(*T, key, NULL, &p) == NULL)  //The reason why * t is used is that the insertion node changes the whole result of T
	{
		s = (Node*)malloc(sizeof(Node));
		s->data = key;
		s->L = s->R = NULL;
		if (p == NULL)  //T-tree is empty, create a new root node
			*T = s;
		else if (key < p->data)  //Insert s as left child
			p->L = s;
		else
			p->R = s;
		return TRUE;
	}
	else
		return FALSE;
}
//Find the node of key element and enter Delete function to Delete the node
/*
Three cases of deleting nodes are analyzed:
Leaf node:
Only left or right subtree nodes:
Both the left and right subtrees have nodes:
*/
int Delete(BTree* p)
{
	BTree q, s;
	//If the right subtree is empty, just reconnect the left subtree of the node and delete free
	if ((*p)->R == NULL)
	{
		q = *p;
		*p = (*p)->L;
		free(q);
	}
	//If the left subtree is empty, just reconnect the right subtree of the node, and delete the free node
	else if ((*p)->L == NULL)
	{
		q = *p;
		*p = (*p)->R;
		free(q);
	}
	else  //Left and right subtrees are not empty
	{
		q = *p;
		s = (*p)->L;
		while ((s->R) != NULL)  //Find the rightmost node s of the left child who deleted node * p
		{
			q = s;
			s = s->R;
		}
		(*p)->data = s->data;  //Replace the value of delete node * p with the value of the rightmost node s of the left child of delete node * p
		if (q != *p)  //In this case, the left child of the deleted node * p has a right subtree, because the value of the rightmost node s of the deleted node * p replaces the value of the deleted node * p, then s - > L replaces the s node, so Q - > R = s - > L
		{
			q->R = s->L;
		}
		else  //In this case, the left child of deleted node * p has no right subtree, and q is the node to be deleted by * p, so q - > L = s - > L
		{
			q->L = s->L;
		}
		free(s);  //Release s node
	}
	return TRUE;
}
//Delete binary sort tree
int DeleteBST(BTree* T, int key)
{
	if ((*T) == NULL)  //Element without key
		return FALSE;
	else
	{
		//Find the node of key element and enter Delete function to Delete the node
		if (key == (*T)->data)
			return Delete(T);
		else if (key < (*T)->data)  //If the key is less than (* t) - > data, search from the left subtree
			return DeleteBST(&(*T)->L, key);
		else    //If the key is greater than (* t) - > data, search from the right subtree
			return DeleteBST(&(*T)->R, key);
	}
}
int main()
{
	BTree T;
	//Input d##
	CreateBTree(&T);
	InsertBST(&T, 'b');
	cout << T->L->data << endl;
	InsertBST(&T, 'e');
	InsertBST(&T, 'f');
	InsertBST(&T, 'c');
	InsertBST(&T, 'a');
	DeleteBST(&T, 'b');
	cout << T->L->data << endl;
	return 0;
}

 

72 original articles published, 3 praised, 10000 visitors+
Private letter follow

Posted by Mardoxx on Mon, 17 Feb 2020 02:36:45 -0800