Autumn recruiting preparations - data structure

binary tree

A full binary tree with a depth of i and a total of pow(2,i) - 1 nodes is called a full binary tree

Huffman tree: The binary tree with the shortest weighted path is called Huffman tree or optimal binary tree.

The number of terminal nodes is n0, the number of nodes with degree 2 is n2, then n0 = n2 + 1

B-tree

  • The root node has at least two child nodes
  • Each node has M-1 keywords, and they are arranged in ascending order
  • The value of the child node located between the M-1 and M keywords is located between the Value corresponding to the M-1 and M keywords
  • Other nodes have at least M/2 children

B+ tree

The B+ tree is a modified tree of the B tree. The difference between it and the B tree is:

  • A node with k children must have k keys
  • Non-leaf nodes only have an index function, and information related to records is placed in leaf nodes.
  • All the leaf nodes of the tree form an ordered linked list, and all records can be traversed in the order of key code sorting.

The difference between B and B+ trees is that the non-leaf nodes of B+ trees only contain navigation information and do not contain actual values. All leaf nodes and connected nodes are connected by a linked list, which is convenient for interval search and traversal.

The advantages of B+ tree are:

  • Since the B+ tree does not contain data information on the internal nodes, more keys can be stored in the memory page. The data is stored more tightly and has better spatial locality. Therefore, accessing data associated with leaf nodes also has a better cache hit rate.

  • The leaf nodes of the B+ tree are all linked, so the traversal of the entire tree only requires a linear traversal of the leaf nodes. And because the data is arranged in sequence and connected, it is convenient for range search and search. The B-tree requires recursive traversal of each layer. Adjacent elements may not be adjacent in memory, so cache hits are not as good as B+ trees.

But the B-tree also has advantages. The advantage is that since each node of the B-tree contains key and value, frequently accessed elements may be closer to the root node, so the access is faster. The following is the difference between B-tree and B+ tree:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-2VcLHveD-1662292694681)(en-resource://database/1121:1)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-iPpxUSsP-1662292694683)(en-resource://database/1123:1)]

After understanding the data structure and then looking at the index, everything is not difficult to understand, just follow the logic. In addition, the difference between the two storage engines:

  1. MyISAM is non-transaction-safe, while InnoDB is transaction-safe
  2. The granularity of MyISAM locks is at the table level, while InnoDB supports row-level locks
  3. MyISAM supports full-text index, while InnoDB does not support full-text index
  4. MyISAM is relatively simple, and its efficiency is better than InnoDB. Small applications can consider using MyISAM
  5. MyISAM tables are saved as files, making cross-platform use more convenient
  6. MyISAM manages non-transactional tables, provides high-speed storage and retrieval and full-text search capabilities, and can be selected if a large number of select operations are performed in the application
  7. InnoDB is used for transaction processing and has features such as ACID transaction support. It is optional if a large number of insert and update operations are performed in the application.

red black tree

To solve the linear search shortcomings of binary search trees, red-black trees mainly include the following five properties.

  • Nodes are either red or black
  • root node is black
  • leaf nodes are black
  • Both children of each red node are black. (There cannot be two consecutive red nodes on all paths from each leaf node to the root)
  • All paths from any node to its leaf nodes contain the same black node.

The insertion and deletion of the red-black tree are likely to destroy the five properties of the red-black tree, so some operations are required to maintain the five properties of the red-black tree from being destroyed. There are three main operations: discoloration, left-handed, right-handed.

Design Patterns

There are five creational patterns: abstract factory pattern, singleton pattern, builder pattern, and prototype pattern.

There are seven structural modes: Adapter mode, Decorator mode, Proxy mode, Appearance mode, Bridge mode, Composition mode, Flyweight mode.

There are eleven types of behavioral patterns: strategy pattern, template method pattern, observer pattern, iterative sub-pattern, chain of responsibility pattern, command pattern, memo pattern, state pattern, visitor pattern, intermediary pattern, and interpreter pattern.

complete binary tree

The number is consistent with the full binary tree, indicating that at most the last line is not full.

The clue binary tree, according to the order of traversal, finds the predecessor and the rear. The predecessor checks whether the left subtree is empty, and the latter checks whether the right subtree is empty.

Huffman tree, first construct a root node according to the minimum two node values, then put this root node into the remaining nodes, find out the two smallest nodes and continue to construct a binary tree, repeat the above work until all nodes are completely After the construction is completed, it is stipulated that the edge of the left subtree is 0, and the edge of the right subtree is 1, and the final Huffman tree is obtained. Thus the Huffman code of each node is obtained.

There are two main algorithms for the minimum spanning tree, the prim algorithm and the kruskal algorithm. The prim algorithm finds the shortest weighted edge each time, and finally connects it; kruskal first finds the shortest edge, and then finds a smaller adjacent edge.

Shortest path: Dijkstra's algorithm, which generates the shortest path in the order of increasing path length.

Floyd's algorithm (Floyd), each time adding a node to find the shortest path.

Topological sorting: Find a point with an in-degree of 0. After finding it, delete all its out-degree edges, and then find a point with an in-degree of 0.

Comparison of Several Sorting Algorithms

Unstable sorting: fast sorting, heap sorting (big root heap, small root heap), Hill sorting (find two groups of comparisons),
stable: simple sorting (insertion, bubbling), merge (comparison of two adjacent groups), radix sorting.

Guess you like

Origin blog.csdn.net/hallobike/article/details/126693628