Summary of knowledge points of binary search tree

1. Binary search tree concept

A binary search tree is also called a binary sort tree. It is either an empty tree or a binary tree with the following properties:

  1. If its left subtree is not empty, the values ​​of all nodes on the left subtree are less than the value of the root node
  2. If its right subtree is not empty, the values ​​of all nodes on the right subtree are greater than the value of the root node
  3. Its left and right subtrees are also binary search trees

E.g:

int a [] = {
    
    5,3,4,1,7,8,2,6,0,9};

Insert picture description here

Two, binary search tree operation

2.1 Find

Insert picture description here

Node* Find(const T& data)
{
    
    
	Node* cur = root;
	while(cur)
	{
    
    
		if (data == cur->data)
			return cur;//查找key==根节点key
		else if (data < cur->data)
			cur = cur->left;//查找key<根节点key
		else
			cur = cur->right;//查找key>根节点key
	}

	return nullptr;
}

2.2 Insert

If the tree is empty, insert the tree directly and
Insert picture description here
not empty, find the insertion position according to the nature of the binary search tree, and insert a new node
Insert picture description here

bool Insert(const T& data)
{
    
    
	if (nullptr == root)
	{
    
    
		root = new Node(data);
		return true;
	}

	// BSTree非空
	// 1. 找待插入节点在树中的位置,并且需要记录其parent的位置

	Node* cur = root;
	Node* parent = nullptr;
	while (cur)
	{
    
    
		parent = cur;
		if (data == cur->data)
			return false;
	    else if (data < cur->data)
			cur = cur->left;
		else
			cur = cur->right;
	}

	//2.插入新节点
	cur = new Node(data);
	if (data < parent->data)
		parent->left = cur;
	else
		parent->right = cur;

	return true;
}

2.3 Delete

First find whether the element is in the binary search tree, if it does not exist, return, otherwise the node to be deleted may be divided into the following four situations:

  1. The node to be deleted has no children
  2. The only node to be deleted is the left child node
  3. The only node to be deleted is the right child node
  4. The node to be deleted has left and right child nodes

1 and 2, 3 can be combined, so there are only three cases:

  1. The node has only the left child and can be deleted directly, so that the parent node points to the left child node of the deleted node
  2. The node has only the right child and can be deleted directly, so that the parent node points to the right child node of the deleted node
  3. Both left and right children exist, and it is troublesome to delete. The method is to find replacement nodes: in the left subtree, find the largest (rightmost) node in the left subtree; in the right subtree, find the smallest (leftmost) node in the right subtree, as shown in the following figure :

Insert picture description here

bool Erase(const T& data)
{
    
    
	if (nullptr == root)
	{
    
    
		return false;
	}

	// BSTree非空
	// 1. 找待删除节点在BSTree中的位置,并保存其双亲
	Node* parent = nullptr;
	Node* cur = root;
	while (cur)
	{
    
    
		if (data == cur->data)
			break;
		else if (data < cur->data)
		{
    
    
			parent = cur;
			cur = cur->left;
		}
		else
		{
    
    
			parent = cur;
			cur = cur->right;
		}
	}

	// 值为data的节点不存在
	if (nullptr == cur)
	{
    
    
		return false;
	}

	// 2. 删除节点
	// cur如果只有右孩子 或者 叶子节点
	if (nullptr == cur->left)
	{
    
    
		if (nullptr == parent)
		{
    
    
			root = cur->right;
		}
		else
		{
    
    
			if (cur == parent->left)
				parent->left = cur->right;
			else
				parent->right = cur->right;
		}
	}
	else if (nullptr == cur->right)
	{
    
    
		//cur只有左孩子
		if (nullptr == parent)
		{
    
    
			root = cur->left;
		}
		else
		{
    
    
			if (cur == parent->left)
				parent->left = cur->left;
			else
				parent->right = cur->left;
		}
	}
	else
	{
    
    
		// cur左右孩子均存在
		// 找替代节点--->假设:在右子树中替代节点
		Node* del = cur->right;
		parent = cur;
		while (del->left)
		{
    
    
			parent = del;
			del = del->left;
		}
		// 将替代节点中的值域交给之前要删除的cur
		cur->data = del->data;

		//相当于变成要删除del
		if (del == parent->left)
			parent->left = del->right;
		else
			parent->right = del->right;

		cur = del;
	}

	delete cur;
	return true;
}

2.4 Verification

The characteristics of the binary search tree conform to the in-order traversal, which can be verified by the in-order traversal.

void _InOrder(Node* pRoot)
{
    
    
	if (pRoot)
	{
    
    
		_InOrder(pRoot->left);
		cout << pRoot->data << " ";
		_InOrder(pRoot->right);
	}
}

Three, the application of binary search tree

  1. K model: The K model only has the key as the key code, and only the Key needs to be stored in the structure. The key code is the value that needs to be searched.
    For example: For a word word, judge whether the word is spelled correctly, the specific method is as follows:
    use each word in the word set as the key, build a binary search tree, search for the existence of the word in the binary search tree, and then The spelling is correct, if it does not exist, the spelling is wrong.
  2. KV model: Each key has a corresponding value Value, that is, a key-value pair of <Key, Value>. This method is very common in real life: for example, the English-Chinese dictionary is the corresponding relationship between English and Chinese, and the corresponding Chinese can be quickly found through English. English words and their corresponding Chinese <word, chinese> form a key-value pair; Another example is to count the number of words. After the statistics are successful, the number of occurrences of a given word can be quickly found. The word and the number of occurrences are <word, count> to form a key-value pair.

Fourth, the performance analysis of the binary search tree

Both insert and delete operations must be searched first, and the search efficiency represents the performance of each operation in the binary search tree. For a binary search tree with n nodes, if the probability of finding each element is equal, the average search length of the binary search tree is a function of the depth of the node in the binary search tree, that is, the deeper the node, the comparison The more times. But for the same key code set, if the order of inserting the keys is different, binary search trees with different structures may be obtained.
Insert picture description here
Therefore, the search efficiency of the binary search tree is calculated according to the worst case: O(N)

Guess you like

Origin blog.csdn.net/zhao_leilei/article/details/111824991