[Data Structure and Algorithm] Learning Record Collection (06) Search

  • This collection is the study record of the "C++ Data Structure and Algorithm" course that the author self-study on the b station. It aims to record the important learning points, thinking content and part of the code, so that it can be read by yourself later, and it can also bring some information to other readers. refer to
  • The content is based on the author’s own understanding or perception, and may contain inappropriate or incorrect contents.
  • System environment: Win10, Visual Studio 2019
  • The pictures in the article refer to the online course handouts of Northeastern University's "Data Structure and Algorithm Design" (2020)

Table of contents

Static / dynamic search table

binary search tree

AVL tree

B - tree


Static / dynamic search table

 sequential search

Inserting sentinels to search in reverse order does not need to consider cross-border issues. The algorithm is simple and does not require source data sorting, but the efficiency is low O(n)

 binary search

Elements must be stored in order, which can reach O(logn)


 

binary search tree

Lookup performance is hit or miss

  • The search performance of ordinary binary search trees is related to the shape of the tree. Its performance ranges from O(logn) ~ O(n) order. If it is a binary tree that degenerates to linear, the search performance is the worst. The ideal situation is full Binary tree or complete binary tree
  • Therefore, certain human control and intervention can be carried out when inserting elements to adjust the shape of the tree, thereby introducing AVL trees...

AVL tree

Highly balanced binary search tree

For the shape of the tree, I can describe it as "perfect", "pretty good", "very bad", etc. If we want to pursue perfection, we have to pay a lot of money. When inserting elements, we have to pay a lot. It takes time and energy to adjust the shape of the tree. In extreme cases, even most of the elements of the tree have to be adjusted in order to pursue perfect form.

Therefore, we want to spend normal energy to achieve a "nearly perfect", that is, "pretty good" tree shape. AVL trees are allowed to be imperfect, allowing subtrees to have different heights, so we have less adjustments to make when elements are inserted.

The concept of balance factor is introduced, which is the absolute value of the height difference between the left and right subtrees of a node. The AVL tree allows this value to be equal to 1 or 0. If the insertion of an element will cause the balance factor of a node to be equal to 2, the element position must be adjusted. Adjustment, the specific adjustment method depends on the situation, there are four situations in total:

  1. External node: left subtree - left subtree insertion, one rotation adjustment
  2. External node: right subtree - right subtree insertion, one rotation adjustment
  3. Internal node: left subtree - right subtree insertion, two rotation adjustments
  4. Internal node: right subtree - left subtree insertion, two rotation adjustments

The specific rotation method will not be extended here. In short, it is to go back to the nearest ancestor node with the problem to solve the problem, so that the entire tree is still an AVL tree.

If we want to delete an element, the operation is more complicated. We need to trace back all the ancestor nodes of the node to ensure that the balance factor of the tree still meets the requirements after deleting the node.


 

B - tree

Reduce disk reads 

There is a problem with general search trees. When the number of elements increases, the height of the tree will also increase, which will bring about a decrease in search efficiency. At the same time, the number of disk accesses will also increase. The reading efficiency of traditional mechanical disks is much slower than memory, so B-tree is used to try to solve the above problem

B-tree can be regarded as a kind of m-way tree (multi-way tree). The degree of the tree can be greater than 2, and m is usually an odd number.

The value range of leaf nodes is divided by keywords. The maximum number of keywords is m-1. B-tree can reduce the height of the number to a large extent and improve search efficiency.


The End

Guess you like

Origin blog.csdn.net/Norths_/article/details/125682466