Detailed java data structure! It's that simple! (Stack, queue, binary tree, red-black tree, etc.)

Insert picture description hereInsert picture description here

1. Data Structure

1.1 Stack

The stack is a table that restricts insertion and deletion to only one position, which is the end of the table, called the top of the stack
. It is last in first out (LIFO). There are only two basic operations on the stack: push (into the stack) and pop (out of the stack). The
former is equivalent to inserting, and the latter is equivalent to deleting the last element.

1.2. Queue

The queue is a special linear table. The special feature is that it only allows delete operations at the front of the table (front), and
inserts at the back of the table (rear). Like the stack, the queue is an operation subject to Linear table of limits. The end that performs the insert operation is called the end of the
team, and the end that performs the delete operation is called the head of the team.

1.3. Link

Linked list is a data structure, and the same level as array. For example, the ArrayList we use in Java is based on an array. The
realization of the principle LinkedList is a linked list. Linked lists are not efficient when looping, but they have obvious advantages when inserting and deleting.

1.4. Hash Table

Hash table (Hash table, also called hash table) is a search algorithm. Different from linked lists, trees and other algorithms, the hash table algorithm
does not require a series of keywords (keywords are certain data elements). The value of a data item is used to identify a data
element) comparison operation.
Want to hash algorithm as far as possible without any comparison, data elements can be obtained by looking first access, and thus will
need to be established between the data element storage location and its keyword (available key represented) determining a Correspondence between each
keyword and a unique storage location in the hash table. Therefore, when searching, just find
the position of the given keyword in the hash table according to this correspondence . This correspondence is called a hash function (which can be represented by h(key)).
The methods used to construct the hash function are:
(1) Direct addressing method: Take the keyword or a linear function value of the keyword as the hash address.
That is: h(key) = key or h(key) = a * key + b, where a and b are constants.
(2) Numerical analysis method
(3) Square value method: Take the middle digits after the square of the keyword as the hash address.
(4) Folding method: divide the keyword into several parts with the same number of bits, and then take the superimposed sum of these parts as the hash address.
(5) Divide and leave the remainder method: take the remainder obtained after dividing the key by a number p that is not greater than the hash table length m as the hash address,
namely: h(key) = key MOD pp ≤ m
(6) Random Number method: Choose a random function, and take the random function value of the key as its hash address,
namely: h(key) = random(key)

1.5. Sort Binary Tree

First, if each node of the ordinary binary tree satisfies: all the node values ​​of the left subtree are less than its root node value, and all the node values ​​of the right subtree are
greater than its root node value, then such a binary tree is a sorted binary tree.

1.5.1. Insert operation

First, start from the root node and find the position you want to insert (that is, the parent node of the new node); the specific process is:
compare the new node with the current node, if the same, it means it already exists and cannot be inserted again; if it is smaller than the current node , Then
search in the left subtree, if the left subtree is empty, the current node is the parent node you are looking for, and the new node can be inserted into the left subtree of the
current node ; if it is greater than the current node, search for the right subtree , If the right subtree is empty, the current node is the parent node you are looking for, and the new node can be inserted into
the right subtree of the current node.

1.5.2. Delete operation

To get the complete information, click me-"""""""""""
delete operations are mainly divided into three situations, that is, the node to be deleted has no child nodes, the node to be deleted has only one child node, and
there are twonodes to be deletedChild nodes.

  1. If the node to be deleted has no child nodes, you can delete it directly, that is, let its parent node blank the child node.
  2. If the node to be deleted has only one child node, replace the node to be deleted as its child node.
  3. If the node to be deleted has two child nodes, first find the replacement node of the node (that is, the smallest node in the right subtree),
    then replace the node to be deleted as a replacement node, and then delete the replacement node.

1.5.3. Query operation

The main process of the search operation is: first compare with the root node, return if they are the same,
search recursively in the left subtree if it is less than the root node, and search recursively in the right subtree if it is greater than the root node. Therefore, in the sorted binary tree, the maximum
(rightmost and deepest child node) and minimum (leftmost and deepest child node) values can be easily obtained .

1.6. Red-Black Tree

RB Tree, the full name is Red-Black Tree, also known as "Red-Black Tree", it is a special binary search tree. Each
node of the red-black tree has a storage bit to indicate the color of the node, which can be red or black.

1.6.1. Characteristics of red-black trees

(1) Each node is either black or red.
(2) The root node is black.
(3) Each leaf node (NIL) is black. [Note: The leaf node here refers to the leaf node that is empty (NIL or NULL)! ]
(4) If a node is red, its child nodes must be black.
(5) All paths from a node to the descendants of the node contain the same number of black nodes.

1.6.2. Left hand

For complete information, click me-"""""""""""
left-handed x, which means that the "right child of x" is set to "the father node of x"; that is, x becomes a left Node (x
becomes the left child of z)! . Therefore, the "left" in left-handed means "the rotated node will become a left node".
LEFT-ROTATE(T, x)
y ← right[x] // Prerequisite: It is assumed that the right child of x is y. Let’s start the formal operation
right[x] ← left[y] // Set "left child of y" to "right child of x", that is, set β to the right child of x
p[left[y]] ← x // Set "x" to "father of y's left child", that is, set β's father to x
p[y] ← p[x] // Set "x's father" to "y's father"
if p[x ] = nil[T]
then root[T] ← y // Case 1: If "father of x" is an empty node, set y as the root node
else if x = left[p[x]]
then left[p [x]] ← y // Case 2: If x is the left child of its parent node, set y to "
the left childof the parent nodeofx"
else right[p[x]] ← y // Case 3: (x is the right child of its parent node) Set y to "the right child of the parent node of x
"
left[y] ← x // Set "x" to the "left child of y"
p[x] ← y // Set the "parent node of x" to "y"

