Data Structures and Algorithms: Tree Search

1. Binary Sorting Tree (BST)

1. Personal understanding

  • Left subtree node value < root node value < right subtree node value
  • Inorder traversal of a binary sort tree yields an increasing ordered sequence

2. Binary tree search

principle:

For a given binary sort tree, if you want to find a node, you can follow the steps below:

  1. Start the comparison from the root node.
  2. If the value you are looking for is equal to the value of the current node, then you have found the target node and return that node.
  3. If the value to be found is less than the value of the current node, continue searching in the left subtree.
  4. If the value to be found is greater than the value of the current node, continue searching in the right subtree.
  5. If the target node is not found in the entire tree, a null pointer is returned or an exception is thrown indicating that it was not found.

The time complexity is O(log n), where n is the number of nodes in the tree, since each lookup reduces the search by half.

3. Search algorithm implementation

//在二叉排序树中查找值为key的结点
BSTNode *BST_ _Search(BSTree T,int key){
    
    
	while(T!=NULL&&key!=T->key){
    
     
	//若树空或等 于根结点值,则结束循环
	if(key<T->key) 
		T=T->lchild;                  //小于,则在左子树上查找
	else 
		T=T->rchild;                  //大于,则在右子树上查找
	}
	return T;
}

This process is obviously a recursive process, and recursion can be used to complete the search. Recursion is not used here. For the recursive algorithm, please refer to [Recursive Algorithm ]

4. Binary sort tree insertion

principle:

If the original binary sorting tree is empty, insert the node directly; otherwise, if the key k is less than the value of the root node, it will be inserted into the left subtree; if the key k is greater than the value of the root node, it will be inserted into the right subtree.

Algorithm implementation:

//在二叉排序树插入关键字为k的新结点(递归实现)
int BST_ Insert(BSTree &T, int k){
    
    
	if(T==NULL){
    
                            //原树为空, 新插入的结点为根结点
		T=(BSTree)malloc(sizeof(BSTNode));
		T->key=k;
		T->lchild=T->rchild=NULL
		return 1;                      //返回1,插入成功
	}
	else if(k==T->key)                  //树中存在相同关键字的结点,插入失败
		return 0;
	else if(k<T->key)                   //插入到T的左子树
	return BST_ Insert(T->lchild,k);
else                                    //插入到T的右子树
	return BST_ Insert(T->rchild,k);
}

5. Binary sort tree deletion

The deletion operation of the binary sorting tree needs to be divided into the following steps:

  1. Find the node to be deleted: start from the root node, compare the key of the node to be deleted with the key of the current node, if they are equal, the node to be deleted is found; otherwise, continue to search in the left subtree or right subtree.

  2. Discussion by situation:

    a. The node to be deleted has no left and right subtrees: just delete the node directly;

    b. The node to be deleted has only the left subtree or the right subtree: hang the left subtree or the right subtree of the node to be deleted on the corresponding position of its parent node, and delete the node to be deleted;

    c. The node to be deleted has both a left subtree and a right subtree: find the predecessor or successor node of the inorder traversal of the node to be deleted (that is, the largest node smaller than the node to be deleted or the smallest node larger than the node to be deleted), Replace the value of the node to be deleted with the value of the predecessor or successor node, and then transform the problem into deleting the predecessor or successor node.

  3. If the root node is deleted, the new root node needs to be returned.

Schematic:

wKjZ.jpg

The code can refer to the blog of the data structure column tree, which I will write in the future.

2. Balanced binary tree (AVL tree)

1. Balanced binary tree

Balanced Binary Tree (Balanced Binary Tree), referred to as balanced tree, is a special binary search tree. Its characteristics are: the height difference between the left and right subtrees of any node does not exceed 1, that is, the absolute value of the difference between the heights of the left and right subtrees of each node does not exceed 1. This can ensure that the time complexity of operations such as search, insertion, and deletion is O(log n), which improves the performance of the tree.

Common implementations of balanced binary trees include AVL trees, red-black trees, and B-trees, among which AVL trees are one of the earliest invented balanced trees. The AVL tree requires that the absolute value of the height difference between the left and right subtrees of each node does not exceed 1, and automatically adjusts the structure of the tree to meet the balance condition by rotating the node after inserting or deleting the node.

The advantage of a balanced binary tree is that while ensuring that the time complexity of basic search, insertion, and deletion operations is O(log n), it also has better space utilization and a smaller tree height, which is suitable for storing large amounts of data. The disadvantage is that compared with ordinary binary search trees, the implementation of balanced trees is more complicated, and maintaining a balanced state increases the cost of inserting and deleting nodes.

