Data structures and algorithms to deal with programmers interview, you must know the eight data structure

What is a data structure?

 

Briefly, the data structure is a container storing a particular layout data. This "layout" determines the data structure is efficient for certain operations, while other operations are inefficient. First, we need to understand the various data structures, in order to select the most appropriate data structure in dealing with practical problems.

 

Why do we need a data structure?

 

Data in computer science among the most critical entity, and the data structure of the data can be stored in some form of organization, therefore, the value of the data structure is self-evident.

 

No matter what problem you solve the manner in which you have to deal with data - whether it relates to staff salaries, stock prices, shopping list, or just simple phone book problem.

 

Data necessary, stored in a specific format according to different scenarios. There are many data structure to meet the needs of data stored in different formats.

 

 

 

Common data structure

 

First, list some of the most common data structure, we will explain one by one:

An array of stacks queue list tree of FIG trie (which is an efficient tree structure, but it is worth noting separately) hash (hash table)

 

Array

 

Arrays are the simplest and most widely used data structures. Other data structures, stacks, queues, etc. by an array evolved. Below is a simple array contains elements (1,2,3 and 4), the length of the array 4.

 

 

Each data element is associated with a positive value, which we call the index, which indicates the position of each element in the array is located. Most languages ​​define initial index will be zero.

 

The following are two types of arrays:

(Shown above) a one-dimensional array of multi-dimensional arrays (an array of arrays)

 

The basic operation of the array

 

Insert-- insert an element at the specified index Get-- Returns the index position of the element Delete-- specified index element Size-- get the number of array elements in all

 

Interview frequently asked questions about the array

 

Looking for an array second smallest element integer array to find the first one is not recurring merge two ordered arrays rearrange the array of positive and negative values

 

Stack

 

The famous undo almost all over the arbitrary application. But have you ever thought about how it work? Solutions to this problem is the order of the final status prior arrangement, in memory storage history working condition (of course, it will be limited to a certain number). This is no way to use an array to achieve. But with the stack, which is very convenient.

 

It can be thought of as a stack book stacked vertically. In order to get the middle of the book, you need to remove all the books placed in it. This is the LIFO (last in, first out) works.

 

FIG stack comprising the three data elements (1, 2 and 3), wherein the first removable top 3 to be:

 

 

The basic operation of the stack

 

Push-- insert an element at the top Pop-- return and remove the top element isEmpty-- If the stack is empty, true is returned Top-- Top element, but does not remove it

 

Frequently Asked Questions about the interview stack

 

Postfix expression is calculated using the stack to sort the elements of the stack is determined whether the expression in parentheses balance

 

queue

 

Similar to the stack, the queue is a data structure of another linear sequential storage elements. The biggest difference is that the queue stack and the stack is a LIFO (last in, first out), and the queue is the FIFO, i.e., first in first out.

 

A perfect example of the reality of the queue: queue ticket booth team. If there are new entrants, he needs to go to the tail queue, rather than the first team - the top surface of the first people to get votes, and then leave the team.

 

FIG comprising the four queue elements (1,2,3 and 4), wherein the first one is removed at the top:

 

Removing the first-team elements, inserting a new element

 

The basic operation of the queue

 

Enqueue () - is inserted in the tail of the queue elements Dequeue () - removing the element head of the queue isEmpty () - if the queue is empty, returns true Top () - returns the first element of the queue

 

Frequently Asked Questions about the interview queue

 

Using a queue represents a stack of the first k elements of reverse queues using the queue to generate a binary number from 1 to n,

 

List

 

Another important linear list is a data structure, a bit like arrays may at first glance, but in the allocation memory, an internal configuration and a data insertion and deletion operations basic aspects are different.

 

Like a chain of nodes linked list, where each node contains pointers pointing to data and subsequent nodes. Further comprising a list head pointer to the first element of the list, but the list is empty, or null point it is no specific content.

 

List generally used to implement the file system, the hash table and adjacency table.

 

This is a list showing the internal structure of:

 

The list includes the following types:

