Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions src/cn/edu/tju/rico/BinarySearchTree/BinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package cn.edu.tju.rico.BinarySearchTree;

public class BinarySearchTree {

private TreeNode root;

/**
* @description ������֪���й�������������
* @author rico
* @created 2017��6��3�� ����6:15:54
* @param input
*/
public BinarySearchTree(int[] input) {
createBinarySearchTree(input);
}

/**
* @description ������֪���й�������������
* @author rico
* @created 2017��6��3�� ����6:15:06
* @param input
*/
public void createBinarySearchTree(int[] input) {
if (input != null) {
for (int i = 0; i < input.length; i++) {
root = insert(input[i], root);
}
}
}

/**
* @description �����������������㷨���ݹ��㷨
* @author rico
* @created 2017��6��3�� ����3:27:43
* @param target
* Ŀ��ֵ
* @param root
* �����������ĸ����
* @return
*/
public TreeNode search(int target, TreeNode root) {
TreeNode result = null;
if (root != null) { // �ݹ���ֹ����
if (target == root.data) { // �ݹ���ֹ����
result = root;
return result;
} else if (target < root.data) { // Ŀ��ֵС�ڸ����ֵ��������������
result = search(target, root.left);
} else { // Ŀ��ֵ���ڸ����ֵ��������������
result = search(target, root.right);
}
}
return result;
}

/**
* @description �����������IJ������
* @author rico
* @created 2017��6��3�� ����5:55:05
* @param target
* @param node
* @return
*/
public TreeNode insert(int target, TreeNode node) {
if (search(target, node) == null) {
if (node == null) {
return new TreeNode(target);
} else {
if (target < node.data) {
node.left = insert(target, node.left);
} else {
node.right = insert(target, node.right);
}
}
}
return node;
}

/**
* @description ɾ���������������ƶ����
* @author rico
* @created 2017��6��3�� ����8:43:29
* @param target
* @param node
* @return
*/
public TreeNode remove(int target, TreeNode node) {
TreeNode tmp = null;
if (node != null) {
if (target < node.data) { // ��������ɾ��
node.left = remove(target, node.left);
} else if (target > node.data) { // ��������ɾ��
node.right = remove(target, node.right);
} else if (node.left != null && node.right != null) { // �ҵ���ɾ����㣬��������������Ϊ��
// �ҵ��Դ�ɾ����������������������һ�����(��С���)
tmp = node.right;
while (tmp.left != null) {
tmp = tmp.left;
}

// ����С��㲹λ��ɾ�����
node.data = tmp.data;

// ɾ����ɾ������������ϲ�λ���
node.right = remove(node.data, node.right);
} else {
if (node.left == null) {
node = node.right;
} else {
node = node.left;
}
}
}
return node;
}

/**
* @description ��������������������ݹ��㷨����������
* @author rico
* @created 2017��6��3�� ����3:52:54
* @param root
*/
public void inOrder(TreeNode node) {
if (node != null) {
inOrder(node.left);
System.out.print(root.data + " ");
inOrder(node.right);
}
}

/**
* @description ��ӡ����������
* @author rico
* @created 2017��6��3�� ����6:08:42
* @param node
*/
public void printTree(TreeNode node) {
if (node != null) {
System.out.print(node.data);
if (node.left != null || node.right != null) {
System.out.print("(");
printTree(node.left);
System.out.print(",");
printTree(node.right);
System.out.print(")");
}
}
}

/**
* @description ���ʶ����������ĸ����
* @author rico
* @created 2017��6��3�� ����3:54:49
* @return
*/
public TreeNode getRoot() {
return root;
}
}
19 changes: 19 additions & 0 deletions src/cn/edu/tju/rico/BinarySearchTree/TreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

package cn.edu.tju.rico.BinarySearchTree;

public class TreeNode {

public int data;
public TreeNode left;
public TreeNode right;

public TreeNode(int data){
this.data = data;
}

@Override
public String toString() {
return "TreeNode [data=" + data + "]";
}

}
89 changes: 89 additions & 0 deletions src/cn/edu/tju/rico/backtrack/EightQueen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cn.edu.tju.rico.backtrack;

import java.util.Arrays;
import java.util.Date;

/**
* Title: �˻ʺ�����(�ݹ��㷨) Description: ��8��8��Ĺ��������ϰڷŰ˸��ʺ�ʹ�䲻�ܻ��๥����
* �����������ʺ󶼲��ܴ���ͬһ�С�ͬһ�л�ͬһб���ϣ����ж����ְڷ���
*
* @author rico
* @created 2017��5��31�� ����4:54:17
*/
public class EightQueen {

private static final short N = 8; // ʹ�ó��������壬����֮���N�ʺ�����
private static int count = 0; // ���������

public static void main(String[] args) {
Date begin = new Date();
// ��ʼ�����̣�ȫ����0
short chess[][] = new short[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
chess[i][j] = 0;
}
}

putQueenAtRow(chess, 0);
Date end = new Date();
System.out.println("��� " + N + " �ʺ����⣬��ʱ��"
+ String.valueOf(end.getTime() - begin.getTime()) + "���룬��������"
+ count);
}

