-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
136 lines (114 loc) · 3.09 KB
/
Controller.java
File metadata and controls
136 lines (114 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
public class Controller implements Initializable {
private AVLTree avl; // avl object to hold the binary search tree
@FXML
private TextField rootVal; // TextField for getting text from user
@FXML
private Button insertBtn, searchBtn, deleteBtn, inorderBtn, preorderBtn, postorderBtn, levelorderBtn, clearBtn, dfsBtn; // Buttons to do operations
@FXML
private Pane pane; // Pane to hold the binary search tree
@FXML
private AnchorPane myAnchorPane;
/**
* Method for inserting value in Tree
*/
public void insertInAVL() {
try {
avl.insert(Integer.parseInt(rootVal.getText())); // Calling insert function in bst
rootVal.clear();
} catch(NumberFormatException nfe) {
showAlert("Please input an integer number.", "Invalid Input", AlertType.ERROR);
}
}
/**
* Method for searching value in Tree
*/
public void searchInAVL() {
try {
avl.search(Integer.parseInt(rootVal.getText())); // Calling search function in binary search tree
rootVal.clear();
} catch(NumberFormatException nfe) {
showAlert("Please input an integer number.", "Invalid Input", AlertType.ERROR);
}
}
/**
* Method for deleting value in Tree
*/
public void deleteInAVL() {
try {
avl.remove(Integer.parseInt(rootVal.getText())); // Calling search function in binary search tree
rootVal.clear();
} catch(NumberFormatException nfe) {
showAlert("Please input an integer number.", "Invalid Input", AlertType.ERROR);
}
}
@Override
/**
* For initializing tree
* @param arg0
* @param arg1
*/
public void initialize(URL arg0, ResourceBundle arg1) {
avl = new AVLTree(pane); // initializing empty AVL Tree
}
/**
* Method associated with in-order button
*/
public void inorder() {
avl.inorder();
}
/**
* Method associated with pre-order button
*/
public void preorder() {
avl.preorder();
}
/**
* Method associated with post-order button
*/
public void postorder() {
avl.postorder();
}
/**
* Method associated with level-order button
*/
public void levelorder() {
avl.levelorder();
}
/**
* Method associated with Depth First Search(DFS) button
*/
public void dfs() {
avl.dfs();
}
/**
* Method for clearing pane
*/
public void clearAVL() {
pane.getChildren().clear(); // To clear all the circle and text
avl.clear(); // To empty the AVL Tree
}
/**
* Method to create and show alert
* @param msg : Message to show on alert description
* @param title : Title of alert box
* @param type : Type of alert box, like, ERROR, INFORMATION, WARNING
*/
public static void showAlert(String msg, String title, AlertType type) {
Alert alert = new Alert(type);
alert.setTitle(title);
alert.setContentText(msg);
alert.setHeaderText(null);
alert.show();
}
}