Last active
July 14, 2022 18:47
-
-
Save evgeniyworkbel/98c3911782045765c9089c394ebe3271 to your computer and use it in GitHub Desktop.
Курс: "Введение в ООП" (Хекслет)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Двоичное дерево — иерархическая структура данных, в которой каждый узел имеет не более двух потомков (детей). Как правило, первый называется родительским узлом, а дети называются левым и правым наследниками. | |
В данном испытании мы будем использовать подвид двоичного дерева — двоичное дерево поиска. Правильное дерево не содержит повторяющихся ключей, и для каждого узла гарантируется, что в левом поддереве все значения меньше текущего, а в правом — больше. | |
Двоичное дерево поиска | |
Node.js | |
Реализуйте и экспортируйте по умолчанию класс, который реализует представление узла. | |
Класс должен содержать: | |
Геттер getKey() — возвращает ключ. | |
Геттеры getLeft(), getRight() — возвращают соответственно левого и правого ребёнка. Если ребёнок в узле отсутствует, геттер возвращает null. | |
Метод insert(key) — выполняет добавление узла, формируя правильное двоичное дерево. | |
Примеры | |
const tree = new Node(); | |
tree.insert(9); | |
tree.insert(17); | |
tree.insert(4); | |
tree.insert(3); | |
tree.insert(6); | |
tree.getKey(); // 9 | |
tree.getLeft().getKey(); // 4 | |
tree.getRight().getKey(); // 17 | |
tree.getLeft().getLeft().getKey(); // 3 | |
tree.getLeft().getRight().getKey(); // 6 | |
Подсказки | |
Двоичное дерево (вики) | |
Двоичное дерево поиска (вики) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment