A tree is a very important data structure. In programming, we often use trees to organize data. Tree traversal is also a common operation. The following two algorithms are provided. The previous one simply traverses all the nodes, and the next one can return the corresponding nodes based on the input conditions.
/** Function: traverse all nodes of ctreectrl * parameter: Tree-The ctreectrl control variable to be traversed and the root node of the hitem-start variable (if you want to change the entire tree, then: hitem = tree. getrootitem () * return value: void */void travelitem (ctreectrl & tree, htreeitem hitem) {htreeitem hcuritem = tree. getchilditem (hitem); htreeitem hnextitem; while (hcuritem) {outputdebugstring (tree. getitemtext (hcuritem); // output node text hnextitem = hcuritem; travelitem (tree, hnextitem); hcuritem = tree. getnextsiblingitem (hcuritem ); }/** Function: return the specified Tree node * parameter: Tree-The ctreectrl control variable to be traversed and the root node of the item-start variable (if you want to change the entire tree, then: hitem = tree. getrootitem (), strtext-itemtext value of the node (return condition) * return value: Specify the handle of the node (if there is no node that meets the search condition, return NULL) */htreeitem finditem (ctreectrl & tree, htreeitem item, cstring strtext) {htreeitem hfind; If (item = NULL) return NULL; while (item! = NULL) {If (tree. getitemtext (item) = strtext) return item; If (tree. itemhaschildren (item) {item = tree. getchilditem (item); hfind = finditem (tree, item, strtext); If (hfind) {return hfind;} else {item = tree. getnextsiblingitem (tree. getparentitem (item) ;}} else {item = tree. getnextsiblingitem (item); If (item = NULL) return NULL;} return item ;}