-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathternary_trees.java
149 lines (131 loc) · 4.57 KB
/
ternary_trees.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
public class ternary_trees {
//insert
// search
// delete
// traverse
// prefix_check
// prefix_print
static class trieNode
{
char data;
boolean eow=true;
trieNode left,eq,right;
trieNode(char data)
{
this.data=data;
eow=false;
left=right=eq=null;
}
}
static trieNode insert(trieNode root,String word,int i)
{
if(root==null) root=new trieNode(word.charAt(i));
if(word.charAt(i)<root.data) root.left=insert(root.left,word,i);
else if(word.charAt(i)>root.data) root.right=insert(root.right,word,i);
else
{
if(i+1<word.length()) root.eq=insert(root.eq,word,i+1);
else root.eow=true;
}
return root;
}
public static boolean search(trieNode root,String word,int i)
{
if(root==null) return false;
if(word.charAt(i)<root.data) return search(root.left,word,i);
if(word.charAt(i)>root.data) return search(root.right,word,i);
if(i+1<word.length()) return search(root.eq,word,i+1);
return root.eow;
}
public static trieNode delete(trieNode root,String word,int i)
{
if(root==null) return root;
char ch=word.charAt(i);
int n=word.length();
if(ch<root.data) root.left = delete(root.left,word,i);
else if(ch>root.data) root.right = delete(root.right,word,i);
else if(ch==root.data)
{
if(i==n-1)
{
if(root.eow==false) return root;
//if(root.eow==true) root.eow=false;
root.eow=false;
}
else root.eq=delete(root.eq,word,i+1);
if(root.eow==false)
{
if(root.eq==null)
{
if(root.left==null) root=root.right;
else if(root.right==null) root=root.left;
else
{
root=root.left;
root.left=null;
}
}
}
}
return root;
}
public static void deleteNode(trieNode root,String word,int i)
{
if(!search(root,word,0)) return;
else delete(root,word,0);
}
public static void traverse(trieNode root,String s1,String prefix)
{
if(root==null) return;
if(root.eow==true) System.out.println(prefix+s1+root.data);
traverse(root.left,s1,prefix);
traverse(root.eq,s1+root.data,prefix);
traverse(root.right,s1,prefix);
}
public static boolean startwith(trieNode root,String prefix,int i)
{
if(root==null) return false;
char ch=prefix.charAt(i);
if(ch<root.data) return startwith(root.left,prefix,i);
if(ch>root.data) return startwith(root.right,prefix,i);
if(i<prefix.length()-1) return startwith(root.eq,prefix,i+1);
if(i==prefix.length()-1) return true;
return false;
//return startwith(root.eq,prefix,i+1);
}
public static void prefix_print(trieNode root,String prefix,int i)
{
if(root==null) return;
if(prefix.charAt(i)<root.data) prefix_print(root.left,prefix,i);
else if(prefix.charAt(i)>root.data) prefix_print(root.right,prefix,i);
else if(i<prefix.length()-1) prefix_print(root.eq,prefix,i+1);
else traverse(root.eq,"",prefix);
}
public static void main(String[] args)
{
trieNode root=null;
//String s[]={"Prakriti","Ragh","Raghav","Rashi","Sunidhi"};
String s[]={"bugs","cat","cats","up"};
//String s[]={"dc","ba","eb"};
for(int i=0;i<s.length;i++)
{
root=insert(root,s[i],0);
}
for(int i=0;i<s.length;i++) System.out.println(s[i]+" is present?? --> "+search(root,s[i],0));
System.out.println("Prakri is present?? --> "+search(root,"Prakri",0));
System.out.println("pfix "+startwith(root,"Ra",0));
System.out.println();
traverse(root,"","");
System.out.println();
//prefix_print(root,"Ra",0);
prefix_print(root,"ca",0);
System.out.println("------------------------------------------------");
for(int i=0;i<s.length;i++)
{
System.out.println("-----"+s[i]+" deleted");
deleteNode(root,s[i],0);
traverse(root,"","");
System.out.println();
}
}
}