Ternary Search TreeIntroduction:Ternary Search Tree is a trie data structure commonly used as a low-memory alternative to trie in various applications such as spell check and looking for nearby neighbors. Ternary Search Trees (TST) stand as a sophisticated and efficient data structure that combines the best of binary search trees and trie structures. Introduced by Jon Bentley and Robert Sedgewick in their seminal paper "Ternary Search Trees" in 1998. To comprehend the ternary search tree, we must first grasp the concept of the trie. A trie, also known as a digital tree, radix tree, or prefix tree, is a type of search tree, an ordered tree data structure used to hold a dynamic set or associative array using strings as keys. Structure of Ternary Search Trees:A Ternary Search Tree is essentially a type of trie where each node has three children instead of the traditional two in binary trees. The nodes are designed to store individual characters from keys in a sorted order. This unique structure enables efficient searching, insertion, and deletion operations. 1. Node Structure:
2. Balanced Structure:
Let's consider an example to illustrate the structure of a Ternary Search Tree: In this example, the TST represents the strings "CAT," "ME," "MET," "MEX," "METAL," and "METEOR." Each node in the tree corresponds to a character in these strings. Representation of Ternary Search Trees:In the trie (standard) data structure, each node contains 26 pointers for its children, but in a ternary search tree, each node contains only 3 pointers:
Aside from the three-pointers mentioned above, each node contains a field for indicating data (character in the case of a dictionary) and a field for indicating the end of a string. So, it's similar to BST, which saves data in a specific order. On the other hand, data in a ternary search tree is dispersed among the nodes. For example, you required the four nodes to store the term "Bird". One advantage of ternary search trees over tries is that ternary search trees take up less space (only three-pointers per node compared to 26 in standard tries). In addition, ternary search trees may be utilized in any situation where a hash table is used to hold strings. Tries are appropriate when there is the correct distribution of words throughout the alphabets, allowing for the most effective use of space. Ternary search trees are preferable when the strings to be stored all have the same prefix; ternary search trees are the most economical in terms of space. Operations on Ternary Search Trees:1. Insertion: The insertion operation involves traversing the tree, creating new nodes as needed to accommodate the characters of the key. The process ensures that each node is inserted in a sorted order, maintaining the inherent structure of the TST. 2. Search: The search operation involves traversing the tree based on the characters of the key. At each step, the algorithm compares the current character with the character stored in the current node. The search is successful when the entire key is traversed, and the corresponding data is found. 3. Deletion: Deletion in TSTs is a complex operation involving recursive traversal and restructuring to maintain the tree's balance. Care must be taken to handle various cases, such as nodes with one, two, or three children. Properties:
Implementation:Explanation:
Program Output: Java CodeNow let's write a Java program to implement a ternary search tree. Output The above Java code gives the following output. Ternary Search Tree Test Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert red Ternary Search Tree : [red] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert blur e Ternary Search Tree : [blue, red] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert green Ternary Search Tree : [blue, green, red] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert yellow Ternary Search Tree : [blue, green, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert pink Ternary Search Tree : [blue, green, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert black Ternary Search Tree : [black, blue, green, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert grey Ternary Search Tree : [black, blue, green, grey, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert mustard Ternary Search Tree : [black, blue, green, grey, mustard, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert White Ternary Search Tree : [White, black, blue, green, grey, mustard, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert purple Ternary Search Tree : [White, black, blue, green, grey, mustard, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 2 Enter the word to search black Search result: true Ternary Search Tree : [White, black, blue, green, grey, mustard, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 3 Enter the word to delete blue mustard Ternary Search Tree : [White, black, blue, green, grey, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 4 Empty Status: false Ternary Search Tree : [White, black, blue, green, grey, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 3 Enter the word to delete black Ternary Search Tree : [White, blue, green, grey, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 5 Ternary Search Tree cleared Ternary Search Tree : [] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Search Tree is empty. 5. To make the Ternary Search Tree empty. 4 Empty Status: true Ternary Search Tree : [] Do you want to continue (Type y or n) n In the above Java code, we have implemented the ternary search tree data structure and various functions like adding new data in the ternary search tree, deleting the existing data from the ternary search tree, to check whether the ternary search tree is empty or not, there is also one operation to empty the tree that will delete all the content within the ternary search tree and the traversal of the ternary search tree. For all these operations of the ternary search tree, there are different functions written, and then according to the need of the operation on the ternary search tree data structure, those functions are called. C++ CodeLet's write the C++ code for the ternary search tree. Output The above C++ code gives the following output. Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert orange Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert banana Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert grapes Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert apple Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert watermelon Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert guava Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert Kiwi Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 3 Contents of the Ternary Search Tree are:: Kiwi apple Banana Watermelon guava grapes orange Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 2 Enter the word to search Kiwi Kiwi found in Ternary Search Tree Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 2 Enter the word to search mango mango not found in Ternary Search Tree Do you want to continue (Type y or n) n In the above C++ code, we have implemented the ternary search tree data structure and various functions like adding new data, deleting the existing data, and the traversal of the ternary search tree. For all these operations of the ternary search tree, there are different functions written, and then according to the need of the operation on the ternary search tree data structure, those functions are called. Real-World Applications:1. Spell Checking: TSTs are widely used in spell-checking algorithms to efficiently search for and suggest correct spellings. 2. IP Routing: Ternary Search Trees are employed in IP routing tables to quickly locate the longest matching prefix for a given IP address. 3. Auto-Completion: TSTs power auto-completion features in search engines and text editors, providing quick and accurate suggestions. 4. Symbol Tables: TSTs serve as a foundation for efficient symbol tables, aiding in the quick retrieval and modification of symbols. Next TopicStock Span Problem |