Skip to content

Commit 2b5f625

Browse files
Merge pull request matthewsamuel95#691 from imaprincess/sudoku
Sudoku Algorithm in Java
2 parents daa1e09 + 13fb627 commit 2b5f625

File tree

3 files changed

+355
-0
lines changed

3 files changed

+355
-0
lines changed

BST/java/BST.java

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
public class BST {
2+
3+
private Node root;
4+
5+
public boolean find(int data) {
6+
7+
Node current = root;
8+
9+
while(current != null) {
10+
if(current.getData() == data)
11+
return true;
12+
if(current.getData() < data)
13+
current = current.getRightNode();
14+
else
15+
current = current.getLeftNode();
16+
}
17+
18+
return false;
19+
}
20+
21+
22+
public void insert(int data) {
23+
24+
Node current = root;
25+
Node parent = root;
26+
boolean isLeftChild = false;
27+
28+
if(root == null){
29+
root = new Node(data);
30+
return;
31+
}
32+
33+
while(current != null) {
34+
parent = current;
35+
if(current.getData() == data)
36+
return;
37+
if(current.getData() < data) {
38+
current = current.getRightNode();
39+
isLeftChild = false;
40+
}
41+
else {
42+
current = current.getLeftNode();
43+
isLeftChild = true;
44+
}
45+
}
46+
47+
if(isLeftChild)
48+
parent.setLeftNode(new Node(data));
49+
else
50+
parent.setRightNode(new Node(data));
51+
}
52+
53+
public boolean delete(int data) {
54+
55+
Node parent = root;
56+
Node current = root;
57+
58+
if(current == null)
59+
return false;
60+
61+
boolean isLeftChild = false;
62+
63+
while(current.getData() != data) {
64+
65+
parent = current;
66+
67+
if(current.getData() < data) {
68+
current = current.getRightNode();
69+
isLeftChild = false;
70+
}
71+
else {
72+
current = current.getLeftNode();
73+
isLeftChild = true;
74+
}
75+
76+
if(current == null)
77+
return false;
78+
}
79+
80+
//Node to be deleted has no children
81+
82+
if(current.getLeftNode() == null && current.getRightNode() == null){
83+
if(current==root){
84+
root = null;
85+
}
86+
if(isLeftChild ==true){
87+
parent.setLeftNode(null);
88+
} else {
89+
parent.setRightNode(null);
90+
}
91+
}
92+
//Node to be deleted has only one child
93+
else if(current.getRightNode() == null){
94+
95+
if(current == root){
96+
root = current.getLeftNode();
97+
} else if(isLeftChild) {
98+
parent.setLeftNode(current.getLeftNode());
99+
} else {
100+
parent.setRightNode(current.getLeftNode());
101+
}
102+
} else if(current.getLeftNode() == null){
103+
if(current == root){
104+
root = current.getRightNode();
105+
} else if(isLeftChild) {
106+
parent.setLeftNode(current.getRightNode());
107+
} else {
108+
parent.setRightNode(current.getRightNode());
109+
}
110+
}
111+
112+
// Node to be deleted has two children
113+
else if(current.getLeftNode() != null && current.getRightNode() != null) {
114+
115+
//We have to find the min node in the right subtree
116+
Node successor = getSuccessor(current);
117+
if(current == root) {
118+
root = successor;
119+
} else if(isLeftChild) {
120+
parent.setLeftNode(successor);
121+
} else {
122+
parent.setRightNode(successor);
123+
}
124+
successor.setLeftNode(current.getLeftNode());
125+
}
126+
return true;
127+
}
128+
129+
public Node getSuccessor(Node deleteNode){
130+
131+
Node successor = null;
132+
Node successorParent = null;
133+
Node current = deleteNode.getRightNode();
134+
while(current != null){
135+
successorParent = successor;
136+
successor = current;
137+
current = current.getLeftNode();
138+
}
139+
140+
//check if successor has the right child (it cannot have left child for sure)
141+
// if it does have the right child, add it to the left of successorParent.
142+
143+
if(successor != deleteNode.getRightNode()){
144+
successorParent.setLeftNode(successor.getRightNode());
145+
successor.setRightNode(deleteNode.getRightNode());
146+
}
147+
148+
return successor;
149+
}
150+
151+
private void display(Node root){
152+
if(root != null){
153+
display(root.getLeftNode());
154+
System.out.print(" " + root.getData());
155+
display(root.getRightNode());
156+
}
157+
}
158+
159+
public void display(){
160+
display(root);
161+
}
162+
163+
public static void main(String arg[]){
164+
BST b = new BST();
165+
b.insert(3);
166+
b.insert(8);
167+
b.insert(1);
168+
b.insert(4);
169+
b.insert(6);
170+
b.insert(2);
171+
b.insert(10);
172+
b.insert(9);
173+
b.insert(20);
174+
b.insert(25);
175+
b.insert(15);
176+
b.insert(16);
177+
System.out.println("Original Tree : ");
178+
b.display();
179+
System.out.println("");
180+
System.out.println("Check whether Node with value 4 exists : " + b.find(4));
181+
System.out.println("Delete Node with no children (2) : " + b.delete(2));
182+
b.display();
183+
System.out.println("\n Delete Node with one child (4) : " + b.delete(4));
184+
b.display();
185+
System.out.println("\n Delete Node with Two children (10) : " + b.delete(10));
186+
b.display();
187+
}
188+
}

