I can fill a TreeView control with particular Registry keys by enumerating registry keys recursively and put them in the TreeView control; then for performance reason, I attempt to u开发者_如何转开发se a nonrecursive/iterative approach to enumerate registry keys, but how can I fill the TreeView since a "tree" is naturally recursive (at least, in my understanding)? Is recursion is the only way to achieve it? Would someone give some code snippets/examples or redirect me to webpages explain this matter?
BTW, I'm a Delphi/Free Pascal programmer, but C/C++ programming language explanation should be no problem at all. Cheers :-)
I have asked about this on the Free Pascal mailing list, too.
Thanks in advance.
First of all, worrying about the "performance" of recursion when you're reading the data from the registry and putting data into a tree control sounds a bit silly. The costs of recursion are in the nanosecond range, while the cost of reading from the registry is in the microsecond to millisecond range. The cost of inserting into the tree control will depend on things like visibility and how many items it contains, but it's typically closer to the range of the registry than of recursion. If you're going to insert a lot of items into a control, you usually want to lock the control so it's not updated during the insertion, then turn updating back on after you're done.
Second, yes, it can be done without recursion. The usual way is to have a container of some sort to hold data that will need to be processed, such as a queue or stack. When you're walking the registry tree, you retrieve data, and when/if you encounter a "subdirectory" in the tree, you push it on the stack. When you finish with the current "directory", you retrieve the next one from the stack/queue/whatever and process it the same way. When the collection is empty, you're done.
If performance is the issue, consider delayed filling of a TreeView. Start from creating top level of a tree. Fill each next level of a tree only when user expands it. This should solve a stack overflow problem as well.
Here you can find sources of the Native Registry Editor project which you could use as a sample.
精彩评论