2. Balanced binary tree insertion

The insertion operation of a balanced binary tree is divided into the following steps:

  1. If the tree is empty, use the node to be inserted as the root node.
  2. If the inserted element is less than the value of the current node, recursively insert on the left subtree; if the inserted element is greater than the value of the current node, recursively insert on the right subtree.
  3. In the process of returning recursively, check whether the current node is unbalanced. If the current node is out of balance, a corresponding rotation operation is required to restore the balance.

For the rotation operation of a balanced binary tree, there are the following two types:

  1. Left rotation: For node A, if its right subtree is 1 or 2 levels higher than the left subtree, then perform left rotation. The specific operation is to take the right child node of A as the new root node, A becomes the left subtree of the new root node, and the original left subtree of the new root node becomes the new right subtree of A.
  2. Right rotation: Similar to left rotation, for node A, if its left subtree is 1 or 2 layers higher than the right subtree, then perform right rotation. The specific operation is to take the left child node of A as the new root node, A becomes the right subtree of the new root node, and the original right subtree of the new root node becomes the new left subtree of A.

In response to this, the blogger suggested going to Station B to watch a five-minute video to understand.

Here the blogger recommends a video for your reference: Balanced Binary Tree Insertion

3. Deletion of balanced binary tree

The deletion operation of a balanced binary tree is similar to that of an ordinary binary search tree, but the tree structure needs to be rebalanced after the node is deleted to ensure that the height of the tree is always log n .

The specific deletion operation can be divided into the following three steps:

  1. Locate the node to be deleted and delete it. If the node to be deleted is a leaf node, delete it directly; if the node to be deleted has only one child node, replace its child node with itself; if the node to be deleted has two child nodes, find the smallest node of its right subtree (or the largest node of the left subtree), assign its value to the current node, and then delete the smallest node of the right subtree (or the largest node of the left subtree).
  2. Backtracking from the parent node of the deleted node, update the balance factor of the ancestor node. If the absolute value of the balance factor of an ancestor node is found to be greater than 1, a rotation operation is required to rebalance it.
  3. Any subtrees that are unbalanced due to deletions are rotated to restore balance.

The specific rotation operation includes four situations of left rotation, right rotation, left and right rotation, and right and left rotation. Different rotation methods need to be selected to achieve balance in different situations. Among them, left rotation moves the left subtree up one layer, right rotation moves the right subtree up one layer, left and right rotation moves the left subtree to the right first and then moves up, and right left rotation moves the right subtree to the left first and then moves up.

3. Red-black tree (RBT)

1. Definition and properties

A red-black tree is a self-balancing binary search tree that guarantees a worst-case time complexity of O(log n) for basic dynamic set operations.

The properties of a red-black tree are as follows:

  1. Each node is either red or black.
  2. The root node is black.
  3. Each leaf node (NIL node) is black.
  4. If a node is red, both of its children are black.
  5. For any node, the simple path from the node to all its descendant leaf nodes contains the same number of black nodes (that is, the black heights are equal).

Among them, property 4 is the difference between red-black tree and ordinary binary search tree. This property ensures that the black height of the red-black tree will not exceed twice the red height, thereby ensuring the balance of the tree.

Motto: "The left and right roots, the roots and leaves are black, not red, and the black path is the same."

2. RBT insertion

Red-black tree is a self-balancing binary search tree, which guarantees that the time complexity of basic dynamic operations (insert, delete, search) is O(log n) in the worst case.

The insertion operation of the red-black tree can be divided into the following steps:

  1. Insert a new node into a red-black tree, using the basic insertion operation of a binary search tree.
  2. Mark new nodes in red.
  3. Make color adjustments to ensure that the five properties of the red-black tree are not violated.
    1. Parent and child nodes cannot be red at the same time.
    2. The root node must be black.
    3. All leaf nodes are black empty nodes (NIL nodes).
    4. All paths from any node to each of its leaf nodes contain the same number of black nodes.
    5. Every red node must have two black children.

Color adjustments include three cases:

First case: the parent node of the new node is black. In this case, there is no problem, just return directly.

The second case: the parent node of the new node is red, and the uncle node of the new node is also red. In this case, you need to recolor, turn the parent node and uncle node into black, and the grandparent node into red, and then continue to adjust the grandparent node as a new node.

The third case: the parent node of the new node is red, and the uncle node of the new node is a black or empty node. In this case, a rotation operation is required to rotate the current node and its parent node to the left or right, and then recolor.

