-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathTernarySearchTree.java
106 lines (92 loc) · 3.89 KB
/
TernarySearchTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Ternary Search Tree (TST) implementation in Java
import java.util.Scanner; // Import the Scanner class
// The algorithm of the Ternary Search Tree (TST)
//1.) Create a TST node class with character data, left, middle, and right pointers, and an end-of-word flag.
//2.) Implement an insert() function that recursively inserts characters into the TST based on comparisons.
//3.) Implement a search() function that recursively searches for a string in the TST by comparing characters.
//4.) Initialize an empty TST and use the insert() function to add strings to the TST.
//5.) Use the search() function to check if a string exists in the TST.
// Main class
public class TernarySearchTree {
// Node class represents a node in the TST
static class Node {
char data; // The character stored in the node
boolean isEndOfWord; // Flag to indicate if the node represents the end of a word
Node left, middle, right; // Pointers to the left, middle, and right child nodes
// Constructor to initialize the node
Node(char data) {
this.data = data;
this.isEndOfWord = false;
this.left = this.middle = this.right = null;
}
}
// Ternary Search Tree class
static class ternarySearchTree {
Node root; // Root node of the TST
// Method to insert a string into the TST
void insert(String word) {
root = insert(root, word, 0);
}
// Recursive method to insert a character into the TST
Node insert(Node node, String word, int index) {
// If the current node is null, create a new node
if (node == null) {
node = new Node(word.charAt(index));
}
// Compare the current character with the character in the node
if (word.charAt(index) < node.data) {
node.left = insert(node.left, word, index);
} else if (word.charAt(index) > node.data) {
node.right = insert(node.right, word, index);
} else {
// If the characters match, move to the next character
if (index + 1 < word.length()) {
node.middle = insert(node.middle, word, index + 1);
} else {
// Mark the node as the end of a word
node.isEndOfWord = true;
}
}
return node;
}
// Method to search for a string in the TST
boolean search(String word) {
return search(root, word, 0);
}
// Recursive method to search for a character in the TST
boolean search(Node node, String word, int index) {
// If the current node is null, the word does not exist in the TST
if (node == null) {
return false;
}
// Compare the current character with the character in the node
if (word.charAt(index) < node.data) {
return search(node.left, word, index);
} else if (word.charAt(index) > node.data) {
return search(node.right, word, index);
} else {
// If the characters match, move to the next character
if (index + 1 == word.length()) {
// Check if the node represents the end of a word
return node.isEndOfWord;
} else {
return search(node.middle, word, index + 1);
}
}
}
}
// Main method
public static void main(String[] args) {
ternarySearchTree tst = new ternarySearchTree();
// Insert some words into the TST
tst.insert("apple");
tst.insert("banana");
tst.insert("carrot");
// Search for words in the TST
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter string");
String s = myObj.nextLine();
if(tst.search(s) == true){System.out.println(s + " is present");}// Output: true (exists)
else {System.out.println(s + " is not present");} // Output: false (does not exist)
}
}