How to implement a three-state decision tree -- Focus on tree traversal

Source: Internet
Author: User
Sometimes we often need to implement tree multi-state selection. This article introduces the specific implementation of a three-state selection tree.
Step 1: generate a dialog box project.
Step 2: Add a tree control and set the required properties.
Step 3: Create a three-state selection box icon and initialize the tree in oninitdialog () to associate the tree control with the image list control consisting of the Selection box icon. For details, see Source code .
Step 4: Implement left-click message of the tree to implement three-state selection.
Void cmutitreedemodlg: onclicktree1 (nmhdr * pnmhdr, lresult * presult) {uint uflags = 0; htreeitem hpitem; cpoint point = getcurrentmessage ()-> pt; screentoclient (& Point ); point. offset (-8,-16); htreeitem hitem = m_tripletree.hittest (point, & uflags); If (uflags & tvht_onitem) {hpitem = m_tripletree.getparentitem (hitem); int m, n, M1, N1; m_tripletree.getitemimage (hitem, m, n); If (M! = 1) {m_tripletree.setitemimage (hitem, 1,1); m_tripletree.getitemimage (hitem, M1, N1); travelchild (hitem, M1); If (hpitem! = NULL) travelsiblingandparent (hitem, M1);} else if (M = 1) {items (hitem,); m_tripletree.getitemimage (hitem, M1, N1); travelchild (hitem, m1); If (hpitem! = NULL) travelsiblingandparent (hitem, M1);} // MessageBox ("XXXX");} * presult = 0 ;}

Among them, the most difficult thing to achieve is tree traversal. I use recursion
Travelchild (htreeitem hitem, int M1 );
Travelsiblingandparent (htreeitem hitem, int M1.

CodeAs follows:

// Recursive void cmutitreedemodlg: travelchild (htreeitem hitem, int m) {htreeitem hchilditem, hbrotheritem; hchilditem = values (hitem); values (hchilditem, M, M ); if (hchilditem! = NULL) travelchild (hchilditem, m); hbrotheritem = m_tripletree.getnextsiblingitem (hchilditem); m_tripletree.setitemimage (hbrotheritem, M, m); If (hbrotheritem! = NULL) travelchild (hbrotheritem, m);} void cmutitreedemodlg: travelsiblingandparent (htreeitem hitem, int m) {int M1, N1; htreeitem identifier, identifier, hparentitem; M1 = m; hnextsiblingitem = m_tripletree.getnextsiblingitem (hitem); While (hnextsiblingitem! = NULL) {m_tripletree.getitemimage (hnextsiblingitem, M1, N1); If (M1! = M) break; else {hnextsiblingitem = m_tripletree.getnextsiblingitem (hnextsiblingitem); If (hnextsiblingitem! = NULL) m_tripletree.getitemimage (hnextsiblingitem, M1, N1) ;}} hprevsiblingitem = m_tripletree.getprevsiblingitem (hitem); While (hprevsiblingitem! = NULL) {m_tripletree.getitemimage (hprevsiblingitem, M1, N1); If (M1! = M) break; else {hprevsiblingitem = m_tripletree.getprevsiblingitem (hprevsiblingitem); If (hprevsiblingitem! = NULL) m_tripletree.getitemimage (hprevsiblingitem, M1, N1) ;}}if (M1 = m) {hparentitem = m_tripletree.getparentitem (hitem); If (hparentitem! = NULL) {m_tripletree.setitemimage (hparentitem, M1, M1); travelsiblingandparent (hparentitem, M1) ;}} if (M1! = M) {hparentitem = m_tripletree.getparentitem (hitem); While (hparentitem! = NULL) {m_tripletree.setitemimage (hparentitem,); hparentitem = m_tripletree.getparentitem (hparentitem );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.