Through the above steps, the insertion operation of the red-black tree can be completed, and the five properties of the red-black tree are guaranteed.

wEhg.jpg

Four. B tree (multi-way balanced search tree)

1. Definition and properties

B-tree, also known as a multi-way balanced search tree, the maximum number of children of all nodes in the B-tree is called the order of the B-tree, usually represented by m. An m-order B-tree is either an empty tree or an m-ary tree that satisfies the following properties:

  • 1) Each node in the tree has at most m subtrees, that is, it contains at most m-1 keywords.
  • 2) If the root node is not a terminal node, there are at least two subtrees.
  • 3) All non-leaf nodes except the root node have at least [m/2] subtrees, that is, contain at least [m/21-1 keywords.
  • 5) All leaf nodes appear on the same level without information (can be regarded as external nodes or search failure nodes similar to the half search decision tree, in fact these nodes do not exist, point to these nodes point pointer is null)

2. B tree height

The height of a B-tree depends on the number of its nodes and the number of keywords each node can hold. A B-tree is often described as a balanced tree because it automatically adjusts its structure as elements are inserted or removed to keep the tree balanced.

For a B-tree, the path length from its root node to the deepest leaf node is the height of the tree. Since each node of a B-tree can contain multiple keys, it is more compact than a binary search tree, which means its height is much lower. Specifically, if a B-tree node can contain at most k keywords, then its height will not exceed logk(n+1), where n is the total number of keywords in the tree.

Therefore, B-trees can usually find, insert, and delete keywords in O(log n) time, and can remain efficient even when the amount of data is very large.

3. Insertion and deletion

The insertion operation of the B tree is roughly divided into the following steps:

  1. First of all, we need to find the position in the B-tree to insert the new keyword. This process is similar to the search operation.
  2. If the corresponding leaf node is found, the new keyword is directly inserted into the leaf node;
  3. If the leaf node is full, then we need to split the node into two nodes first, and adjust the link of the parent node to the two nodes;
  4. Repeat the above process until the new keyword is successfully inserted.

The deletion operation of the B-tree is also more complicated, roughly divided into the following steps:

  1. Find the keyword to be deleted in the B tree, and return directly if it cannot be found;
  2. If the node where the keyword to be deleted is located is not a leaf node, you need to replace the keyword to be deleted with the predecessor or successor keyword of the node, and delete the predecessor or successor keyword;
  3. If the node where the keyword to be deleted is located is a leaf node, delete the keyword directly;
  4. If the deletion operation causes the number of keywords of a node to be less than the minimum value, a node merge operation is required to merge the node with its sibling nodes and adjust the links of the parent node to the two nodes;
  5. Repeat the above steps until the deletion is complete.

It should be noted that in the B-tree, in order to ensure balance, both insertion and deletion operations may cause node splitting or merging, thereby changing the structure of the B-tree. Therefore, the insertion and deletion operations of B-trees are more complicated than ordinary binary search trees.

Five. B+ tree

B+ tree is a data structure based on B tree. Compared with B tree, it has better performance and space utilization when storing large amounts of data.

The main difference between B+ tree and B tree is:

  1. The non-leaf nodes in the B+ tree do not save the value corresponding to the keyword, but only save the keyword and the pointer to the child node;
  2. All keywords are stored in the leaf nodes, and the leaf nodes are connected in order to form a linked list;
  3. Each keyword in the leaf node will be saved together with its corresponding value;
  4. The pointer of the leaf node points to the real storage address of the record.

The advantages of B+ tree are:

  1. Because all keywords are stored in leaf nodes, there is no need to traverse redundant non-leaf nodes when traversing all keywords, which greatly reduces the number of I/O operations and improves search efficiency;
  2. The linked list formed between leaf nodes can further improve the efficiency of range query;
  3. Non-leaf nodes take up less space because they only need to store keys and pointers to child nodes, not the corresponding values.

Generally speaking, B+ tree is suitable for reading large-scale data and can provide faster query speed and better space utilization. Therefore, in most application scenarios, B+ tree is a better choice than B tree.

6. Comparative Analysis

1. Time complexity

wlvb.jpg

2. Personal understanding

Balanced binary tree and red-black tree are both binary sorting trees, but new rules are added on this basis.

Therefore, the method that needs to be positioned when performing insertion and deletion operations is also the method used by the binary sorting tree.

Explanation: Rising Star Project: Data Structure and Algorithm, @西安第一感情, Creation Punch 3!

Guess you like

Origin blog.csdn.net/weixin_51496226/article/details/130855798