Advancement of recursive in C ++ applications

zhaozj2021-02-08  279

"Recursive" is mainly solved in C algorithm or data structure, and recursive utilization can greatly simplify the algorithm or data structure, the code is simple and clear, the same, the same, the subject, the subject, the required algorithm, required The predefined and corresponding results will be different, and the recursive may use the reduction part definition, and the code implementation is greatly reduced, and it is known. Below is a comparison of examples from the database:

Data structure (table structure) used in implementation

Serial number English name Chinese name Description 1 ID Permissions ID INT

2 ParentID Parent Permission ID INT Item Terminate the Father Handle 3 Name Permissions Name VARCHAR (32)

4 IDCode menu Id INT Permissions and menu items

As can be seen from the data structure, the hierarchical relationship of the permissions can be described by the tree structure of the ParentID, which is a typical tree-characteristic data structure, and the recursive can simplify the implementation process of the program, but simply passed the experiment. The use of recursive will result in performance in performance, and operational effects cannot satisfy the user's basic operation, and will describe the procedure after implementing the recursive retrieval.

1. Through the memory of the tree nodes to achieve fake recursive

DWORD DWFUNCTION = 0; // Function ID

HtreeItem Hitemlayer [5] [2]; // Used to save the node currently operating, for backtrack

INT NidCollection [5] [2]; // Keep the ID of the parent layer node, used to identify the parent layer of the next node

// Set the tree root

HITEMLAYER [0] [0] = m_treeoperatorPermission.insertItem (_T ("Permissions Settings"), 3, 3);

m_treeopertyMData (Hitemlayer [0] [0], DWFunction);

HITEMLAYER [0] [1] = Hitemlayer [0] [0];

Nidcollection [0] [0] = 0; // Father ID

Nidcollection [0] [1] = 0; // Current layer ID

INT nCurParentlay = 0;

CADORECORDSET Collection (& M_CONN); // ADO object, used to take out record set from the database

CString strsqlstring ("SELECT ID, PARENTID, NAME, IDCODE from TBL_Function Order By ID, ParentID");

Collection.Open (strsqlstring))

{

INT ncount = collection.getRecordcount ();

CString strfunctionname;

For (int i = 0; i

{

/ / Remove node data from the database

Collection.GetfieldValue ("Name", strfunctionName);

int NID;

Int nparentId;

Collection.GetfieldValue ("ID", NID);

Collection.GetfieldValue ("ParentID", NParentID;

DO

{

/ / Decades whether the parent node it retains is consistent, is used to determine if the sub-node is currently inserted or from the parent node

IF (nparentID == nidcollection [ncurparentlay] [0]) {

/ / Insert the sub-node to the parent layer and keep the current node data for backtracking

Hitemlayer [ncurparentlay] [1] = m_treeopertyratorPermission.Insertitem ((lpctstr) StrfunctionName, 0, 1, Hitemlayer [ncurparentlay] [0]);

Nidcollection [ncurparentlay] [1] = NID;

M_TreeoPeratorPermission.sethalfchecked (Hitemlayer [ncurparentlay] [1]);

DWFunction = NID;

M_TreeOperatorPermission.SetItemData (Hitemlayer [ncurparentlay] [1], dwfunction);

}

Else if (nparentId == Nidcollection [ncurparentlay] [1])

{

// Create a sub-layer in the current layer

Hitemlayer [ncurparentlay 1] [1] = m_treeoperatorPermission.InsertItem ((LPCTSTR) StrfunctionName, 0, 1, Hitemlayer [ncurparentlay] [1]);

Hitemlayer [ncurparentlay 1] [0] = HITEMLAYER [NCURPARENTLAY] [1];

Nidcollection [ncurparentlay 1] [0] = nparentID;

Nidcollection [NCURPARENTLAY 1] [1] = NID;

M_treeoperatorPermission.setChecked (Hitemlayer [NCurparentlay 1] [1], FALSE);

DWFunction = NID;

M_TreeOperatorPermission.SetItemData (Hitemlayer [ncurparentlay 1] [1], dwfunction);

NCURPARENTLAY ;

}

Else

{

//, used to find the matching parent node, easy to insert the node

Ncurparentlay -;

CONTINUE;

}

Break;

WHILE (TRUE);

Collection.movenext ();

}

M_TreeOperatorPermission.expand (Hitemlayer [0], TVE_EXPAND);

}

Collection.Close ();

m_treeoperatorpermission.clerallCheck ();

Return 0;

Comments: This method is to achieve recursive phase modifications through a state method, and it can be seen that the programmer must specify its implementation process in detail to make other programmers to read (of course, the comment is originally It should be, what is mentioned here How to make other programs easier to understand the code).

In this program, the path to the current node to the current node is used. It is used to retrore the next node's father's finger. The programmer is a spending machine, making a label in his feet, which makes it back. It is possible to recognize the road, and it is also easy to explore the next road, not repeating the same branch (formation cycle).

Advantages: This program only uses a retrieval statement to implement the initialization of the permissions tree, reducing the number of database connections, thus being optimal in performance, that is, the most important data operation. Disadvantages: The complexity of the code has brought great possibilities for the presence of code hidden dangers, and the data also has a certain requirement to meet the order.

2. Application of recursive algorithm

Long INITDEFAULTPERMISIONTREE (Int NparentID, HTREEITID, HITEM)

{

CString strsqlstring;

strsqlstring.format ("Select ID, Name from TBL_FUNCTION WHERE PARENTID =% D", NParentID;

CadorecordSet Collection; & M_Conn;

Collection.Open (strsqlstring))

{

// Take all data

Carray nidarray;

Carray strnameArray;

INT ncount = collection.getRecordcount ();

For (int i = 0; i

{

int NID;

CString strname;

Collection.GetfieldValue ("ID", NID);

Collection.GetfieldValue ("Name", Strname;

Collection.movenext ();

Nidarray.Add (NID);

StrnameArray.Add (Strname);

}

Collection.Close ();

// Insert the data removed from the database to the tree map

For (i = 0; i

{

Int nid = nidarray.getat (i);

HtreeItem hsonItem = m_treeoperty.insertitem (StrnameArray.getat (i), 0, 0, hItem);

M_TreeOperatorPermission.SetItem, NID;

// Tell the purpose of using M_TReedatamap (CMAP )

M_TreedataMap.Setat (NID, HSONITEM);

/ / Recursively insert sub-node data for the current node

INITDEFAULTPERMISSIONTREE (Nidarray.getat (i), HSonItem);

}

}

Return 0;

}

Comments: Simply look at this procedure, only one loop is completed, the data is completed and displayed (this program uses two loops only want to reduce the number of database connections due to recursive), obvious, code clear and easy to understand. There is no need to have too much annotations to understand the implementation process.

There is no more auxiliary variable in the implementation of the first example to help the memory tree structure, which is completed by the recursive characteristics.

Advantages: It is clear, easy to understand, the biggest feature is to implement the default for its implementation, which is the basic ideological understanding of the recursive procedure, otherwise the programmer absolutely can't think of this algorithm is available for return. .

Disadvantages: The advantage of the first example is, in fact, the shortcomings of this example, recursive generated outbrand stack operations and comparable additional data (such as database connections, etc.) will have a negative impact on the performance of the program. The case is more serious for the case, of course, for the non-tree-style realization algorithm, if the recursive implementation algorithm is adopted, if the accumulation of 1 to 100, although the recursive algorithm can be achieved, it can still use conventional The algorithm is implemented, and the recursive algorithm is not promoted here. Normal algorithm int sum (int nmax) {int NSUM = 0; for (int i = 1; i <= nsum = i;} return nsum;} Recursive algorithm int sum (int nmax) { IF (nmax> 0) {RETURN SUM (nmax - 1) nmax;} else {return 0;}}

In summary, the recursive algorithm should be used to achieve more difficulties in achieving a conventional algorithm, and a recursive feature will use a recursive algorithm, otherwise it is not recommended to apply the recursive algorithm, such as the calculation 1 ~ 100 of the calculations described later. Here is the application of resolute denial of the recursive algorithm.

Writing code should consider multifacence, including code readability, understandability, and simplicity (this feature has certain limitations), and perform performance and other factors.

For a performance requirements, it is not high, but the recursive can increase the readability and understandability of the code and can greatly simplify the implementation process of the code, and recursive will be preferred.

For execution performance requirements, the programmer may be required to replace other similar algorithms to ensure performance priority, but part, and other algorithms are used to replace recursive failure to improve the performance of algorithms, but recursively is the best algorithm (generally Refers to the required recursive levels).

In summary, use recursive, there is also a disadvantage, whether the programmer should adopt a recursive algorithm during the implementation process, should consider whether the recursive algorithm affects the overall requirements of the relevant module or system.

转载请注明原文地址:https://www.9cbs.com/read-1025.html

New Post(0)