If you’re mathematically inclined, the examples that were used to introduce the .concepts of recursive programming are quite interesting. But most people aren’t, so let’s look at a couple of practical examples. We’ll return to the Binary Search algorithm we explored in Chapter 5, Basic ActiveX Controls, and implement it with a recursive function. The Binary Search algorithm searches a list of ordered (sorted) entries to locate any given item. This algorithm starts,by comparing the desired item with the middle entry of the list. Because the entries are sorted, depending on the outcome of the comparison, half the list will be rejected. If the desired element is above the middle element of the list! the lower half of the list will be rejected, The search continues in the upper half, and the process is repeated, The desired element is located somewhere in the ‘upper half of the list, and the algorithm now’compares the desired element with the middle element of the upper half of the list. Depending on the outcome of this comparison, one half of the already reduced list is rejected again, and. the process is repeated until we are left with a single element, which is the desfred element. If it isn’t, you know that the element you’re looking for doesn’t belong to the list.
The idea behind the Binary Search algorithm is to halve the list at each iteration and reach the location of the desired element quickly, If you start with a list of 1,024 items, after the first comparison you’ll be left with 512 elements, after the second comparison you’ll be left with 256 elements, after the third comparison you’ll be : left with 128 elements, and so on. It will take only 10 comparisons to narrow the list down to the item you want. The definition of the Binary Search algorithm is recursive in nature and it can be implemented easily and efficiently with a recursive function.
The BSearch() Fundion
Following is a description of the BSearchO function, whicl1 implements the Binary Search algorithm:
1.. First, BSearch() retrieves the list’s middle element.
2. It then compares thedesired element with the list’s middle element.
3. If the desired element is found, the task is finished.
4. If the desired element is larger than the list’s middle element:
• The lower half of the list is rejected.
• The upper half of the list is rejected
5. If the list has one or no element(s), it quits.. The element isn’t in the list.
6. BSearch() then searches the rest of the list.
The last test is essential; without it, the search would never end. If the list’s size is reduced to a single element, you know that this element is the desired one or that the desired element doesn’t exist. The implementation of the BSearchO function in Visual Basic follows .
The E search function is a straightforward coding of the verb 1explanation of the algorithm. Here’s what it does:
1. First; it calculates th index of the middle element and compares the until middtle element with the sired element (KeyField).
2. If poth elements are the same, the desired lement has peen local d and thearch need not continue.
3 The function retums th index of the desired leent in the list.
If both ; aren’t the SAme, the velues of th two limits of the afray are compared.
4. If* upp r limit is equal to or Jess than the lower limit (Uli:tt is, if the list has been r duced to a single element), the desired element iinot in the Hat,and lnsad of an index, the function returns the value -1.
5. If th end of the ll~t haii not been reacle, the function comparead the delired
element with the array’s middle element and shortens the array accordingly by c:hiU\gi.ngthe valuee of th upper and lower varl blest which are the indices delimiting the section of the arr.ay in which the desired element resides. FIgire 11,II: shows th 13Se.m:h application of Chapter 5, Bap;c ActiveX Controls, Oll!}’
this time ..the BSearchO function is implemented recur ively ‘n, new BSearch appliy cation (1n this chapter’s folder on the CD) demonstrates how to U e the BSearch() function presel’\ted earli r tn tl is section to seerch for an item in a sorted li9t.
Scanning Folders Recursively
The examples of recursive functions we have looked at so far probably haven’t entirely convinced you of the usefulness of recursion. The factorial of a number can be easily calculated with a For … Next loop, and the Binary Search algorithm was-implemented nonrecursively earlier in this book. So, what good is recursion? Th, answer is the FileScan application, which can’t be implemented nonrecursively. I hope that the previous examples helped you understand the principles of recursive programming and that you’re ready for some real recursion. We’U·design an application similar to the Windows Explorer, which scans an el)tUe folder, including its subfolders. As the application scans the files and subfolders of a folder or an entire volume, it can locate files by name, size, and date. It 1110 move Use around, and in general, perform all the operations of the Wmdow Exploret”, plus any other custom operation you might require. Much of the functionaUt)· of this application is provided by Wmdows Explorer, but as you’ll ~eeihortly, thiil application is highly customizable. It can serve as your starting point for nlAI\y file operation that Windows Explorer doesn’t provide.P lf example, YOU 1′ custom Explorer could expand all tile sub folders each time open a f~lder or display the full path name of each folder. Later. YOu ‘application that generates a list of atl U~efilt:s in a folder, include Ins its SUb. foldllr & Ofgardzea by older, A ill” ideal for implementing with a recursive function Use ; operation defined recursively, Suppose you want to scan all the entries of’ft folder and & ocnte the file.<)whose size exceeds 1MBor count the: files. If an entry 11another folder, thp !’\ameprocess must be repeated for that subfolder. If the subwId. the process must be repeated. nus application calls for a because every time it runs into a sub folder, it must interrupt me of the current folder and start scanning the subfolder by calling itself. If you spend -some time thinking about the implementation of this algorithm, you’ll here’s no simple way to eta it without recursion. Actually, once you’ve nature 01 a process, you’ll knuw you must code it as a recumve producer.