Basic knowledge of data structure 1

Source: Internet
Author: User
Tags sorts

A Understand and be able to write code quickly and accurately. (★★★★★)

1. Several common sorts (codes)

Comparison-based sorting algorithm: The Nether is NLGN

1.1 Selectionsort: Each time the bottom element is selected, it is placed in the leftmost position of the current loop.

1.2 Bubblesort: Each comparison of two adjacent numbers, so that the largest number like bubbles to the right.

1.3 Insertionsort: Each time you pick up a number, insert it into the correct position on the left array.

1.4 QuickSort: Select a number, as the standard, less than it is placed on the left, greater than it on the right. and put it in the middle; recursively sorts the left and right sub-arrays.

When implemented:

1. Determine the recursive end condition, initialize the left and right cursors, select the standard number;

2. While loop, does while implementing two cursors simultaneously moving to the middle, displacement;

3. The number of permutation criteria and the right-hand cursor;

4. Recursively call to sort the left and right sub-arrays.

1.5 Heapsort: Implemented with maximum heap.

When implemented: Build heap: Displace the top element of the heap and the last element, the heap size decreases, keeping the new heap as the maximum heap;

Keep the maximum heap: keep the maximum heap from the bottom up, from the first parent node to the root.

1.6 MergeSort: Split fractional Group, recursive implementation of sorting, two-way merge. Use Sentinels to prevent cursors from crossing the bounds.

Linear time-running algorithms:

1.7 Countingsort: Assuming that the data is distributed between 0 and K. For each input x, determine the number of numbers less than X. Assuming that the number is less than x 17, then x should be in the 18th output position.

1.8 Radix sort (cardinal sort): Starting from the lowest bit, each bit uses a stable sorting algorithm (such as counting sort).

1.9 Bucket Sort: Used when the input data is more uniform.

First divides the data interval into n buckets, divides the data into the corresponding buckets, inserts the data into the bucket, and then strings the sorting results of each bucket.

2. Two-point lookup (code)

To find an ordered array, note that the array may have the same number.

3. Single linked list (code)

Single linked list of basic operations: build, find, insert, delete, etc.

4. Two fork Tree (code)

The establishment and traversal of binary tree (pre-order, post-order, middle sequence, layered), destruction, height calculation, etc.

5. Figure (Code)

5.1 means: adjacency table, adjacency matrix;

5.2 Traversal: Depth-first search, breadth-first search;

5.3 Minimum Spanning Tree:

Kruskal algorithm: Each time the edge of the current minimum weight is taken, if the two nodes connected by this edge are not in the same connected component, it is added to the smallest spanning tree until all vertices are overwritten.

Prim algorithm: Starting at any root vertex, each time you take the edge with the least weight of any vertex in the tree, add it to the tree until all vertices are overwritten.

5.4 Direction diagram single source shortest path:

Dijkstra algorithm (requires a non-negative ownership value): The algorithm given a source point, each time from the remaining vertices to select a vertex with the shortest path estimation of u, add it to the set S, and all the edges of u to relax.

Two Understand. (★★★★)

1. Stacks and queues: when head[q] = tail[q]+1 in the queue, the queue is full.

2. Doubly linked list:

In order for the deleted, inserted code to write well, the Sentinel was raised:

3. Hash table: The element with the keyword K is placed in the slot H (k), with the link method to resolve the collision, such as:

Three small points of knowledge:

Loading factor: Number of elements n/slots M

Hash function: Division: h (k) = k mod M. m is usually a prime number not too close to the integer power of 2.

Open addressing: Finds items in a hash list continuously until an empty slot is found. The removal operation is more difficult.

4. Two fork Find tree:

4.1 Nature: The parent node's keyword is greater than its left node, less than its right node.

4.2 Traversal; Insert and delete; find: Find a keyword, maximum, minimum element, precursor, and successor. Search for subsequent algorithms:

If the right subtree of node x is not empty, then it has the leftmost node of the subtree;

If the right subtree of node x is empty and X has a successor, then its successor is its lowest ancestor y, and the left son of Y is also the ancestor of X.

5. Red and black Trees

5.1 Definition: A binary lookup tree that adds a storage bit to a node to represent the color of a node. Red and black trees limit the way each node is shaded from the root to the leaf, ensuring that no path is twice times longer than the other path. Thus is close to the equilibrium.

5.2 5 Property:

1) Each node is red or black;

2) The root node is black;

3) Each leaf node (NIL) is black;

4) If a node is red, then its two sons are black;

5) For each node, include the same number of black nodes on all paths from the node to its descendant nodes

5.3 Examples:

5.4 Application:

In C + + STL, many parts (currently including set, Multiset, map, Multimap) have applied the variants of red-black trees (there are some changes in the red-black trees in SGI STL, which provide better performance and support for set operations). It is complex, but its operation has a good worst-case run time and is efficient in practice: it can do search, insert, and delete operations in O (log n) time.

5.5 AVL tree: A highly balanced two-fork lookup tree, which is 1 of the height of the Saozi right subtree for each node x.

5.6 Treep: When the nodes are arranged, let the keyword follow the nature of the binary lookup tree, and the precedence follows the nature of the minimum heap.

The root keyword is g, and the priority is 4.

6. B-Tree: B-tree nodes can have many children.