private static void putQueenAtRow(short[][] chess, int row) {
// �ݹ���ֹ�жϣ����row==N����˵���Ѿ��ɹ��ڷ���8���ʺ� ����������ֹ�ݹ�
if (row == N) {
count++;
System.out.println("�� " + count + " �ֽ⣺");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(chess[i][j] + " ");
}
System.out.println();
}
return;
}

short[][] chessTemp = chess.clone();

/**
* ����һ�е�ÿһ��λ�ó����ŷŻʺ� Ȼ����״̬�������ȫ�����ִ�еݹ麯���ڷ���һ�лʺ�
*/
for (int i = 0; i < N; i++) {
// �ڷ���һ�еĻʺ�֮ǰҪ���������һ�аڷŵļ�¼����ֹ��Ⱦ����
for (int j = 0; j < N; j++)
chessTemp[row][j] = 0;

chessTemp[row][i] = 1;

if (isSafety(chessTemp, row, i)) {
putQueenAtRow(chessTemp, row + 1);
// System.out.println("-----------");
// for (int k = 0; k < N; k++) {
// for (int j = 0; j < N; j++) {
// System.out.print(chess[k][j] + " ");
// }
// System.out.println();
// }
}
}
}

private static boolean isSafety(short[][] chess, int row, int col) {
// �ж����ϡ����ϡ������Ƿ�ȫ
int step = 1;
while (row - step >= 0) {
if (chess[row - step][col] == 1) // ����
return false;
if (col - step >= 0 && chess[row - step][col - step] == 1) // ����
return false;
if (col + step < N && chess[row - step][col + step] == 1) // ����
return false;

step++;
}
return true;
}
}
49 changes: 49 additions & 0 deletions src/cn/edu/tju/rico/sort/QuickSort_PartitionOnly.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cn.edu.tju.rico.sort;

import java.util.Arrays;


/**
* Title:��������ı���
* Description: �����ϲ��ϵ��þ�����ŵĻ����㷨
* ʱ�临�Ӷȣ�O(n^2)
* @author rico
* @created 2017��6��2�� ����9:10:43
*/
public class QuickSort_PartitionOnly {
/**
* @description �����������е�ÿ��Ԫ��Ϊ��׼����л��֣�
* ֱ����������Ԫ�ض�
* @author rico
* @param array
*/
public void quicksort(int[] array) {
if (array != null && array.length != 0) {
for (int i = 0; i < array.length; i++) {
// �����ǿ��ŵĻ����㷨
int base_index = 0;
int base = array[i];
base_index = i;
for (int j = i+1; j < array.length; j++) {
if (array[j] <= base ) {
base_index ++;
if (base_index != j) {
int temp = array[base_index];
array[base_index] = array[j];
array[j] = temp;
}
}
}
array[i] = array[base_index];
array[base_index] = base;
System.out.println(Arrays.toString(array));
}
}
}

public static void main(String[] args) {
// int[] array = { 1, 2, 3, 2, 2, 2, 5, 4, 2 };
int[] array = { 1, 2, 3, 5, 0, 4, 9, 2, 6 };
new QuickSort_PartitionOnly().quicksort(array);
}
}
35 changes: 35 additions & 0 deletions src/cn/edu/tju/rico/test/BinarySearchTreeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.edu.tju.rico.test;

import cn.edu.tju.rico.BinarySearchTree.BinarySearchTree;

public class BinarySearchTreeTest {
public static void main(String[] args) {
int[] input = {53,78,65,17,87,9,81,45,23};
BinarySearchTree tree = new BinarySearchTree(input);

System.out.println("�������������������");
tree.inOrder(tree.getRoot());
System.out.println();
System.out.println("\n------------------------\n");
System.out.println("��ӡ������������");
tree.printTree(tree.getRoot());
System.out.println();
System.out.println("\n------------------------\n");

System.out.println("��������������Ŀ��ֵ��");
System.out.println(tree.search(23, tree.getRoot()));
System.out.println("\n------------------------\n");

System.out.println("���������������Ŀ��ֵ��");
tree.insert(10, tree.getRoot());
tree.printTree(tree.getRoot());
System.out.println();
System.out.println("\n------------------------\n");

System.out.println("�����������ɾ��Ŀ��ֵ��");
tree.remove(78, tree.getRoot());
tree.printTree(tree.getRoot());
System.out.println();
System.out.println("\n------------------------\n");
}
}
2 changes: 1 addition & 1 deletion src/cn/edu/tju/rico/test/SortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void main(String[] args) {

System.out.println("\n----------------------\n");
System.out.println("ϣ������ �� ");
int[] target7 = { 21, 25, 49, 25, 16, 8, 31, 41 };
int[] target7 = { 21, 25, 49, 25, 16, 8, 31, 41,1,16 };
System.out.println("ԭ���� �� " + Arrays.toString(target7));
ShellSort.shellSort(target7);
System.out.println(Arrays.toString(target7));
Expand Down
Loading