Skip to content

Commit

Permalink
Merge commit 'bedb0a4e8d1084fa54868a9ffa57cb3cad8dd226' into App
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVelezLl committed May 16, 2020
2 parents 58703d3 + bedb0a4 commit dfae247
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 36 deletions.
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# AVL TREE

[![language][language-shield]][language-url]
[![Codacy Badge][codacybadge-shield]][codacybadge-url]
[![repoSize][repoSize-shield]][repo]
[![Contributors][contributors-shield]][contributors-url]
[![forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues][issues-shield]][issues-url]
[![lastCommit][lastCommit-shield]][lastCommit-url]
[![LinkedIn][linkedin-shield]][linkedin-url]
[![Twitter][twitter-shield]][twitter-url]

**AVL TREE** is an application developed on Java that simulate the behavior of an Avl Tree, with the aim of the user learn in a better way how an Avl works.

![TheTree](./captures/App.png)

## The Tree

The Avl tree is a search binary tree wich we can use to represent information hierarchically. This tree is also self balancing, that means that the tree is always balanced, in such a way that for every node the difference of the heigh of left branch and the heigh of right branch is at most one.

The representation of the avl tree in this application is only to save integers, but in an Avl Tree you can save any data type that can be comparable.

### Rules

As [freecodecamp](https://www.freecodecamp.org/news/avl-tree-insertion-rotation-and-balance-factor/) describes, a search binary tree is a TDA composed of nodes that have the following rules:

1. Each has a root node at the top.

2. Every node has at the most, two children.

3. Every node has only one parent (the root is the one node that has no parents).

4. For each node, its left child is less or equal than the node, wich is less or equal than its right child.

And the Avl tree has additionally this rule:

1.- The difference between the depth of right and left subtrees cannot be more than one. In order to maintain this guarantee, an implementation of an AVL will include an algorithm to rebalance the tree when adding an additional element would upset this guarantee.

There are several algotihm to aim that, a few of these are: LL rotation, RR rotation, LR rotation and RL rotation, you can watch all these algorithsm to balance the tree in this [video](https://www.youtube.com/watch?v=7m94k2Qhg68).

## Avl-tree 🔥

In this aplication you have three options to experiment with the tree, you have the option to add a new node, the option to remove a node, and you can also clear the tree removing all the nodes of the current tree.

The aplication behind the scenes will aply all the rules and algorithms to keep balanced the tree.

## Prerequisites 📋

You should have the java virtual machine installed(JVM) on your computer to run the game, if you do not have the java virtual machine you could install it from [here](https://www.java.com/es/download/).

### Installation 🔧

You only need to download this project.
To run the game you need to get into the folder "dist", which is in the root directory, and then execute the "Avl-Tree.jar" file.

Or you can also run this from the shell, you need to cd to the project folder, and then execute:

```bash
java -jar dist/Avl-Tree.jar
```

## Built with 🛠️

* Java8
* JavaFx

[language-shield]: https://img.shields.io/badge/Java-v1.8.0-blue?style=plastic
[language-url]: https://www.java.com/es/download/
[codacybadge-shield]: https://img.shields.io/codacy/grade/3c17ebf7c1954006a60b08b8af2c58e2?style=plastic
[codacybadge-url]: https://www.codacy.com/manual/AlexVelezLl/AVL-TREE?utm_source=github.com&utm_medium=referral&utm_content=AlexVelezLl/AVL-TREE&utm_campaign=Badge_Grade
[repoSize-shield]: https://img.shields.io/github/repo-size/AlexVelezLl/AVL-TREE?style=plastic
[repo]: https://github.com/AlexVelezLl/AVL-TREE
[contributors-shield]: https://img.shields.io/github/contributors/AlexVelezLl/AVL-TREE?style=plastic
[contributors-url]: https://github.com/AlexVelezLl/AVL-TREE/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/AlexVelezLl/AVL-TREE?style=plastic
[forks-url]: https://github.com/AlexVelezLl/AVL-TREE/network/members
[stars-shield]: https://img.shields.io/github/stars/AlexVelezLl/AVL-TREE?style=plastic
[stars-url]: https://github.com/AlexVelezLl/AVL-TREE/stargazers
[issues-shield]: https://img.shields.io/github/issues/AlexVelezLl/AVL-TREE?style=plastic
[issues-url]: https://github.com/AlexVelezLl/AVL-TREE/issues
[lastCommit-shield]: https://img.shields.io/github/last-commit/AlexVelezLl/AVL-TREE?style=plastic
[lastCommit-url]: https://github.com/AlexVelezLl/AVL-TREE/commits
[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=plastic&logo=linkedin&colorB=555
[linkedin-url]:https://www.linkedin.com/in/alex-velez-llaque-4b3327191/
[twitter-shield]:https://img.shields.io/twitter/follow/AlexVelezLl?label=Follow&style=social
[twitter-url]:https://twitter.com/AlexVelezLl
Binary file added captures/App.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 67 additions & 36 deletions src/Interfaz/InterfazArbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
Expand All @@ -29,10 +32,16 @@ public class InterfazArbol {
private Pane root;
private AVL<Integer> avlTree;
private ScrollPane sp;
private final TextField txtInsertar;
private final TextField txtDelete;
StackPane arbol;

public InterfazArbol() {
root = new VBox();
txtInsertar = new TextField();
txtDelete = new TextField();
avlTree = new AVL<>(Integer::compareTo);
arbol = new StackPane();
sp = new ScrollPane();
createRoot();
}
Expand All @@ -51,9 +60,17 @@ private void createRoot() {
HBox hbOpc = new HBox();
hbOpc.setAlignment(Pos.CENTER);
hbOpc.setTranslateY(6);
TextField txtInsertar = new TextField();
txtInsertar.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER) {
insertar();
}
});
txtDelete.setOnKeyPressed(e->{
if(e.getCode()==KeyCode.ENTER){
eliminar();
}
});
Button btnInsertar = new Button("Insertar");
TextField txtDelete = new TextField();
Button btnDelete = new Button("Eliminar");
HBox hbInst = new HBox();
hbInst.getChildren().addAll(txtInsertar, btnInsertar);
Expand All @@ -70,7 +87,6 @@ private void createRoot() {
optRect.setFill(Color.CADETBLUE);
StackPane optStck = new StackPane();
optStck.getChildren().addAll(optRect, hbOpc);
StackPane arbol = new StackPane();
arbol.setMinHeight(200);
arbol.setMinWidth(1366);
arbol.setMinWidth(avlTree.height() * 100);
Expand All @@ -85,42 +101,11 @@ private void createRoot() {
sp.setMinHeight(500);
sp.setMaxHeight(500);
btnInsertar.setOnAction(e -> {
Integer num = 0;
try {
num = Integer.parseInt(txtInsertar.getText());
txtInsertar.setText("");
txtInsertar.requestFocus();
avlTree.insert(num);

arbol.getChildren().clear();
Pane avlPane = avlTree.mostrarArbol();
if (avlTree.height() <= 7)
avlPane.setTranslateX(100 * (7 - avlTree.height()));
arbol.getChildren().add(avlPane);
arbol.setMinWidth(avlTree.height() * 100);
} catch (Exception ex) {

}

insertar();
});

btnDelete.setOnMouseClicked(e -> {
Integer num = 0;
try {
num = Integer.parseInt(txtDelete.getText());
txtDelete.setText("");
txtDelete.requestFocus();
avlTree.delete(num);
arbol.getChildren().clear();
Pane avlPane = avlTree.mostrarArbol();
if (avlTree.height() <= 7)
avlPane.setTranslateX(100 * (7 - avlTree.height()));
arbol.getChildren().add(avlPane);
arbol.setMinWidth(avlTree.height() * 100);

} catch (Exception ex) {

}
eliminar();
});

StackPane pnSalir = new StackPane();
Expand All @@ -138,6 +123,52 @@ private void createRoot() {

}

private void eliminar() {
Integer num = 0;
try {
num = Integer.parseInt(txtDelete.getText());
txtDelete.setText("");
txtDelete.requestFocus();
avlTree.delete(num);
arbol.getChildren().clear();
Pane avlPane = avlTree.mostrarArbol();
if (avlTree.height() <= 7)
avlPane.setTranslateX(100 * (7 - avlTree.height()));
arbol.getChildren().add(avlPane);
arbol.setMinWidth(avlTree.height() * 100);

} catch (Exception ex) {
Alert alerta = new Alert(AlertType.ERROR);
alerta.setTitle("Error");
alerta.setContentText("Por favor ingrese un numero valido");
alerta.showAndWait();
}

}

private void insertar() {
Integer num = 0;
try {
num = Integer.parseInt(txtInsertar.getText());
txtInsertar.setText("");
txtInsertar.requestFocus();
avlTree.insert(num);

arbol.getChildren().clear();
Pane avlPane = avlTree.mostrarArbol();
if (avlTree.height() <= 7)
avlPane.setTranslateX(100 * (7 - avlTree.height()));
arbol.getChildren().add(avlPane);
arbol.setMinWidth(avlTree.height() * 100);
} catch (Exception ex) {
// Si la tecla que inserto no es ningun numero
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("WOOPS!");
alert.setContentText("Por favor ingrese un numero valido.");
alert.showAndWait();
}
}

public Pane getRoot() {
return root;
}
Expand Down

0 comments on commit dfae247

Please sign in to comment.