Hello. I have a problem with my code.
I have implemented BST Tree as well as DHT but my code doesn't work.
Here it is:
import java.util.Scanner; public class JavaApplication18 { public static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int n = scan.nextInt(); DHT dictionary = new DHT(n); String s = scan.next(); int m = 0; while(!"#".equals(s)) { int p = hash(s, n); dictionary.insert(s, p); m++; } System.out.println(m); int k = scan.nextInt(); String function; for(int j = 0; j < k; j++) { function = scan.next(); if(function.charAt(0) == 'S') { String info = s.substring(7); int y = hash(info, n) - 1; System.out.println(info + ": " + dictionary.search(info, y)); } else if(function.charAt(0) == 'I') { String info = s.substring(7); int y = hash(info, n) - 1; System.out.println(info + ": " + dictionary.insert(info, y)); } else { String info = s.substring(7); int y = hash(info, n) - 1; System.out.println(info + ": " + dictionary.delete(info, y)); } } } public static class Word { public String info; public Word left; public Word right; public int hMany; public int val; Word(String value, int k) { info = value; left = right = null; hMany = 1; val = k; } } public static class BST { protected Word root; public BST() { root = null; } protected int insert(String x, int k) { System.out.print("kkk"); Word s, p = root, prev; s = new Word(x, k); boolean t = false; if(root == null) { root = s; } else { prev = null; while(p != null) { prev = p; if(s.val == p.val && x.equals(p.info)) { p.hMany++; t = true; break; } else if(s.val < p.val) { p = p.left; } else { p = p.right; } } if(t == false && s.val < p.val) { prev.left = s; } else if(t == false) { prev.right = s; } } if(t) { return p.hMany; } else { return s.hMany; } } protected int remove(String x, int t) { Word p = root; Word ds = null; int lp = 0; while(p != null) // finding parent { if(t < p.left.val) { p = p.left; } else if(p.right.val == t && p.right.info.equals(x)) { ds = p.right; lp = 1; break; } else if(p.left.val == t && p.left.info.equals(x)) { ds = p.left; lp = 2; break; } else { p = p.right; } } boolean g = false; if(ds.hMany > 1) // deleting { ds.hMany--; } else { g = true; if(lp == 2) { while(true) { if(ds.left == null && ds.right == null) { p.left = null; break; } else if(ds.left != null && ds.right == null) { p.left = ds.left; break; } else if(ds.right != null && ds.left == null) { p.left = ds.right; break; } else { Word temp = ds.right; while(temp.left != null && temp.left.left != null) { temp = temp.left; } ds.info = temp.left.info; p = temp; ds = temp.left; } } } else { while(true) { if(ds.left == null && ds.right == null) { p.right = null; break; } else if(ds.left != null && ds.right == null) { p.right = ds.left; break; } else if(ds.right != null && ds.left == null) { p.right = ds.right; break; } else { Word temp = ds.right; while(temp.left != null && temp.left.left != null) { temp = temp.left; } ds.info = temp.left.info; p = temp; ds = temp.left; } } } } if(g) { return 0 ; } else { return ds.hMany; } } private int find(String x, int t) { Word p = root; while(p != null && t != p.val) { if(t < p.val) { p = p.left; } else if(t == p.val && x.equals(p.info)) { break; } else { p = p.right; } } return p.hMany; } } public static class DHT { public BST[] bst; DHT(int n) { bst = new BST[n]; for(int i = 0; i < n; i++) { bst[i] = new BST(); } } public int insert(String x, int k) { return bst[k].insert(x, k); } public int search(String x, int k) { return bst[k].find(x, k); } public int delete(String x, int k) { return bst[k].remove(x, k); } } public static int hash(String key, int l) { int value = 0; int len = key.length() - 1; for(int j = 0; j < key.length(); j++ ) { value += toDig(key.charAt(j)) * Math.pow(53, len); len--; } value = value % l; return value; } private static int toDig(char a) { int value = 0; if(Character.isLowerCase(a)) { value = a - ('a' - 1); } else if(Character.isUpperCase(a)) { value = a - ('A' - 27); } else if(a == ' ') { value = 53; } return value; } }
It takes:
1. The length of the DHT
2. Words separated with spaces and finished with #
3. The number of operations on the dictionary
4. Operations (SEARCH, DELETE, INSERT)
While typing:
7
a b c d e f g h i j k a a b w # // this is the place where my program crashes
6
SEARCH a
INSERT z
DELETE a
SEARCH x
DELETE b
DELETE w
A "wild" exception appears:
Exception in thread "main" java.lang.NullPointerException
at javaapplication17.JavaApplication17$DHT.insert(Jav aApplication17.java:249)
It should return:
n= 15
a: 3
z: 1
a: 2
x: 0
b: 1
w: 0
I will appreciate any help.
Thanks.