Single chain (unidirectional) doubly linked list (bidirectional)

 

The list of basic operations:

 

InsertAtEnd - inserted at the end of the list of specified elements InsertAtHead - insert the specified element at the beginning of the linked list / head Delete - delete the specified element from the list of links DeleteAtHead - Remove the first item linked list of Search - Returns the specified element from the list isEmpty - If the list is empty, return true

 

Interview frequently asked questions about the list

 

Inversion list loop detection list in the resulting list penultimate N nodes duplicate entries in the list to delete

 

Map

 

FIG is a set of nodes in the network form interconnected. Also referred to as a vertex node. A pair of nodes (x, y) is called the edge (edge), is connected to the vertex of vertices x y. Edges may comprise a weight / cost, show x from the apex to the cost of y.

 

 

The graph type

Undirected graph directed graph

 

In the programming language, may be represented by FIG two forms:

Adjacency matrix adjacency list

 

Common graph traversal algorithm

BFS depth-first search

 

Frequently Asked Questions about the interview graph

Implement depth first search and breadth check whether the tree in FIG number of edges calculated graph to find the shortest path between two vertices

 

tree

 

The tree structure is a hierarchical data structure, by the vertices (nodes) and their edges are connected composition. FIG similar tree, but the tree and important features distinguishing the loop does not exist in the tree.

 

Tree structure is widely used in artificial intelligence and complex algorithms, which can provide an effective mechanism to solve the problem of storage.

 

This is a simple schematic view of tree, the tree data structure and basic terminology used:

Root - root

Parent - parent

Child - a child node

Leaf - leaf node

Sibling - siblings

 

The following are the main types of tree structure:

N-ary tree balanced tree binary binary search trees AVL tree red-black tree 2-3 tree

 

Among them, the binary tree and binary search tree is the most common trees.

 

Frequently asked questions about the interview tree structure:

Height binary tree to find the maximum value in the k-th binary search tree nodes from the root node to find the k locate a given node in a binary tree ancestor node

 

Trie (Trie)

 

Dictionary tree, also known as "prefix tree", is a special kind of tree data structure is very effective for solving problems related to the string. It provides quick retrieval, searching mainly for word in the dictionary, automatically providing advice in the search engines, even for IP routing.

 

The following are examples of storage "top", "so" and "their" are three words in the dictionary tree:

 

These words in the end portion so as to store the top, wherein the green nodes "p", "s" and "r" represent respectively a bottom "top", "thus" and "theirs" a.

 

Frequently asked questions about the interview trie

Calculating the total number of words in the dictionary tree print all the words stored in the dictionary tree used for the elements of the array trie sorted form a word from the dictionary trie used to build T9 dictionary (trie + DFS)

 

Hash table

 

哈希法(Hashing)是一个用于唯一标识对象并将每个对象存储在一些预先计算的唯一索引(称为“键(key)”)中的过程。因此,对象以键值对的形式存储,这些键值对的集合被称为“字典”。可以使用键搜索每个对象。基于哈希法有很多不同的数据结构,但最常用的数据结构是哈希表。

 

哈希表通常使用数组实现。

 

散列数据结构的性能取决于以下三个因素:

哈希函数哈希表的大小碰撞处理方法

 

下图为如何在数组中映射哈希键值对的说明。该数组的索引是通过哈希函数计算的。

 

 

面试中关于哈希结构的常见问题:

在数组中查找对称键值对追踪遍历的完整路径查找数组是否是另一个数组的子集检查给定的数组是否不相交

 

以上是在编程面试之前你应该知晓的八大数据结构。数据结构与算法,应对程序员面试,你必须知道的八大数据结构数据结构与算法,应对程序员面试,你必须知道的八大数据结构数据结构与算法,应对程序员面试,你必须知道的八大数据结构数据结构与算法,应对程序员面试,你必须知道的八大数据结构数据结构与算法,应对程序员面试,你必须知道的八大数据结构

Guess you like

Origin www.cnblogs.com/spark9988/p/11521574.html