Questions about deleting redundant nodes in the dictionary tree

Source: Internet
Author: User
# Include <cstdio> # include <cstring> # include <algorithm> # include <queue> # include <string> # include <iostream> using namespace STD; struct node {int CNT; // number of records; struct node * Next [26]; node () {CNT = 0; memset (next, null, sizeof (next ));}}; void buildtrie (node * root, string s) //; {node * P = root; node * TMP = NULL; int L = S. size (); For (INT I = 0; I <L; I ++) {If (p-> next [s [I]-'a'] = NULL) {TMP = new node; P-> next [s [I]-'a '] = TMP;} p = p-> next [s [I]-'a'];} p-> CNT ++;} void findtrie (node * root, string s) // query {node * P = root; int L = S. size (); For (INT I = 0; I <L; I ++) {P = p-> next [s [I]-'a'];} printf ("% d \ n", p-> cnt-1);} void del (node * root) // Why the root value is always the value passed by the main function, this value is also used for recursion and is only executed once. This is the result of my debugging, but the code for deleting the node is like this. Yes, but I don't know why. I don't understand debugging either. I can explain it to me. /// Thank you very much {for (INT I = 0; I <26; I ++) if (root-> next [I]) del (root-> next [I]); Delete (Root);} int main () {int N; string S; node root; scanf ("% d", & N); While (n --) {CIN> S; sort (S. begin (), S. end (); buildtrie (& root, S); findtrie (& root, S); del (& root);} return 0 ;}

 

Questions about deleting redundant nodes in the dictionary tree

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.