6.1 Typical characteristics:

(a) The keywords for each node are arranged in a non-descending order;

(b) The parent node's keyword, between the child node keywords (that is, the individual children can be separated). As shown in the following:

6.2 Application: B-Tree indexes are a way to access and find files (called records or key values) in a database. The b-tree algorithm reduces the intermediate process that is experienced when locating records, thus speeding up the access speed.

6.3 B + Tree: All the collateral data is stored on the leaf node, and only the keywords and children's pointers are saved to the node. As shown in the following:

Categories of linked lists

Single linked list

Single-linked list is a kind of chain access structure, in order to find the first Data element, we must find the first i-1 data element. The shaded area in the figure represents the data field, and the white space represents the pointer field. And the last pointer field is empty.

Circular link List

The circular chain list is another form of chain-storage structure. Its characteristic is that the pointer field of the last node in the table points to the head node, and the entire list forms a ring. The circular chain list is divided into single-cycle linked list and multi-chain circular chain list.

Double linked list

A doubly linked list, also known as a doubly linked list, is a list of links with two pointers in each data node, pointing directly to successive and direct precursors. So, starting from any node in a doubly linked list, it is easy to access its predecessor and successor nodes. The flexibility of a doubly linked list is better than a single-linked list, but it is much more expensive (there are two pointers).

Comparison of sequential tables and linked lists

The sequential table storage location is contiguous, a data structure that can be accessed immediately, a sequential table must specify a length before use, and once memory is allocated, it cannot be changed dynamically in use. His advantage is that accessing the data is convenient and can immediately access any one of the data in the table.

A linked list is a data structure that describes an element's relationship through pointers, which can be physical spaces where physical addresses are not contiguous. The linked list element cannot be accessed immediately, and the element must be searched step-by, starting from the table header. Its advantages are: For the array, you can dynamically change the length of the data, allocating physical space.

In use: If an array in use, query more, and insert, delete data less, the length of the array is not changed, the selection of the order table is more reasonable. If you insert, delete, and vary the length of an array, you can choose the linked list.

Trees (tree)

A tree is a n>0 set K that contains n (s) nodes, and a relationship n,n is defined in K that satisfies the following conditions:

(1) There is only one node K0, he has no precursor to the relationship N, called K0 as the root node of the tree. Referred to as root (root).

(2) In addition to K0, each node in K has and has only one precursor for the relationship N.

(3) The nodes in K can have M successors (m>=0) for the relationship N.

The tree has the following characteristics:

(1) Each node has 0 or more child nodes.

(2) There is only one parent node for each child node.

(3) A node that does not have a parent node is called the root node.

Some terms about the tree
Node Degree: The number of sub-trees that a node contains is called the degree of the node;

leaf node or end node: a node with zero degrees is called a leaf node;

Non-terminal node or branch node: a node with a degree nonzero;

Parents or parent nodes: If a node has a child node, then it is called the parent node of its child node;

child node or child node: the root node of a subtree containing a node is called a child node of that node;

Sibling nodes: nodes with the same parent node are called sibling nodes;

The height or depth of a tree: Defines the root node level of a tree as 1, and the other node's level is its parent node level plus 1. The maximum level of all nodes in a tree is called the depth of the tree. Hierarchy of nodes: From the beginning of the root definition, the root is the 1th layer, the root of the sub-node is the 2nd layer, and so on;

Degree of the tree: the degree of the largest node in a tree is called the degree of the tree;

Ancestor of a node: all nodes from the root to the branch of the node;

Descendants: Any node in a subtree that is rooted in a node is known as the descendant of that node.

Forest: The collection of trees that are disjoint by M (m>=0) is called a forest;

The traversal of a tree

The traversal of a tree is divided into: pre-sequence traversal, post-order traversal, and hierarchical traversal. The traversal order of the pre-sequence traversal is the first access to the root node, then the leaf node, and the traversal order of the sequential traversal is to first access the leaf node and then access the root node, while the hierarchical traversal is traversed by the hierarchy.

Two-fork Tree

A binary tree is an ordered tree with a maximum of two subtrees per node. The subtree is often referred to as the left subtree and the right subtree (subtree). The binary tree is divided into two-fork tree, complete binary tree, and incomplete binary tree.

Graph (graph)

The graph is composed of set E of the node's poor set V and the edge. Among them, in order to distinguish with the tree structure, the nodes are often called vertices in the graph structure, the edges are the ordered pairs of vertices, if there is an edge between the two vertices, it means that the two vertices have neighboring relations. Among them, the graph is divided into the non-direction graph and the direction graph.

The traversal of graphs

The traversal of graphs is divided into depth-first traversal and breadth-first traversal. The idea of depth-first traversal is similar to a tree's first-order traversal. Its traversal process can be described as: a vertex V, access to the vertex, and then proceed from the inaccessible adjacency Point V to continue the depth of the first traversal of the remaining vertices in the diagram until all the paths in the diagram with the V have been accessed until the end of the vertex.

The breadth-first traversal method is described as starting with a vertex V, accessing all of the inaccessible adjacency points of v after accessing the vertex V, and then accessing the adjacency points of each adjacency point, and the access order should remain the first accessed vertex whose adjacency point is also first accessed until all vertices in the diagram are accessed.

Basic knowledge of data structure 1

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.