首页 > 代码库 > Ternary Tree
Ternary Tree
前一篇文章介绍了Trie树。它实现简单但空间效率低。假设要支持26个英文字母,每一个节点就要保存26个指针,因为节点数组中保存的空指针占用了太多内存。让我来看看Ternary Tree。
When you have to store a set of strings, what data structure should you use?
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,结合了字典树的时间效率和二叉搜索树的空间效率长处。
为了避免多余的指针占用内存,每一个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.
// 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 = http://www.mamicode.com/data;"hljs-number">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:
Following is traversal of ternary search tree
bug
cat
cats
up
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.
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.
Ternary Tree