Learning data structure on the shoulders of giants is difficult to stay away from!

Preface

Today we will learn the knowledge of data structure, which is very useful for solid Java basic skills. After learning it, we will have a feeling of being a big boss, hehe. Data structure, also known as Data Structure, is a structure for storing data. There is a certain relationship between data and data. Such a relationship includes the logical relationship of data, the storage relationship of data, and the operation relationship of data. Organize a copy MySQL study notes , data structure and MySQL are still inseparable. In Java, data structures can generally be divided into two categories: linear data structures and non-linear data structures. Haha, is this name very soulful?

Linear data structure

1) Array

You can know it at a glance, such as String [], int []; there are other things you can see through (see the source code), such as ArrayList, which encapsulates the array internally.

The biggest advantage of the data structure of the array is that it can be operated according to the subscript (or index). When inserting, it can be directly inserted into the specific position according to the subscript, but at the same time, all the following elements need to be moved backward. , The more data you need to move, the more tired you are.

Suppose you now have an ArrayList, and you are ready to add an element 55 at the fourth position (subscript 3).

Learning data structure on the shoulders of giants is difficult to stay away from!

At this time, the elements of ArrayList after 5 positions in China will move backward.

Learning data structure on the shoulders of giants is difficult to stay away from!

Prepare to remove 23 from the ArrayList.

Learning data structure on the shoulders of giants is difficult to stay away from!

At this time, the elements with subscripts 7, 8, and 9 move forward.

Learning data structure on the shoulders of giants is difficult to stay away from!

Briefly summarize the time complexity of ArrayList, which is convenient for everyone to use as a reference when learning.

1. The time complexity of accessing an element through the subscript (that is, get(int index)) is O(1), because it is direct, no matter how many times the data increases, the time consumption remains the same.

2. The time complexity of adding an element (that is, add()) is O(1) because it is added directly to the end.

3. The time complexity of deleting an element is O(n). Because the list is traversed, the amount of data increases several times, and the time-consuming increases several times.

4. The time complexity of finding an unsorted list is O(n), because the list needs to be traversed; the time complexity of finding a sorted list is O(log n), because the binary search method can be used, when the data increases by n times When the time, the time-consuming is increased by logn times (the log here is based on 2, and half of the possibility is eliminated every time).

2) Linked list

The linked list is not continuous in the physical storage space, but each node either knows who its next node is or who its previous node is, as if we are separated by thousands of mountains and rivers, but there is a link in the heart. . Like LinkedList is the most typical linked list structure, which is linked to each other by reference.

Each element in the LinkedList can be called a node, and each node contains three items: one is the element itself, the other is the reference address that points to the next element, and the third is that points to the previous element. Reference address.

The LinkedList looks like this:

Learning data structure on the shoulders of giants is difficult to stay away from!

  • Since the first node does not have the previous node, prev is null;
  • Since the last node does not have the next node, next is null;
  • This is a doubly linked list, each node is composed of three parts, the front and back nodes and the value.

Compared with ArrayList, LinkedList has the following advantages:

1. LinkedList allows dynamic allocation of memory, which means that memory allocation is done by the compiler at runtime, and we don't need to specify the size when LinkedList is declared.

2. LinkedList does not need to store elements in consecutive positions, because nodes can specify the next node or the previous node by reference. In other words, the LinkedList is very cheap when inserting and deleting elements, because there is no need to move other elements, only the reference addresses of the previous node and the next node need to be updated.

3) Stack

The stack is a very useful data structure. It is like a stack of plates. The first is placed at the bottom, the second is placed on top of the first, the third is placed on top of the second, and the last is placed at the bottom. Above. The stack follows the last-in first-out principle, which is "Last In First Out" (referred to as LIFO)-the last one comes in, the first goes out.

For a data structure such as a stack, it has two common actions:

  • Push, there are many Chinese interpretations, I personally prefer to call it "press into", very vivid. When we want to put a piece of data on the top of the stack, this action is called push.
  • Pop, similarly, I personally prefer to call it "pop", with a very strong animation effect, is there any? When we want to remove a piece of data from the stack, this action is called pop.

Learning data structure on the shoulders of giants is difficult to stay away from!

4) Queue

Queue, only allows adding data at the end of the line, and removing data at the head of the line. Queues appear very frequently in Java, and there are a variety of different classes to meet the needs of different scenarios. Like priority queue PriorityQueue, delay queue DelayQueue and so on. The queue follows First In First Out, abbreviated as FIFO , which is first in first out, and the first one that enters the queue comes out first.

Learning data structure on the shoulders of giants is difficult to stay away from!

Non-linear data structure

1) Tree

A tree is a typical nonlinear structure, which is a hierarchical collection composed of n (n>0) finite nodes. It is called a "tree" because this data structure looks like an upside-down tree, except that the roots are on the top and the leaves are on the bottom. The tree data structure has the following characteristics:

  • Each node has only a limited number of child nodes or no child nodes;
  • A node without a parent node is called the root node;
  • Every non-root node has one and only one parent node;
  • Except for the root node, each child node can be divided into multiple disjoint subtrees.

The following figure shows some terms of the tree:

Learning data structure on the shoulders of giants is difficult to stay away from!

