\ Documentclass [a4paper, 11pt, oneside, openany] {article} \ usepackage {CJK} \ usepackage {times} \ usepackage {listings} \ usepackage {xcolor} \ usepackage {color} \ usepackage [top = 1in, bottom = 1in, left = 0.75in, right = 0.65in] {geometry} % margin \ usepackage {indentfirst} \ setlength {\ parskip} {0.7ex plus0.3ex minus0.3ex} \ begin {document} \ lstset {numbers = left, numberstyle = \ tiny, keywordstyle = \ color {blue! 70}, commentstyle = \ color {red! 50! Green! 50! Blue! 50}, frame = shadowbox, rulesepcolor = \ color {red! 20! Green! 20! Blue! 20}, xleftmargin = 0pt, xrightmargin = 6pt, breakindent = 0pt} \ begin {CJK *} {GBK} {com} \ title {data structure job-2.19} \ maketitle \ date {} Made by CTeX. source code on http://blog.csdn.net/c0de4fun. all rights reserved. data Structure 2.19 question [question requirements]: it is known that the elements in a linear table are sorted in ascending order by values, and a single-chain table is used as the storage structure. It is required to write a {\ color {red} {\ CJKfamily {hei} algorithm to delete the elements with a central value greater than mink and smaller than maxk in the linked list and release the space of the deleted node, analyze the time complexity of the algorithm. Note that mink and maxk may not be in the linked list. [Algorithm analysis]: The question emphasizes efficiency. Here we adopt the "space for Time" policy and construct a binary search tree for the entire interval based on the elements in the linked list. All non-leaf nodes on the tree represent the {\ color {red} {\ CJKfamily {hei} range} of the element, while the leaf node represents the corresponding element, if we set \ begin {equation} N = LengthOf (mink, maxk) \ end {equation}, obviously, the time complexity of searching for a single {\ color {red} {\ CJKfamily {hei} on this tree is \ begin {equation} TimeComplexityOfSearchSingleElement = \ Theta (\ lg N) \ end {equation} because we need to perform a search for each element in the interval (whether or not the element exists !) \ Noindent. Therefore, the overall time complexity of deleting all elements in the (mink, maxk) linked list is \ begin {equation} TimeComplexity = O (N \ lg N) the space complexity of \ end {equation} is \ begin {equation} SpaceComplexity = O (N \ lg N) \ end {equation} [Algorithm Improvement and comparison ]: i. Improved time complexity in order to improve time complexity, we can open a hash table and set the key of the hash table to the value of the element in the linked list, the value of the hash table is set to the position of the elements in the linked list. In this way, we can delete corresponding elements in the time complexity of \ begin {math} O (1) \ end {math, the access to elements can also reach the \ begin {math} O (1) \ end {math} level. The reason we chose the binary search tree is that we think this question should be tested in binary search :-) 2. Compared with binary search, in fact, if we use binary search, it can save half of the space overhead, but binary search has a problem in this question, that is, the {\ color {red} {\ CJKfamily {hei} interval (mink, maxk) the elements in may not all be in the linked list}. Therefore, some additional processing is required to ensure that the elements in the corresponding range can be found in the binary search, which undoubtedly increases the complexity of the program. [Program source code ]: \ clearpage \ begin {lstlisting} [language = {[ANSI] C}] # include <iostream> # include <algorithm> # include <cstdlib> # include <cstring> # include <cstdio >#include <map> using namespace std; const int EMPTY_VALUE = 0; const int MAX_SIZE = 10000; int nums [MAX_SIZE]; int next [MAX_SIZE]; int tree [MAX_SIZE * 2]; map <int, int> hash; int comp (const void * a, const void * B) {return * (int *) a-* (int *) B;} void buildTree (const I Nt l, const int r) {int mid = (l + r)/2; if (l = r) {tree [mid] = hash [nums [mid]; return;} buildTree (l, mid); buildTree (mid + 1, r);} void del (const int l, const int r, const int dest) {int mid = (l + r)/2; if (nums [tree [mid] = dest) {cout <"Found" <nums [tree [mid] <"in linkTable! "<Endl; int k = nums [tree [mid]; hash [nums [tree [mid] = 0; // First node is dummy. nums [tree [mid] = EMPTY_VALUE; cout <"Del" <k <"successfully! "<Endl; return;} if (r <= mid | l> = mid) {return;} else {if (nums [tree [mid]> dest) del (l, mid, dest); if (nums [tree [mid] <dest) del (mid + 1, r, dest) ;}} int main () {int num; hash. clear (); memset (nums, EMPTY_VALUE, sizeof (nums); cout <"Please enter the Number of elements you want to sort" <endl; cin> num; for (int I = 1; I <= num; I ++) {cout <"Please enter the" <I <"th elemen T "<endl; cin> nums [I]; hash [nums [I] = I;} qsort (nums, num + 1, sizeof (nums [0]), comp); buildTree (1, num); cout <"Please enter the boundary of [L, R]" <endl; int left, right; cout <"The left is:"; cin> left; cout <"The right is:"; cin> right; for (int I = left + 1; I <right; I ++) {del (1, num, I) ;}cout <"The link table after deleting... "<endl; for (int I = 0; I <= num; I ++) {in T cnt = 1; if (nums [I]! = EMPTY_VALUE) {cout <nums [I] <""; cnt ++;} if (cnt % 5) = 0) cout <endl ;} return 0;} \ end {lstlisting} \ end {CJK *} \ end {document}