Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions hw_01/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>1</groupId>
<artifactId>hw_01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>hw_01</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<source>1.8</source>
<target>1.8</target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
107 changes: 107 additions & 0 deletions hw_01/src/main/java/hw_01/Bor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package hw_01;

public class Bor implements Trie {
final private Node bor;
private int size = 0;

public Bor() {
bor = new Node();
}

public boolean add(String element) {
if (contains(element)) {
return false;
}

Node p = bor;

p.nwords++;
for (int i = 0; i < element.length(); i++) {
int currIdx = arrNum(element.charAt(i));

if (p.lifes[currIdx] == null) {
p.lifes[currIdx] = new Node();
size++;
}
p = p.lifes[currIdx];
p.nwords++;
}

p.isTerminal = true;
return true;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

странное форматирование, табы/пробелы?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вроде тут только табы.


public boolean contains(String element) {
Node p = bor;
for (int i = 0; i < element.length(); i++) {
int currIdx = arrNum(element.charAt(i));

if (p.lifes[currIdx] == null) {
return false;
}
p = p.lifes[currIdx];
}

return p.isTerminal;
}

public boolean remove(String element) {
if (!contains(element)) {
return false;
}

Node p = bor;
p.nwords--;
for (int i = 0; i < element.length(); i++) {
int currIdx = arrNum(element.charAt(i));

if (p.lifes[currIdx].nwords == 1) {
size -= element.length() - i;
p.lifes[currIdx] = null;
return true;
}
p = p.lifes[currIdx];
p.nwords--;
}

p.isTerminal = false;

return true;
}

public int size() {
return size;
}

public int howManyStartsWithPrefix(String prefix) {
Node p = bor;
for (int i = 0; i < prefix.length(); i++) {
int currIdx = arrNum(prefix.charAt(i));

if (p.lifes[currIdx] == null) {
return 0;
}
p = p.lifes[currIdx];
}

return p.nwords;
}

private int arrNum(char a) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

на буквах экономить не стоит, особенно если они помогают понимать код, что верно почти всегда

if ('a' <= a && a <= 'z') {
return a - 'a';
}

if ('A' <= a && a <= 'Z') {
return a - 'A' + 'z' - 'a' + 1;
}

return -1;
}

private class Node {
public boolean isTerminal;
public int nwords;
Node[] lifes = new Node[2*('z' - 'a' + 1)];
}
}
32 changes: 32 additions & 0 deletions hw_01/src/main/java/hw_01/Trie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package hw_01;

public interface Trie {

/**
* Expected complexity: O(|element|)
* @return <tt>true</tt> if this set did not already contain the specified
* element
*/
boolean add(String element);

/**
* Expected complexity: O(|element|)
*/
boolean contains(String element);

/**
* Expected complexity: O(|element|)
* @return <tt>true</tt> if this set contained the specified element
*/
boolean remove(String element);

/**
* Expected complexity: O(1)
*/
int size();

/**
* Expected complexity: O(|prefix|)
*/
int howManyStartsWithPrefix(String prefix);
}
116 changes: 116 additions & 0 deletions hw_01/src/test/java/hw_01/TrieImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package hw_01;

import junit.framework.TestCase;

public class TrieImplTest extends TestCase {
private Bor b;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно бор создавать в каждом тестовом методе, будет чуть длиньше файл, зато "чище"


public void setUp() {
b = new Bor();
}

public void testAddSizeContains() {
assertFalse(b.contains("Hello"));
assertTrue(b.add("Hello"));
assertTrue(b.contains("Hello"));
assertEquals(5, b.size());
assertEquals(1, b.howManyStartsWithPrefix("Hell"));
}

public void testAddContainsPrefix() {
b.add("Hello");
assertFalse(b.contains("Hell"));
assertTrue(b.add("Hell"));
assertTrue(b.contains("Hell"));
assertEquals(5, b.size());
assertEquals(2, b.howManyStartsWithPrefix("Hell"));
}

public void testAddOutstandingWord() {
b.add("Hello");
assertTrue(b.add("Head"));
assertEquals(7, b.size());
}

public void testRemovePrefix() {
b.add("Hello");
b.add("Hell");
assertFalse(b.remove("He"));
assertTrue(b.remove("Hell"));
assertEquals(5, b.size());
assertEquals(1, b.howManyStartsWithPrefix("Hell"));
}

public void testTemoveSuffix() {
b.add("Hello");
b.add("Hell");
assertTrue(b.remove("Hello"));
assertEquals(4, b.size());
}
/*
public void testBorBigTest() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ненужный код лучше удалять, чтобы он не мозолил глаза
в крайнем случае, он останется в истории системы контроля версий

//b = new Bor();
//Добавление
assertFalse(b.contains("Hello"));
assertTrue(b.add("Hello"));
assertTrue(b.contains("Hello"));
assertEquals(5, b.size());
assertFalse(b.add("Hello"));
assertEquals(5, b.size());
assertFalse(b.contains("Hell"));
assertTrue(b.add("Hell"));
assertTrue(b.contains("Hell"));
assertEquals(5, b.size());
assertFalse(b.add("Hell"));
assertEquals(5, b.size());
assertFalse(b.contains("Head"));
assertTrue(b.add("Head"));
assertTrue(b.contains("Head"));
assertEquals(7, b.size());
assertFalse(b.contains(""));
assertTrue(b.add(""));
assertTrue(b.contains(""));
assertEquals(7, b.size());

//Проверка префиксов
assertEquals(3, b.howManyStartsWithPrefix("H"));
assertEquals(4, b.howManyStartsWithPrefix(""));
assertEquals(1, b.howManyStartsWithPrefix("Hea"));
assertEquals(2, b.howManyStartsWithPrefix("Hell"));
assertEquals(0, b.howManyStartsWithPrefix("h"));

//Удаление
assertFalse(b.remove("h"));
assertFalse(b.remove("H"));
assertTrue(b.remove("Hell"));
assertFalse(b.remove("Hell"));
assertFalse(b.contains("Hell"));
assertEquals(7, b.size());
assertEquals(1, b.howManyStartsWithPrefix("Hell"));

assertTrue(b.remove(""));
assertFalse(b.remove(""));
assertEquals(7, b.size());
assertEquals(2, b.howManyStartsWithPrefix(""));

assertTrue(b.remove("Hello"));
assertFalse(b.remove("Hello"));
assertEquals(4, b.size());

assertTrue(b.contains("Head"));
assertTrue(b.add("HeadZzZ"));
assertEquals(7, b.size());
assertEquals(2, b.howManyStartsWithPrefix("Head"));
assertEquals(1, b.howManyStartsWithPrefix("HeadZzZ"));
assertFalse(b.remove("HeadZ"));
assertTrue(b.remove("HeadZzZ"));
assertEquals(4, b.size());
assertEquals(1, b.howManyStartsWithPrefix("Head"));
assertEquals(1, b.howManyStartsWithPrefix(""));
assertEquals(0, b.howManyStartsWithPrefix("HeadZ"));

assertTrue(b.remove("Head"));
assertEquals(0, b.size());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тесты стоит представлять в виде наименее возможных тестовых случаев, чтобы их можно было читать, понять, что проверено, а что нет

}
*/
}