The root node is level 0, its child nodes are level 1, the child nodes of child nodes are level 2, and so on.

  • Depth: For any node n, the depth of n is the only path length from the root to n, and the depth of the root is 0.
  • Height: For any node n, the height of n is the longest path length from n to a leaf, and the height of all leaves is 0.

The tree can be subdivided into the following types:

1. Ordinary tree

There are no restrictions on child nodes.

Learning data structure on the shoulders of giants is difficult to stay away from!

2. Binary tree

Each node contains a tree with at most two child nodes. Binary trees can be divided into multiple types according to different manifestations.

2.1, ordinary binary tree

The parent node of each child node does not necessarily have a binary tree of two child nodes.

2.2, complete binary tree

For a binary tree, suppose its depth is d (d>1). Except for the dth layer, the number of nodes in the other layers has reached the maximum value, and all the nodes of the dth layer are continuously and tightly arranged from left to right.

2.3, full binary tree

A binary tree with the maximum number of nodes at each level. There are two manifestations. The first one, as shown in the figure below (each layer is full), satisfies the maximum number of nodes in each layer of 2.

Learning data structure on the shoulders of giants is difficult to stay away from!

3. Binary search tree:

The English name is Binary Search Tree, or BST, which needs to meet the following conditions:

  • The left subtree of any node is not empty, and the values ​​of all nodes on the left subtree are less than the value of its root node;
  • The right subtree of any node is not empty, and the values ​​of all nodes on the right subtree are greater than the value of its root node;
  • The left and right subtrees of any node are also binary search trees.

Learning data structure on the shoulders of giants is difficult to stay away from!

3.1, balanced binary tree

If and only if the height difference between the two subtrees of any node is not more than 1 binary tree. The highly balanced binary tree proposed by mathematicians Adelse-Velskil and Landis of the former Soviet Union in 1962 is also called AVL tree according to the English name of scientists.

The balanced binary tree is essentially a binary search tree, but in order to limit the height difference between the left and right subtrees, and avoid tilting trees and other situations that tend to linear structure evolution, so the left and right subtrees of each node in the binary search tree are used. Because of the limitation, the height difference between the left and right subtrees is called the balance factor, and the absolute value of the balance factor of each node in the tree is not greater than 1.

The difficulty of balancing a binary tree is how to maintain left-right balance by left-handed or right-handed when deleting or adding nodes.

The red-black tree is a common balanced binary tree, the nodes are red or black, and the balance of the binary tree is maintained through color constraints:

  • Each node can only be red or black
  • The root node is black
  • Each leaf node (NIL node, empty node) is black.
  • If a node is red, then its two child nodes are black. That is to say, two adjacent red nodes cannot appear on a path.
  • All paths from any node to each of its leaves contain the same number of black nodes.

4. B tree

A self-balanced binary search tree that optimizes read and write operations, can keep data in order, and has more than two subtrees.

Learning data structure on the shoulders of giants is difficult to stay away from!

5. B+ tree

Variant of B-tree

Learning data structure on the shoulders of giants is difficult to stay away from!

The TreeNode in HashMap uses red-black trees, while B-trees and B+ trees have typical applications in the principle of database indexing.

2) Hash table

Hash Table, also known as Hash Table, is a data structure that can be directly accessed by key-value. Its biggest feature is that it can quickly find, insert, and delete. The algorithm used is called hash, which transforms an input of any length into a fixed-length output, and the output is a hash value. Like MD5 and SHA1, hash algorithms are used.

Every Java object will have a hash value. The default is to execute the hash algorithm by calling the local method to calculate the key value of the object's memory address + the value of the object.

The biggest feature of an array is that it is easy to find, but it is difficult to insert and delete; while the linked list is just the opposite, it is difficult to find, but it is easy to insert and delete. The hash table perfectly combines the advantages of the two, and Java's HashMap also adds the advantages of the tree on this basis.

Learning data structure on the shoulders of giants is difficult to stay away from!

The hash table has a fast (constant level) query speed, and a relatively fast addition and deletion speed, so it is very suitable for use in an environment with a large amount of data. ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

For any two different data blocks, the possibility of the same hash value is extremely small, that is, for a given data block, it is extremely difficult to find a data block with the same hash value. Furthermore, for a data block, even if only one bit of it is changed, the change in its hash value will be very large-this is the value of the existence of the Hash! ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Although the possibility is extremely small, it will still happen. If the hash conflicts, Java's HashMap will add a linked list at the same position in the array. If the length of the linked list is greater than 8, it will be converted into a red-black tree for processing-this It is the so-called zipper method (array + linked list).

3) Figure

A graph is a complex nonlinear structure consisting of a finite non-empty set of vertices and a set of edges between vertices, usually expressed as: G(V, E), where G represents a graph, and V is the vertex in view G E is the set of edges in graph G.

Learning data structure on the shoulders of giants is difficult to stay away from!

The above figure has 4 vertices V0, V1, V2, V3, and there are 5 edges between the 4 vertices.

In a linear structure, the data elements satisfy a unique linear relationship, and each data element (except the first and last one) has a unique "precursor" and "successor";

In the tree structure, there is an obvious hierarchical relationship between data elements, and each data element is only related to one element in the upper level (parent node) and multiple elements (child nodes) in the next level; readers today Benefits: study notes + real interview questions .

In the graph structure, the relationship between nodes is arbitrary, and any two data elements in the graph may be related.

Guess you like

Origin blog.51cto.com/14994509/2678757