1.6.3. Right-hand

Rotating x to the right means that "the left child of x" is set to the "parent node of x"; that is, x becomes a right node (x
becomes the right child of y)! Therefore, the "right" in right rotation means "the rotated node will become a right node."
RIGHT-ROTATE(T, y)
x ← left[y] // Premise: here assume that the left child of y is x. Let’s start the formal operation
left[y] ← right[x] // Set "the right child of x" to "the left child of y", that is, set β to the left child of y
p[right[x]] ← y // Set "y" to "father of the right child of x", that is, set the father of β to y
p[x] ← p[y] // Set "y's father" to "x's father"
if p[y ] = nil[T]
then root[T] ← x // Case 1: If "the father of y" is an empty node, set x as the root node
else if y = right[p[y]]
then right[p [y]] ← x // Case 2: If y is the right child of its parent node, set x to "
the left child of the parent node of y "
else left[p[y]] ← x // Case 3 : (Y is the left child of its parent node) Set x to "the left child of the parent node of y
"
right[x] ← y // Set "y" to the "right child of x"
p[y] ← x // Set the "parent node of y" to "x"

1.6.4. Add

Step 1: Treat the red-black tree as a binary search tree and insert the node.
Step 2: Color the inserted node as "red".
According to the situation of the parent node of the inserted node, "when the node z is colored as a red node and insert a binary tree" can be divided into three
cases for processing.
① Situation description: The inserted node is the root node.
Treatment method: directly paint this node in black.
② Description: The parent node of the inserted node is black.
Solution: No need to do anything. After the node is inserted, it is still a red-black tree.
③ Description: The parent node of the inserted node is red. In this case, there must be inserted into the node is non-empty grandparent
's; further speaking, there must also be inserted into the node uncle node (even if uncle node is empty, we also see it as the presence of an empty section of
the point itself is a black node ). After understanding this point, we further divide this situation into 3
cases (Case) based on the "uncle node situation" .
Step 3: Make it a red-black tree again through a series of operations such as rotation or coloring.

1.6.5. Delete

Step 1: Treat the red-black tree as a binary search tree and delete the node.
This is the same as "deleting nodes in a regular binary search tree". There are three situations:
① The deleted node has no sons and is a leaf node. Then, it is OK to delete the node directly.
② The deleted node has only one son. Then, delete the node directly, and replace its position with the only child node of the node.
③ The deleted node has two sons. Then, first find out its successor node; then copy
"the content of its successor node" to "the content of this node"; after that, delete "its successor node".
Step 2: Modify the tree through a series of "rotation and recoloring" to make it a red-black tree again.
Because after deleting the node in the "first step", it may violate the characteristics of the red-black tree. Therefore
, the tree needs to be modified by "rotating and recoloring" to make it a red-black tree again.
Choose 3 cases of recoloring.
① Situation description: x is a "red+black" node.
Processing method: directly set x to black and finish. At this time, the nature of the red and black trees is fully restored.
② Situation description: x is a "black+black" node, and x is the root.
Solution: Do nothing, end. At this time, the nature of the red and black trees is fully restored.
③ Situation description: x is a "black + black" node, and x is not a root.
Treatment method: This situation can be divided into 4 seed situations. The situation of these 4 seeds is shown in the following table:

1.7. B-TREE

For complete information, click me-""""""""""""
B-tree is also called balanced multi-way search tree. The characteristics of a m-order B-tree (m-ary tree) are as follows (where ceil(x) is a
functionthat takes the upper limit):

  1. Each node in the tree has at most m children;
  2. Except for the root node and the leaf node, every other node has at least ceil(m / 2) children;
  3. If the root node is not a leaf node, it has at least 2 children (special case: a root node without children, that is, the root node is a leaf
    node, and the entire tree has only one root node);
  4. All leaf nodes appear in the same layer. The leaf nodes do not contain any keyword information (it can be regarded as external nodes or
    nodes that fail to query . In fact, these nodes do not exist, and the pointers to these nodes are null);
  5. Each non-terminal node contains n key information: (n, P0, K1, P1, K2, P2,..., Kn, Pn). Among them
    :
    a) Ki (i=1...n) is a keyword, and the keywords are sorted in order K(i-1)< Ki.
    b) Pi is the node pointing to the root of the subtree, and the pointer P(i-1) points to all the nodes of the subtree with keywords that are less than Ki but greater than K(i-
    1).
    c) The number n of keywords must satisfy: ceil(m / 2)-1 <= n <= m-1.

The difference between a B+tree of order m and a B-tree of order m is:
1. A node with n subtrees contains n keywords; (B-tree is n subtrees with n-1 keywords )
2. All the leaf nodes contain the information of all the keywords, and the pointers to the records containing these keywords, and the leaf nodes
themselves are linked in order of the size of the keywords from small to large. (The leaf nodes of the B-tree do not include all the information that needs to be searched)
3. All non-terminal nodes can be regarded as the index part, and the nodes only contain the largest (or smallest) keyword in the root node of the subtree .
(The non-terminal nodes of B-tree also contain valid information that needs to be found)

1.8. Bitmap

The principle of bitmap is to use a bit to identify whether a number exists, and use a bit to store a data, so this can
greatly save space. Bitmap is a very commonly used data structure, such as used in Bloom Filter; used for
sorting non-repeated integers and so on. Bitmap is usually implemented based on an array. Each element in the array can be seen as a series of binary numbers, and all elements
form a larger binary set.

For complete information, click me-""" "" "" "" ""

Guess you like

Origin blog.csdn.net/Java_Yhua/article/details/111045835