Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the trie dictionary tree in C language?

Shulou Source: shulou.com Published: 2022-06-02 11:14:08 10月02日 Update

This article is about what the trie dictionary tree is in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Introduction to Dictionary Tree

The dictionary tree is simply represented by a two-dimensional array. If you want to save more space, the great god's approach must be done with a pointer linked list. For example, if we use the two-dimensional array gtrie [I] [j] = k to represent the dictionary tree, then they have three meanings: iMagijLigue k, which is the core of the dictionary tree. Only by understanding the meaning of iQuery jPowerk, can we better write code to build dictionary trees and use dictionary trees.

I > this can be understood as the root node (all the above eMagin k characters can be thought of as I). If this has no children and there are no branches below, it can be understood as I.

J > j can be understood as the child of I, and his value means the number of children.

K > this, I think, is the total number of characters and the meaning of counting the number of all characters.

Instance code

# include "stdio.h"

# define false (0)

# define true (1)

Int g_trie [100] [100]

Char g_str [100]

Int g_root =

Int g_tot =

Void insert (char * s)

{

Int id =

G_root =

While (* s! ='\ 0')

{

Id = * sails + -'a'

If (gtrie [g _ root] [id] = =)

{

Gtrie [g _ root] [id] = + + g_tot

}

G_root = g _ root [g _ root] [id]

}

}

Int find_str (char * s)

{

G_root =

While (* s! ='\ 0')

{

Int id = * sails + -'a'

If (gtrie [g _ root] [id] = =) return false

G_root = g _ root [g _ root] [id]

}

Return true

}

Void main (void)

{

Int N =

Memset (g_trie, sizeof (int) * 100 * 100)

Scanf (&% d, & N)

For (int I =; I isEndOfWord = false

For (I =; I children [I] = NULL

}

Return pNode

}

/ / If not present, inserts key into trie

/ / If the key is prefix of trie node, just marks leaf node

Void insert (struct TrieNode * root, const char * key)

{

Int level

Int length = strlen (key)

Int index

Struct TrieNode * pCrawl = root

For (level =; level children [index])

PCrawl- > children [index] = getNode ()

PCrawl = pCrawl- > children [index]

}

/ / mark last node as leaf

PCrawl- > isEndOfWord = true

}

/ / Returns true if key presents in trie, else false

Bool search (struct TrieNode * root, const char * key)

{

Int level

Int length = strlen (key)

Int index

Struct TrieNode * pCrawl = root

For (level =; level children [index])

Return false

PCrawl = pCrawl- > children [index]

}

Return (pCrawl! = NULL & & pCrawl- > isEndOfWord)

}

/ / Driver

Int main ()

{

/ / Input keys (use only'a 'through 'z'and lower case)

Char keys [] [8] = {"the", "a", "there", "answer", "any"

"by", "bye", "their"}

Char output [] [32] = {"Not present in trie", "Present in trie"}

Struct TrieNode * root = getNode ()

/ / Construct trie

Int i

For (I =; I

Tags: Dictionary array space code character child meaning language thing content that is more article two-dimensional good practical representative practice writing branch Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Microsoft Docker Shulou Tech Info