-
Notifications
You must be signed in to change notification settings - Fork 103
Lesson 42 (tree) task1 for review #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: for-pr
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| package com.walking.lesson42_tree.task1; | ||
|
|
||
| import com.walking.lesson42_tree.task1.model.Task; | ||
| import com.walking.lesson42_tree.task1.structure.BinarySearchTree; | ||
| import com.walking.lesson42_tree.task1.structure.BinarySearchTree_v2; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Реализуйте бинарное дерево поиска. Учтите возможность использования дерева как для Comparable-сущностей, | ||
| * так и для сортировки на основе компаратора. | ||
|
|
@@ -15,5 +22,91 @@ | |
| */ | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Comparator<Task> taskNameComparator = new Comparator<Task>() { | ||
| @Override | ||
| public int compare(Task o1, Task o2) { | ||
| return o1.getName().compareTo(o2.getName()); | ||
| } | ||
| }; | ||
|
|
||
| BinarySearchTree<Task> binarySearchTree = new BinarySearchTree<>(); | ||
|
|
||
| List<Task> tasks = getMiddleList(); | ||
|
|
||
| for (Task task : tasks) { | ||
| binarySearchTree.put(task); | ||
| } | ||
|
|
||
| printTreeOverview(binarySearchTree); | ||
|
|
||
| Task foundTask = binarySearchTree.get(tasks.get(0)); | ||
| System.out.println("Найдена задача: " + foundTask); | ||
|
|
||
| Task removedTask = binarySearchTree.remove(tasks.get(0)); | ||
| System.out.println("Извлечена задача:" + removedTask); | ||
|
|
||
| printTreeOverview(binarySearchTree); | ||
|
|
||
| binarySearchTree.balance(); | ||
|
|
||
| printTreeOverview(binarySearchTree); | ||
|
|
||
| foundTask = binarySearchTree.get(tasks.get(0)); | ||
| System.out.println("Найдена задача: " + foundTask); | ||
| } | ||
|
|
||
| public static List<Task> getAscendingList() { | ||
| Task task0 = new Task("05"); | ||
| Task task1 = new Task("10"); | ||
| Task task2 = new Task("15"); | ||
| Task task3 = new Task("19"); | ||
| Task task4 = new Task("20"); | ||
| Task task5 = new Task("21"); | ||
| Task task6 = new Task("25"); | ||
| Task task7 = new Task("30"); | ||
| Task task8 = new Task("35"); | ||
|
|
||
| return List.of(task0, task1, task2, task3, task4, task5, task6, task7, task8); | ||
| } | ||
|
|
||
| public static List<Task> getMiddleList() { | ||
| Task task0 = new Task("20"); | ||
| Task task1 = new Task("15"); | ||
| Task task2 = new Task("10"); | ||
| Task task3 = new Task("05"); | ||
| Task task4 = new Task("19"); | ||
| Task task5 = new Task("30"); | ||
| Task task6 = new Task("25"); | ||
| Task task7 = new Task("21"); | ||
| Task task8 = new Task("35"); | ||
|
|
||
| return List.of(task0, task1, task2, task3, task4, task5, task6, task7, task8); | ||
| } | ||
|
|
||
| public static void print(List<?> list) { | ||
| for (Object object : list) { | ||
| System.out.print(object); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как будто мало чем отличается от sout(list) в таком виде |
||
| } | ||
|
|
||
| System.out.println(); | ||
| } | ||
|
|
||
| public static void printTreeOverview(BinarySearchTree<?> tree) { | ||
| System.out.println(".".repeat(80)); | ||
|
|
||
| System.out.println("Корень дерева: " + tree.getRoot()); | ||
|
|
||
| System.out.println("Содержимое дерева в порядке возрастания ширины (BFS):"); | ||
| tree.breadthFirstSearchTraversal(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. никогда раньше не задумывался, как будет обход в ширину на английском. Но тут по смыслу тогда должно быть printBreadthFirstSearch() |
||
| System.out.println(); | ||
|
|
||
| System.out.println("Содержимое дерева в порядке возрастания значений (DFS:inOrder):"); | ||
| List<?> ascendingOrder = tree.getAscendingOrder(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вот такой вариант лучше, чем sout внутри класса дерева. Пусть даже в случае с обходом в ширину возвращался бы |
||
| print(ascendingOrder); | ||
|
|
||
| System.out.println("min = " + tree.getMin()); | ||
| System.out.println("max = " + tree.getMax()); | ||
|
|
||
| System.out.println(".".repeat(80)); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.walking.lesson42_tree.task1.model; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Task implements Comparable<Task> { | ||
| private final String name; | ||
|
|
||
| public Task(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[%s]".formatted(name); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| Task task = (Task) o; | ||
|
|
||
| return Objects.equals(name, task.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return name != null ? name.hashCode() : 0; | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Task o) { | ||
| return name.compareTo(o.getName()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
методы, наверно, приватные должны быть