Data Structure: eight types of data structures

Source: Internet
Author: User

Directory

  • Data Structure Classification
  • 1. Array
  • 2. Stack
  • 3. Queue
  • 4. Linked List
  • 5. Tree
  • 6. Hash
  • 7. Heap
  • 8. Diagram

@

Data Structure Classification

A Data Structure is a  elements that have one or more relationships with each other and the relationship between the data elements in the collection.
Common data structures include: array, stack, linked list, queue, tree, graph, heap, and hash ,:

1. Array

An array is a structure that can store multiple elements continuously in the memory, and the allocation in the memory is continuous. Elements in the array are accessed through the array subscript, And the array subscript starts from 0. For example, the following code assigns the first element of the array to 1.

int[] data = new int[100];data[0]  = 1;

Advantages:
1. Fast element query by index
2. Convenient array traversal by index

Disadvantages:
1. After the array size is fixed, it cannot be expanded.
2. Arrays can only store one type of data.
3. Adding and deleting operations are slow because other elements need to be moved.

Applicable scenarios:
Frequent queries have little requirements on storage space and are rarely added or deleted.

2. Stack

A stack is a special linear table that can only be operated at one end of a linear table. operations are allowed at the top of the stack, but not at the bottom of the stack. Stack features: first-in-first-out, or later-in-first-out. The operation of adding elements from the top of the stack is called in the stack.

The stack structure is like a container. The more you put it in, the later you can take it out. Therefore, stacks are often used to implement recursive functions, such as the Fibonacci series.

3. Queue

Like a stack, a queue is also a linear table. The difference is that a queue can add elements at one end and retrieve elements at the other end, that is, first-in-first-out. The operation to put elements from one end is called "join", and the elements are taken as "exit", as shown in the following figure:

Use Cases: this feature is applicable to multi-thread blocking queue management.

4. Linked List

A linked list is a non-sequential storage structure of physical storage units. The logical sequence of data elements is achieved through the linked list pointer address. Each element contains two nodes, one is the data domain (memory space) of the storage element, and the other is the pointer domain pointing to the next node address. According to pointer pointing, linked lists can form different structures, such as single-chain tables, two-way linked lists, and circular linked lists.

Advantages of linked list:
A linked list is a commonly used data structure. You can add or subtract any element without initializing the capacity;
When adding or deleting an element, you only need to change the pointer domain of the first and second element nodes to the address. Therefore, you can add or delete the Element quickly;

Disadvantages:
A large amount of pointer fields are occupied;
It takes a lot of time to search for elements in a traversal table.

Applicable scenarios:
Scenarios where the data volume is small and needs to be increased frequently.

5. Tree

TreeIt is a data structure that consists of n (n> = 1) finite nodes and a set of hierarchies. It is called a "Tree" because it looks like an upside down tree. That is to say, it is root-up and leaf-down. It has the following features:

  • Each node has zero or multiple subnodes;
  • A node without a parent node is called a root node;
  • Each non-root node has only one parent node;
  • In addition to the root node, each subnode can be divided into multiple non-Intersecting Subtrees;

In daily applications, we discuss and use more of the structure of the tree, that isBinary Tree.

A binary tree is a special type of tree with the following features:

1. Each node has a maximum of two Subtrees, And the node has a maximum degree of 2.
2. The left and right subtree are ordered, and the order cannot be reversed.
3. Even if a node has only one subtree, the left and right subtree must be distinguished.

Binary Tree is a useful compromise. It adds and deletes elements quickly, and there are also a lot of algorithm Optimizations in search. Therefore, binary trees have the advantages of linked lists, it also has the advantage of arrays, which is an optimization solution of the two, which is very useful in processing large volumes of dynamic data.

Extension:
Binary Trees have many extended data structures, including balanced binary trees, red/black trees, and B + trees. These binary trees are derived from many functions and are widely used in practical applications, for example, the index structure of the MySQL database uses the B + tree, and the underlying source code of hashmap uses the red/black tree. These binary trees are powerful, but the algorithm is complicated. If you want to learn them, you still need to spend time exploring them.

6. Hash

A hash table is a data structure that is directly accessed based on key codes and values (key and value). It is mapped to a position in the set through key and value, in this way, you can quickly find the corresponding elements in the set.

Record storage location = f (key)

MappingsFA hash function is also called a hash function. A hash function converts a key into an integer using a fixed algorithm function, then the number is used to return the remainder of the array length. The remainder result is used as the subscript of the array, and the value is stored in the array space where the number is the base object, this storage space can take full advantage of array search to find elements, so the search speed is very fast.

Hash Tables are also common in applications. For example, some collection classes in Java are constructed based on the hash principle, such as hashmap and hashtable. Using the advantages of hash tables, it is very convenient to search for elements in a set. However, because the hash table is based on the data structure derived from arrays, it is slow to add or delete elements, therefore, an array linked list is often used, that is, the zipper method. The zipper method is a structure of arrays combined with linked lists. The underlying storage of hashmap earlier adopted this structure, and it was not changed to the structure of array and red/black tree after jdk1.8, an example is shown below:

It can be seen that the Left is obviously an array. Each member of the array includes a pointer pointing to the head of a linked list. Of course, this linked list may be empty or contain many elements. We distribute elements to different linked lists based on some features of the elements. We also find the correct linked list based on these features and then find this element from the linked list.

There are many application scenarios for hash tables, and of course there are also many issues to consider, such as hash conflicts. If the processing is poor, it will waste a lot of time, leading to application crash.

7. Heap

Heap is a special data structure that can be viewed as an array object of a tree. It has the following properties:

  • The value of a node in the heap is always not greater than or less than the value of its parent node;

  • The heap is always a Complete Binary Tree.

The largest heap of the root node is called the largest heap or large root heap, and the smallest heap of the root node is called the smallest heap or small root heap. Common heap types include binary heap and Fibonacci heap.

The heap is defined as follows: the sequence of n elements {K1, K2, KI ,..., KN} is called a heap only when the subrelation is satisfied.
(KI <= k2i, Ki <= k2i + 1) or (KI> = k2i, Ki> = k2i + 1), (I = 1, 2, 3, 4... n/2), the expression that satisfies the former is a small top heap, and the expression that satisfies the latter is a large top heap. The structure of the two can be arranged in a Complete Binary Tree, as shown in the following figure:

Because of the characteristics of heap ordering, it is generally used for sorting in arrays, called heap sorting.

8. Diagram


A graph consists of a finite set V of a node and a set E of an edge. In order to distinguish it from the tree structure, the node is often called a vertex in the graph structure, and the edge is an ordered even pair of vertex. If there is an edge between two vertices, the two vertices are adjacent.

The vertex direction can be divided into an undirected graph and a directed graph:


The figure is a complex data structure with complicated and efficient algorithms for data storage, there are storage structures such as the adjacent matrix, the adjacent table, the cross-linked list, the multiple adjacent tables, and the number of edge sets. We will not deploy them here. If you are interested, you can study them on your own.

Data Structure: eight types of data structures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.