[Java from the beginning to the end] No7. Review the basics of Map collection and tree data structure

Map collection basics and tree data structure

Basic review → Map collection basics and tree data structure
——————————————————————————————————
Part of the article Selected from the book "Coding for Efficiency: Java Development Manual"

1.Map collection basics

In previous blogs, we have roughly analyzed the source code and learned about the ArrayList and LinkedList in the List collection. Today's blog post is mainly a foreshadowing of the Map collection (because it is a review, so there will be no very basic concepts). After that, We will conduct source code analysis and study of common implementation classes in the Map collection. Attached is a link to the previous blog:
[Java from scratch to the end of the bald head] No4.JDK1.8 ArrayList source code learning and expansion
[Java from the beginning to the bald end] No5.JDK1.8 LinkedList source code learning

Let's take a look at the collection framework diagram in Java. The Map collection is only a part of it, but it is also a very important part.
Insert image description hereThe book says it very well,"Program = data structure + algorithm", Sets, as the carrier of data structures, can process and output elements, and use certain algorithms to achieve the most basic addition, deletion, modification, and query functions. Therefore, sets are the basis of all programming languages.
So let’s briefly review the knowledge of data structures first. I will summarize and summarize the content in the book:

  1. Data structure definition
    Data structure refers to the way data is organized in a logical sense and its corresponding processing method . 1-1.
    What is the logical meaning ?
    The abstract expression of data structures is very rich, but the actual physical storage method is relatively simple. The storage of the tree structure on the disk is definitely not a tree arrangement. The storage model of this tree structure is a structural model in a logical sense, which is completely different from the actual physical storage method (of course there are also data structures that conform to the physical storage model. For example, the underlying array structure is a continuous memory storage space)
    1-2.What is data organization ?
    There are many ways to organize things in a logical sense, such as trees, graphs, queues, hashes, etc. The tree can be a binary tree, a ternary tree, a B+ tree, etc., the graph can be a directed graph or an undirected graph, the queue is a first-in-first-out linear structure, and the hash is a data organization method that is directly positioned according to a certain algorithm. (In general, it means effectively organizing and arranging data in various ways and adapting to various scenarios)
    1-3.What is data processing ?
    Based on the established data organization method, a specific algorithm is used to realize the addition, deletion, modification, search and traversal of data. Different data processing methods often have very large performance differences.

  2. Data structure classification
    Data structure is the cornerstone of algorithm implementation. It is an internal skill that embodies basic logical thinking. It is also an important item in the competency map of computer practitioners. If you don't understand data structures at all, it will be difficult to write excellent code. A defective underlying data structure can easily lead to high system risks and poor scalability, so the data structure needs to be carefully designed and reviewed. From the perspective of the number of direct predecessors and direct successors, data structures can generally be divided into the following four categories.
    2-1.linear structure
    0 to 1 immediate predecessors and immediate successors. When the linear structure is non-empty, it has a unique first element and a unique last element, and except for these two, all elements have unique direct predecessors and direct successors. Linear structures include sequence lists, linked lists, stacks, queues, etc. , where stacks and queues are structures with restricted access. The stack is last in, first out, that is, Last In, First-Out, referred to as LIFO; the queue is first in, first out, that is, First-In, First-Out, referred to as FIFO.
    2-2.tree structure
    0 to 1 immediate predecessors and 0 to n immediate successors (n greater than or equal to 2). Tree is a very important hierarchical nonlinear data structure, like trees in nature. Because the tree structure is relatively stable and balanced, it is widely used in the computer field.
    2-3.graph structure
    0 to n immediate predecessors and immediate successors (n greater than or equal to 2). Graph structures include simple graphs, multiple graphs, directed graphs and undirected graphs, etc. (This seems to have no corresponding implementation in Java storage structure)
    2-4.Hash structure
    There are no direct predecessors and direct successors. The hash structure associates the index with the stored value through a specific hash function. It is a data structure with very high search efficiency. This is also the most commonly used data structure in our Map collection.

If the data structure is just different in structure, it will be meaningless. These specific structures must serve to implement some kind of efficient algorithm. Only the combination of the two can exert their power. Let's go straight to the book about the complexity of data and time. The content of the degree is so well written that I can’t simplify it any more:
Insert image description hereInsert image description here
Let’s take a look at the summary of the Map collection:
Insert image description hereInsert image description hereIn addition to providing the traditional add, delete, modify, and search methods, the Map interface also has three unique methods of the Map class, namely return All keys, return all Values, return all KV key-value pairs. The source code plus comments are as follows:

// 返回 Map 类对象中的 Key 的 Set 视图
Set<K> keySet(); 

// 返回 Map 类对象中的所有 Value 集合的 Collection 视图
// 返回的集合实现类为 Values extends AbstractCollection<V>
Collection<V> values();

