Algorithm Interpretation: Data structure

Source: Internet
Author: User

What is a data structure?

Effective management mechanisms for large amounts of data. There are many types of data structures used when describing computer algorithms:

Arrays: Data structures for continuous arrangement of data, with data arranged linearly and without gaps
Linked lists: Data structures in sequential order, linked lists can manage data in a linear arrangement, and can be stored over long distances
Stack: Like a pile of books on a table to manage the data structure of the data, the stack is a data management method that reverses the order of the data input, and the reverse output
Queue: As in the supermarket checkout, the first customer to pay first, the queue is the data output sequence and input sequence consistent data management method
Tree: As a branch can be divided into two, three or even more branches, and each branch can also be separated into two, three or more branches

Stack

In the stack management data, the data input operation is called the stack (push), and the data output operation is called making the Stack (POP). A stack is a management method that has the last input data to output this feature first, which we call LIFO (Lifo--late in first out), or advanced-out (FILO).

Queue

Just the stack is advanced, or LIFO, then the queue is FIFO (Fifo--first in First out), or the data structure of the backward (lilo--late in late out).

Linked list

The data length of the linked list is not limited and can be physically discontinuous.

A one-dimensional array manages the pre-and post-relationship of the data based on the linear arrangement of the data without gaps, which means that the data is placed in an adjacent box so that the data structure of the order is known. When the elements of an array are loose, the order of the data in the address space of the array is not known, which means that the elements in the array must be tightly placed together and cannot be kept away from each other.

A one-way list is independent of where the data is stored. Data can be managed in the correct order, even where the data is stored.

In terms of how many valid data are managed, the array manages the number of data according to the variable, and the list is "pointer to the next data" when it reaches the tail of the chain.

One-way linked list

Linked by a directional chain in a list, the method of connecting a certain sequence of data is called a one-way list.

One-way lists have two elements: data (the values that are managed by the list, such as integers saved in a node, floating-point numbers, and strings), and pointers to the next node (subsequent pointers, which indicate the physical address of the next element).

The shopping order list indicates:

Buy fruit from your home--fruit store--go to the butcher's and buy fish----go to the fish shop to buy a snack: There's no way to go home, without considering the existence of a circular list.

Doubly linked list

In the list, there is a chain between the front and back forward two directions, and the way in which sequential data is connected is called a doubly linked list.

A doubly linked list has the following three elements: data, a pointer to the previous node, and a pointer to a backward node

A pointer to a head node and a tail node is also required in a doubly linked list, and each element in the doubly linked list is composed of data and pointers to the front and back nodes.

Once the data has been traversed backwards, start with the element pointed out from the pointer, use the successor pointer, get the next element, the next element, and loop through it.

When you traverse the data from the back forward, start with the element indicated by the tail pointer, use the precursor pointer to get the previous element, and then iterate through the previous element.

Find element

When you find the nth element of a sequentially arranged data, the array can find the element directly based on the element number. The list must be traversed from the head node up to the nth element, such as finding the 5th element, and the array can be positioned directly through array[4], while the list is searched in the following order:

Find a 1th node
From the 1th node to the successor 2nd node.
From the 2nd node to the successor 3rd node.
From the 3rd node to the successor 4th node.
From the 4th node to the successor 5th node.

No 5th node can be found without the above 5 steps. And so on, if you look for a 10,000th node .... Therefore, in the case of finding the nth element, the array is more efficient than a linked list.

Insert Delete Data

When inserting new data at a specific location in a sequentially arranged data column, the array moves all elements behind the insertion position backwards, and the linked list only needs to break the chain around the data position, connecting the new data to the linked list.

Delete data is the same, for the array to delete data, all the data after the delete data to move forward, and linked list, no matter how many data after the deleted data, only need to disconnect the deleted data link, and then the front and back two nodes directly connected to it.

Therefore, in the delete and insert operations, the linked list is more efficient.

Two-fork Tree

A single-linked list is a data structure that uses "pointers to successors" to connect data in a managed order. The binary tree has two single-linked lists pointing to subsequent pointers.

In a binary tree, the constituent elements are called nodes. A binary tree is made up of a parent node and two sub-nodes.

The binary tree does not necessarily have the left and right nodes, but only the node that is the only one, or only the node is allowed, but there are three or more sub-nodes are not allowed.

Nodes that do not have a parent node are called "root nodes", and nodes that have no child nodes are called "leaf nodes."

Heap

Heap, the value of the parent node must not be greater than the value of the child node, or the value of the parent node must not be smaller than the value of the child node.

The heap is applied to the data structure for the minimum value.

You can change the above heap to an array:

Hash table

(an array called the root array and the number of elements is N) + (the element of the root array as the head pointer of the linked list) = Hash table

Hash table management data first to the value of the root array element determines where to put in, the process of getting this value is called the hash function.

The hash function calculates the value of a "0 ~ (N-1)" (number of array elements) range based on the value of the input element, which is called a hash value. Depending on the hash value and the number of the array element, the position of the element in the root array can be determined.

When looking for specific data from a hash table-managed data, the hash value is first requested, then the object's position in the table is searched, and then the search is done sequentially until the target element is found.

The hash function--simply, returns the value of the quotient of the number of inputs and the number of elements in the hash table.

Based on the assumption that the element "Coke" needs to be found in a hash value of 2, the hash function can be used to locate the "cola" element in the linked list that the root array [2] points to.

Figure

The diagram shows a method of more than two project relationships, the item to be expressed in the diagram is called the node, and the line linking the item is called the edge. Graph is the data structure that uses "nodes" and "edges" to represent data connection relationships.

Graph theory--a study of the nature of graphs that specialize in the collection of edges and the set of points.

Algorithm Interpretation: Data structure

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.