-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTernaryTreeDictionaryFind.java
99 lines (97 loc) · 2.55 KB
/
TernaryTreeDictionaryFind.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
package pree;
import java.*;
class Node{
char val;
boolean isEnd;
Node left, right, middle;
Node(){
val=0;
left=right=middle=null;
isEnd=false;
}
Node(char value){
val=value;
left=right=middle=null;
isEnd=false;
}
};
public class TernaryTree {
static Node root;
TernaryTree(){
root=new Node();
root.val=0;
root.left=root.right=root.middle=null;
root.isEnd = false;
}
public static Node insertWord(Node n1, char[] word, int ptr){
if(n1==null){
n1 = new Node(word[ptr]);
if(ptr+1 <word.length){
n1.middle=insertWord(n1.middle,word, ptr+1);
}
}
if(word[ptr]<n1.val){
n1.left=insertWord(n1.left,word,ptr);
}
else if(word[ptr]>n1.val){
n1.right=insertWord(n1.right,word,ptr);
}
else{
if(ptr+1 < word.length){
insertWord(n1.middle,word,ptr+1);
}
else{
n1.isEnd=true;
}
}
return n1;
};
public static boolean findWord(Node n1, char[] word,int ptr){
if(n1==null) return false;
if(n1.isEnd) return true;
else{
if(word[ptr]==n1.val){
if(ptr+1 < word.length){
return(findWord(n1.middle,word,ptr+1));
}
else return true;
}
else if(word[ptr]<n1.val){
return(findWord(n1.left,word,ptr));
}
else if(word[ptr]>n1.val){
return(findWord(n1.right,word,ptr));
}
else return false;
}
}
/*
public static void printDictionaryWord(Node nDict,char[] word,int pos){
if(nDict==null){
System.out.println("No matching Dictionary words were found");
return;
}
else{
if(nDict.val==word[pos]){
printDictionaryWord(nDict,word,pos++);
}
else if(nDict.val != word[pos]){
if()
}
}
System.out.println("");
}
*/
public static void main(String args[]){
Node n = null;
char[] word1 = "krishna".toCharArray();
char[] word2 = "vijayapuri".toCharArray();
char[] word3 = "vijy".toCharArray();
n=insertWord(n,word1,0);
n=insertWord(n,word2,0);
boolean wordIsPresent = findWord(n, word2,0);
System.out.println("vijayapuri word is present: "+wordIsPresent);
wordIsPresent = findWord(n, word3,0);
System.out.println("Sri word is present: "+wordIsPresent);
}
};