Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0903 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
172 changes: 172 additions & 0 deletions src/cn/edu/tju/rico/BinarySearchTree/BinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package cn.edu.tju.rico.BinarySearchTree;

/**
* Title: ���������������ݽṹ�����㷨
* Description: ����������������һ�ſ��������߾����������ʣ�
* ���нڵ�Ĺؼ��뻥����ͬ����Ψһ��
* �����������нڵ㶼С�ڸ���㣬�����������нڵ㶼���ڸ����
* �����������������Ƕ���������
*
* �����������IJ��롢������ɾ���ڵ��ʱ�临�ӶȾ�ΪO(lgn)
*
* @author rico
* @created 2017��6��4�� ����9:51:16
*/
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(node.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 + "]";
}

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

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;
}
}
37 changes: 36 additions & 1 deletion src/cn/edu/tju/rico/list/LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,42 @@ public void reverseLinkedList() {
}
head.next = pre; // ��ԭ������ͷ���ָ��ת�������
}


// public void ReverseList() {
// Node<E> head1 = head.next;
// if (head1 != null && head1.next != null) {
// Node<E> p1 = head1;
// Node<E> p2 = p1.next;
// while(p2 != null){
// Node<E> tmp = p2.next;
// p2.next = p1;
// if (p1 == head1) {
// p1.next = null;
// }
// p1 = p2;
// p2 = tmp;
// }
//
// head.next = p1;
// }
// }

// public void ReverseList1() {
// java.util.LinkedList<E> stack = new java.util.LinkedList<E>();
// Node<E> head1 = head.next;
// Node<E> cur = head1;
// while(cur != null){
// stack.push(cur.data);
// cur = cur.next;
// }
//
// cur = head1;
// while (cur!= null) {
// cur.data = stack.pop();
// cur = cur.next;
// }
// }

/**
* @description �жϵ������Ƿ�Ϊ��
* @author rico
Expand Down
2 changes: 1 addition & 1 deletion src/cn/edu/tju/rico/list/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class Node<T> {
//���ɼ���
Node<T> next;
public Node<T> next;
T data;

/**
Expand Down
Loading