BST/java/Node.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Node {
2+
3+
private int data;
4+
private Node leftNode;
5+
private Node rightNode;
6+
7+
public Node(int data) {
8+
this.data = data;
9+
}
10+
11+
public int getData() {
12+
return data;
13+
}
14+
15+
public Node getLeftNode() {
16+
return leftNode;
17+
}
18+
19+
public Node getRightNode() {
20+
return rightNode;
21+
}
22+
23+
public void setData(int data) {
24+
this.data = data;
25+
}
26+
27+
public void setLeftNode(Node leftNode) {
28+
this.leftNode = leftNode;
29+
}
30+
31+
public void setRightNode(Node rightNode) {
32+
this.rightNode = rightNode;
33+
}
34+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
public class Sudoku {
2+
3+
public final int SUDOKU_SIZE = 9;
4+
public final int UNASSIGNED = 0;
5+
6+
7+
private int unassignedPositionRow;
8+
private int unassignedPositionColumn;
9+
private int[][] matrix = new int[SUDOKU_SIZE][SUDOKU_SIZE];
10+
11+
public Sudoku() {
12+
13+
clear();
14+
}
15+
16+
public void clear() {
17+
for(int i = 0; i < SUDOKU_SIZE; i++)
18+
for(int j = 0; j < SUDOKU_SIZE; j++)
19+
matrix[i][j] = UNASSIGNED;
20+
}
21+
22+
public void insertRandomNumbers(int n) {
23+
24+
for(int i = 0; i < n; i++) {
25+
int randomRow = (int)(Math.random() * (SUDOKU_SIZE - 1));
26+
int randomColumn = (int)(Math.random() * (SUDOKU_SIZE - 1));
27+
int randomNumber = (int)(Math.random() * (SUDOKU_SIZE - 1) + 1);
28+
matrix[randomRow][randomColumn] = randomNumber;
29+
}
30+
}
31+
32+
public boolean solve() {
33+
34+
if(!findUnassignedPosition())
35+
return true;
36+
37+
for(int n = 1; n <= SUDOKU_SIZE; n++) {
38+
39+
if(canBePlaced(unassignedPositionRow, unassignedPositionColumn, n)) {
40+
final int i = unassignedPositionRow;
41+
final int j = unassignedPositionColumn;
42+
matrix[i][j] = n;
43+
44+
if(solve())
45+
return true;
46+
47+
matrix[i][j] = UNASSIGNED;
48+
}
49+
}
50+
51+
return false;
52+
}
53+
54+
private boolean findUnassignedPosition() {
55+
56+
for(int i = 0; i < SUDOKU_SIZE; i++) {
57+
for(int j = 0; j < SUDOKU_SIZE; j++) {
58+
if(matrix[i][j] == UNASSIGNED) {
59+
unassignedPositionRow = i;
60+
unassignedPositionColumn = j;
61+
return true;
62+
}
63+
}
64+
}
65+
66+
return false;
67+
}
68+
69+
private boolean canBePlaced(int row, int column, int number) {
70+
71+
return !isUsedInRow(row, number) && !isUsedInColumn(column, number) && !isUsedInBox(row, column, number);
72+
}
73+
74+
private boolean isUsedInRow(int row, int number) {
75+
76+
for(int i = 0; i < SUDOKU_SIZE; i++)
77+
if(matrix[row][i] == number)
78+
return true;
79+
return false;
80+
}
81+
82+
private boolean isUsedInColumn(int column, int number) {
83+
84+
for(int i = 0; i < SUDOKU_SIZE; i++)
85+
if(matrix[i][column] == number)
86+
return true;
87+
return false;
88+
}
89+
90+
private boolean isUsedInBox(int row, int column, int number) {
91+
92+
int startRow = row - row % 3;
93+
int startColumn = column - column % 3;
94+
95+
for(int i = 0; i < 3; i++)
96+
for(int j = 0; j < 3; j++)
97+
if(matrix[startRow + i][startColumn + j] == number)
98+
return true;
99+
return false;
100+
}
101+
102+
public void print() {
103+
System.out.println("-------------------------");
104+
105+
for(int i = 0; i < SUDOKU_SIZE; i++) {
106+
for (int j = 0; j < SUDOKU_SIZE; j++) {
107+
if (j == 0)
108+
System.out.print("| ");
109+
System.out.print(matrix[i][j] + " ");
110+
if (j % 3 == 2)
111+
System.out.print("| ");
112+
113+
}
114+
System.out.println();
115+
if(i % 3 == 2)
116+
System.out.println("-------------------------");
117+
}
118+
119+
}
120+
121+
public static void main(String[] args) {
122+
123+
Sudoku sudoku = new Sudoku();
124+
sudoku.insertRandomNumbers(3);
125+
System.out.println("Current sudoku state:");
126+
sudoku.print();
127+
System.out.println("Solving...");
128+
if(sudoku.solve())
129+
sudoku.print();
130+
else
131+
System.out.println("No solution found! :(");
132+
}
133+
}

0 commit comments

Comments
 (0)