Basic knowledge of data structure and common questions

Source: Internet
Author: User

1. What is a strongly connected graph: A directed graph is strongly connected. When and only when G has a loop, it contains at least one node.

2. compress the 10th order symmetric matrix and store it in one-dimensional array a. The minimum length of array a is
If the 10th order symmetric matrix is compressed and stored in one-dimensional array A, the length of array a is at least (c ).
(A) 100 (B) 40 (c) 55 (d) 80
Answer: (100-10)/2) + 10

3. What is heap?

Heap is a fully sorted binary tree. The data values of any non-terminal node are not greater than (or less than) the values of its left and right child nodes.

4. write code, reverse a single-chain table, and implement them in iterative and recursive forms.

Typedef struct node linknode; struct node {int data; linknode * Next;}; // return the new linked list header node linknode * reverse_link (linknode * head) {If (Head = NULL) return NULL; linknode * Prev, * curr, * reverse_head, * temp; Prev = NULL, curr = head; while (curr-> next) {temp = curr-> next; curr-> next = Prev; Prev = curr; curr = temp;} curr-> next = Prev; reverse_head = curr; return reverse_head;} linknode * reverse_link_recursive (linknode * head) {If (Head = NULL) return NULL; linknode * curr, * reverse_head, * temp; If (Head-> next = NULL) // The linked list contains only one node, the reversed header pointer remains unchanged. Return head; else {curr = head; temp = head-> next; // temp is (A2 ,... an) header pointer reverse_head = reverse_link_recursive (temp); // reverse linked list (A2 ,... an) and return the reversed header pointer temp-> next = curr; // link A1 to curr-> next = NULL;} return reverse_head; // (A2 ,... an) the head pointer of the reverse linked list is (A1, A2 ,... an) Reverse the head pointer of the linked list}

5. Briefly describe what is hashtable and how to solve the hash conflict?

A hash table (hashtable) is also called a "scatter". hashtable is a set of key pairs and value pairs that are organized according to the hash program code of the index key. A hashtable object is composed of buckets that contain elements in a collection. Bucket is a virtual sub-group of hashtable elements, which makes searching and obtaining in most sets easier and faster.

The hash function is an algorithm used to return the numerical hash program code based on the index key. The index key is the attribute value of the stored object ). When an object is added to hashtable, it is stored in the bucket related to the hash program code that matches the object hash program code. When you search for a value in hashtable, the hash program code generates the value and searches for buckets related to the hash program code. For example, student and teacher are placed in different buckets, while dog and God are placed in the same bucket. Therefore, the index key performs better when it is the only one that gets elements from hashtable. Hash has the following advantages.
No sorting is required in advance.
The search speed has nothing to do with the amount of data.
The encryption technology of digital signatures is highly confidential.
Data Compression can be used to save space.

1. Open address Method
When a conflict occurs, a probe (or probe) technique is used to form a probe (TEST) sequence in the hash. Search by unit along the sequence until a given keyword is found or an open address is reached (that is, the address unit is empty, when exploring an open address, you can save the address unit of the new node to be inserted ). When an open address is found, no keyword is found in the table, that is, the search fails.
Note:
① When a hash is created using the open addressing method, all units in the table (more strictly speaking, the keywords stored in the unit) must be left blank before the table is created.
② The expression of a null unit is related to a specific application.
According to the method for forming the probe sequence, the open site method can be divided into linear probe method, linear compensation probe method, random probe method, and so on.
(1) linear probing)
The basic idea of this method is:
Think of the scattered list T [0 .. M-1] As a circular vector. If the initial probe address is D (that is, H (key) = D), the longest probe sequence is:
D, D + L, d + 2 ,..., M-1 ,..., D-1
That is, starting from address d during probe, t [d] is first probe, and t [d + 1],…, Until M-1], then it loops to T [0], t [1],…, Until T [D-1] is explored.
The probe process ends in three cases:
(1) If the unit of the current probe is empty, the search fails (if it is inserted, the key is written to it );
(2) If the unit of the current probe contains a key, the search is successful, but insertion means failure;
(3) If no empty unit or key is found when T [D-1] is detected, search or insertion means failure (the table is full at this time ).
The general form of open address method is used. The probe sequence of the linear probe method is:
Hi = (H (key) + I) % M 0 ≤ I ≤ M-1 // that is, DI = I
The linear probing method is used to deal with conflicts, which has clear ideas and simple algorithms, but has the following Disadvantages:
① Another program is required to handle overflow. An overflow table can be set up to store records that cannot be stored in the hash table. The simplest structure of this overflow table is the sequential table. The search method can be sequential.
② It is very difficult to delete a hash table created based on the above algorithm. If you want to delete a record from the hash table HT, the location of the record should be set to null. However, we cannot do this, but can only mark the deleted record. Otherwise, it will affect future searches.
③ Linear probing is prone to clustering. The so-called clustering phenomenon means that the records stored in the hash table are connected into one piece in the table. Conflicts are processed by linear probing. If the continuous sequence of generated hash addresses is longer (that is, the longer the hash addresses of different keyword values are adjacent to each other), when a new record is added to the table, the higher the possibility of conflicts with this sequence. Therefore, the long continuous sequence of the hash address is short and the continuous sequence grows fast, which means that further clustering will occur once a clustering occurs (with conflict.
(2) Linear Compensation Detection Method
The basic idea of Linear Compensation probing is:
Change the step size of the linear test from 1 to Q, and change J = (J + 1) % m in the above algorithm to: J = (J + q) % m, Q and m are mutually required to be able to detect all units in the hash table.
[Example] PDP-11 small computer assembler used in the compliance table, this method is used to solve the conflict, the length of the table M = 1321, choose q = 25.
2. Zipper
(1) zipper Solution
The zipper method solves the conflict by linking all nodes with synonyms in the same single-chain table. If the length of the selected hash is m, you can define the hash as a number group of pointers consisting of m head pointers T [M-1]. All nodes with the hash address I are inserted into a single-chain table with the T [I] As the header pointer. The initial values of each component in T should be null pointers. In the zipper method, the filling factor α can be greater than 1, but generally α ≤ 1.
[Example] contains M = 5, H (K) = K mod 5, keyword value sequence Example 5, 21, 17, 9, 15, 36, 41, 24, shows the hash table created by the external link address method:

(2) Advantages of zipper
Compared with the open address method, the zipper method has the following advantages:
① The zipper method is simple to deal with conflicts without accumulation, that is, non-synonyms will never conflict, so the average search length is short;
② Because the Node space on each linked list in the zipper method is dynamically applied, it is more suitable for situations where the table length cannot be determined before table creation;
③ In order to reduce conflicts, the open addressing method requires a small filling factor α, which wastes a lot of space when the node size is large. In the zipper method, α ≥ 1 is recommended, and when the knots are large, the pointer domain added in the zipper method is negligible, thus saving space;
④ The delete node operation is easy to implement in the hash list constructed by the zipper method. Simply delete the corresponding node on the linked list. For the hash list constructed by the open address method, the space of the deleted knots cannot be empty simply by deleting nodes, otherwise, the search path of the synonym node in the hash list is truncated. This is because in various open address methods, empty address units (that is, open addresses) are the conditions for failed search. Therefore, the delete operation is performed on the hash list that uses the open address method to handle conflicts. The delete mark can only be performed on the deleted node, but cannot be deleted.

(3) Shortcomings of the zipper Method
The disadvantage of the zipper method is that the pointer requires extra space. Therefore, when the node size is small, the open address method saves more space. If the space saved is used to expand the size of the hash, the filling factor can be reduced, which reduces conflicts in the open addressing method and increases the average search speed.

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.