Skip to content

Commit

Permalink
complete model
Browse files Browse the repository at this point in the history
  • Loading branch information
felixnguyen678 committed Apr 16, 2021
1 parent a68fbc1 commit 09ca653
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 81 deletions.
6 changes: 3 additions & 3 deletions src/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class App {
public static void start() throws IOException {
// repository
Repository repository = Repository.getInstance();
repository.reset("slang.txt");
System.out.println(repository.randomSlang().getSlang());

System.out.println(
repository.findBySlang(new Slang("BHD")).getMeanings()
);


}
public static void main(String[] args) throws IOException {
start();

}
}
97 changes: 58 additions & 39 deletions src/Model/AVLTree.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Model;

import java.util.Random;

/*
An AVL tree is a sorted binary tree in which the heights of two subtrees at any
given node differ by at most 1
Expand All @@ -21,20 +23,25 @@
*/
@SuppressWarnings("Duplicates")
public class AVLTree<E extends Comparable<E>> {
public AVLNode<E> root;
private AVLNode<E> root;

public E getRoot(){
return root.elem;
}
private int height(AVLNode<E> n) {
return n == null ? -1 : n.height;
}

private int max(int a, int b) {
return a > b ? a : b;
}

private int getBalance(AVLNode<E> n) {
return n == null ? 0 : height(n.right) - height(n.left);
}
AVLNode<E> rotateRight(AVLNode<E> y) {
private void updateHeight(AVLNode<E> node){
node.height = 1 + max(height(node.left), height(node.right));
}
private AVLNode<E> rotateRight(AVLNode<E> y) {
AVLNode<E> x = y.left;
AVLNode<E> z = x.right;
x.right = y;
Expand All @@ -43,7 +50,7 @@ AVLNode<E> rotateRight(AVLNode<E> y) {
updateHeight(x);
return x;
}
AVLNode<E> rotateLeft(AVLNode<E> y) {
private AVLNode<E> rotateLeft(AVLNode<E> y) {
AVLNode<E> x = y.right;
AVLNode<E> z = x.left;
x.left = y;
Expand All @@ -52,7 +59,7 @@ AVLNode<E> rotateLeft(AVLNode<E> y) {
updateHeight(x);
return x;
}
AVLNode<E> rebalance(AVLNode<E> z) {
private AVLNode<E> rebalance(AVLNode<E> z) {
updateHeight(z);
int balance = getBalance(z);
if (balance > 1) {
Expand All @@ -72,7 +79,7 @@ AVLNode<E> rebalance(AVLNode<E> z) {
}
return z;
}
AVLNode<E> insertHelper(AVLNode<E> node, E elem) {
private AVLNode<E> insertHelper(AVLNode<E> node, E elem) {
if (node == null) {
return new AVLNode<E>(elem);
} else if (node.elem.compareTo(elem) > 0) {
Expand All @@ -85,21 +92,42 @@ AVLNode<E> insertHelper(AVLNode<E> node, E elem) {
}
return rebalance(node);
}
private AVLNode<E> mostLeftChild(AVLNode<E> node) {
while (node.right != null) {
node = node.right;
}
return node;
}
private AVLNode<E> findHelper(AVLNode<E> n, E elem) {
// usual search
while (n != null) {
// pass right subtree as new tree
if (n.elem.compareTo(elem) > 0)
n = n.left;

// pass left subtree as new tree
else if (n.elem.compareTo(elem) < 0)
n = n.right;
else
return n; // if the key is found return 1
}
return new AVLNode<>(null);
}

AVLNode<E> delete(AVLNode<E> node, E elem) {
private AVLNode<E> deleteHelper(AVLNode<E> node, E elem) {
if (node == null) {
return node;
} else if (node.elem.compareTo(elem) > 0) {
node.left = delete(node.left, elem);
node.left = deleteHelper(node.left, elem);
} else if (node.elem.compareTo(elem) < 0) {
node.right = delete(node.right, elem);
node.right = deleteHelper(node.right, elem);
} else {
if (node.left == null || node.right == null) {
node = (node.left == null) ? node.right : node.left;
} else {
AVLNode<E> mostLeftChild = mostLeftChild(node.right);
node.elem = mostLeftChild.elem;
node.right = delete(node.right, node.elem);
node.right = deleteHelper(node.right, node.elem);
}
}
if (node != null) {
Expand All @@ -109,58 +137,49 @@ AVLNode<E> delete(AVLNode<E> node, E elem) {
}



void updateHeight(AVLNode<E> node){
node.height = 1 + max(height(node.left), height(node.right));
}

// insert element
public void insert(E elem) {
if (elem == null) throw new IllegalArgumentException();
root = insertHelper(root, elem);
}



private AVLNode<E> mostLeftChild(AVLNode<E> node) {
while (node.right != null) {
node = node.right;
}
return node;
}

// search element
public E find(E elem) {
if (elem == null) throw new IllegalArgumentException();

return findHelper(root, elem).elem;
}

private AVLNode<E> findHelper(AVLNode<E> n, E elem) {
// usual search
while (n != null) {
// pass right subtree as new tree
if (n.elem.compareTo(elem) > 0)
n = n.left;
public void delete(E elem){
root = deleteHelper(root, elem);
}

// pass left subtree as new tree
else if (n.elem.compareTo(elem) < 0)
n = n.right;
else
return n; // if the key is found return 1
}
return new AVLNode<>(null);
public AVLNode<E> randomNodeHelper(AVLNode<E> node){
Random rn = new Random();
int randInt = rn.nextInt(100);
if(node == null)
return randomNodeHelper(root);
if(randInt % 7 == 0 )
return node;
if(randInt % 2 == 0)
return randomNodeHelper(node.left);
else return randomNodeHelper(node.right);
}


public E randomNode(){
return randomNodeHelper(root).elem;
}

private class AVLNode<E> {
private int height;
private E elem;
private AVLNode<E> left, right;
public int height;
public E elem;
public AVLNode<E> left, right;

public AVLNode(E elem) {
this.elem = elem;
}
}

}
8 changes: 6 additions & 2 deletions src/Model/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public class Keyword implements Comparable<Keyword> {
public Keyword(String _keyword){
this.keyword = _keyword;
}
public Keyword(String _keyword, ArrayList<Slang> _slangs){
this.keyword = _keyword;
this.slangs = _slangs;
}
public void addSlang(Slang _slang){
slangs.add(_slang);
}
Expand All @@ -25,8 +29,8 @@ public ArrayList<Slang> getSlangs() {

@Override
public int compareTo(Keyword _keyword) {
return this.keyword.compareTo(_keyword.keyword);
return this.keyword.toLowerCase().compareTo(_keyword.keyword.toLowerCase());
}


}
}
107 changes: 70 additions & 37 deletions src/Model/Repository.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package Model;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.security.Key;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class Repository {
public AVLTree<Slang> slangAVLTree;
private AVLTree<Slang> slangAVLTree;
private AVLTree<Keyword> keywordAVLTree;
private ArrayList<Slang> slangHistory;
private ArrayList<Keyword> keywordHistory;
Expand All @@ -23,7 +22,7 @@ private Repository(){
public static Repository getInstance(){ return instance;}



//---- CRUD modules
// get history
public ArrayList<Slang> getSlangHistory(){
return slangHistory;
Expand All @@ -37,52 +36,82 @@ public void addSlangHistory(Slang _slang){
public void addKeywordHistory(Keyword _keyword) { keywordHistory.add(_keyword); };
// find by slang
public Slang findBySlang(Slang req){
Slang res = slangAVLTree.find(req);

if(res == null){ // if there are not any slang, construct new Slang and put into history and return null
//System.out.println("This slang does not exist");
req.addMeaning("This slang does not exist");
slangHistory.add(req);
return req;
} else{ //else put into history and return slang
slangHistory.add(res);
return res;
}
return slangAVLTree.find(req);
}
// find by Keyword
public ArrayList<Slang> findByKeyword(Keyword req){
Keyword res = keywordAVLTree.find(req);

if(res != null) {
ArrayList<Slang> res_slang_set = new ArrayList<Slang>();
Slang res_slang = new Slang(
"There are not any slang like this",
"");
req.addSlang(res_slang);
keywordHistory.add(req);
return res_slang_set;
} else{
keywordHistory.add(res);
return res.getSlangs();
}
public Keyword findByKeyword(Keyword req){
return keywordAVLTree.find(req);
}
// split key words
// add slang and keyword
public void addSlang(Slang _slang){
// add to slang tree and keyword tree both
slangAVLTree.insert(_slang);
//slangAVLTree.insert( new Slang("sdf", "sdf"));
/*
for(String item : _slang.meanings){
String[] kw = item.split("//W+");
for(String i : _slang.getMeanings()){// add keyword to keywordAVL tree
String[] split = i.split("\\W+");
for(String j : split){
Keyword keyword = null;
if((keyword = keywordAVLTree.find(new Keyword(j))) != null){
keyword.addSlang(_slang);
} else {
keyword = new Keyword(j);
keyword.addSlang(_slang);
keywordAVLTree.insert(keyword);
}

}
}
*/
}
public void deleteSlang(Slang _slang){

Slang search = slangAVLTree.find(_slang);
if(search != null){
ArrayList<String> split1 = search.getMeanings();
for(String i: split1){
String[] split2 = i.split("\\W+");
for(String j : split2) {


Keyword keyword = keywordAVLTree.find(new Keyword(j));
if(keyword != null) {
int loc = 0;
ArrayList<Slang> slangArrayList = keyword.getSlangs();
for (int t = 0; t < slangArrayList.size(); t++) {
if (keyword.getSlangs().get(t).compareTo(_slang) == 0) {
loc = t;
}
}
slangArrayList.remove(loc);
Keyword renewedKeyword = new Keyword(j, slangArrayList);
keywordAVLTree.delete(renewedKeyword);
keywordAVLTree.insert(renewedKeyword);
}
}
}
}

slangAVLTree.delete(_slang);



}
// overwrite ( delete and insert)
public void overwriteSlang(Slang _slang){
Slang search = slangAVLTree.find(_slang);
if(search != null){
deleteSlang(search);
search = slangAVLTree.find(search);
if(search == null) System.out.println("ys");
addSlang(_slang);
}
}
public void duplicateSlang(Slang _slang){
Slang search = slangAVLTree.find(_slang);
search.addMeaning(_slang.getMeanings().get(0));
overwriteSlang(search);
}


//----- IO Modules -----
//import data
public void import_data(String _filepath) throws IOException {
System.out.println("importing");
Expand All @@ -91,7 +120,8 @@ public void import_data(String _filepath) throws IOException {

while((line = bufferedReader.readLine())!= null){
String[] _split = line.split("`");
addSlang(new Slang(_split[0], _split[1]));
Slang slang = new Slang(_split[0], _split[1]);
addSlang(slang);
}
System.out.println("successfully imported");
}
Expand All @@ -104,4 +134,7 @@ public void reset(String _filepath) throws IOException {
import_data(_filepath);

}
public Slang randomSlang(){
return slangAVLTree.randomNode();
}
}

0 comments on commit 09ca653

Please sign in to comment.