Search Radix search trie (RST) R-way trie (RT) De la Briandias trie (DLB)
Binary search tree (BST) Left branch is less than Right branch is larger than Create a tree with 0, 1, 2, 3 (in order)
Create BST << 1 << 2 << Can we do better?
Radix search trie Using a pair instead of only Value (BST) Key is parsed along the tree edges Value is stored at a node Assume each Value is linked with only one Key Create a tree with 0, 1, 2, 3 (in order)
Create RST Value Key Binary representation Worst case bounds by binary representation length (log n), not by n as in BST Can we apply to string? “binary” or 2-way tree each node links to 2 children Consider a lower case alphabet string: each node (character) links to (followed by) 26 children (characters) 26-way tree
R-way trie (lecture example) she, sells, sea, shells, by, the, sea, shore (Keys are on nodes, not on edges) How can we indicate “she” is a complete word? Using a flag variable in each node? true false Worst case bounds by the character length of the string
Create R-way trie shells, she Special node (starting of any string) s s h e l l s false h e l l s true Is this approach good?
The ugly truth she, sea s false h e 1 link for letter “h” 1 link for letter “e” a c ? ? 1 node = 26 links + 1 flag variable The same prefix? Impossible combinations? Can we do it better?
De la Briandais (DLB) Replace the fixed link array by a flexible linked list s ab cd ef ghij kl mn opqrst uv wx yz s eh next Linked-list head
Trade off Save a lot of space, especially when the real case has sparse strings Increase searching time. Why? R-way trie: Directly go to a child in the array DLB: linearly go the child in the linked list s eh
Create a DLB shells, she, sea, seat s s h e l l s false h e l l s true e a false true s e a t t
Delete a word in DLB s h e l l s false true e a false true t If the path does not belong to other words, remove. Otherwise, leave it alone. shells Change to false no children: delete False, no children delete False, no children delete A true node stop sea Change to false, has a child stop false
Exercises (on paper) R-way: Implement only the insert operation Charset: ’a’ – ‘z’; no recursive function call DLB: Substitute some part in R-way algorithm Define ListNode; Node *findNode(ListNode *head, char ch); Node *createAndInsertNode(ListNode &*head, char ch);
Exercises (on paper) R-way: struct Node { int val; Node *child[26]; }; int charToIndex(char ch) { return ch – ‘a’; } void insert(Node *root, string key, int val) { struct Node *ptr = root; for char: ch in key { if (ptr->child[charToIndex(ch)] == NULL) { ptr->child[charToIndex(ch)] = createNewNode(); } ptr = ptr->child[charToIndex(ch)]; } ptr->val = val; }
Exercises (on paper) DLB: struct Node { int val; ListNode *head; }; void insert(Node *root, string key, int val) { struct Node *ptr = root; for char: ch in key { struct Node *f_res = findNode(ptr->head, ch); if (f_res == NULL) { f_res = createAndInsertNode(ptr->head, ch); } ptr = f_res; } ptr->val = val; } 1. Define ListNode; 2. Node *findNode(ListNode *head, char ch); 3. Node * createAndInsertNode(ListNode &*head, char ch);
Exercises (on paper) R-way: Implement only the insert operation Charset: ’a’ – ‘z’; no recursive function call DLB: Substitute some part in R-way algorithm Define ListNode; ListNode *findChar(ListNode *head, char ch); ListNode *createAndInsertNode(ListNode *head, char ch); int charToIndex(char ch) { return ch – ‘a’; }
Exercises (on paper) RST: Create a tree for: 2, 3, 4, 6 Delete values: 4, 6 DLB: Create a tree for: baby, bad, bank, box, dad, dance Delete words: bad, bank, dance
Exercises RST (creation)
Exercises RST (deletion)
Exercise DLB (creation) b o x a b y n k d d a d n c e true
Exercise DLB (deletion) b o x a b y d a d true