Commit 981f5f3d by Paktalin

Initial commit

parents
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="true" project-jdk-name="10" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Task.iml" filepath="$PROJECT_DIR$/Task.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/junit-4.7.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="JUnit5.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.0.0/junit-jupiter-api-5.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.0.0/opentest4j-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.0.0/junit-platform-commons-1.0.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
class BinaryTree {
private Node root;
BinaryTree() {
root = null;
}
static int getHeight(Node node) {
if (node == null)
return -1;
int leftHeight = getHeight(node.getLeft());
int rightHeight = getHeight(node.getRight());
if (leftHeight > rightHeight)
return leftHeight + 1;
return rightHeight + 1;
}
void addNode(int item) {
Node newNode = new Node(item);
//if the tree doesn't contain nodes yet, then the new node becomes the root of this tree
if (root == null)
root = newNode;
else {
Node currentNode = root;
Node parent;
while (true) {
parent = currentNode;
//if item is less then current node, then move to the left child
if (item < currentNode.getValue()) {
currentNode = currentNode.getLeft();
if (currentNode == null) {
parent.setLeft(newNode);
return;
}
}
//if item is greater then current node, then move to the right child
else {
currentNode = currentNode.getRight();
if (currentNode == null) {
parent.setRight(newNode);
return;
}
}
}
}
}
static BinaryTree createTree(int... items) {
BinaryTree tree = new BinaryTree();
for (int item : items)
tree.addNode(item);
return tree;
}
Node getRoot() {
return root;
}
}
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class BinaryTreeTest {
private static BinaryTree testTree;
@BeforeEach
void executedBeforeEach() {
testTree = createSampleTree();
assertEquals(6, testTree.getRoot().getValue());
assertEquals(2, testTree.getRoot().getLeft().getValue());
assertEquals(8, testTree.getRoot().getRight().getValue());
assertEquals(1, testTree.getRoot().getLeft().getLeft().getValue());
}
@Test
void getRoot() {
assertEquals(6, testTree.getRoot().getValue());
}
@Test
void getHeight() {
BinaryTree emptyTree = new BinaryTree();
assertEquals(-1, BinaryTree.getHeight(emptyTree.getRoot()));
assertEquals(2, BinaryTree.getHeight(testTree.getRoot()));
testTree.addNode(5);
assertEquals(2, BinaryTree.getHeight(testTree.getRoot()));
testTree.addNode(4);
assertEquals(3, BinaryTree.getHeight(testTree.getRoot()));
}
/**
verify that a new node is placed on the right place
6
/ \
2 8
/ \
1 5
/
4
*/
@Test
void addNode() {
testTree.addNode(5);
Node root = testTree.getRoot();
Node addedNode = root.getLeft().getRight();
assertEquals(5, addedNode.getValue());
testTree.addNode(4);
addedNode = root.getLeft().getRight().getLeft();
assertEquals(4, addedNode.getValue());
}
/**
create a tree with input data
25
/ \
/ \
11 72
/ / \
3 35 86
/ \
31 49
*/
@Test
void createTree() {
BinaryTree tree = BinaryTree.createTree(25, 11, 72, 35, 86, 3, 31, 49);
assertEquals(25, tree.getRoot().getValue());
assertEquals(11, tree.getRoot().getLeft().getValue());
assertEquals(72, tree.getRoot().getRight().getValue());
assertEquals(3, tree.getRoot().getLeft().getLeft().getValue());
assertEquals(35, tree.getRoot().getRight().getLeft().getValue());
assertEquals(86, tree.getRoot().getRight().getRight().getValue());
assertEquals(31, tree.getRoot().getRight().getLeft().getLeft().getValue());
assertEquals(49, tree.getRoot().getRight().getLeft().getRight().getValue());
}
/**
create a tree with some random data
6
/ \
2 8
/
1
*/
static BinaryTree createSampleTree() {
BinaryTree tree = new BinaryTree();
tree.addNode(6);
tree.addNode(2);
tree.addNode(8);
tree.addNode(1);
return tree;
}
}
\ No newline at end of file
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(
"\nHello! Here you can get the height of your tree! \n" +
"I assumed that for simplicity the tree will contain only integer values.\n" +
"It is also a binary search tree, so the smaller values are placed on the left and bigger are placed on the right.\n\n" +
"Please, type \"default\" to get the height of the default tree\n" +
"or enter integer values separated by comas to be placed in a tree (Example: \"23,6,324,17,32\")\n");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
calculateHeight(input);
}
private static void calculateHeight(String input) {
BinaryTree tree;
if (input.equals("default")) {
tree = createDefaultTree();
printDefaultTree();
}
else {
int[] values = toIntArray(input);
if (values != null)
tree = BinaryTree.createTree(values);
else {
System.out.println("Wrong input format! Try again.\n");
main(null);
return;
}
}
printHeight(tree.getRoot());
}
private static int[] toIntArray(String input) {
String[] stringValues = input.split(",");
System.out.println(Arrays.toString(stringValues));
int[] values = new int[stringValues.length];
for (int i = 0; i < stringValues.length; i++) {
try {
values[i] = Integer.valueOf(stringValues[i]);
} catch (NumberFormatException e) {
return null;
}
}
return values;
}
private static BinaryTree createDefaultTree() {
return BinaryTree.createTree(25, 11, 72, 86, 3, 35, 31, 49);
}
private static void printHeight(Node root) {
int height = BinaryTree.getHeight(root);
if (height == -1)
System.out.println("The tree is empty.");
else
System.out.println("\nThe height of the tree is " + height);
}
private static void printDefaultTree() {
System.out.println("The default tree looks like this:\n" +
" 25\n" +
" / \\\n" +
" 11 72\n" +
" / / \\\n" +
"3 35 86\n" +
" / \\\n" +
" 31 49"
);
}
}
class Node {
private int value;
private Node left, right;
Node(int item) {
value = item;
left = right = null;
}
int getValue() {
return value;
}
Node getLeft() {
return left;
}
void setLeft(Node left) {
this.left = left;
}
void setLeft(int item) {
this.left = new Node(item);
}
Node getRight() {
return right;
}
void setRight(Node right) {
this.right = right;
}
void setRight(int item) {
this.right = new Node(item);
}
}
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class NodeTest {
private Node testNode;
@BeforeEach
void executedBeforeEach() {
testNode = new Node(5);
}
@Test
void getValue() {
assertEquals(5, testNode.getValue());
}
@Test
void getLeft() {
assertNull(testNode.getLeft());
}
@Test
void setLeft() {
testNode.setLeft(new Node(3));
assertEquals(3, testNode.getLeft().getValue());
testNode.setLeft(3);
assertEquals(3, testNode.getLeft().getValue());
}
@Test
void getRight() {
assertNull(testNode.getRight());
}
@Test
void setRight() {
testNode.setRight(new Node(9));
assertEquals(9, testNode.getRight().getValue());
testNode.setRight(9);
assertEquals(9, testNode.getRight().getValue());
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment