Skip to content

Conversation

@DmKazakov
Copy link
Owner

No description provided.

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>ru,spbau.mit.kazakov.MyTreeSet</groupId>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Из-за запятой после ru ругается на неправильный groupId и не собирается из консоли

* therefore generic type must implements Comparable interface.
*/
public MyTreeSet() {
comparator = (o1, o2) -> ((Comparable<E>) o1).compareTo(o2);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Имеет смысл заглушить предупреждение, //noinspection unchecked

*/
@Override
public boolean contains(@NotNull Object value) {
return find((E) value) != null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Тут как раз идея в том, что значение, которое мы ищем contains, может вообще не быть типа E, просто компаратор должен говорить, что значения равны. То есть find по идее тоже должен принимать Object и не делать никаких предположений о типах сравниваемых объектов. Хотя из-за стирания работать должно и так.

Copy link
Owner Author

Choose a reason for hiding this comment

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

@yurii-litvinov но компаратор же у меня такой: Comparator<? super E> comparator, т.е. ему не получится подсунуть Object. Или тут нужно не спускаться по дереву, а работать за линию, проверяя на equals все элементы?

/**
* Node for binary search tree. Stores left, right children and value.
*/
private class Node {
Copy link
Collaborator

Choose a reason for hiding this comment

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

static? Параметр-тип, правда, придётся писать явно, но оно того стоит обычно.

Copy link
Owner Author

Choose a reason for hiding this comment

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

@yurii-litvinov Я правильно понимаю, что в таком варианте, везде, где я пишу node.value, мне нужно дописывать каст к E?

}

E prev = next;
next = higher(next);
Copy link
Collaborator

Choose a reason for hiding this comment

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

next() за примерно логарифм? Можно же за линейное время.

TreeSet<Integer> random = new TreeSet<>();

for (int i = 0; i < 100; i++) {
Integer valueToAdd = ThreadLocalRandom.current().nextInt(0, 30);
Copy link
Collaborator

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.

@yurii-litvinov а если я буду использовать какой-нибудь сид?

mySet.remove('t');

assertArrayEquals(new Character[]{'v', 'z'}, mySet.toArray());
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Судя по coverage, нет теста на самый интересный случай удаления --- когда у удаляемого узла есть оба сына (причём, тестить надо на случай, когда они нетривиальные поддеревья, с этим часто бывают проблемы).

next = higher(next);
return prev;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Кстати, было бы круто, если бы итератор инвалидировался при модификации дерева.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants