-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBinarySearchTree.java
172 lines (147 loc) · 5.19 KB
/
BinarySearchTree.java
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.util.*;
import java.io.*;
import java.lang.*;
/**
* Write a description of class BST here.
*
* @author (Oarabile Mwiya)
* @version (Final version)
* @Date (17/10/2019)
*/
class BinarySearchTree {
static String villageName;
static BinarySearchTree bst = new BinarySearchTree();
public static void main(String [] args)throws Exception{
Random randoms = new Random();
FileReader file = new FileReader("botswanatownsAndVillages.txt");
Scanner scan =new Scanner(file);
String towns ="botswanatownsAndVillages.txt";
int size = bst.read(towns);
String [] villages = new String[size];
ArrayList<String> villageArray=new ArrayList<>();
int i =0;
//Traverse the file line by line
while (scan.hasNextLine()) {
String line = scan.nextLine();
villageArray.add(line);
bst.insert(line);
}
root = null;
//Method to Randomise the villages and towns
while(villageArray.size()>0){
int x = randoms.nextInt(villageArray.size());
bst.insert(villageArray.get(x)) ;
villageArray.remove(x);
}
System.out.println("Inorder traversal");
bst.inorder();
System.out.println("\npreOrder traversal");
bst.preorder();
System.out.println("\npostOrder traversal");
bst.postorder();
scan.close();
}
// Method to shuffle an arraylist
public static void shuffle( ArrayList<String> list){
Collections.shuffle(list);
System.out.println("Inorder traversal");
bst.inorder();
System.out.println("\npreOrder traversal");
bst.preorder();
System.out.println("\npostOrder traversal");
bst.postorder();
}
/* Class containing left and right child of current node and villageName value*/
class Node {
String villageName;
Node left, right;
public Node(String item) {
villageName = item;
left = right = null;
}
} static Node root; // Root of BST
// Constructor
BinarySearchTree() {
root = null;
}
// This method mainly calls insertVillage()
void insert(String villageName) {
root = insertVillageName(root, villageName);
}
/* A recursive function to insert a new village in BST */
Node insertVillageName(Node root, String villageName) {
/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(villageName);
return root;
}
/* Otherwise, recur down the tree */
if ( villageName.compareTo(root.villageName) < 0 ){
root.left = insertVillageName(root.left, villageName);
}
else if (villageName.compareTo( root.villageName) > 0 ){
root.right = insertVillageName(root.right, villageName);
}
/* return the (unchanged) node pointer */
return root;
}
// This method mainly calls InorderRec()
void inorder() {
inorderVillages(root);
}
// This method mainly calls preorderRec()
void preorder() {
preOrderVillages(root);
}
// This method mainly calls postorderRec()
void postorder() {
postOrderVillages(root);
}
// A utility function to do inorder traversal of BST
void inorderVillages(Node root) {
if (root != null) {
inorderVillages(root.left);
System.out.println(root.villageName);
inorderVillages(root.right);
}
}
public Node search(Node root, String villageName) {
// Base Cases: root is null or villageName is present at root
if (root==null || villageName.equals(root.villageName))
return root;
// val is greater than root's villageName
if (villageName.compareTo(root.villageName) > 0)
return search(root.left, villageName);
// val is less than root's village
return search(root.right, villageName);
}
// A utility function to do PostOrder traversal of BST
public void postOrderVillages(Node root){
if(root != null){
postOrderVillages(root.left);
postOrderVillages(root.right);
System.out.println(root.villageName);
}
}
// A utility function to do PreOrder traversal of BST
public void preOrderVillages(Node root){
if(root != null){
System.out.println(root.villageName);
preOrderVillages(root.left);
preOrderVillages(root.right);
}
}
//method to count number of villages in the file
public int read(String path)throws Exception
{
FileReader file = new FileReader(path);
BufferedReader buff = new BufferedReader(file);
String line = null;
int count = 0;
while((line = buff.readLine())!=null)
{
count++;
}
return count;
}
}