|
| 1 | +package proj1; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +class node |
| 5 | +{ |
| 6 | + node lc,rc; |
| 7 | + String name; |
| 8 | + String meaning; |
| 9 | + int h; |
| 10 | + public node(String name,String meaning) //parameterized constructor of class node |
| 11 | + { |
| 12 | + this.name=name.toLowerCase(); |
| 13 | + this.meaning=meaning; |
| 14 | + //lc=rc=null; |
| 15 | + h=1; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class AVL |
| 20 | +{ |
| 21 | + Scanner sc = new Scanner (System.in); |
| 22 | + private node root; |
| 23 | + public AVL() |
| 24 | + { |
| 25 | + root=null; |
| 26 | + } |
| 27 | + |
| 28 | + int height(node root) // height to calculate balance factor of tree |
| 29 | + { |
| 30 | + int lh, rh; |
| 31 | + if(root == null) |
| 32 | + return 0; |
| 33 | + if(root.lc == null) |
| 34 | + lh = 0; |
| 35 | + else |
| 36 | + lh = 1 + root.lc.h; |
| 37 | + if(root.rc == null) |
| 38 | + rh = 0; |
| 39 | + else |
| 40 | + rh = 1 + root.rc.h; |
| 41 | + if(lh > rh) |
| 42 | + return lh; |
| 43 | + else |
| 44 | + return rh; |
| 45 | + } |
| 46 | + |
| 47 | + int balanceFactor(node root) |
| 48 | + {int bf, lh, rh; |
| 49 | + if(root == null) |
| 50 | + return 0; |
| 51 | + if(root.lc == null) |
| 52 | + lh = 0; |
| 53 | + else |
| 54 | + lh = 1 + height(root.lc); |
| 55 | + if(root.rc == null) |
| 56 | + rh = 0; |
| 57 | + else |
| 58 | + rh = 1 + height(root.rc); |
| 59 | + bf = lh - rh; |
| 60 | + return bf; |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | +node LL(node ptr) |
| 65 | + { |
| 66 | + //Right Rotation |
| 67 | + node tmp = root.lc; |
| 68 | + root.lc = tmp.rc; |
| 69 | + tmp.rc = root; |
| 70 | + tmp.h = height(tmp); |
| 71 | + root.h = height(root); |
| 72 | + return tmp; |
| 73 | + } |
| 74 | + |
| 75 | + node RR(node ptr) |
| 76 | + { |
| 77 | + //Left Rotation |
| 78 | + node tmp = root.rc; |
| 79 | + root.rc = tmp.lc; |
| 80 | + tmp.lc = root; |
| 81 | + tmp.h = height(tmp); |
| 82 | + root.h = height(root); |
| 83 | + return tmp; |
| 84 | + } |
| 85 | + |
| 86 | + node LR(node root) |
| 87 | + { |
| 88 | + root.lc=RR(root.lc); |
| 89 | + root=LL(root); |
| 90 | + return root; |
| 91 | + } |
| 92 | + |
| 93 | + node RL(node root) |
| 94 | + { |
| 95 | + root.rc=LL(root.rc); |
| 96 | + root=RR(root); |
| 97 | + |
| 98 | + return root; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + node insert(node root, node temp){ |
| 103 | + int bf; |
| 104 | + if(root == null){ |
| 105 | + root = new node(temp.name, temp.meaning); |
| 106 | + return root; |
| 107 | + } |
| 108 | + if(temp.name.compareTo(root.name) < 0){ |
| 109 | + root.lc = insert(root.lc, temp); |
| 110 | + bf = balanceFactor(root); |
| 111 | + if(bf == 2){ |
| 112 | + if(temp.name.compareToIgnoreCase(root.lc.name) < 0) |
| 113 | + root = LL(root); |
| 114 | + else |
| 115 | + root = LR(root); |
| 116 | + } |
| 117 | + } |
| 118 | + else{ //cn.compareToIgnoreCase(root.customerName) > 0 |
| 119 | + root.rc = insert(root.rc, temp); |
| 120 | + bf = balanceFactor(root); |
| 121 | + if(bf == -2){ |
| 122 | + if(temp.name.compareToIgnoreCase(root.rc.name) > 0) |
| 123 | + root = RR(root); |
| 124 | + else |
| 125 | + root = RL(root); |
| 126 | + } |
| 127 | + } |
| 128 | + root.h = height(root); |
| 129 | + return root; |
| 130 | + } |
| 131 | + |
| 132 | + void create(String Name,String mean) //Accept general information form customer |
| 133 | + { |
| 134 | + |
| 135 | + node temp=new node(Name,mean); |
| 136 | + root=insert(root,temp); |
| 137 | + |
| 138 | + |
| 139 | + } |
| 140 | + void display(node localRoot) // Inorder traversal |
| 141 | + { |
| 142 | + if(localRoot != null){ |
| 143 | + display(localRoot.lc); |
| 144 | + System.out.println(localRoot.name+" -"+localRoot.meaning); |
| 145 | + display(localRoot.rc); |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + node getRoot() {//utility function for restricted access |
| 150 | + return root; |
| 151 | + } |
| 152 | + void findWord() {//juhi |
| 153 | + System.out.print("\nEnter word : "); |
| 154 | + String target=sc.nextLine().toLowerCase(); |
| 155 | + node current=root; |
| 156 | + while(current!=null) { |
| 157 | + int comparison=target.compareTo(current.name); |
| 158 | + if(comparison==0) { |
| 159 | + System.out.println("\nWord : "+current.name.toUpperCase()+"\t\t-\t\tMeaning : "+current.meaning); |
| 160 | + return; |
| 161 | + } |
| 162 | + else if(comparison<0) { |
| 163 | + current=current.lc; |
| 164 | + } |
| 165 | + else {//comparison>0 |
| 166 | + current=current.rc; |
| 167 | + } |
| 168 | + } |
| 169 | + System.out.println("\nWord not found! Please be more specific."); |
| 170 | + } |
| 171 | + int displayWordsAt(node head,String i,int t) //juhi |
| 172 | + { |
| 173 | + if (head != null) |
| 174 | + { if(head.name.startsWith(i)) { |
| 175 | + t++; |
| 176 | + System.out.println("Word : "+head.name+"\t\t-\t\tMeaning : "+head.meaning); |
| 177 | + } |
| 178 | + t=displayWordsAt(head.lc, i,t); |
| 179 | + t=displayWordsAt(head.rc, i,t); |
| 180 | + return t; |
| 181 | + } |
| 182 | + return t; |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + int totalWordsCount(node r) {//vaishnav |
| 189 | + if (r == null) { |
| 190 | + return 0; |
| 191 | + } |
| 192 | + else |
| 193 | + { |
| 194 | + int l = 1; |
| 195 | + l += totalWordsCount(r.lc); |
| 196 | + l += totalWordsCount(r.rc); |
| 197 | + return l; |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + } |
| 202 | + int wordsCountAt(node rr,char j) {//vaishnav |
| 203 | + if (rr == null) { |
| 204 | + return 0; |
| 205 | + } |
| 206 | + else |
| 207 | + { |
| 208 | + int l = 1; |
| 209 | + l += wordsCountAt(rr.lc,j); |
| 210 | + l += wordsCountAt(rr.rc,j); |
| 211 | + return l; |
| 212 | + } |
| 213 | + |
| 214 | + |
| 215 | +} |
| 216 | + int wordCountAt(char j) {//srushti |
| 217 | + int count=0; |
| 218 | + |
| 219 | + node ptr=root; |
| 220 | + while(ptr!=null ){ |
| 221 | + if(ptr.name.charAt(0)==j){ |
| 222 | + |
| 223 | + count++; |
| 224 | + } |
| 225 | + char c=ptr.name.charAt(0); |
| 226 | + if(Character.compare(j, c)<0) |
| 227 | + ptr=ptr.lc; |
| 228 | + else |
| 229 | + ptr=ptr.rc; |
| 230 | + } |
| 231 | + return count; |
| 232 | + } |
| 233 | + void wordStartsWithVowel() {//juhi |
| 234 | + System.out.println("\nStarts with Vowel : 'a' \n"); |
| 235 | + displayWordsAt(root,"a",0); |
| 236 | + System.out.println("\nStarts with Vowel : 'e' \n"); |
| 237 | + displayWordsAt(root,"e",0); |
| 238 | + System.out.println("\nStarts with Vowel : 'i' \n"); |
| 239 | + displayWordsAt(root,"i",0); |
| 240 | + System.out.println("\nStarts with Vowel : 'o' \n"); |
| 241 | + displayWordsAt(root,"o",0); |
| 242 | + System.out.println("\nStarts with Vowel : 'u' \n"); |
| 243 | + displayWordsAt(root,"u",0); |
| 244 | + } |
| 245 | + void wordCountStartsWithVowel() {//juhi |
| 246 | + int t=0; |
| 247 | + { |
| 248 | + int c= wordCountAt('a'); |
| 249 | + System.out.println("Total no. of words starting with vowel : 'a' are - "+c); |
| 250 | + t=t+c; |
| 251 | + } |
| 252 | + { |
| 253 | + int c= wordCountAt('e'); |
| 254 | + System.out.println("Total no. of words starting with vowel : 'e' are - "+c); |
| 255 | + t=t+c; |
| 256 | + } |
| 257 | + { |
| 258 | + int c= wordCountAt('i'); |
| 259 | + System.out.println("Total no. of words starting with vowel : 'i' are - "+c); |
| 260 | + t=t+c; |
| 261 | + } |
| 262 | + { |
| 263 | + int c= wordCountAt('o'); |
| 264 | + System.out.println("Total no. of words starting with vowel : 'o' are - "+c); |
| 265 | + t=t+c; |
| 266 | + } |
| 267 | + { |
| 268 | + int c= wordCountAt('u'); |
| 269 | + System.out.println("Total no. of words starting with vowel : 'u' are - "+c); |
| 270 | + t=t+c; |
| 271 | + } |
| 272 | + System.out.println("\nTotal no. of words starting with vowels are : "+t); |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +public class Main{ |
| 277 | + |
| 278 | + public static void main(String[] args) { |
| 279 | + AVL avl=new AVL(); |
| 280 | + Scanner sc=new Scanner(System.in); |
| 281 | + avl.create("brb","Be right back"); |
| 282 | + avl.create("btw","By the way"); |
| 283 | + avl.create("ama", "Ask Me Anything"); |
| 284 | + avl.create("lmk","Let me know"); |
| 285 | + avl.create("gtg","Got to go"); |
| 286 | + avl.create("dm", "Direct Message"); |
| 287 | + avl.create("idk", "I don't know"); |
| 288 | + |
| 289 | + avl.create("rofl","Rolling on floor laughing"); |
| 290 | + avl.create("stfu", "Shut the *swear word!* up"); |
| 291 | + //avl.create("icymi", "In case you missed it"); |
| 292 | + avl.create("tl","Too long" ); |
| 293 | + avl.create("ikr", "I know right"); |
| 294 | + //avl.create("dr", "Didn�t read"); |
| 295 | + //avl.create("nvm", "Nevermind"); |
| 296 | + //avl.create("tgif","Thank goodness it�s Friday"); |
| 297 | + //avl.create("tbh","To be honest"); |
| 298 | + //avl.create("tbf", "To be frank"); |
| 299 | + avl.create("rn", "Right now"); |
| 300 | + avl.create("qotd","Quote of the day"); |
| 301 | + avl.create("ootd","Outfit of the day"); |
| 302 | + //avl.create("lol","Laugh out loud"); |
| 303 | + avl.create("ttyl", "Talk to you later"); |
| 304 | + //avl.create("hit me up"," Hit me up"); |
| 305 | + //avl.create("fwiw", "For what it�s worth"); |
| 306 | + //avl.create("imo", "In my opinion"); |
| 307 | + //avl.create("imho", "In my humble opinion"); |
| 308 | + //avl.create("idk","I don�t know"); |
| 309 | + avl.create("tba", "To be announced"); |
| 310 | + //avl.create("tbd", "To be decided"); |
| 311 | + |
| 312 | + int ch; |
| 313 | + do{ |
| 314 | + System.out.println("**************************** Menu ********************************"); |
| 315 | + System.out.println("1.Find a Word"); |
| 316 | + System.out.println("2.Display words starting with given letter"); |
| 317 | + System.out.println("3.Total no. of words in dictionary"); |
| 318 | + System.out.println("4.Total no. of words starting with given letter"); |
| 319 | + System.out.println("5.Display all words"); |
| 320 | + System.out.println("6.Display Words starting with vowels");//words at |
| 321 | + System.out.println("7.Total no. of words starting with vowels"); |
| 322 | + System.out.println("8.Exit"); |
| 323 | + System.out.println("******************************************************************"); |
| 324 | + System.out.println("Enter your choice : "); |
| 325 | + ch=sc.nextInt(); |
| 326 | + switch(ch) |
| 327 | + { |
| 328 | + case 1: |
| 329 | + |
| 330 | + avl.findWord(); |
| 331 | + break; |
| 332 | + case 2: |
| 333 | + System.out.print("\nEnter the starting letter of the words you want to find : "); |
| 334 | + String c=sc.next(); |
| 335 | + if(c.length()!=1) { |
| 336 | + System.out.println("\nEnter a single letter!"); |
| 337 | + break; |
| 338 | + } |
| 339 | + else { |
| 340 | + int j=0; |
| 341 | + if(avl.displayWordsAt(avl.getRoot(),c,j)==0) |
| 342 | + System.out.println("No word starts with the letter '"+c+"'"); |
| 343 | + break; |
| 344 | + } |
| 345 | + case 3: System.out.println("\nTotal no. of words in the dictionary are : "+avl.totalWordsCount(avl.getRoot())); |
| 346 | + break; |
| 347 | + case 4: System.out.print("\nEnter the starting letter of the words you want to find : "); |
| 348 | + String b=sc.next(); |
| 349 | + if(b.length()!=1) { |
| 350 | + System.out.println("\nEnter a single letter!"); |
| 351 | + break; |
| 352 | + } |
| 353 | + else { |
| 354 | + System.out.println(avl.wordCountAt(b.toLowerCase().charAt(0))); |
| 355 | + break; |
| 356 | + } |
| 357 | + case 5: |
| 358 | + avl.display(avl.getRoot()); |
| 359 | + break; |
| 360 | + case 6: |
| 361 | + avl.wordStartsWithVowel(); |
| 362 | + break; |
| 363 | + case 7: |
| 364 | + avl.wordCountStartsWithVowel(); |
| 365 | + break; |
| 366 | + case 8: System.out.println("Program ended"); |
| 367 | + |
| 368 | + break; |
| 369 | + default: System.out.println("Invalid option"); |
| 370 | + |
| 371 | + } |
| 372 | + } while(ch != 8); |
| 373 | + |
| 374 | + } |
| 375 | + |
| 376 | +} |
0 commit comments