[Data structure]——Search, hash table short answer question template

1. The concept of search

(1) Static search and dynamic search

1. Briefly describe the concepts of static search and dynamic search, and give examples of corresponding search names.

: Finding whether a specific element is in the table and its related attributes is called a static search, such as order, half, block, hash search, etc.; if you need to insert or delete elements of the lookup table during the search process, it is a dynamic search. Search, such as binary search tree, balanced binary tree, B-tree, etc. in tree search.

(2) Application of binary search

1. Under what circumstances can binary search be used? If the number of data elements is n, what is the average search length for a successful search?

: Binary (halved) search requires that the linear table must adopt a sequential storage structure, and the elements in the table are ordered by keywords. In a binary decision tree, the number of comparisons will not exceed the height of the tree h=⌈ at most. log2(n+1)⌉, that is, the average search length for a successful binary search is O(log2n).

2. For an ordered sequence list, is binary search always faster than sequential search? Why?

: No, it’s just that in general, the binary search is faster than the sequential search, because the binary search can exclude half of the elements in each comparison, thus reducing the search time. For example, if the element you want to find is the first element of a sequential list, sequential search will be faster than binary search.

(3) Searching for surveillance posts in the algorithm

1. In many search and sorting algorithms, "monitoring posts" are often used. What is its purpose?

: The function of the monitoring post is to eliminate the need to check whether the entire table has been searched every time during the search process, thereby improving the search efficiency.

2. Hash search

(1) Synonyms

1. In a hash table that uses linear detection to handle conflicts, are all synonyms necessarily adjacent in the table?

: Not necessarily adjacent. Because a conflict occurs, that is, two different keywords are hashed to the same address by the hash function, both competing for the hash address.

(2) Construct a hash function

1. Briefly describe the common methods of constructing hash functions.

: Briefly describe the common methods of constructing hash functions as follows:
(1) Direct addressing method: select a keyword or a linear function value of a keyword as the hash address ;
(2) Division with remainder method: select the remainder of dividing the keyword by an integer as the hash address, that is, the remainder obtained by taking the remainder operation as the hash address;
(3) Square-middle method: Take the middle digits of the square of the keyword as the hash address;
(4) Folding method: Divide the keyword into equal digits Several segments are then stacked and summed as a hash address.

2. How to measure the advantages and disadvantages of Hash function?

: To evaluate the advantages and disadvantages of the Hash function: whether the keywords can be evenly mapped to the hash space, whether there is a good way to resolve conflicts, whether the calculation of the hash function is simple and efficient, etc. In addition, a good hash function can reduce collisions, but it is impossible to avoid them.

(3) Steps of hash search

1. Assuming that a hash table has been established according to the hash function H and the conflict processing method R, try to write the steps of hash search.

: Suppose the keyword KEY of the hash table is searched, first calculate the address through the hash function, and then compare the keyword KEY with the keyword of the address. If the current address is empty, the search fails; if they are the same, the search succeeds; If they are different, the next address is obtained through the conflict handling method R and compared until they are the same. If the new address obtained according to the conflict handling method R is empty again, it proves that the search failed.

(4) Conflict and conflict resolution

1. Briefly describe the concept of conflict in hash table technology, and point out three methods to resolve conflicts.

: The situation where two different keywords are hashed to the same address by the hash function is called a conflict. The methods to resolve the conflict include linear detection method, square detection method, double hashing method, chain address method, etc.

3. Tree search

(1) Definition of binary search tree

1. What is a binary search tree?

: A binary tree used for searching, in which the value of each node is not less than the value of the left subtree node and not greater than the value of the right subtree node.

(2) Definition of balanced binary search tree

1. What is a balanced binary search tree? What are the dynamic balance adjustment operations?

: The balanced binary tree is based on the binary search tree. If the absolute value of the difference between the heights of the left and right subtrees in the binary search tree does not exceed 1, it is called a balanced binary tree. Its left and right subtrees It is also a balanced binary tree. There are the following dynamic balance adjustment operations:
①Left side rotation LL;
②Right side rotation RR;
③Left first and then Right double rotation LR;
④ First right and then left double rotation RL.

(3) The number of balanced binary search tree nodes

1. How many nodes does an AVL tree with m layers have at least and at most?

: A balanced binary tree (AVL) with m levels, with at most 2m-1 nodes. In addition, by Fibonacci sequence, we can get at least f(m) = f(m-1) + f(m-2) + 1 node, where f(1) = 1, f(2) = 2 , f(3) = 4.

Guess you like

Origin blog.csdn.net/qq_43085848/article/details/135003975