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
31 changes: 31 additions & 0 deletions lab04/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>lab04</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
114 changes: 114 additions & 0 deletions lab04/src/main/java/org/example/BSTNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.example;

public class BSTNode {
private Node root;
private class Node {
private int key;
private String val;
private Node left,right;
private int size;

public Node(int key, String val, int initSize) {
this.key = key;
this.val = val;
size = initSize;
}
}

public int getSize() {
return getSize(root);
}
private int getSize(Node node) {
if(node == null)
return 0;
else
return node.size;
}

public void insertValue(int key, String val) {
root = insertValue(root, key, val);
}
private Node insertValue(Node node, int key, String val) {
if (node == null) {
return new Node(key,val,1);
}

if (key > node.key) {
node.right = insertValue(node.right,key,val);
} else if (key < node.key) {
node.left = insertValue(node.left,key,val);
} else {
node.val = val;
}

node.size = getSize(node.left) + getSize(node.right) + 1;

return node;
}

public void deleteValue(int key) {
root = deleteValue(root, key);
}
private Node deleteValue(Node node, int key) {
if (node == null) return null;

if (key > node.key) node.right = deleteValue(node.right, key);
else if (key < node.key) node.left = deleteValue(node.left, key);
else {
if (node.right == null) return node.left;
else if (node.left == null) return node.right;

root.key = minValue(root.right);

root.right = deleteValue(root.right, root.key);
}

node.size = getSize(node.left) + getSize(node.right) + 1;

return node;
}

private int minValue(Node node) {
int minValue = node.key;
while (node.left != null) {
minValue = node.left.key;
node = node.left;
}

return minValue;
}

public String searchValue(int key){
return searchValue(root, key);
}
private String searchValue (Node root, int key)
{
if (root == null) {
return null;
}

if (key == root.key){
return root.val;
}

if (key > root.key)
return searchValue(root.right, key);

return searchValue(root.left, key);
}

public void depthOrderTraversal() {
depthTraversal(root);
}

private void depthTraversal(Node node) {
if (node != null) {
System.out.print("Key: " + node.key + " Value: " + node.val + "\n");
depthTraversal(node.left);
depthTraversal(node.right);
}
}


}

110 changes: 110 additions & 0 deletions lab04/src/test/java/org/example/BSTTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.example;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;

import java.util.HashMap;
import java.util.Map;

public class BSTTest {
Map<Integer,String> tree;

@BeforeEach
void treeInit() {
tree = new HashMap<>();

tree.put(11,"Одинадцять");
tree.put(19,"Дев'ятнадцять");
tree.put(15,"П'ятнадцять");
tree.put(25,"Двадцять п'ять");
tree.put(12,"Дванадцять");
tree.put(13,"Тринадцять");
tree.put(16,"Шістнадцять");
tree.put(7,"Сім");
tree.put(24,"Двадцять чотири");
tree.put(20,"Двадцять");
tree.put(10,"Десять");
tree.put(21,"Двадцять один");
tree.put(8,"Вісім");
tree.put(17,"Сімнадцять");
tree.put(18,"Вісімнадцять");
tree.put(9,"Дев'ять");
tree.put(26,"Двадцять шість");
tree.put(23,"Двадцять три");
tree.put(14,"Чотирнадцять");
tree.put(22,"Двадцять два");
}

private BSTNode mapTransferBstnode() {
BSTNode node = new BSTNode();

for (Map.Entry<Integer,String> item: tree.entrySet()) {
node.insertValue(item.getKey(), item.getValue());
}
return node;
}

@Test
void Test1() {
BSTNode node = mapTransferBstnode();

Assertions.assertEquals(20,tree.size());
}

@Test
void Test2() {
BSTNode node = mapTransferBstnode();

node.depthOrderTraversal();
System.out.println();
Assertions.assertEquals(20,tree.size());
}

@Test
void Test3() {
BSTNode node = mapTransferBstnode();

Assertions.assertEquals("Двадцять один",node.searchValue(21));
Assertions.assertNull(node.searchValue(90));
Assertions.assertEquals("Дев'ятнадцять",node.searchValue(19));
Assertions.assertNull(node.searchValue(45));
}

@Test
void Test4() {
BSTNode node = new BSTNode();

for (Map.Entry<Integer,String> item: tree.entrySet()) {
if(item.getKey()!=20) {
node.insertValue(item.getKey(), item.getValue());
}
}


node.insertValue(20,"Двадцять");

System.out.println(node.searchValue(20));
Assertions.assertEquals("Двадцять",node.searchValue(20));
node.insertValue(20,"Інше двадцять");

node.depthOrderTraversal();
System.out.println();
Assertions.assertEquals(20, node.getSize());

System.out.println(node.searchValue(20));
Assertions.assertEquals("Інше двадцять",node.searchValue(20));
}
@Test
void Test5() {
BSTNode node = mapTransferBstnode();

node.deleteValue(14);

node.depthOrderTraversal();
System.out.println();
Assertions.assertEquals(19, node.getSize());
Assertions.assertNull(node.searchValue(14));
}
}
Binary file added lab04/target/classes/classpath.index
Binary file not shown.
Binary file not shown.
Binary file added lab04/target/classes/org/example/BSTNode.class
Binary file not shown.
Binary file added lab04/target/classes/org/example/Main.class
Binary file not shown.
Binary file added lab04/target/test-classes/classpath.index
Binary file not shown.
Binary file not shown.