str.replaceAll("JAVA","尚硅谷。。");
// 单链表
sparseArr int[sum + 1][3]
chessArr2 = int[11][11];
package sparseArray;
/**
* @author houbj
* @date 2020/12/7 14:39
*/
public class SparseArray {
public static void main(String[] args) {
/**
* 1. 创建一个二维数组 11 * 11
* 2. 0:表示没有棋子, 1:表示有黑子, 2:表示白子
*/
int chessArr[][] = new int[11][12];
System.out.println(chessArr.length+"行," +chessArr[0].length+ "列");
chessArr[1][2] = 1;
chessArr[2][3] = 2;
chessArr[10][6] = 5;
/**
* 3. 输出原始数组
*/
System.out.println("1.原始数组输出:");
for (int[] arr: chessArr){
for(int a: arr) {
System.out.printf("%d\t",a);
}
System.out.println();
}
/**
* 4. 二维数组转稀疏数组
*/
int sum = 0;
for (int[] arr: chessArr){
for(int a: arr) {
if (a != 0) sum ++;
}
}
System.out.println("总共有 "+ sum + " 个数据。");
/**
* 5. 创建稀疏数组
*/
int sparseArr[][] = new int[sum+1][3];
sparseArr[0][0] = chessArr.length;
sparseArr[0][1] = chessArr[0].length;
sparseArr[0][2] = sum;
int tag = 0;
for (int i = 0; i < chessArr.length ; i ++) {
for (int j = 0; j < chessArr[0].length; j++ ){
if (chessArr[i][j] != 0) {
tag ++;
sparseArr[tag][0] = i;
sparseArr[tag][1] = j;
sparseArr[tag][2] = chessArr[i][j];
}
}
}
/**
* 6. 稀疏数组输出
*/
System.out.println("2.稀疏数组输出:");
for (int[] arr: sparseArr) {
for (int a: arr) {
System.out.printf("%d\t",a);
}
System.out.println();
}
int chaseArr2 [][] = new int[sparseArr[0][0]][sparseArr[0][1]];
for (int i = 1 ; i < sparseArr.length ; i ++) {
chaseArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
System.out.println("3.原始数组输出:");
for (int[] arr: chaseArr2){
for(int a: arr) {
System.out.printf("%d\t",a);
}
System.out.println();
}
}
}
package queueArray;
/**
* @author houbj
* @date 2020/12/7 15:52
*/
public class QueueArray {
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(4);
arrayQueue.addQueue(5);
arrayQueue.addQueue(6);
arrayQueue.show();
System.out.println("队列头为:"+arrayQueue.showFront());
arrayQueue.popQueue();
arrayQueue.show();
System.out.println("队列头为:"+arrayQueue.showFront());
}
}
class ArrayQueue {
private int maxSize; // 表示数组的最大容量
private int front; // 队列头
private int rear; // 队列尾
private int[] arr; // 该数据用于存放数据,模拟队列
public ArrayQueue(int arrSize){
maxSize = arrSize;
arr = new int[maxSize];
front = -1;
rear = -1;
}
public boolean isFull(){
return rear == maxSize-1;
}
public boolean isEmpty(){
return rear == front;
}
public void addQueue(int n){
if (isFull()) {
System.out.println("队列已满...");
} else {
rear ++ ;
arr[rear] = n;
}
}
public int popQueue(){
if (isEmpty()) {
throw new RuntimeException("队列为空...");
} else {
front ++ ;
return arr[front];
}
}
public void show(){
if (isEmpty()) {
System.out.println("队列为空...");
} else {
System.out.println("队列为:");
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
}
public int showFront(){
if (isEmpty()) {
throw new RuntimeException("队列为空...");
} else {
return arr[front+1];
}
}
}
package queueArray;
/**
* @author houbj
* @date 2020/12/7 20:22
*/
public class CircleQueueArray {
public static void main(String[] args) {
CircleArray circleArrayQueue = new CircleArray(5);
circleArrayQueue.addQueue(1);
circleArrayQueue.addQueue(2);
circleArrayQueue.addQueue(3);
circleArrayQueue.addQueue(4);
circleArrayQueue.addQueue(5);
circleArrayQueue.showQueue();
circleArrayQueue.getQueue();
circleArrayQueue.showQueue();
circleArrayQueue.addQueue(6);
circleArrayQueue.showQueue();
}
}
class CircleArrayQueue {
private int maxSize; // 表示数组的最大容量
private int front; // 队列头
private int rear; // 队列尾
private int[] arr; // 该数据用于存放数据,模拟队列
public CircleArrayQueue(int arrMaxSize){
maxSize = arrMaxSize;
arr = new int[maxSize];
}
public boolean isFull(){
System.out.println( "full --- rear: "+ rear + " , front:"+front);
return (rear + 1) % maxSize == front;
}
public boolean isEmpty(){
return rear==front;
}
public void addQueue(int n){
if (isFull()) {
System.out.println("队列满,不能添加数据。。");
return;
}
System.out.println("addQueue -- rear: "+ rear + " , front:"+front);
arr[rear] = n;
rear = (rear + 1) % maxSize;
System.out.println("addQueue -- 显示队列:");
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
}
}
public int popQueue(){
if (isEmpty()) {
throw new RuntimeException("队列空,不可以删除数据。。");
}
int value = arr[front];
front = (front+1)%maxSize;
return value;
}
public void show(){
if (isEmpty()) {
System.out.println("队列空。。。");
return;
}
System.out.println("显示队列:");
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
}
}
public int size(){
return (rear+maxSize-front)%maxSize;
}
}
package LinkedList;
/**
* @author houbj
* @date 2020/12/8 11:54
*/
public class SingleLinked {
public static void main(String[] args) {
ChargeList chargeList = new ChargeList();
chargeList.addNode(new HeroNode(1,"l", "ll"));
chargeList.addNode(new HeroNode(2,"d", "dd"));
chargeList.addNode(new HeroNode(3,"c", "cc"));
chargeList.show();
chargeList.show2();
}
}
/**
* 定义一个类:管理linkedList
*/
class ChargeList{
/**
* 先初始化一个头节点,头节点不要动
*/
private HeroNode head = new HeroNode(0,"","");
/**
* 添加链表
* @param heroNode
*/
public void addNode(HeroNode heroNode) {
/**
* 找到当前链表的最后节点,将next域指向最新节点
*/
HeroNode temp = this.head;
while (true) {
if (temp.next == null) {
temp.next = heroNode;
break;
}
temp = temp.next;
}
}
/**
* 显示链表
*/
public void show(){
System.out.println("遍历链表 -- : ");
if (head.next == null) {
System.out.println("链表为空");
} else {
HeroNode node = head.next;
while (true) {
if (node == null) {
break;
} else {
System.out.println(node);
node = node.next;
}
}
}
}
public void show2(){
System.out.println("遍历链表 -- : ");
HeroNode temp = this.head;
while (true) {
if (temp.next == null) {
return;
}
temp = temp.next;
System.out.println(temp);
}
}
}
/**
* 每个对象就是一个节点
*/
class HeroNode{
public int num;
public String name;
public String nickName;
HeroNode next;
public HeroNode(int num, String name, String nickName){
this.num = num;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode{" +
"num=" + num +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
", next=" + next +
'}';
}
}
HeroNode temp = head.next;
while(temp != null) {
length++;
temp = temp.next;
}
/**
* 错误实例
*/
public void reverse(HeroNode node){
if (node.next == null || node.next.next == null) {
System.out.println("无需反转----");
return;
}
HeroNode temp = node.next;
HeroNode newNode = new HeroNode(0,"","");
HeroNode tag;
boolean flag = true;
while (flag) {
if (temp.next == null) {
flag = false;
}
tag = newNode.next;
newNode.next = temp;
newNode.next.next = tag;
temp = temp.next;
}
System.out.println(newNode);
}
/**
* 正确实例
*/
public void reverseNode(HeroNode node) {
if (node.next == null || node.next.next == null) {
System.out.println("无需反转----");
return;
}
HeroNode cur = head.next; // 定义一个辅助的指针变量,帮助我们遍历原来的链表
HeroNode next = null; // 指向当前节点(cur)的下一个节点
HeroNode newNode = new HeroNode(0,"",""); //遍历一个新节点,放置在这个头节点后面
while (cur!= null) {
next = cur.next; //先保存当前节点的下一个节点
cur.next = newNode.next; // 将cur下一个节点指向新链表的最前端
newNode.next = cur;
cur = next;
}
node.next = newNode.next;
System.out.println(node);
}
/**
* 正确实例
*/
public void reverse2(HeroNode node){
if (node.next == null || node.next.next == null) {
System.out.println("无需反转----");
return;
}
HeroNode temp = node.next;
HeroNode newNode = new HeroNode(0,"","");
HeroNode tag;
HeroNode tag2;
boolean flag = true;
while (flag) {
if (temp.next == null) {
flag = false;
}
tag = temp.next;
tag2 = newNode.next;
newNode.next = temp;
newNode.next.next = tag2;
temp = tag;
}
System.out.println(newNode);
}
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.add("jack");
stack.add("tom");
stack.add("smith");
while (stack.size() >0) {
System.out.println(stack.pop());
}
}
class ChargeDoubleLinked{
DoubleHeroNode head = new DoubleHeroNode(0, "", "");
public DoubleHeroNode getHead(){
return head;
}
public void addNode(DoubleHeroNode node){
DoubleHeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = node;
node.pre = temp;
}
}
package LinkedList;
/**
* 单向循环链表
* @author houbj
* @date 2020/12/9 10:21
*/
public class CircleLinked {
public static void main(String[] args) {
ChargeCircleNode node = new ChargeCircleNode(new CircleNode(1,"11", "111"));
node.show();
node.add(new CircleNode(2,"22", "111"));
node.show();
}
}
class ChargeCircleNode{
private CircleNode head;
public ChargeCircleNode(CircleNode node){
this.head = node;
head.next = node;
}
public CircleNode getHead(){
return head;
}
public void add(CircleNode node){
if (head == null) {
throw new RuntimeException("没有该循环链表");
}
CircleNode temp = head;
while (true) {
if (temp.next == head) {
break;
}
temp = temp.next;
}
temp.next = node;
node.next = head;
}
public void show(){
if (head == null) {
System.out.println("单向环形链表为空");
return;
}
CircleNode temp = head;
while (true) {
System.out.println(temp);
if (temp.next == head) {
System.out.println("node - next:"+ temp.next);
break;
}
temp = temp.next;
}
}
}
class CircleNode{
private int id;
private String name;
private String nickName;
CircleNode next;
public CircleNode(int id,String name,String nickName){
this.id = id;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "CircleNode{" +
"id=" + id +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
package Stack;
import java.util.Arrays;
/**
* 用数组模拟栈
* @author houbj
* @date 2020/12/9 12:00
*/
public class ArrayStack {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(5);
stack.pushStack("l");
stack.pushStack("i");
stack.pushStack("i");
stack.pushStack("i");
stack.pushStack("i");
stack.pushStack("i");
System.out.println(stack);
stack.popStack();
stack.popStack();
stack.popStack();
stack.popStack();
stack.popStack();
stack.popStack();
stack.popStack();
System.out.println(stack);
}
private int top;
private String[] str;
public ArrayStack(int num){
str = new String[num];
top = -1;
}
public void pushStack(String strInfo) {
if (top + 1 >= str.length) {
System.out.println("该栈已满---");
return;
}
top++;
str[top] = strInfo;
}
public void popStack(){
if (top ==-1) {
System.out.println("该栈已空---");
return;
}
System.out.println(str[top] + " 已出栈 ---");
str[top] = null;
top--;
}
@Override
public String toString() {
return "ArrayStack{" +
"top=" + top +
", str=" + Arrays.toString(str) +
'}';
}
}
输入一个表达式【7 * 2 * 2 - 5 + 1 - 5 + 3 + 3】,计算结果
如果发现当前的符号栈为空,符号直接入栈
如果符号栈中有操作符,则比较操作符的优先级,如果栈中的优先级大于或者等于栈中的操作符,就需要从数栈中pop出两个数,在符号栈中pop出一个符号,再进行运算,将得到的结果入数栈,然后将未入栈的操作符入栈
如果当前操作符的优先级大于栈中的操作符,就直接入符号栈
输入一个表达式【711 * 22 * 2 - 35 + 1 - 5 + 3 + 3】,计算结果
输入一个表达式【(711 * 22 * 2 - 35) + 1 - 5 + 3 + 3】,计算结果
package Stack;
/**
* @author houbj
* @date 2020/12/9 17:27
*/
public class Recursion {
public static void main(String[] args) {
System.out.println("test1----- :");
test1(4);
System.out.println("test2----- :");
test2(4);
System.out.println("test3----- :");
System.out.println(test3(3));
}
public static void test1(int n){
if (n>2) {
test1(n - 1);
}
System.out.println("n = " + n);
}
public static void test2(int n){
if (n>2) {
test2(n - 1);
} else
System.out.println("n = " + n);
}
public static int test3(int n) {
if (n == 1) {
return 1;
} else return test3(n-1) * n;
}
}
// 输出结果:
test1----- :
n = 2
n = 3
n = 4
test2----- :
n = 2
test3----- :
6
public class MiGong {
public static void main(String[] args) {
int [][] map = new int[8][7];
for (int i = 0; i < 8; i++) {
map[i][0] = 1;
map[i][map[0].length -1]= 1;
}
for (int i = 0; i < 7; i++) {
map[0][i] = 1;
map[map.length -1 ][i] = 1;
}
map[3][1] = 1;
map[3][2] = 1;
show(map);
System.out.println( "----- ");
run(map, 1,1);
show(map);
}
/**
* 1 表示墙,0 表示没走过,2 表示路径 3 表示路不通
* 策略 下右上左
* @param map
* @param i
* @param j
* @return
*/
public static boolean run (int [][] map, int i, int j ) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
// 按照策略来走
map[i][j] = 2;
if (run(map, i+1, j)) { //下
return true;
} else if (run(map, i, j+1)){
return true;
} else if(run(map, i-1, j)) {
return true;
} else if(run(map, i, j -1)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else return false;
}
}
public static void show(int [][] map) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
System.out.print(map[i][j] + " " );
}
System.out.println();
}
}
}
package recursion;
/**
* @author houbj
* @date 2020/12/10 10:57
*/
public class EightQueen {
public static int max = 8;
public static int[] arr = new int[max];
public static void main(String[] args) {
EightQueen eightQueen = new EightQueen();
eightQueen.run(0);
}
public void run(int n){
if (n == max) {
show();
return;
}
for (int i = 0; i <max ; i++) {
arr[n] = i;
if (check(n)) {
run(n+1);
}
}
}
/**
* 判断是否符合规则
*/
public boolean check(int n){
for (int i = 0; i < n; i++) {
if (arr[i] == arr[n] || Math.abs(n - i) == Math.abs(arr[n]-arr[i])) {
return false;
}
}
return true;
}
/**
* 输出
*/
public static void show(){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+ " ");
}
System.out.println();
}
}
int i = 1;
int j = 0;
i++;
j++;
int n = j + i ;
int i = 1;
while(i<n) {
i = i * 2; // log2^n
}
int i = 1;
while(i<n) {
i = i * 3; // log3^n
}
for (int i = 0; i < n; i++) {
j = i;
i ++ ;
}
for (int i = 0; i < n; i++) {
j = i;
i ++ ;
while (m < n) {
i = i * 2;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
i++;
j++;
}
}
1.类似于时间复杂度的讨论,一个算法的空间复杂度(Space Complexity)定义为该算法所耗费的存储空间,它也是问题规模n的函数。
2.空间复杂度(Space Complexity)是对一个算法在运行过程中临时占用存储空间大小的量度。有的算法需要占用的临时工作单元数与解决问题的规模n有关,它随着n的增大而增大,当n较大时,将占用较多的存储单元,例如快速排序和归并排序算法就属于这种情况
3.在做算法分析时,主要讨论的是时间复杂度。从用户使用体验上看,更看重的程序执行的速度。一些缓存产品(redis, memcache)和算法(基数排序)本质就是用空间换时间.
package sort;
import java.util.Arrays;
/**
* @author houbj
* @date 2020/12/10 16:35
*/
public class BubbleSort {
public static void main(String[] args) {
int [] arr = new int[]{11,23 ,2, 46,12,33,89,32,27,29};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int [] arr){
int tag = 0;
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j] > arr[j+1]) {
tag = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tag;
}
}
}
}
}
import java.util.Arrays;
/**
* @author houbj
* @date 2020/12/10 16:35
*/
public class BubbleSort {
public static void main(String[] args) {
int [] arr = new int[]{11,23 ,2, 46,12,33,89,32,27,29};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int [] arr){
int tag = 0;
boolean flag = false;
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j] > arr[j+1]) {
flag = true;
tag = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tag;
}
}
if (!flag) {
break;
} else {
flag = false; // 重置 flag,进行下一次判断
}
System.out.println("进行一次循环: " + Arrays.toString(arr));
}
}
}
import java.util.Arrays;
/**
* 选择排序
* @author houbj
* @date 2020/12/10 17:22
*/
public class ChooseSort {
public static void main(String[] args) {
int [] arr = new int[]{11,23 ,2, 46,12,33,89,32,27,29};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int [] arr){
int tag;
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
tag = arr[i];
arr[i] = arr[j];
arr[j] = tag;
}
}
}
}
}
import java.util.Arrays;
/**
* @author houbj
* @date 2020/12/10 21:44
*/
public class InsertSort {
public static void main(String[] args) {
int [] arr = new int[]{11,23 ,2, 46,12,33,89,32,27,29};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int [] arr) {
for (int i = 1; i <= arr.length -1 ; i++) {
int indexValue = arr[i];
int index = i - 1;
while (index >= 0 && indexValue < arr[index]) {
arr[index+1] = arr[index];
index -- ;
}
arr[index + 1] = indexValue;
System.out.println(indexValue);
System.out.println(Arrays.toString(arr));
}
}
}
package sort;
import java.util.Arrays;
/**
* @author houbj
* @date 2020/12/14 10:12
*/
public class ShellSort {
public static void main(String[] args) {
int [] arr = new int[]{11,23 ,2, 46,12,33,89,32,27,29};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int[] arr){
int temp = 0;
for (int tag = arr.length / 2 ; tag > 0; tag /= 2) {
for (int i = tag; i < arr.length; i++) {
for (int j = i - tag; j >= 0 ; j -= tag) {
if (arr[j+tag] < arr[j]) {
temp = arr[j+tag];
arr[j+tag] = arr[j];
arr[j] = temp;
}
}
}
// System.out.println(Arrays.toString(arr));
// System.out.println("--");
}
}
}
package sort;
import java.util.Arrays;
/**
* @author houbj
* @date 2020/12/14 10:12
*/
public class ShellSort {
public static void main(String[] args) {
int [] arr = new int[]{11,23 ,2, 46,12,33,89,32,27,29};
sort(arr);
System.out.println(Arrays.toString(arr));
arr = new int[]{11,23 ,2, 46,12,33,89,32,27,29};
shellSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void shellSort(int [] arr) {
int temp = 0;
for (int tag = arr.length / 2 ; tag > 0; tag /= 2) {
for (int i = tag; i < arr.length ; i++) {
int j = i;
temp = arr[j];
while (j - tag >= 0 && temp < arr[j-tag]) {
arr[j] = arr[j-tag];
j -= tag;
}
arr[j] = temp;
}
}
}
}
package sort;
import java.util.Arrays;
/**
* @author houbj
* @date 2020/12/14 14:58
*/
public class QuickSort {
public static void main(String[] args) {
int [] arr = new int[]{11,23 ,2, 46,12,33,89,32,27,29};
sort(arr, 0 , arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void sort(int [] arr, int left, int right) {
int l = left;
int r = right;
int middle = arr[(left+right)/2];
System.out.println("middle:" + middle);
// = 0;
// while 循环的目的是 把比middle小的值放在左边,比middle大的值放在右边
while (l < r) {
while (arr[l] < middle) {
++ l;
}
while (arr[r] > middle) {
-- r;
}
// 如果 l >= r,代表左边全部是小于middle,右边大于middle
if (l >= r) {
break;
}
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
// 交换过后发现 array[l] == middle, l 前移
if (arr[l] == middle) {
r--;
}
if (arr[r] == middle) {
l++;
}
}
if (l == r) {
++l;
--r;
}
if (left < r) {
sort(arr, left, r);
}
if (right > l) {
sort(arr, l, right);
}
}
}
public class MergeSort {
public static void main(String[] args) {
int [] arr = new int[]{11,23 ,2, 46,12,33,89,32,27,29};
int [] temp = new int[arr.length];
sort(arr, 0, arr.length-1, temp);
System.out.println(Arrays.toString(arr));
}
public static void sort(int[] arr,int left, int right, int[] temp) {
if (left < right) {
int mid = (left + right) / 2;
// 向左递归进行分解
sort(arr, left, mid, temp);
// 向右递归进行分解
sort(arr, mid+1, right, temp);
merge(arr,left,mid,right,temp);
}
}
/**
*
* @param arr 原始数组
* @param left 左边有序序列的初始索引
* @param middle 中间索引
* @param right 右边索引
* @param temp 做中转的数组
*/
public static void merge(int[] arr, int left, int middle, int right,int[] temp) {
int l = left;
int r = middle + 1;
int t = 0; // 指向当前数组的索引
// step 1:
// 先把左右两边(有序)的数组按照规则填充到temp数组
// 直到左右两边的有序序列,有一边处理完为止
while (l <= middle && r <= right) {
// 如果左边的有序元素小于右边的有序元素 则将左边元素填充至temp
if (arr[l] <= arr[r]) {
temp[t] = arr[l];
t++;
l++;
} else {
temp[t] = arr[r];
t++;
r++;
}
}
// step 2:
// 把有剩余数据的一边数据依次全部填充到temp
while (l <= middle) {
temp[t] = arr[l];
l ++;
t ++;
}
while (r <= right) {
temp[t] = arr[r];
r ++;
t ++;
}
// step 3:
// 将temp数组的元素拷贝到arr
t = 0;
int tempLeft = left;
while (tempLeft <= right) {
arr[tempLeft] = temp[t];
tempLeft++;
t++;
}
}
}
package sort;
import java.util.Arrays;
/**
* @author houbj
* @date 2020/12/15 11:38
*/
public class RadixSort {
public static void main(String[] args) {
int [] arr = new int[]{11,23 ,2, 3647,46,12,33,89,32,27,29,138,0};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int[] arr){
// 1. 得到数组中最大数的位数
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
int maxLength = (max+"").length(); // 该数组中最大数的长度
int bucket[][] = new int[10][arr.length]; // 定义一个二维数组表示一个桶
int[] bucketElementCount = new int[arr.length];
for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
// 针对每个数的各个位,放置在不同的桶中
for (int j = 0; j < arr.length; j++) {
int digitElement = arr[j]/n%10; // 取出每个元素各个位上的值,放在桶中
bucket[digitElement][bucketElementCount[digitElement]] = arr[j];
bucketElementCount[digitElement] ++;
}
int index = 0 ; //按照桶中元素的顺序,将桶中的数据依次取出,放如原来的数组中
for (int j = 0; j < bucketElementCount.length; j++) {
if (bucketElementCount[j] != 0) {
for (int k = 0; k < bucketElementCount[j]; k++) {
arr[index++] = bucket[j][k];
}
}
bucketElementCount[j] = 0;
}
System.out.println("第" + (i+1)+"轮:"+ Arrays.toString(arr));
}
}
}
package Search;
/**
* @author houbj
* @date 2020/12/15 15:06
*/
public class LineSearch {
public static void main(String[] args) {
int [] arr = new int[]{0, 2, 11, 12, 23, 27, 29, 32, 33, 46, 89, 138, 3647};
int n = search(arr, 23);
System.out.println("在第"+ n+"个位置找到该数据");
}
/**
* 找到一个就返回
* @param arr
* @param num
* @return
*/
public static int search(int[] arr, int num) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == num) {
return i + 1;
}
}
return -1;
}
}
package Search;
import java.util.ArrayList;
/**
* 二分查找
* @author houbj
* @date 2020/12/15 15:20
*/
public class BinarySearch {
public static void main(String[] args) {
int [] arr = new int[]{0, 2, 11, 12, 23, 27, 29, 32, 33,33,33,33,33, 46, 89, 138, 3647};
int n = search(arr, 0, arr.length-1, 29);
System.out.println("在第"+ n+"个位置找到该数据");
ArrayList list = NotOneSearch(arr, 0, arr.length-1, 33);
System.out.println(list.toString());
}
/**
* 数组中只有一个目标数
* @param arr
* @param left
* @param right
* @param findVal
* @return
*/
private static int search(int[] arr, int left, int right, int findVal) {
if (left > right) {
return -1;
}
int mid = (left + right)/2;
if (findVal < arr[mid]) {
return search(arr,left, mid-1, findVal);
} else if (findVal > arr[mid]) {
return search(arr, mid+1, right, findVal) ;
} else {
return mid;
}
}
/**
* 数组中只有一个目标数
* @param arr
* @param left
* @param right
* @param findVal
* @return
*/
private static ArrayList NotOneSearch(int[] arr, int left, int right, int findVal) {
ArrayList list = new ArrayList();
if (left > right) {
return list;
}
int mid = (left + right)/2;
if (findVal < arr[mid]) {
return NotOneSearch(arr,left, mid-1, findVal);
} else if (findVal > arr[mid]) {
return NotOneSearch(arr, mid+1, right, findVal) ;
} else {
list.add(mid);
int temp = mid -1;
while (true) { // 左边查找
if (temp < 0 || arr[temp] != findVal) {
break;
}
list.add(temp);
temp --;
}
temp = mid+1;
while (true) { // 左边查找
if (temp > arr.length-1 || arr[temp] != findVal) {
break;
}
list.add(temp);
temp ++;
}
}
return list;
}
}
package Search;
import java.util.ArrayList;
/**
* @author houbj
* @date 2020/12/15 16:12
*/
public class InsertValueSearch {
public static void main(String[] args) {
int [] arr = new int[]{0, 2, 11, 12, 23, 27, 29, 32, 33,33,33,33,33, 46, 89, 138, 3647};
ArrayList list = NotOneSearch(arr, 0, arr.length-1, 3);
System.out.println(list.toString());
}
/**
* 数组中只有一个目标数
* @param arr
* @param left
* @param right
* @param findVal
* @return
*/
private static ArrayList NotOneSearch(int[] arr, int left, int right, int findVal) {
ArrayList list = new ArrayList();
// arr[0] > findVal || arr[arr.length-1] < findVal 必须需要的
// 否则我们得到的mid可能越界
if (left > right || arr[0] > findVal || arr[arr.length-1] < findVal) {
return list;
}
int mid = left +(right-left)* (findVal-arr[left]) / (arr[right]-arr[left]);
if (findVal < arr[mid]) {
return NotOneSearch(arr,left, mid-1, findVal);
} else if (findVal > arr[mid]) {
return NotOneSearch(arr, mid+1, right, findVal) ;
} else {
list.add(mid);
int temp = mid -1;
while (true) { // 左边查找
if (temp < 0 || arr[temp] != findVal) {
break;
}
list.add(temp);
temp --;
}
temp = mid+1;
while (true) { // 左边查找
if (temp > arr.length-1 || arr[temp] != findVal) {
break;
}
list.add(temp);
temp ++;
}
}
return list;
}
}
while(n>fib(k)-1)
k++;
package Search;
import java.util.ArrayList;
import java.util.Arrays;
/**
* @author houbj
* @date 2020/12/16 10:04
*/
public class FibSearch {
public static int maxSize = 10;
public static void main(String[] args) {
int [] arr = new int[]{0, 2, 11, 12, 23, 27 , 138, 3647};
int n = search(arr, 222);
System.out.println(n);
}
private static int search(int[] arr, int key) {
int low = 0;
int high = arr.length - 1;
int k = 0; // 表示斐波那契分割数值下标
int mid = 0;
int f[] = fib(); // 获取斐波那契数列
while (high > f[k]-1) {
k++;
}
// 因为F[k]可能大于数组长度,所以要补齐
int[] temp = Arrays.copyOf(arr, f[k]);
for (int i = high+1; i < temp.length; i++) {
temp[i] = arr[high];
}
while (low <= high) {
mid=low+f[k-1]-1;
if (key < temp[mid]) { // 应该向左边查找
high = mid -1;
k--;
} else if (key > temp[mid]) { // 应该向数组的后面查找
low = mid +1;
k -= 2;
} else {
if (mid <= high) {
return mid;
} else {
return high;
}
}
}
return -1;
}
public static int[] fib(){
int[] fib = new int[maxSize];
fib[0] = 1;
fib[1] = 1;
for (int i = 2; i < fib.length; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib;
}
}
package HashTable;
/**
* @author houbj
* @date 2020/12/16 14:16
*/
public class HashTable {
public static void main(String[] args) {
HashTab hashTab = new HashTab(7);
hashTab.add(new Employee(1,"li", 12,"www"));
hashTab.add(new Employee(2,"lLi", 12,"www"));
hashTab.add(new Employee(3,"lLLi", 12,"www"));
hashTab.add(new Employee(5,"lLLLi", 12,"www"));
hashTab.add(new Employee(0,"lLLLLi", 12,"www"));
hashTab.show();
}
}
class HashTab{
private EmployeeLinkedList[] employeeLinkedLists;
private int size;
public HashTab(int size) {
this.size = size;
this.employeeLinkedLists = new EmployeeLinkedList[size];
for (int i = 0; i < size; i++) {
this.employeeLinkedLists[i] = new EmployeeLinkedList();
}
}
public void add(Employee emp){
int num = hasFunction(emp.getId());
employeeLinkedLists[num].add(emp);
}
public void show(){
for (int i = 0; i < size; i++) {
employeeLinkedLists[i].list(i+1);
}
}
/**
* 编写散列函数
* @param id
* @return
*/
public int hasFunction(int id){
return id%size;
}
}
class EmployeeLinkedList{
private Employee head;
/**
* 添加雇员
* @param employee
*/
public void add(Employee employee){
if (this.head == null) {
this.head = employee;
return;
}
Employee emp = head;
while (true) {
if (emp.next == null) {
break;
}
emp = emp.next;
}
emp.next = employee;
}
/**
* 显示
* @param num
*/
public void list(int num){
if (this.head == null) {
System.out.println("第 " +num+ " 条链为空。。。");
return;
}
System.out.print("第 " +num+ " 条链,信息为:");
Employee emp = head;
while (true) {
System.out.printf("= > id = %d , name = %s ",head.getId(), head.getName());
if (emp.next == null ){
break;
}
emp = emp.next;
}
System.out.println();
}
public Employee getHead() {
return head;
}
public void setHead(Employee head) {
this.head = head;
}
}
class Employee{
private int id;
private String name;
private int age;
private String number;
public Employee next;
public Employee(int id, String name, int age, String number) {
this.id = id;
this.name = name;
this.age = age;
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
树有很多种,每个节点上最多只能有两个子节点的树称为二叉树
二叉树的子节点分为 左节点 和 右节点
完全二叉树 :如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,并且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树
package Tree;
/**
* @author houbj
* @date 2020/12/16 16:20
*/
public class BinaryTree {
public static void main(String[] args) {
BTree bTree = new BTree();
Node node1 = new Node(1,"songj");
Node node2 = new Node(2,"wuy");
Node node3 = new Node(3,"ljy");
Node node4 = new Node(4,"lc");
Node node5 = new Node(5,"guansheng");
node1.setLeft(node2);
node1.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
bTree.setNode(node1);
bTree.preShow();
bTree.midShow();
bTree.postShow();
bTree.preSearch(5);
bTree.midSearch(3);
bTree.postSearch(4);
bTree.delete(3);
bTree.preShow();
bTree.delete(5);
bTree.preShow();
}
}
/**
* 定义一个二叉树
*
*/
class BTree{
private Node node;
/**
* 删除
* @param id
*/
public void delete(int id) {
if (this.node.getId() == id) {
this.node = null;
return;
} else {
this.node.delete(id);
}
}
public void preSearch(int id){
Node nodeSearch = null;
nodeSearch = node.preOrderSearch(id);
if (nodeSearch != null) {
System.out.println(nodeSearch);
} else {
System.out.println("前序:未找到该数据");
}
}
public void midSearch(int id){
Node nodeSearch = null;
nodeSearch = node.midOrderSearch(id);
if (nodeSearch != null) {
System.out.println(nodeSearch);
} else {
System.out.println("后序:未找到该数据");
}
}
public void postSearch(int id){
Node nodeSearch = null;
nodeSearch = node.postOrderSearch(id);
if (nodeSearch != null) {
System.out.println(nodeSearch);
} else {
System.out.println("后序:未找到该数据");
}
}
public void preShow(){
System.out.println("前序遍历----- ");
if (this.node != null) {
this.node.preOrder();
} else {
System.out.println("二叉树为空,无法遍历");
}
}
public void midShow(){
System.out.println("中序遍历----- ");
if (this.node != null) {
this.node.midOrder();
} else {
System.out.println("二叉树为空,无法遍历");
}
}
public void postShow(){
System.out.println("后序遍历----- ");
if (this.node != null) {
this.node.postOrder();
} else {
System.out.println("二叉树为空,无法遍历");
}
}
public Node getNode() {
return node;
}
public void setNode(Node node) {
this.node = node;
}
}
class Node{
private int id;
private String name;
private Node left;
private Node right;
public Node(int id, String name) {
this.id = id;
this.name = name;
}
public void delete(int id){
if (this.left != null && this.left.id == id) {
this.left = null;
return;
}
if (this.right != null && this.right.id == id) {
this.right = null;
return;
}
if (this.left != null) {
this.left.delete(id);
}
if (this.right != null) {
this.right.delete(id);
}
}
/**
* 前序查找
* @param id
* @return
*/
public Node preOrderSearch(int id){
if (this.id == id) {
return this;
}
Node node = null;
if (this.left != null) {
node = this.left.preOrderSearch(id);
}
if (null != node) {
return node;
}
if (this.right != null) {
node = this.right.preOrderSearch(id);
}
return node;
}
/**
* 中序查找
* @param id
* @return
*/
public Node midOrderSearch(int id){
Node node = null;
if (this.left != null) {
node = this.left.midOrderSearch(id);
}
if (null != node) {
return node;
}
if (this.id == id) {
return this;
}
if (this.right != null) {
node = this.right.midOrderSearch(id);
}
return node;
}
/**
* 后序查找
* @param id
* @return
*/
public Node postOrderSearch(int id){
Node node = null;
if (this.left != null) {
node = this.left.postOrderSearch(id);
}
if (null != node) {
return node;
}
if (this.right != null) {
node = this.right.postOrderSearch(id);
}
if (null != node) {
return node;
}
if (this.id == id) {
node = this;
}
return node;
}
/**
* 前序遍历
*/
public void preOrder(){
System.out.println(this);
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
}
/**
* 中序遍历
*/
public void midOrder(){
if (this.left != null) {
this.left.midOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.midOrder();
}
}
/**
* 后序遍历
*/
public void postOrder(){
if (this.left != null) {
this.left.postOrder();
}
if (this.right != null) {
this.right.postOrder();
}
System.out.println(this);
}
@Override
public String toString() {
return "Node{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
}
package Tree;
/**
* 顺序存储二叉树
* @author houbj
* @date 2020/12/17 11:07
*/
public class OrderBTree {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5,6,7};
ArrayTree arrayTree = new ArrayTree(arr);
arrayTree.preShowArray(0);
}
}
class ArrayTree{
private int[] array;
public ArrayTree(int[] array) {
this.array = array;
}
/**
* 前序遍历
* @param index
*/
public void preShowArray(int index){
if (array == null || array.length == 0) {
System.out.println("数组为空----");
return;
}
System.out.println(array[index]);
if ((index*2+1) < array.length) {
preShowArray(index*2+1);
}
if ((index*2+2) < array.length) {
preShowArray(index*2+2);
}
}
}
package Tree;
/**
* 中序遍历线索二叉树
* @author houbj
* @date 2020/12/18 10:23
*/
public class MidThreadedBTree {
public static void main(String[] args) {
MidNode root = new MidNode(1, "l");
MidNode node3 = new MidNode(3, "l");
MidNode node8 = new MidNode(8, "l");
MidNode node10 = new MidNode(10, "l");
MidNode node6 = new MidNode(6, "l");
MidNode node14 = new MidNode(14, "l");
root.setLeft(node3);
node3.setRight(node10);
node3.setLeft(node8);
root.setRight(node6);
node6.setLeft(node14);
MidBTree midBTree = new MidBTree();
midBTree.setNode(root);
midBTree.threadedNodes(root);
MidNode midNode = node10.getLeft();
System.out.println("node10 - left : " + midNode);
System.out.println("node10 - right : " + node10.getRight());
midBTree.show();
}
}
class MidBTree{
private MidNode node;
// 为了实现线索化,需要创建要给指向当前节点的前驱节点的指针
// 在进行线索化时,pre 总是保留前一个结点
private MidNode pre = null;
/**
* 编写对二叉树进行中序线索化的方法
* @param node
*/
public void threadedNodes(MidNode node){
if (node == null) return;
// 1. 先线索化左子树
threadedNodes(node.getLeft());
// 2. 再线索化当前结点
if ( node.getLeft() == null) {
node.setLeft(pre);
node.setLeftTag(1);
}
if (pre != null && pre.getRight() == null) {
pre.setRight(node);
pre.setRightTag(1);
}
pre = node;
// 3. 最后线索化右子树
threadedNodes(node.getRight());
}
/**
* 遍历
*/
public void show(){
MidNode temp = node;
while (temp != null){
while (temp.getLeftTag() == 0) {
temp = temp.getLeft();
}
System.out.println(temp);
while (temp.getRightTag() == 1) {
temp = temp.getRight();
System.out.println(temp);
}
temp = temp.getRight();
}
}
public MidNode getNode() {
return node;
}
public void setNode(MidNode node) {
this.node = node;
}
}
class MidNode{
private int id;
private String name;
private MidNode left;
private MidNode right;
private int leftTag;
private int rightTag;
public MidNode(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "MidNode{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public int getLeftTag() {
return leftTag;
}
public void setLeftTag(int leftTag) {
this.leftTag = leftTag;
}
public int getRightTag() {
return rightTag;
}
public void setRightTag(int rightTag) {
this.rightTag = rightTag;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MidNode getLeft() {
return left;
}
public void setLeft(MidNode left) {
this.left = left;
}
public MidNode getRight() {
return right;
}
public void setRight(MidNode right) {
this.right = right;
}
}
package Tree;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author houbj
* @date 2020/12/18 15:27
*/
public class HuffmanTree {
public static void main(String[] args) {
preOrder(CreateHuffmanTree(new int[]{13,7,8,3,29,6,1}));
}
public static HuffmanNode CreateHuffmanTree(int[] arr){
List<HuffmanNode> nodes = new ArrayList<HuffmanNode>();
for (int value: arr) {
nodes.add(new HuffmanNode(value));
}
System.out.println("node : " + nodes);
HuffmanNode node = null;
while (nodes.size() > 1) {
Collections.sort(nodes);
System.out.println("nodeCompare : " + nodes);
HuffmanNode left = nodes.get(0);
HuffmanNode right = nodes.get(1);
node = new HuffmanNode(left.getValue() + right.getValue());
node.setLeft(left);
node.setRight(right);
nodes.remove(left);
nodes.remove(right);
nodes.add(node);
System.out.println("nodes : " + nodes);
}
return nodes.get(0);
}
public static void preOrder(HuffmanNode node){
if (node!= null ){
node.preOrder();
} else {
System.out.println("node 为空-----");
}
}
}
class HuffmanNode implements Comparable<HuffmanNode>{
private int value;
private HuffmanNode left;
private HuffmanNode right;
public HuffmanNode(int value) {
this.value = value;
}
/**
* 前序遍历
*/
public void preOrder(){
System.out.println(this);
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public HuffmanNode getLeft() {
return left;
}
public void setLeft(HuffmanNode left) {
this.left = left;
}
public HuffmanNode getRight() {
return right;
}
public void setRight(HuffmanNode right) {
this.right = right;
}
@Override
public String toString() {
return "HuffmanNode{" +
"value=" + value +
'}';
}
@Override
public int compareTo(HuffmanNode o) {
return this.value - o.value;
}
}
package Tree;
/**
* @author houbj
* @date 2020/12/21 14:37
*/
public class BinarySortTree {
public static void main(String[] args) {
int[] value = {7,3,10,12,5,1,9};
BinaryTreeCreate btCreate = new BinaryTreeCreate();
for (int i = 0; i < value.length; i++) {
btCreate.addNode(new BinarySortNode(value[i]));
}
btCreate.midShow();
}
}
class BinaryTreeCreate{
BinarySortNode root ;
public void addNode(BinarySortNode node){
if (root == null) {
root = node;
} else {
root.add(node);
}
}
public void midShow(){
if (root == null ){
System.out.println("根结点为空---");
return;
}else {
root.midShow();
}
}
}
class BinarySortNode{
private int value;
private BinarySortNode left;
private BinarySortNode right;
/**
* 添加
* @param node
*/
public void add(BinarySortNode node){
if (node == null) {
return;
}
if (this.value > node.value) {
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
}
/**
* 中序遍历
*/
public void midShow(){
if (this.left != null) {
this.left.midShow();
}
System.out.println(this);
if (this.right != null) {
this.right.midShow();
}
}
public BinarySortNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinarySortNode getLeft() {
return left;
}
public void setLeft(BinarySortNode left) {
this.left = left;
}
public BinarySortNode getRight() {
return right;
}
public void setRight(BinarySortNode right) {
this.right = right;
}
@Override
public String toString() {
return "BinarySortNode{" +
"value=" + value +
'}';
}
}
if (node.getLeft() == null && node.getRight() == null) { // 删除叶子节点
System.out.println("该节点为叶子节点---,删除叶子节点-------:"+ node.getValue());
if (parentNode.getLeft() != null && parentNode.getLeft().getValue() == value) {
parentNode.setLeft(null);
} else if(parentNode.getRight() != null && parentNode.getRight().getValue() == value) {
parentNode.setRight(null);
} else {
System.out.println("找错了--- ");
}
}
if (node.getLeft() != null) {
if (parentNode != null ){
if (parentNode.getLeft().getValue() == value) {
parentNode.setLeft(node.getLeft());
} else if (parentNode.getRight().getValue() ==value){
parentNode.setRight(node.getLeft());
}
} else {
this.root = node.getLeft();
}
} else if (node.getRight() != null){
if (parentNode != null ) {
if (parentNode.getLeft().getValue() == value) {
parentNode.setLeft(node.getRight());
} else if (parentNode.getRight().getValue() == value){
parentNode.setRight(node.getRight());
}
} else {
this.root = node.getRight();
}
}
else if(node.getLeft() !=null && node.getRight() != null){ // 删除有两个子节点的节点
System.out.println("该节点为叶子节点---,删除有2个子节点的节点-------:"+ node.getValue());
int minValue = findMinNode(node.getRight());
node.setValue(minValue);
}
public BinarySortNode findNodeParent(int value){
if (root == null) {
return null;
} else {
return this.root.searchParentNode(value);
}
}
package Tree;
/**
* @author houbj
* @date 2020/12/21 14:37
*/
public class BinarySortTree {
public static void main(String[] args) {
int[] value = {7,3,10,12,5,1,9,2};
BinaryTreeCreate btCreate = new BinaryTreeCreate();
for (int i = 0; i < value.length; i++) {
btCreate.addNode(new BinarySortNode(value[i]));
}
btCreate.midShow();
btCreate.delete(3);
btCreate.midShow();
}
}
class BinaryTreeCreate{
BinarySortNode root ;
public void addNode(BinarySortNode node){
if (root == null) {
root = node;
} else {
root.add(node);
}
}
public void midShow(){
if (root == null ){
System.out.println("根结点为空---");
return;
}else {
root.midShow();
}
}
/**
* 找到右节点上的最小节点
*/
public int findMinNode(BinarySortNode node){
BinarySortNode searchNode = node;
while (searchNode.getLeft() != null) {
searchNode = searchNode.getLeft();
}
delete(searchNode.getValue());
return searchNode.getValue();
}
/**
* 查找要删除的节点
* @param value
* @return
*/
public BinarySortNode findNode(int value){
if (root == null) {
return null;
} else {
return this.root.searchNode(value);
}
}
/**
* 查找要删除节点的父节点
* @param value
* @return
*/
public BinarySortNode findNodeParent(int value){
if (root == null) {
return null;
} else {
return this.root.searchParentNode(value);
}
}
public void delete(int value) {
if (root == null) {
System.out.println("根结点为空");
return;
}else {
BinarySortNode node = findNode(value);
System.out.println("node : " + node);
if (node == null ) {
System.out.println("该节点不存在--- ");
return;
} else {
if (this.root.getLeft() == null && this.root.getRight() == null){
System.out.println("该树只有一个节点");
this.root=null;
return;
}else {
BinarySortNode parentNode = findNodeParent(value);
if (node.getLeft() == null && node.getRight() == null) { // 删除叶子节点
System.out.println("该节点为叶子节点---,删除叶子节点-------:"+ node.getValue());
if (parentNode.getLeft() != null && parentNode.getLeft().getValue() == value) {
parentNode.setLeft(null);
} else if(parentNode.getRight() != null && parentNode.getRight().getValue() == value) {
parentNode.setRight(null);
} else {
System.out.println("找错了--- ");
}
}
else if(node.getLeft() !=null && node.getRight() != null){ // 删除有两个子节点的节点
System.out.println("该节点为叶子节点---,删除有2个子节点的节点-------:"+ node.getValue());
int minValue = findMinNode(node.getRight());
node.setValue(minValue);
}
else { //删除有一个子节点的节点
System.out.println("该节点为叶子节点---,删除有一个子节点的节点-------:"+ node.getValue());
if (node.getLeft() != null) {
if (parentNode != null ){
if (parentNode.getLeft().getValue() == value) {
parentNode.setLeft(node.getLeft());
} else if (parentNode.getRight().getValue() ==value){
parentNode.setRight(node.getLeft());
}
} else {
this.root = node.getLeft();
}
} else if (node.getRight() != null){
if (parentNode != null ) {
if (parentNode.getLeft().getValue() == value) {
parentNode.setLeft(node.getRight());
} else if (parentNode.getRight().getValue() == value){
parentNode.setRight(node.getRight());
}
} else {
this.root = node.getRight();
}
}
}
}
}
}
}
}
class BinarySortNode{
private int value;
private BinarySortNode left;
private BinarySortNode right;
/**
* 查找要删除节点
* @param value
* @return
*/
public BinarySortNode searchNode(int value){
if (this.value == value) {
return this;
} else if (value < this.value) {
if (this.left == null) {
return null;
}
return this.left.searchNode(value);
} else {
if (this.right == null) {
return null;
}
return this.right.searchNode(value);
}
}
/**
* 查找要删除元素的父节点
* @param value
* @return
*/
public BinarySortNode searchParentNode(int value){
if ((this.left != null && this.left.value == value) ||
(this.right != null && this.right.value == value)) {
return this;
} else {
if (value < this.value && this.left != null) {
return this.left.searchParentNode(value);
} else if (value >= this.value && this.right != null) {
return this.right.searchParentNode(value);
} else {
return null;
}
}
}
/**
* 添加
* @param node
*/
public void add(BinarySortNode node){
if (node == null) {
return;
}
if (this.value > node.value) {
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
}
/**
* 中序遍历
*/
public void midShow(){
if (this.left != null) {
this.left.midShow();
}
System.out.println(this);
if (this.right != null) {
this.right.midShow();
}
}
public BinarySortNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinarySortNode getLeft() {
return left;
}
public void setLeft(BinarySortNode left) {
this.left = left;
}
public BinarySortNode getRight() {
return right;
}
public void setRight(BinarySortNode right) {
this.right = right;
}
@Override
public String toString() {
return "BinarySortNode{" +
"value=" + value +
'}';
}
}
/**
* 添加 元素的时候进行左旋
* @param node
*/
public void add(AVLNode node){
if (node == null) {
return;
}
if (this.value > node.value) {
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
// 如果左子树高度 小于 右子树高度,则 左旋
if(findRightTreeHeight() - findLeftTreeHeight() > 1) {
leftRotate();
}
}
/**
* 左旋
*/
private void leftRotate(){
System.out.println("左旋");
// 以当前根节点的值创建新的节点
AVLNode node = new AVLNode(value);
// 把新节点的左子树设置为新节点的左子树
node.left = left;
// 把新节点的右子树设置为设置节点的右子树的左子树
node.right = right.left;
// 把当前节点的值替换为 右子节点的值
value = right.value;
// 将此节点的 right 替换
right = right.right;
// 将 此节点 的 left 替换
left = node;
}
/**
* 添加时进行右旋
* @param node
*/
public void add(AVLNode node){
if (node == null) {
return;
}
if (this.value > node.value) {
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
// 如果左子树高度 小于 右子树高度,则 左旋
if(findRightTreeHeight() - findLeftTreeHeight() > 1) {
leftRotate();
}
if (findLeftTreeHeight() - findRightTreeHeight() > 1) {
rightRotate();
}
}
/**
* 右旋
*/
public void rightRotate(){
System.out.println("右旋-");
AVLNode node = new AVLNode(value);
node.left = left.right;
node.right = right;
value = left.value;
left = left.left;
right = node;
}
/**
* 添加
* @param node
*/
public void add(AVLNode node){
if (node == null) {
return;
}
if (this.value > node.value) {
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
// 如果左子树高度 小于 右子树高度,则 左旋
if(findRightTreeHeight() - findLeftTreeHeight() > 1) {
if (right!=null && right.findLeftTreeHeight() > right.findLeftTreeHeight()) {
right.rightRotate();
leftRotate();
} else {
leftRotate();
}
}
if (findLeftTreeHeight() - findRightTreeHeight() > 1) {
if (left != null && left.findRightTreeHeight() > left.findLeftTreeHeight()) {
left.leftRotate();
rightRotate();
} else {
rightRotate();
}
}
}
package Tree;
/**
* @author houbj
* @date 2020/12/22 11:42
*/
public class AVLTree {
public static void main(String[] args) {
int[] arr = {4,3,5,6,7,8};
CreateAVLTree btCreate = new CreateAVLTree();
for (int i = 0; i < arr.length; i++) {
btCreate.addNode(new AVLNode(arr[i]));
}
btCreate.midShow();
System.out.println("Tree height : " + btCreate.root.findNodeHeight());
System.out.println("Tree left height : " + btCreate.root.findLeftTreeHeight());
System.out.println("Tree right height : " + btCreate.root.findRightTreeHeight());
arr = new int[]{10, 12, 8, 9, 7, 6};
btCreate = new CreateAVLTree();
for (int i = 0; i < arr.length; i++) {
btCreate.addNode(new AVLNode(arr[i]));
}
btCreate.midShow();
System.out.println("Tree height : " + btCreate.root.findNodeHeight());
System.out.println("Tree left height : " + btCreate.root.findLeftTreeHeight());
System.out.println("Tree right height : " + btCreate.root.findRightTreeHeight());
System.out.println("root :"+ btCreate.root);
arr = new int[]{10, 11, 7, 6, 8, 9};
btCreate = new CreateAVLTree();
for (int i = 0; i < arr.length; i++) {
btCreate.addNode(new AVLNode(arr[i]));
}
btCreate.midShow();
System.out.println("Tree height : " + btCreate.root.findNodeHeight());
System.out.println("Tree left height : " + btCreate.root.findLeftTreeHeight());
System.out.println("Tree right height : " + btCreate.root.findRightTreeHeight());
System.out.println("root :"+ btCreate.root);
arr = new int[]{2, 1, 6, 5, 7, 3};
btCreate = new CreateAVLTree();
for (int i = 0; i < arr.length; i++) {
btCreate.addNode(new AVLNode(arr[i]));
}
btCreate.midShow();
System.out.println("Tree height : " + btCreate.root.findNodeHeight());
System.out.println("Tree left height : " + btCreate.root.findLeftTreeHeight());
System.out.println("Tree right height : " + btCreate.root.findRightTreeHeight());
System.out.println("root :"+ btCreate.root);
}
}
class CreateAVLTree{
AVLNode root ;
public void addNode(AVLNode node){
if (root == null) {
root = node;
} else {
root.add(node);
}
}
public void midShow(){
if (root == null ){
System.out.println("根结点为空---");
return;
}else {
root.midShow();
}
}
/**
* 找到右节点上的最小节点
*/
public int findMinNode(AVLNode node){
AVLNode searchNode = node;
while (searchNode.getLeft() != null) {
searchNode = searchNode.getLeft();
}
delete(searchNode.getValue());
return searchNode.getValue();
}
/**
* 查找要删除的节点
* @param value
* @return
*/
public AVLNode findNode(int value){
if (root == null) {
return null;
} else {
return this.root.searchNode(value);
}
}
/**
* 查找要删除节点的父节点
* @param value
* @return
*/
public AVLNode findNodeParent(int value){
if (root == null) {
return null;
} else {
return this.root.searchParentNode(value);
}
}
public void delete(int value) {
if (root == null) {
System.out.println("根结点为空");
return;
}else {
AVLNode node = findNode(value);
System.out.println("node : " + node);
if (node == null ) {
System.out.println("该节点不存在--- ");
return;
} else {
if (this.root.getLeft() == null && this.root.getRight() == null){
System.out.println("该树只有一个节点");
this.root=null;
return;
}else {
AVLNode parentNode = findNodeParent(value);
if (node.getLeft() == null && node.getRight() == null) { // 删除叶子节点
System.out.println("该节点为叶子节点---,删除叶子节点-------:"+ node.getValue());
if (parentNode.getLeft() != null && parentNode.getLeft().getValue() == value) {
parentNode.setLeft(null);
} else if(parentNode.getRight() != null && parentNode.getRight().getValue() == value) {
parentNode.setRight(null);
} else {
System.out.println("找错了--- ");
}
}
else if(node.getLeft() !=null && node.getRight() != null){ // 删除有两个子节点的节点
System.out.println("该节点为叶子节点---,删除有2个子节点的节点-------:"+ node.getValue());
int minValue = findMinNode(node.getRight());
node.setValue(minValue);
}
else { //删除有一个子节点的节点
System.out.println("该节点为叶子节点---,删除有一个子节点的节点-------:"+ node.getValue());
if (node.getLeft() != null) {
if (parentNode != null ){
if (parentNode.getLeft().getValue() == value) {
parentNode.setLeft(node.getLeft());
} else if (parentNode.getRight().getValue() ==value){
parentNode.setRight(node.getLeft());
}
} else {
this.root = node.getLeft();
}
} else if (node.getRight() != null){
if (parentNode != null ) {
if (parentNode.getLeft().getValue() == value) {
parentNode.setLeft(node.getRight());
} else if (parentNode.getRight().getValue() == value){
parentNode.setRight(node.getRight());
}
} else {
this.root = node.getRight();
}
}
}
}
}
}
}
}
class AVLNode{
private int value;
private AVLNode left;
private AVLNode right;
/**
* 左旋
*/
private void leftRotate(){
System.out.println("左旋-");
// 以当前根节点的值创建新的节点
AVLNode node = new AVLNode(value);
// 把新节点的左子树设置为新节点的左子树
node.left = left;
// 把新节点的右子树设置为设置节点的右子树的左子树
node.right = right.left;
// 把当前节点的值替换为 右子节点的值
value = right.value;
// 将此节点的 right 替换
right = right.right;
// 将 此节点 的 left 替换
left = node;
}
/**
* 右旋
*/
public void rightRotate(){
System.out.println("右旋-");
AVLNode node = new AVLNode(value);
node.left = left.right;
node.right = right;
value = left.value;
left = left.left;
right = node;
}
/**
* 找到左子树的高度
* @return
*/
public int findLeftTreeHeight(){
if (left == null) {
return 0;
} else {
return left.findNodeHeight();
}
}
/**
* 找到右子树的高度
* @return
*/
public int findRightTreeHeight(){
if (right == null) {
return 0;
} else {
return right.findNodeHeight();
}
}
/**
* 查找树的高度
* @return
*/
public int findNodeHeight(){
return Math.max(left == null ? 0 : left.findNodeHeight(), right == null ? 0 : right.findNodeHeight()) + 1;
}
/**
* 查找要删除节点
* @param value
* @return
*/
public AVLNode searchNode(int value){
if (this.value == value) {
return this;
} else if (value < this.value) {
if (this.left == null) {
return null;
}
return this.left.searchNode(value);
} else {
if (this.right == null) {
return null;
}
return this.right.searchNode(value);
}
}
/**
* 查找要删除元素的父节点
* @param value
* @return
*/
public AVLNode searchParentNode(int value){
if ((this.left != null && this.left.value == value) ||
(this.right != null && this.right.value == value)) {
return this;
} else {
if (value < this.value && this.left != null) {
return this.left.searchParentNode(value);
} else if (value >= this.value && this.right != null) {
return this.right.searchParentNode(value);
} else {
return null;
}
}
}
/**
* 添加
* @param node
*/
public void add(AVLNode node){
if (node == null) {
return;
}
if (this.value > node.value) {
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
// 如果左子树高度 小于 右子树高度,则 左旋
if(findRightTreeHeight() - findLeftTreeHeight() > 1) {
if (right!=null && right.findLeftTreeHeight() > right.findLeftTreeHeight()) {
right.rightRotate();
leftRotate();
} else {
leftRotate();
}
}
if (findLeftTreeHeight() - findRightTreeHeight() > 1) {
if (left != null && left.findRightTreeHeight() > left.findLeftTreeHeight()) {
left.leftRotate();
rightRotate();
} else {
rightRotate();
}
}
}
/**
* 中序遍历
*/
public void midShow(){
if (this.left != null) {
this.left.midShow();
}
System.out.println(this);
if (this.right != null) {
this.right.midShow();
}
}
public AVLNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public AVLNode getLeft() {
return left;
}
public void setLeft(AVLNode left) {
this.left = left;
}
public AVLNode getRight() {
return right;
}
public void setRight(AVLNode right) {
this.right = right;
}
@Override
public String toString() {
return "AVLNode{" +
"value=" + value +
'}';
}
}
package Graph;
import java.util.ArrayList;
/**
* 图
* @author houbj
* @date 2020/12/24 09:45
*/
public class Graph {
private ArrayList<String> vertexList; //存储顶点的集合
private int[][] edges; // 存储图对应的邻接矩阵
private int numOfEdges; // 边的数量
public static void main(String[] args) {
int n = 5; // 节点个数为5
String vertexValue[] = {"A","B", "C", "D", "E"};
Graph graph = new Graph(n);
for (String value:vertexValue){
graph.insertVertex(value);
}
graph.insertEdges(1,3,3);
graph.insertEdges(4,3,2);
graph.show();
}
public Graph(int n){ // 顶点个数
vertexList = new ArrayList<String>(n);
edges = new int[n][n];
numOfEdges = 0;
}
/**
* 插入节点
*/
public void insertVertex(String vertex){
vertexList.add(vertex);
}
/**
*
* @param v1 第一个元素顶点的下标
* @param v2 第二个元素顶点的下标
* @param weight 权值
*/
public void insertEdges(int v1, int v2, int weight) {
edges[v1][v2] = weight;
edges[v2][v1] = weight;
numOfEdges++;
}
/**
* 得到节点的个数
* @return
*/
public int getNumOfVertex(){
return vertexList.size();
}
/**
*
* @return 返回边的数目
*/
public int getNumOfEdges(){
return numOfEdges;
}
/**
*
* @param n
* @return 返回对应下标的节点值
*/
public String getInsertVertex(int n){
return vertexList.get(n);
}
/**
* 返回权值
* @param v1
* @param v2
* @return
*/
public int getWeight(int v1, int v2){
return edges[v1][v2];
}
public void show(){
System.out.print(" ");
for (int i = 0; i < vertexList.size(); i++) {
System.out.print(vertexList.get(i) + " ");
}
System.out.println();
for (int i = 0; i < edges.length; i++) {
System.out.print(vertexList.get(i) + " " );;
for (int j = 0; j < edges[i].length; j++) {
System.out.print(edges[i][j] + " ");
}
System.out.println();
}
}
}
package Graph;
import java.util.ArrayList;
/**
* 图
* @author houbj
* @date 2020/12/24 09:45
*/
public class Graph {
private ArrayList<String> vertexList; //存储顶点的集合
private int[][] edges; // 存储图对应的邻接矩阵
private int numOfEdges; // 边的数量
private boolean[] isVisited;
public static void main(String[] args) {
int n = 5; // 节点个数为5
String vertexValue[] = {"A","B", "C", "D", "E"};
Graph graph = new Graph(n);
for (String value:vertexValue){
graph.insertVertex(value);
}
graph.insertEdges(1,3,3);
graph.insertEdges(4,3,2);
graph.show();
graph.dfs();
}
/**
* 得到第一个邻接节点的下标
* @param index
* @return
*/
public int getFirstNeighbor(int index){
for (int i = 0; i < vertexList.size(); i++) {
if (edges[index][i] > 0){
return i;
}
}
return -1;
}
/**
* 根据前一个领结节点的下标获取下一个领结节点
* @param v1
* @param v2
* @return
*/
public int getNextNeighbor(int v1, int v2){
for (int i = v2 + 1; i < vertexList.size(); i++) {
if (edges[v1][i] > 0) {
return i;
}
}
return -1;
}
public void dfs(boolean[] isVisited, int i) {
System.out.print(getInsertVertex(i) + "->");
isVisited[i] = true;
int w = getFirstNeighbor(i);
while (w != -1) {
if (!isVisited[w]) {
dfs(isVisited,w);
} else { //如果已经被访问过
w= getNextNeighbor(i, w);
}
}
}
/**
* 对dfs进行重载,遍历我们的所有节点,并进行DFS
*/
public void dfs(){
for (int i = 0; i < getNumOfVertex(); i++) {
if (!isVisited[i]) {
dfs(isVisited,i);
}
}
}
public Graph(int n){ // 顶点个数
vertexList = new ArrayList<String>(n);
edges = new int[n][n];
numOfEdges = 0;
isVisited = new boolean[n];
}
/**
* 插入节点
*/
public void insertVertex(String vertex){
vertexList.add(vertex);
}
/**
*
* @param v1 第一个元素顶点的下标
* @param v2 第二个元素顶点的下标
* @param weight 权值
*/
public void insertEdges(int v1, int v2, int weight) {
edges[v1][v2] = weight;
edges[v2][v1] = weight;
numOfEdges++;
}
/**
* 得到节点的个数
* @return
*/
public int getNumOfVertex(){
return vertexList.size();
}
/**
*
* @return 返回边的数目
*/
public int getNumOfEdges(){
return numOfEdges;
}
/**
*
* @param n
* @return 返回对应下标的节点值
*/
public String getInsertVertex(int n){
return vertexList.get(n);
}
/**
* 返回权值
* @param v1
* @param v2
* @return
*/
public int getWeight(int v1, int v2){
return edges[v1][v2];
}
public void show(){
System.out.print(" ");
for (int i = 0; i < vertexList.size(); i++) {
System.out.print(vertexList.get(i) + " ");
}
System.out.println();
for (int i = 0; i < edges.length; i++) {
System.out.print(vertexList.get(i) + " " );;
for (int j = 0; j < edges[i].length; j++) {
System.out.print(edges[i][j] + " ");
}
System.out.println();
}
}
}
1 若结点w尚未被访问,则访问结点w并标记为已访问。
2 结点w入队列
3 查找结点u的继w邻接结点后的下一个邻接结点w,转到步骤6。
package tenAlto;
/**
* 二分查找,非递归
* @author houbj
* @date 2020/12/25 09:36
*/
public class BinarySearch {
public static void main(String[] args) {
int [] arr = new int[]{1,20,34,36,46,100};
int result = search(arr,2);
System.out.println("result : "+result);
}
public static int search(int[] arr, int target){
int left = 0;
int right = arr.length-1;
int count = 1;
while (left<=right) {
System.out.println("count: " + count++);
int mid = (left + right) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] > target){
right = mid-1;
} else {
left = mid+1;
}
}
return -1;
}
}
package tenAlto;
/**
* 汉诺塔
* @author houbj
* @date 2020/12/25 10:13
*/
public class HanNuoTower {
public static void main(String[] args) {
tower(3,'A','B','C');
}
public static void tower(int num, char a, char b, char c){
if (num == 1) {
System.out.println("第一个盘从 "+ a + " -->" + c);
} else {
// (1)先把 最上面的盘 A->B,移动过程中回使用到C
tower(num-1,a,c,b);
// (2)把下面的盘放到C
System.out.println("第"+ num +"个盘,从" + a +"-->" +c);
// 把B塔所有的盘从B 到 C
tower(num-1, b, a, c);
}
}
}
每次遍历到的第i个物品,根据w[i]和v[i]来确定是否需要将该物品放入背包中。即对于给定的n个物品,设v[i]、w[i]分别为第i个物品的价值和重量,C为背包的容量。再令v[i][j]表示在前i个物品中能够装入容量为j的背包中的最大价值
v[i][0]=v[0][j]=0; //表示 填入表 第一行和第一列是0
当w[i]> j 时:v[i][j]=v[i-1][j] // 当准备加入新增的商品的容量大于 当前背包的容量时,就直接使用上一个单元格的装入策略
当j>=w[i]时: v[i][j]=max{v[i-1][j], v[i]+v[i-1][j-w[i]]}
// 当 准备加入的新增的商品的容量小于等于当前背包的容量,
// 装入的方式:
v[i-1][j]: 就是上一个单元格的装入的最大值
v[i] : 表示当前商品的价值
v[i-1][j-w[i]] : 装入i-1商品,到剩余空间j-w[i]的最大值
当j>=w[i]时: v[i][j]=max{v[i-1][j], v[i]+v[i-1][j-w[i]]}
package tenAlto;
/**
* @author houbj
* @date 2020/12/25 13:46
*/
public class ViolenceMatch {
public static void main(String[] args) {
String str1= "硅硅谷 尚硅谷你尚硅 尚硅谷你尚硅谷你尚硅你好";
String str2 = "尚硅谷你尚硅你";
System.out.println(match(str1,str2));
}
public static int match(String str1, String str2){
char[] s1 = str1.toCharArray();
char[] s2 = str2.toCharArray();
int s1Len = s1.length;
int s2Len = s2.length;
int i = 0; // 指向 s1 的索引
int j = 0; // 指向 s2 的索引
while (i<s1Len && j < s2Len) { // 保证匹配时,不越界
if (s1[i] == s2[j]) {
i++;
j++;
} else {
i = i-(j-1);
j = 0;
}
}
if (j == s2.length) {
return i - j;
}
return -1;
}
}
package tenAlgo;
import java.util.Arrays;
/**
* @author houbj
* @date 2020/12/25 15:53
*/
public class KMPALgo {
public static void main(String[] args) {
String str1 = "BBC ABCDAB ABCDABCDABDE";
String str2 = "ABCDABD";
// String str3 = "ABAABBB";
int[] next = kmpNext(str2);
System.out.println("next : " + Arrays.toString(next));
System.out.println("index : " + kmpSearch(str1, str2, next));
}
public static int kmpSearch(String str1, String str2, int[] next){
for (int i = 0, j = 0; i < str1.length(); i++) {
while (j > 0 && str1.charAt(i) != str2.charAt(j)) {
j = next[j - 1];
}
if (str1.charAt(i) == str2.charAt(j)) {
j++;
}
if (j == str2.length()) {
return i-j+1;
}
}
return -1;
}
/**
* 获取到一个字符串的部分匹配值
* @param str 子串
* @return
*/
public static int[] kmpNext(String str){
int[] next = new int[str.length()];
next[0] = 0;
for (int i = 1, j = 0; i < str.length(); i++) {
while (j > 0 && str.charAt(i) != str.charAt(j)) {
// System.out.println("i : " + i + " j : "+ j);
j = next[j-1];
// System.out.println("- " + j);
}
if (str.charAt(i) == str.charAt(j)) {
j ++;
}
next[i] = j;
System.out.println(j);
}
return next;
}
}