// 返回 Map 类对象中的Key-Value键值对(Entry<K,V>)的 Set 视图
Set<Map.Entry<K, V>> entrySet();

Usually these returned views support clear operations, but modifying and adding elements will throw an exception because AbstractCollection does not implement the add operation, but does implement remove, clear and other related operations. So when using these views to return collections, be careful not to operate such related methods. Whether to set KV to null shall be subject to the implementation class constraints. This is a very difficult knowledge point to remember:
Insert image description hereInsert image description here

2. Briefly describe the tree data structure

Above we have seen the implementation of multiple data structures. Why should we talk about tree data structures? Mainly because tree structures are widely used in the computer field. The most important thing is our topic today, the Map collection in Java. The underlying code implementation principle is based on trees. The bottom layer of Java's tree implements a red-black tree , but it is also necessary to understand the development of the tree structure. Let's take a look at it together. This is just me after reading the book. After reading the content, I will summarize it myself. I will just talk about it in general. The book is more detailed and more difficult. For those who are interested, I still recommend reading the original text.
Okay, let's officially start: I
will recommend a website to everyone in the front row. This website simulates our data structure. There are various data structures. After clicking in, there will be a visual interface. You can add data to it intuitively. See the data structure's reactive results.

Data Structure Visualizationbold style

  1. Tree structure → Binary tree structure
    The tree in the computer data structure is upside down (left side of the picture). Because the branches and nodes of the tree are messy, we cannot effectively maintain and use this storage data structure. Along with the efficient binary search method , our binary tree structure was born, each node has at most two child nodes (right of the picture)
    Insert image description here

  2. Balanced Binary Tree
    If the growth of the tree only leans to one side, then the tree will deviate, and our data structure will also deviate. In extreme cases, as shown on the left, it becomes a linked list, losing the meaning of being a tree and losing its meaning. The excellent data processing method of the tree structure is undoubtedly a waste, so in order to make the tree structure play its role, we slightly restrict it to restore the balance of the left and right nodes of the tree, as shown on the right side of the figure, we limit the height difference of the left and right subtrees, and get Balanced binary tree.
    Insert image description here

  3. Binary search tree
    is also called binary search tree, that is, BST → Binary Search Tree, also called binary sort tree BST → Binary Sort Tree
    . In fact, the tree has not developed to this point to exert the power of binary search method. The important premise of binary search method is The data is in order.
    At this time, the binary search tree requires that the values ​​of all nodes on its left subtree are less than it, and the values ​​of all nodes on its right subtree are greater than it.
    The left picture of a balanced binary tree can be converted into a binary search tree, as shown below.
    Insert image description here
    The binary search tree also has the problem of the previous balanced binary tree, that is, as the tree grows and the data increases, it is easy to cause the tree to become deviated and unbalanced, so in order To maintain balance, many trees came out later, such as AVL trees, red-black trees, SBT (Size Balanced Tree, Treap), etc. Next we will mainly look at AVL trees and red-black trees.

  4. AVL tree
    AVL tree algorithm is a balanced binary tree algorithm named after Soviet mathematicians Adelson-Velsky and Landis, which can maximize the efficiency of using binary trees. AVL tree is a balanced binary search tree (a combination of balanced binary tree and binary search tree), which is rebalanced through tree rotation after adding and deleting nodes. Generally divided into left-handed and right-handed
    Insert image description hereInsert image description here

  5. Red-black tree
    Red-black tree is similar to AVL tree. It is also a balanced binary search tree. The same point is that it is re-balanced through tree rotation after adding and deleting nodes. The difference is that, first of all, the red-black tree adds color attributes to the nodes. The purpose is of course to improve the speed of data structure calculation and use through node color judgment. The second point is that the red-black tree is a balanced tree, but it is not. It is not an AVL tree that maintains absolute balance. The height difference between the left and right subtrees cannot exceed 1. The maximum height of the left and right subtrees of a red-black tree must not exceed twice the maximum height of the other subtree. That is, the height of the left subtree is 3. The height of the right subtree is 6, which is no problem in a red-black tree. Comparing red-black trees and AVL trees, the in-depth content in the book is more and more complicated. I will briefly introduce it here. I am just reviewing the content. I am not a beginner in this content. If you still have questions, I will
    still
    Insert image description hereInsert image description hereIt is still recommended to read the original book content, which is more detailed. The name of the book is given at the beginning of the article.
    If we don’t understand the content of these data structures, it will be a big obstacle for us to read the underlying implementation source code of the Map collection, because a lot of the source code is written and written to implement the red-black tree. The data structure operation of the red-black tree is like an instruction manual. , the prescription, and the code are just the medicines you get according to the prescription.
    OK, see you next time when we study Map source code.

Guess you like

Origin blog.csdn.net/cjl836735455/article/details/106958509