Ternary Tree

  前一篇文章介紹了Trie樹。它實現簡單但空間效率低。假設要支持26個英文字母,每個節點就要保存26個指針,由於節點數組中保存的空指針佔用了太多內存。讓我來看看Ternary Tree。
  When you have to store a set of strings, what data structure should you use?node

You could use hash tables, which sprinkle the strings throughout an array. Access is fast, but information about relative order is lost. Another option is the use of binary search trees, which store strings in order, and are fairly fast. Or you could use digital search tries, which are lightning fast, but use lots of space.
  In this article, we’ll examine ternary search trees, which combine the time efficiency of digital tries with the space efficiency of binary search trees. The resulting structure is faster than hashing for many typical search problems, and supports a broader range of useful problems and operations. Ternary searches are faster than hashing and more powerful, too.
  三叉搜索樹Ternary Tree,結合了字典樹的時間效率和二叉搜索樹的空間效率長處。git

爲了不多餘的指針佔用內存,每個Trie節點再也不用數組來表示,而是表示成「樹中有樹」。Trie節點裏每個非空指針都會在三叉搜索樹裏獲得屬於它本身的節點。
  Each node has 3 children: smaller (left), equal (middle), larger (right).

  Follow links corresponding to each character in the key.
   ・If less, take left link; if greater, take right link.
   ・If equal, take the middle link and move to the next key character.
  Search hit. Node where search ends has a non-null value.
  Search miss. Reach a null link or node where search ends has null value.


數組

markdown

// C program to demonstrate Ternary Search Tree (TST) insert, travese // and search operations #include <stdio.h> #include <stdlib.h> #define MAX 50 // A node of ternary search tree struct Node { char data; // True if this character is last character of one of the words unsigned isEndOfString: 1; struct Node *left, *eq, *right; }; // A utility function to create a new ternary search tree node struct Node* newNode(char data) { struct Node* temp = (struct Node*) malloc(sizeof( struct Node )); temp->data = data; temp->isEndOfString = 0; temp->left = temp->eq = temp->right = NULL; return temp; } // Function to insert a new word in a Ternary Search Tree void insert(struct Node** root, char *word) { // Base Case: Tree is empty if (!(*root)) *root = newNode(*word); // If current character of word is smaller than root's character, // then insert this word in left subtree of root if ((*word) < (*root)->data) insert(&( (*root)->left ), word); // If current character of word is greate than root's character, // then insert this word in right subtree of root else if ((*word) > (*root)->data) insert(&( (*root)->right ), word); // If current character of word is same as root's character, else { if (*(word+1)) insert(&( (*root)->eq ), word+1); // the last character of the word else (*root)->isEndOfString = 1; } } // A recursive function to traverse Ternary Search Tree void traverseTSTUtil(struct Node* root, char* buffer, int depth) { if (root) { // First traverse the left subtree traverseTSTUtil(root->left, buffer, depth); // Store the character of this node buffer[depth] = root->data; if (root->isEndOfString) { buffer[depth+1] = '\0'; printf( "%s\n", buffer); } // Traverse the subtree using equal pointer (middle subtree) traverseTSTUtil(root->eq, buffer, depth + 1); // Finally Traverse the right subtree traverseTSTUtil(root->right, buffer, depth); } } // The main function to traverse a Ternary Search Tree. // It mainly uses traverseTSTUtil() void traverseTST(struct Node* root) { char buffer[MAX]; traverseTSTUtil(root, buffer, 0); } // Function to search a given word in TST int searchTST(struct Node *root, char *word) { if (!root) return 0; if (*word < (root)->data) return searchTST(root->left, word); else if (*word > (root)->data) return searchTST(root->right, word); else { if (*(word+1) == '\0') return root->isEndOfString; return searchTST(root->eq, word+1); } } // Driver program to test above functions int main() { struct Node *root = NULL; insert(&root, "cat"); insert(&root, "cats"); insert(&root, "up"); insert(&root, "bug"); printf("Following is traversal of ternary search tree\n"); traverseTST(root); printf("\nFollowing are search results for cats, bu and cat respectively\n"); searchTST(root, "cats")?

printf("Found\n"): printf("Not Found\n"); searchTST(root, "bu")? printf("Found\n"): printf("Not Found\n"); searchTST(root, "cat")? printf("Found\n"): printf("Not Found\n"); return 0; }

Output:less

Following is traversal of ternary search tree
bug
cat
cats
uppost

Following are search results for cats, bu and cat respectively
Found
Not Found
Found
Time Complexity: The time complexity of the ternary search tree operations is similar to that of binary search tree. i.e. the insertion, deletion and search operations take time proportional to the height of the ternary search tree. The space is proportional to the length of the string to be stored.ui

Hashing.
・Need to examine entire key.
・Search hits and misses cost about the same.
・Performance relies on hash function.
・Does not support ordered symbol table operations.
TSTs.
・Works only for string (or digital) keys.
・Only examines just enough key characters.
・Search miss may involve only a few characters.
・Supports ordered symbol table operations (plus extras!). Red-black BST.
・Performance guarantee: log N key compares.
・Supports ordered symbol table API.
Hash tables.
・Performance guarantee: constant number of probes.
・Requires good hash function for key type.
Tries. R-way, TST.
・Performance guarantee: log N characters accessed.
・Supports character-based operations.this

相關文章
相關標籤/搜索