From df1224d30ae972b75dca9f3aa1ec14f15f171605 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 10:37:39 +0800
Subject: [PATCH 01/46] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=86=92=E6=B3=A1?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F=E7=AE=97=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/enums/StatusCode.java | 33 ++++++++++++
src/main/java/com/lin/sort/BubbleSort.java | 53 +++++++++++++++++++
.../java/com/lin/TestSort/BubbleSortTest.java | 20 +++++++
3 files changed, 106 insertions(+)
create mode 100644 src/main/java/com/lin/enums/StatusCode.java
create mode 100644 src/main/java/com/lin/sort/BubbleSort.java
create mode 100644 src/test/java/com/lin/TestSort/BubbleSortTest.java
diff --git a/src/main/java/com/lin/enums/StatusCode.java b/src/main/java/com/lin/enums/StatusCode.java
new file mode 100644
index 0000000..5fea2c4
--- /dev/null
+++ b/src/main/java/com/lin/enums/StatusCode.java
@@ -0,0 +1,33 @@
+package com.lin.enums;
+
+public enum StatusCode {
+
+
+ MIX_SORT(-1, "小到大"),
+ MAX_SORT(1, "大到小");
+
+
+ int code;
+ String desc;
+
+ StatusCode(int code, String desc) {
+ this.code = code;
+ this.desc = desc;
+ }
+
+ public int getCode() {
+ return code;
+ }
+
+ public void setCode(int code) {
+ this.code = code;
+ }
+
+ public String getDesc() {
+ return desc;
+ }
+
+ public void setDesc(String desc) {
+ this.desc = desc;
+ }
+}
diff --git a/src/main/java/com/lin/sort/BubbleSort.java b/src/main/java/com/lin/sort/BubbleSort.java
new file mode 100644
index 0000000..ea131da
--- /dev/null
+++ b/src/main/java/com/lin/sort/BubbleSort.java
@@ -0,0 +1,53 @@
+package com.lin.sort;
+
+import com.lin.enums.StatusCode;
+
+/**
+ * 冒泡排序
+ */
+public class BubbleSort {
+
+
+ public static void bubbleSorted(int[] array, StatusCode statusCode) {
+ for (int i = 0; i < array.length; i++) {
+ for (int j = 0; j < array.length; j++) {
+ int falg = compare(array[i], array[j]);
+ //可修改排序方向
+ if (falg == statusCode.getCode()) {
+ swap(array, i, j);
+ }
+ }
+ }
+ }
+
+ /**
+ * 交换
+ *
+ * @param array
+ * @param a
+ * @param b
+ */
+ public static void swap(int[] array, int a, int b) {
+ int temp = array[a];
+ array[a] = array[b];
+ array[b] = temp;
+ }
+
+ /**
+ * 比较器
+ *
+ * @param a
+ * @param b
+ * @return
+ */
+ public static int compare(int a, int b) {
+ if (a > b) {
+ return 1;
+ } else if (a < b) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+}
diff --git a/src/test/java/com/lin/TestSort/BubbleSortTest.java b/src/test/java/com/lin/TestSort/BubbleSortTest.java
new file mode 100644
index 0000000..d20ecbb
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/BubbleSortTest.java
@@ -0,0 +1,20 @@
+package com.lin.TestSort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.BubbleSort;
+import org.junit.Test;
+
+public class BubbleSortTest {
+
+
+ @Test
+ public void bubbleSortedTest(){
+ int[] a = {1,6,8,2,4,5,7,1,0};
+ BubbleSort.bubbleSorted(a, StatusCode.MAX_SORT);
+ for (int i : a) {
+ System.out.println(i);
+ }
+
+ }
+
+}
--
Gitee
From 18804d4a10e1ad2c4037e389363c2ca0856f8ada Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 10:47:34 +0800
Subject: [PATCH 02/46] =?UTF-8?q?=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/BubbleSort.java | 33 ++--------------
src/main/java/com/lin/sort/SelectionSort.java | 19 ++++++++++
src/main/java/com/lin/sort/util/SortUtil.java | 38 +++++++++++++++++++
3 files changed, 60 insertions(+), 30 deletions(-)
create mode 100644 src/main/java/com/lin/sort/SelectionSort.java
create mode 100644 src/main/java/com/lin/sort/util/SortUtil.java
diff --git a/src/main/java/com/lin/sort/BubbleSort.java b/src/main/java/com/lin/sort/BubbleSort.java
index ea131da..8927176 100644
--- a/src/main/java/com/lin/sort/BubbleSort.java
+++ b/src/main/java/com/lin/sort/BubbleSort.java
@@ -1,6 +1,7 @@
package com.lin.sort;
import com.lin.enums.StatusCode;
+import com.lin.sort.util.SortUtil;
/**
* 冒泡排序
@@ -11,43 +12,15 @@ public class BubbleSort {
public static void bubbleSorted(int[] array, StatusCode statusCode) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
- int falg = compare(array[i], array[j]);
+ int falg = SortUtil.compare(array[i], array[j]);
//可修改排序方向
if (falg == statusCode.getCode()) {
- swap(array, i, j);
+ SortUtil.swap(array, i, j);
}
}
}
}
- /**
- * 交换
- *
- * @param array
- * @param a
- * @param b
- */
- public static void swap(int[] array, int a, int b) {
- int temp = array[a];
- array[a] = array[b];
- array[b] = temp;
- }
- /**
- * 比较器
- *
- * @param a
- * @param b
- * @return
- */
- public static int compare(int a, int b) {
- if (a > b) {
- return 1;
- } else if (a < b) {
- return -1;
- } else {
- return 0;
- }
- }
}
diff --git a/src/main/java/com/lin/sort/SelectionSort.java b/src/main/java/com/lin/sort/SelectionSort.java
new file mode 100644
index 0000000..4cd73a8
--- /dev/null
+++ b/src/main/java/com/lin/sort/SelectionSort.java
@@ -0,0 +1,19 @@
+package com.lin.sort;
+
+import com.lin.enums.StatusCode;
+
+public class SelectionSort {
+
+
+
+
+ public static void selectionSorted(int[] array , StatusCode statusCode){
+
+
+
+
+ }
+
+
+
+}
diff --git a/src/main/java/com/lin/sort/util/SortUtil.java b/src/main/java/com/lin/sort/util/SortUtil.java
new file mode 100644
index 0000000..331e81f
--- /dev/null
+++ b/src/main/java/com/lin/sort/util/SortUtil.java
@@ -0,0 +1,38 @@
+package com.lin.sort.util;
+
+/**
+ * 排序工具
+ */
+public class SortUtil {
+
+ /**
+ * 交换
+ *
+ * @param array
+ * @param a
+ * @param b
+ */
+ public static void swap(int[] array, int a, int b) {
+ int temp = array[a];
+ array[a] = array[b];
+ array[b] = temp;
+ }
+
+ /**
+ * 比较器
+ *
+ * @param a
+ * @param b
+ * @return
+ */
+ public static int compare(int a, int b) {
+ if (a > b) {
+ return 1;
+ } else if (a < b) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+
+}
--
Gitee
From b1865bffbb41ede649fe1de93251c82c635ff95f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 10:58:42 +0800
Subject: [PATCH 03/46] =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E9=80=89=E6=8B=A9?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/SelectionSort.java | 22 ++++++++++++++-----
.../com/lin/TestSort/SelectionSortTest.java | 18 +++++++++++++++
2 files changed, 35 insertions(+), 5 deletions(-)
create mode 100644 src/test/java/com/lin/TestSort/SelectionSortTest.java
diff --git a/src/main/java/com/lin/sort/SelectionSort.java b/src/main/java/com/lin/sort/SelectionSort.java
index 4cd73a8..39d6b86 100644
--- a/src/main/java/com/lin/sort/SelectionSort.java
+++ b/src/main/java/com/lin/sort/SelectionSort.java
@@ -1,19 +1,31 @@
package com.lin.sort;
import com.lin.enums.StatusCode;
+import com.lin.sort.util.SortUtil;
public class SelectionSort {
+ public static void selectionSorted(int[] array, StatusCode statusCode) {
+ //确定最小的位置
+ for (int i = 0; i < array.length; i++) {
+ int min = i;
+ //确定最小的位置存放的数据
+ for (int j = i + 1; j < array.length; j++) {
+ //用标志为查出为排序队列中最小/大的
+ if (statusCode.getCode() == SortUtil.compare(array[j], array[min])) {
+ min = j;
+ }
+ }
+ //将最小的放在最前面
+ if (min != i) {
+ SortUtil.swap(array, i, min);
+ }
-
- public static void selectionSorted(int[] array , StatusCode statusCode){
-
-
+ }
}
-
}
diff --git a/src/test/java/com/lin/TestSort/SelectionSortTest.java b/src/test/java/com/lin/TestSort/SelectionSortTest.java
new file mode 100644
index 0000000..a53ff6f
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/SelectionSortTest.java
@@ -0,0 +1,18 @@
+package com.lin.TestSort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.SelectionSort;
+import org.junit.Test;
+
+public class SelectionSortTest {
+
+
+ @Test
+ public void selectionSortedTest() {
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
+ SelectionSort.selectionSorted(a,StatusCode.MIX_SORT);
+ for (int i : a) {
+ System.out.println(i);
+ }
+ }
+}
--
Gitee
From 41e3f5d1234760b02796f2f4f59f1eedd05044ea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 11:27:10 +0800
Subject: [PATCH 04/46] =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=8F=92=E5=85=A5?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/InsertionSort.java | 26 +++++++++++++++++++
.../com/lin/TestSort/InsertionSortTest.java | 18 +++++++++++++
2 files changed, 44 insertions(+)
create mode 100644 src/main/java/com/lin/sort/InsertionSort.java
create mode 100644 src/test/java/com/lin/TestSort/InsertionSortTest.java
diff --git a/src/main/java/com/lin/sort/InsertionSort.java b/src/main/java/com/lin/sort/InsertionSort.java
new file mode 100644
index 0000000..d80426a
--- /dev/null
+++ b/src/main/java/com/lin/sort/InsertionSort.java
@@ -0,0 +1,26 @@
+package com.lin.sort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.util.SortUtil;
+
+public class InsertionSort {
+
+
+ public static void insertionSorted(int[] array, StatusCode statusCode) {
+ //默认第0个是排好序的,i以前的排好序
+ for (int i = 1; i < array.length; i++) {
+ int get = array[i];
+ int j = i - 1;
+ //从当前位值倒序往前查找
+ while (j >= 0 && statusCode.getCode() == SortUtil.compare(get, array[j])) {
+ array[j + 1] = array[j];
+ j--;
+ }
+ //不满足比较条件时放在位置的右边
+ array[j + 1] = get;
+ }
+
+ }
+
+
+}
diff --git a/src/test/java/com/lin/TestSort/InsertionSortTest.java b/src/test/java/com/lin/TestSort/InsertionSortTest.java
new file mode 100644
index 0000000..119081b
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/InsertionSortTest.java
@@ -0,0 +1,18 @@
+package com.lin.TestSort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.InsertionSort;
+import org.junit.Test;
+
+public class InsertionSortTest {
+
+
+ @Test
+ public void insertionSortedtest() {
+ int[] a = {1,6,8,2,4,5,7,1,0};
+ InsertionSort.insertionSorted(a, StatusCode.MIX_SORT);
+ for (int i : a) {
+ System.out.println(i);
+ }
+ }
+}
--
Gitee
From d2d37bc0f4c76babb7c9d911e1b7666cf12496c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 13:56:48 +0800
Subject: [PATCH 05/46] =?UTF-8?q?=E5=B8=8C=E5=B0=94=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/ShellSort.java | 33 +++++++++++++++++++
.../java/com/lin/TestSort/ShellSortTest.java | 21 ++++++++++++
2 files changed, 54 insertions(+)
create mode 100644 src/main/java/com/lin/sort/ShellSort.java
create mode 100644 src/test/java/com/lin/TestSort/ShellSortTest.java
diff --git a/src/main/java/com/lin/sort/ShellSort.java b/src/main/java/com/lin/sort/ShellSort.java
new file mode 100644
index 0000000..0306c19
--- /dev/null
+++ b/src/main/java/com/lin/sort/ShellSort.java
@@ -0,0 +1,33 @@
+package com.lin.sort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.util.SortUtil;
+
+public class ShellSort {
+
+
+ public static void shellSorted(int[] array, StatusCode statusCode) {
+ int shell = 0;
+ //寻找最大的希尔值
+ while (shell <= array.length) {
+ shell = shell * 3 + 1;
+ }
+ //通过希尔来降低排序复杂度
+ while (shell >= 1) {
+ //确定希尔值后,内部使用呢直接插入排序
+ for (int i = shell; i < array.length; i++) {
+ int j = i - shell;
+ int get = array[i];
+ while (j >= 0 && statusCode.getCode() == SortUtil.compare(get, array[j])) {
+ array[j + shell] = array[j];
+ j = j - shell;
+ }
+ array[j + shell] = get;
+ }
+ shell = (shell - 1) / 3;
+ }
+
+
+ }
+
+}
diff --git a/src/test/java/com/lin/TestSort/ShellSortTest.java b/src/test/java/com/lin/TestSort/ShellSortTest.java
new file mode 100644
index 0000000..f138176
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/ShellSortTest.java
@@ -0,0 +1,21 @@
+package com.lin.TestSort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.ShellSort;
+import org.junit.Test;
+
+public class ShellSortTest {
+
+ @Test
+ public void shellSortTest(){
+ int[] a = {1,6,8,2,4,5,7,1,0};
+ ShellSort.shellSorted(a,StatusCode.MIX_SORT);
+ for (int i : a) {
+ System.out.println(i);
+ }
+ }
+
+
+
+
+}
--
Gitee
From 1685666e8dc314ef5045b4a11e7d6a8140a07643 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 15:38:54 +0800
Subject: [PATCH 06/46] =?UTF-8?q?=E9=80=92=E5=BD=92=E5=B8=8C=E5=B0=94?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/MergeSort.java | 81 +++++++++++++++++++
.../java/com/lin/TestSort/MergeSortTest.java | 28 +++++++
2 files changed, 109 insertions(+)
create mode 100644 src/main/java/com/lin/sort/MergeSort.java
create mode 100644 src/test/java/com/lin/TestSort/MergeSortTest.java
diff --git a/src/main/java/com/lin/sort/MergeSort.java b/src/main/java/com/lin/sort/MergeSort.java
new file mode 100644
index 0000000..20c83d2
--- /dev/null
+++ b/src/main/java/com/lin/sort/MergeSort.java
@@ -0,0 +1,81 @@
+package com.lin.sort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.util.SortUtil;
+
+public class MergeSort {
+
+
+ /**
+ * 非递归归并排序(错误)
+ *
+ * @param array
+ * @param statusCode
+ */
+ public static void mergeSortedIteration(int array[], StatusCode statusCode) {
+ int left, mid, right;
+ for (int i = 1; i < array.length; i *= 2) {
+ left = 0;
+ while (left + i < array.length) {
+ mid = left + i - 1;
+ right = mid + 1 < array.length ? mid + 1 : array.length - 1;
+ merge(array, statusCode, left, mid, right);
+ left = mid + 1;
+ }
+ }
+ }
+
+
+ /**
+ * 递归使用归并排序
+ *
+ * @param array
+ * @param statusCode
+ * @param left
+ * @param right
+ */
+ public static void mergeSortedRecursion(int[] array, StatusCode statusCode, int left, int right) {
+ if (left == right)
+ return;
+ int mid = (left + right) / 2;
+ mergeSortedRecursion(array, statusCode, left, mid);
+ mergeSortedRecursion(array, statusCode, mid + 1, right);
+ merge(array, statusCode, left, mid, right);
+ }
+
+ /**
+ * 数组合并
+ *
+ * @param array
+ * @param statusCode
+ * @param left
+ * @param mid
+ * @param right
+ */
+ private static void merge(int[] array, StatusCode statusCode, int left, int mid, int right) {
+ int len = right - left + 1;
+ int[] temp = new int[len];
+ int index = 0;
+ //前半段数组起始
+ int i = left;
+ //后半段数组起始
+ int j = mid + 1;
+ //两组数据归并操作
+ while (i <= mid && j <= right) {
+ temp[index++] = statusCode.getCode() == SortUtil.compare(array[i], array[j]) ? array[i++] : array[j++];
+ }
+ //上面的循环无法将两个子数组的数据全部循环到
+ while (i <= mid) {
+ temp[index++] = array[i++];
+ }
+ while (j <= right) {
+ temp[index++] = array[j++];
+ }
+ //数据放入原
+ int tindex = 0;
+ while (tindex < temp.length) {
+ array[left++] = temp[tindex++];
+ }
+
+ }
+}
diff --git a/src/test/java/com/lin/TestSort/MergeSortTest.java b/src/test/java/com/lin/TestSort/MergeSortTest.java
new file mode 100644
index 0000000..180110c
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/MergeSortTest.java
@@ -0,0 +1,28 @@
+package com.lin.TestSort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.MergeSort;
+import org.junit.Test;
+
+public class MergeSortTest {
+
+
+ @Test
+ public void mergeSortedRecursionTest() {
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
+ MergeSort.mergeSortedRecursion(a, StatusCode.MAX_SORT, 0, a.length-1);
+ for (int i : a) {
+ System.out.println(i);
+ }
+ }
+
+ @Test
+ public void mergeSortedIterationTest(){
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
+ MergeSort.mergeSortedIteration(a, StatusCode.MIX_SORT);
+ for (int i : a) {
+ System.out.println(i);
+ }
+ }
+
+}
--
Gitee
From af6401df5ec0a42778c2f6a97f187b54bfdbd42b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 15:39:57 +0800
Subject: [PATCH 07/46] =?UTF-8?q?=E9=80=92=E5=BD=92=E5=B8=8C=E5=B0=94?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/MergeSort.java | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/main/java/com/lin/sort/MergeSort.java b/src/main/java/com/lin/sort/MergeSort.java
index 20c83d2..70afaee 100644
--- a/src/main/java/com/lin/sort/MergeSort.java
+++ b/src/main/java/com/lin/sort/MergeSort.java
@@ -38,8 +38,11 @@ public class MergeSort {
if (left == right)
return;
int mid = (left + right) / 2;
+ //前半段数组归并
mergeSortedRecursion(array, statusCode, left, mid);
+ //后半段数组归并
mergeSortedRecursion(array, statusCode, mid + 1, right);
+ //归并合并
merge(array, statusCode, left, mid, right);
}
--
Gitee
From d6128599f1f96c0b5c0f49b3fc4de546d5a9d3c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 16:33:10 +0800
Subject: [PATCH 08/46] =?UTF-8?q?=E5=A0=86=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/HeapSort.java | 65 +++++++++++++++++++
.../java/com/lin/TestSort/HeapSortTest.java | 19 ++++++
2 files changed, 84 insertions(+)
create mode 100644 src/main/java/com/lin/sort/HeapSort.java
create mode 100644 src/test/java/com/lin/TestSort/HeapSortTest.java
diff --git a/src/main/java/com/lin/sort/HeapSort.java b/src/main/java/com/lin/sort/HeapSort.java
new file mode 100644
index 0000000..e1b47fa
--- /dev/null
+++ b/src/main/java/com/lin/sort/HeapSort.java
@@ -0,0 +1,65 @@
+package com.lin.sort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.util.SortUtil;
+
+public class HeapSort {
+
+ /**
+ * 堆排序
+ *
+ * @param array
+ * @param statusCode
+ */
+ public static void heapSorted(int[] array, StatusCode statusCode) {
+ int heap_size = buildHeap(array, array.length, statusCode); // 建立一个最大堆
+ while (heap_size > 1) { // 堆(无序区)元素个数大于1,未完成排序
+ // 将堆顶元素与堆的最后一个元素互换,并从堆中去掉最后一个元素
+ // 此处交换操作很有可能把后面元素的稳定性打乱,所以堆排序是不稳定的排序算法
+ SortUtil.swap(array, 0, --heap_size);
+ heapify(array, 0, heap_size, statusCode); // 从新的堆顶元素开始向下进行堆调整,时间复杂度O(logn)
+ }
+ }
+
+
+ /**
+ * 堆结构调整
+ *
+ * @param array
+ * @param i
+ * @param size
+ */
+ private static void heapify(int[] array, int i, int size, StatusCode statusCode) // 从A[i]向下进行堆调整
+ {
+ int left_child = 2 * i + 1; // 左孩子索引
+ int right_child = 2 * i + 2; // 右孩子索引
+ int max = i; // 选出当前结点与其左右孩子三者之中的最大值
+ if (left_child < size && statusCode.getCode() == SortUtil.compare(array[max], array[left_child]))
+// if (left_child < size && array[left_child] > array[max])
+ max = left_child;
+ if (right_child < size && statusCode.getCode() == SortUtil.compare(array[max], array[right_child]))
+// if (right_child < size && array[right_child] > array[max])
+ max = right_child;
+ if (max != i) {
+ SortUtil.swap(array, i, max); // 把当前结点和它的最大(直接)子节点进行交换
+ heapify(array, max, size, statusCode); // 递归调用,继续从当前结点向下进行堆调整
+ }
+ }
+
+ /**
+ * 构造堆
+ *
+ * @param array
+ * @param n
+ * @return
+ */
+ private static int buildHeap(int[] array, int n, StatusCode statusCode) // 建堆,时间复杂度O(n)
+ {
+ int heap_size = n;
+ for (int i = heap_size / 2 - 1; i >= 0; i--) // 从每一个非叶结点开始向下进行堆调整
+ heapify(array, i, heap_size, statusCode);
+ return heap_size;
+ }
+
+
+}
diff --git a/src/test/java/com/lin/TestSort/HeapSortTest.java b/src/test/java/com/lin/TestSort/HeapSortTest.java
new file mode 100644
index 0000000..8a57c96
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/HeapSortTest.java
@@ -0,0 +1,19 @@
+package com.lin.TestSort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.HeapSort;
+import org.junit.Test;
+
+public class HeapSortTest {
+
+
+ @Test
+ public void heapSrotTest() {
+ int[] a = {1,6,8,2,4,5,7,1,0};
+ HeapSort.heapSorted(a,StatusCode.MAX_SORT);
+ for (int i : a) {
+ System.out.println(i);
+ }
+ }
+
+}
--
Gitee
From 273a7806e095933a74946fe6def5ccd3cc54688c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 17:51:37 +0800
Subject: [PATCH 09/46] =?UTF-8?q?=E5=BF=AB=E9=80=9F=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/QuickSort.java | 35 +++++++++++++++++++
.../com/lin/TestSort/QuickSortedTest.java | 19 ++++++++++
2 files changed, 54 insertions(+)
create mode 100644 src/main/java/com/lin/sort/QuickSort.java
create mode 100644 src/test/java/com/lin/TestSort/QuickSortedTest.java
diff --git a/src/main/java/com/lin/sort/QuickSort.java b/src/main/java/com/lin/sort/QuickSort.java
new file mode 100644
index 0000000..bb96319
--- /dev/null
+++ b/src/main/java/com/lin/sort/QuickSort.java
@@ -0,0 +1,35 @@
+package com.lin.sort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.util.SortUtil;
+
+public class QuickSort {
+
+
+ private static int partition(int[] array, int left, int right, StatusCode statusCode) // 划分函数
+ {
+ int pivot = array[right]; // 这里每次都选择最后一个元素作为基准
+ int tail = left - 1; // tail为小于基准的子数组最后一个元素的索引
+ for (int i = left; i < right; i++) // 遍历基准以外的其他元素
+ {
+ if (SortUtil.compare(pivot, array[i]) == 0 || !(statusCode.getCode() == SortUtil.compare(pivot, array[i]))) //会破环稳定性
+// if (array[i] <= pivot) // 把小于等于基准的元素放到前一个子数组末尾
+ {
+ SortUtil.swap(array, ++tail, i);
+ }
+ }
+ SortUtil.swap(array, tail + 1, right); // 最后把基准放到前一个子数组的后边,剩下的子数组既是大于基准的子数组
+ // 该操作很有可能把后面元素的稳定性打乱,所以快速排序是不稳定的排序算法
+ return tail + 1; // 返回基准的索引
+ }
+
+ public static void quickSorted(int[] array, int left, int right, StatusCode statusCode) {
+ if (left >= right)
+ return;
+ int pivot_index = partition(array, left, right, statusCode); // 基准的索引
+ quickSorted(array, left, pivot_index - 1, statusCode);
+ quickSorted(array, pivot_index + 1, right, statusCode);
+ }
+
+
+}
diff --git a/src/test/java/com/lin/TestSort/QuickSortedTest.java b/src/test/java/com/lin/TestSort/QuickSortedTest.java
new file mode 100644
index 0000000..a163556
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/QuickSortedTest.java
@@ -0,0 +1,19 @@
+package com.lin.TestSort;
+
+import com.lin.enums.StatusCode;
+import com.lin.sort.QuickSort;
+import org.junit.Test;
+
+public class QuickSortedTest {
+
+
+ @Test
+ public void quickSortedTest() {
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
+ QuickSort.quickSorted(a, 0, a.length - 1,StatusCode.MAX_SORT);
+ for (int i : a) {
+ System.out.println(i);
+ }
+ }
+
+}
--
Gitee
From 8496be9a370786c45ce7be761aa65eeb58f9bfe5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 9 Mar 2020 21:37:03 +0800
Subject: [PATCH 10/46] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/HeapSort.java | 3 +++
src/main/java/com/lin/sort/InsertionSort.java | 3 +++
src/main/java/com/lin/sort/MergeSort.java | 3 +++
src/main/java/com/lin/sort/QuickSort.java | 3 +++
src/main/java/com/lin/sort/SelectionSort.java | 3 +++
src/main/java/com/lin/sort/ShellSort.java | 3 +++
6 files changed, 18 insertions(+)
diff --git a/src/main/java/com/lin/sort/HeapSort.java b/src/main/java/com/lin/sort/HeapSort.java
index e1b47fa..c642739 100644
--- a/src/main/java/com/lin/sort/HeapSort.java
+++ b/src/main/java/com/lin/sort/HeapSort.java
@@ -3,6 +3,9 @@ package com.lin.sort;
import com.lin.enums.StatusCode;
import com.lin.sort.util.SortUtil;
+/**
+ * 堆排序
+ */
public class HeapSort {
/**
diff --git a/src/main/java/com/lin/sort/InsertionSort.java b/src/main/java/com/lin/sort/InsertionSort.java
index d80426a..f722d2f 100644
--- a/src/main/java/com/lin/sort/InsertionSort.java
+++ b/src/main/java/com/lin/sort/InsertionSort.java
@@ -3,6 +3,9 @@ package com.lin.sort;
import com.lin.enums.StatusCode;
import com.lin.sort.util.SortUtil;
+/**
+ * 插入排序
+ */
public class InsertionSort {
diff --git a/src/main/java/com/lin/sort/MergeSort.java b/src/main/java/com/lin/sort/MergeSort.java
index 70afaee..4360e68 100644
--- a/src/main/java/com/lin/sort/MergeSort.java
+++ b/src/main/java/com/lin/sort/MergeSort.java
@@ -3,6 +3,9 @@ package com.lin.sort;
import com.lin.enums.StatusCode;
import com.lin.sort.util.SortUtil;
+/**
+ * 归并排序
+ */
public class MergeSort {
diff --git a/src/main/java/com/lin/sort/QuickSort.java b/src/main/java/com/lin/sort/QuickSort.java
index bb96319..4a73982 100644
--- a/src/main/java/com/lin/sort/QuickSort.java
+++ b/src/main/java/com/lin/sort/QuickSort.java
@@ -3,6 +3,9 @@ package com.lin.sort;
import com.lin.enums.StatusCode;
import com.lin.sort.util.SortUtil;
+/**
+ * 快速排序
+ */
public class QuickSort {
diff --git a/src/main/java/com/lin/sort/SelectionSort.java b/src/main/java/com/lin/sort/SelectionSort.java
index 39d6b86..f139da2 100644
--- a/src/main/java/com/lin/sort/SelectionSort.java
+++ b/src/main/java/com/lin/sort/SelectionSort.java
@@ -3,6 +3,9 @@ package com.lin.sort;
import com.lin.enums.StatusCode;
import com.lin.sort.util.SortUtil;
+/**
+ * 选择排序
+ */
public class SelectionSort {
diff --git a/src/main/java/com/lin/sort/ShellSort.java b/src/main/java/com/lin/sort/ShellSort.java
index 0306c19..9453abc 100644
--- a/src/main/java/com/lin/sort/ShellSort.java
+++ b/src/main/java/com/lin/sort/ShellSort.java
@@ -3,6 +3,9 @@ package com.lin.sort;
import com.lin.enums.StatusCode;
import com.lin.sort.util.SortUtil;
+/**
+ * 希尔排序
+ */
public class ShellSort {
--
Gitee
From 5231ca87c96215782e97d7a32308f045926e640f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Tue, 10 Mar 2020 10:04:14 +0800
Subject: [PATCH 11/46] =?UTF-8?q?=E4=BA=8C=E5=88=86=E6=8F=92=E5=85=A5?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/BubbleSort.java | 7 +++-
src/main/java/com/lin/sort/InsertionSort.java | 35 +++++++++++++++++++
src/main/java/com/lin/sort/QuickSort.java | 12 +++++++
src/main/java/com/lin/sort/SelectionSort.java | 6 ++++
src/main/java/com/lin/sort/ShellSort.java | 6 ++++
.../com/lin/TestSort/InsertionSortTest.java | 11 ++++++
6 files changed, 76 insertions(+), 1 deletion(-)
diff --git a/src/main/java/com/lin/sort/BubbleSort.java b/src/main/java/com/lin/sort/BubbleSort.java
index 8927176..7e17c14 100644
--- a/src/main/java/com/lin/sort/BubbleSort.java
+++ b/src/main/java/com/lin/sort/BubbleSort.java
@@ -9,6 +9,12 @@ import com.lin.sort.util.SortUtil;
public class BubbleSort {
+ /**
+ * 冒泡排序
+ *
+ * @param array
+ * @param statusCode
+ */
public static void bubbleSorted(int[] array, StatusCode statusCode) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
@@ -22,5 +28,4 @@ public class BubbleSort {
}
-
}
diff --git a/src/main/java/com/lin/sort/InsertionSort.java b/src/main/java/com/lin/sort/InsertionSort.java
index f722d2f..33bf8f2 100644
--- a/src/main/java/com/lin/sort/InsertionSort.java
+++ b/src/main/java/com/lin/sort/InsertionSort.java
@@ -9,6 +9,12 @@ import com.lin.sort.util.SortUtil;
public class InsertionSort {
+ /**
+ * 插入排序
+ *
+ * @param array
+ * @param statusCode
+ */
public static void insertionSorted(int[] array, StatusCode statusCode) {
//默认第0个是排好序的,i以前的排好序
for (int i = 1; i < array.length; i++) {
@@ -25,5 +31,34 @@ public class InsertionSort {
}
+ /**
+ * 二分插入排序
+ *
+ * @param array
+ * @param statusCode
+ */
+ public static void insertionSortedDichotomy(int[] array, StatusCode statusCode) {
+ for (int i = 1; i < array.length; i++) {
+ int get = array[i];
+ int left = 0;
+ int right = i - 1;
+ //二分查找数据所处的位置,也是在之前的队列中
+ while (left <= right)
+ {
+ int mid = (left + right) / 2;
+ if (statusCode.getCode() == SortUtil.compare(get,array[mid]))
+// if (array[mid] > get)
+ right = mid - 1;
+ else
+ left = mid + 1;
+ }
+ //拿走的是位置i的数据,left之后的往后移动
+ for (int j = i - 1; j >= left; j--)
+ {
+ array[j + 1] = array[j];
+ }
+ array[left] = get;
+ }
+ }
}
diff --git a/src/main/java/com/lin/sort/QuickSort.java b/src/main/java/com/lin/sort/QuickSort.java
index 4a73982..9ad9d23 100644
--- a/src/main/java/com/lin/sort/QuickSort.java
+++ b/src/main/java/com/lin/sort/QuickSort.java
@@ -9,6 +9,15 @@ import com.lin.sort.util.SortUtil;
public class QuickSort {
+ /**
+ * 分治块
+ *
+ * @param array
+ * @param left
+ * @param right
+ * @param statusCode
+ * @return
+ */
private static int partition(int[] array, int left, int right, StatusCode statusCode) // 划分函数
{
int pivot = array[right]; // 这里每次都选择最后一个元素作为基准
@@ -26,6 +35,9 @@ public class QuickSort {
return tail + 1; // 返回基准的索引
}
+ /**
+ * 快速排序
+ */
public static void quickSorted(int[] array, int left, int right, StatusCode statusCode) {
if (left >= right)
return;
diff --git a/src/main/java/com/lin/sort/SelectionSort.java b/src/main/java/com/lin/sort/SelectionSort.java
index f139da2..bd55362 100644
--- a/src/main/java/com/lin/sort/SelectionSort.java
+++ b/src/main/java/com/lin/sort/SelectionSort.java
@@ -9,6 +9,12 @@ import com.lin.sort.util.SortUtil;
public class SelectionSort {
+ /**
+ * 选择排序
+ *
+ * @param array
+ * @param statusCode
+ */
public static void selectionSorted(int[] array, StatusCode statusCode) {
//确定最小的位置
for (int i = 0; i < array.length; i++) {
diff --git a/src/main/java/com/lin/sort/ShellSort.java b/src/main/java/com/lin/sort/ShellSort.java
index 9453abc..7043626 100644
--- a/src/main/java/com/lin/sort/ShellSort.java
+++ b/src/main/java/com/lin/sort/ShellSort.java
@@ -9,6 +9,12 @@ import com.lin.sort.util.SortUtil;
public class ShellSort {
+ /**
+ * 希尔排序
+ *
+ * @param array
+ * @param statusCode
+ */
public static void shellSorted(int[] array, StatusCode statusCode) {
int shell = 0;
//寻找最大的希尔值
diff --git a/src/test/java/com/lin/TestSort/InsertionSortTest.java b/src/test/java/com/lin/TestSort/InsertionSortTest.java
index 119081b..543c400 100644
--- a/src/test/java/com/lin/TestSort/InsertionSortTest.java
+++ b/src/test/java/com/lin/TestSort/InsertionSortTest.java
@@ -15,4 +15,15 @@ public class InsertionSortTest {
System.out.println(i);
}
}
+
+ @Test
+ public void insertionSortedDichotomyTest() {
+ int[] a = {1,6,8,2,4,5,7,1,0};
+ InsertionSort.insertionSortedDichotomy(a, StatusCode.MIX_SORT);
+ for (int i : a) {
+ System.out.println(i);
+ }
+ }
+
+
}
--
Gitee
From b041d500b38d9303a203ef5edc64c9a277c53f59 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Tue, 10 Mar 2020 10:19:45 +0800
Subject: [PATCH 12/46] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=B6=E9=97=B4?=
=?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/enums/StatusCode.java | 3 ++-
src/main/java/com/lin/sort/BubbleSort.java | 1 +
src/main/java/com/lin/sort/HeapSort.java | 1 +
src/main/java/com/lin/sort/InsertionSort.java | 1 +
src/main/java/com/lin/sort/MergeSort.java | 1 +
src/main/java/com/lin/sort/QuickSort.java | 1 +
src/main/java/com/lin/sort/SelectionSort.java | 1 +
7 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/src/main/java/com/lin/enums/StatusCode.java b/src/main/java/com/lin/enums/StatusCode.java
index 5fea2c4..dadd128 100644
--- a/src/main/java/com/lin/enums/StatusCode.java
+++ b/src/main/java/com/lin/enums/StatusCode.java
@@ -4,7 +4,8 @@ public enum StatusCode {
MIX_SORT(-1, "小到大"),
- MAX_SORT(1, "大到小");
+ MAX_SORT(1, "大到小"),
+ EQUAL(0, "相等");
int code;
diff --git a/src/main/java/com/lin/sort/BubbleSort.java b/src/main/java/com/lin/sort/BubbleSort.java
index 7e17c14..b7063bd 100644
--- a/src/main/java/com/lin/sort/BubbleSort.java
+++ b/src/main/java/com/lin/sort/BubbleSort.java
@@ -5,6 +5,7 @@ import com.lin.sort.util.SortUtil;
/**
* 冒泡排序
+ *
O(n)~O(n^2)~O(n^2)
*/
public class BubbleSort {
diff --git a/src/main/java/com/lin/sort/HeapSort.java b/src/main/java/com/lin/sort/HeapSort.java
index c642739..0c498da 100644
--- a/src/main/java/com/lin/sort/HeapSort.java
+++ b/src/main/java/com/lin/sort/HeapSort.java
@@ -5,6 +5,7 @@ import com.lin.sort.util.SortUtil;
/**
* 堆排序
+ * O(nlogn)~O(nlogn)~O(nlogn)
*/
public class HeapSort {
diff --git a/src/main/java/com/lin/sort/InsertionSort.java b/src/main/java/com/lin/sort/InsertionSort.java
index 33bf8f2..21b660c 100644
--- a/src/main/java/com/lin/sort/InsertionSort.java
+++ b/src/main/java/com/lin/sort/InsertionSort.java
@@ -5,6 +5,7 @@ import com.lin.sort.util.SortUtil;
/**
* 插入排序
+ * O(n)~O(n^2)~O(n^2)
*/
public class InsertionSort {
diff --git a/src/main/java/com/lin/sort/MergeSort.java b/src/main/java/com/lin/sort/MergeSort.java
index 4360e68..1105f45 100644
--- a/src/main/java/com/lin/sort/MergeSort.java
+++ b/src/main/java/com/lin/sort/MergeSort.java
@@ -5,6 +5,7 @@ import com.lin.sort.util.SortUtil;
/**
* 归并排序
+ * O(nlogn)~O(nlogn)~O(nlogn)
*/
public class MergeSort {
diff --git a/src/main/java/com/lin/sort/QuickSort.java b/src/main/java/com/lin/sort/QuickSort.java
index 9ad9d23..e0e3376 100644
--- a/src/main/java/com/lin/sort/QuickSort.java
+++ b/src/main/java/com/lin/sort/QuickSort.java
@@ -5,6 +5,7 @@ import com.lin.sort.util.SortUtil;
/**
* 快速排序
+ * O(nlogn)~O(nlogn)~O(n^2)
*/
public class QuickSort {
diff --git a/src/main/java/com/lin/sort/SelectionSort.java b/src/main/java/com/lin/sort/SelectionSort.java
index bd55362..5f56fdd 100644
--- a/src/main/java/com/lin/sort/SelectionSort.java
+++ b/src/main/java/com/lin/sort/SelectionSort.java
@@ -5,6 +5,7 @@ import com.lin.sort.util.SortUtil;
/**
* 选择排序
+ * O(n^1.3)~O(nlogn)~O(n^2)~O(n^2)
*/
public class SelectionSort {
--
Gitee
From e558af89eb62025b682f09fe4a7ee89f913e45f2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Tue, 10 Mar 2020 10:25:21 +0800
Subject: [PATCH 13/46] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=B6=E9=97=B4?=
=?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=EF=BC=8C=E7=A8=B3=E5=AE=9A=E6=80=A7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/BubbleSort.java | 1 +
src/main/java/com/lin/sort/HeapSort.java | 1 +
src/main/java/com/lin/sort/InsertionSort.java | 1 +
src/main/java/com/lin/sort/MergeSort.java | 1 +
src/main/java/com/lin/sort/QuickSort.java | 1 +
src/main/java/com/lin/sort/SelectionSort.java | 3 ++-
src/main/java/com/lin/sort/ShellSort.java | 2 ++
7 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/src/main/java/com/lin/sort/BubbleSort.java b/src/main/java/com/lin/sort/BubbleSort.java
index b7063bd..3de43c3 100644
--- a/src/main/java/com/lin/sort/BubbleSort.java
+++ b/src/main/java/com/lin/sort/BubbleSort.java
@@ -6,6 +6,7 @@ import com.lin.sort.util.SortUtil;
/**
* 冒泡排序
* O(n)~O(n^2)~O(n^2)
+ * 稳定
*/
public class BubbleSort {
diff --git a/src/main/java/com/lin/sort/HeapSort.java b/src/main/java/com/lin/sort/HeapSort.java
index 0c498da..6ab5f7f 100644
--- a/src/main/java/com/lin/sort/HeapSort.java
+++ b/src/main/java/com/lin/sort/HeapSort.java
@@ -6,6 +6,7 @@ import com.lin.sort.util.SortUtil;
/**
* 堆排序
* O(nlogn)~O(nlogn)~O(nlogn)
+ * 不稳定
*/
public class HeapSort {
diff --git a/src/main/java/com/lin/sort/InsertionSort.java b/src/main/java/com/lin/sort/InsertionSort.java
index 21b660c..f19c696 100644
--- a/src/main/java/com/lin/sort/InsertionSort.java
+++ b/src/main/java/com/lin/sort/InsertionSort.java
@@ -6,6 +6,7 @@ import com.lin.sort.util.SortUtil;
/**
* 插入排序
* O(n)~O(n^2)~O(n^2)
+ * 稳定
*/
public class InsertionSort {
diff --git a/src/main/java/com/lin/sort/MergeSort.java b/src/main/java/com/lin/sort/MergeSort.java
index 1105f45..5df23b9 100644
--- a/src/main/java/com/lin/sort/MergeSort.java
+++ b/src/main/java/com/lin/sort/MergeSort.java
@@ -6,6 +6,7 @@ import com.lin.sort.util.SortUtil;
/**
* 归并排序
* O(nlogn)~O(nlogn)~O(nlogn)
+ * 稳定
*/
public class MergeSort {
diff --git a/src/main/java/com/lin/sort/QuickSort.java b/src/main/java/com/lin/sort/QuickSort.java
index e0e3376..f4f76c4 100644
--- a/src/main/java/com/lin/sort/QuickSort.java
+++ b/src/main/java/com/lin/sort/QuickSort.java
@@ -6,6 +6,7 @@ import com.lin.sort.util.SortUtil;
/**
* 快速排序
* O(nlogn)~O(nlogn)~O(n^2)
+ * 不稳定
*/
public class QuickSort {
diff --git a/src/main/java/com/lin/sort/SelectionSort.java b/src/main/java/com/lin/sort/SelectionSort.java
index 5f56fdd..3b6e7d3 100644
--- a/src/main/java/com/lin/sort/SelectionSort.java
+++ b/src/main/java/com/lin/sort/SelectionSort.java
@@ -5,7 +5,8 @@ import com.lin.sort.util.SortUtil;
/**
* 选择排序
- * O(n^1.3)~O(nlogn)~O(n^2)~O(n^2)
+ * O(n^2)~O(n^2)~O(n^2)
+ * 不稳定
*/
public class SelectionSort {
diff --git a/src/main/java/com/lin/sort/ShellSort.java b/src/main/java/com/lin/sort/ShellSort.java
index 7043626..9bc7739 100644
--- a/src/main/java/com/lin/sort/ShellSort.java
+++ b/src/main/java/com/lin/sort/ShellSort.java
@@ -5,6 +5,8 @@ import com.lin.sort.util.SortUtil;
/**
* 希尔排序
+ * O(n^1.3)~O(nlogn)~O(n^2)~O(n^2)
+ * 不稳定
*/
public class ShellSort {
--
Gitee
From 73aae2a2cfed8d4f2591750de030dd2c9be55425 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Tue, 10 Mar 2020 11:15:03 +0800
Subject: [PATCH 14/46] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=B8=A1=E5=B0=BE?=
=?UTF-8?q?=E9=85=92=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/BubbleSort.java | 28 ++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/src/main/java/com/lin/sort/BubbleSort.java b/src/main/java/com/lin/sort/BubbleSort.java
index 3de43c3..2bbfa36 100644
--- a/src/main/java/com/lin/sort/BubbleSort.java
+++ b/src/main/java/com/lin/sort/BubbleSort.java
@@ -30,4 +30,32 @@ public class BubbleSort {
}
+ /**
+ * 鸡尾酒排序
+ *
+ * @param array
+ * @param n
+ */
+ public static void CocktailSort(int[] array, int n) {
+ int left = 0; // 初始化边界
+ int right = n - 1;
+ while (left < right) {
+ for (int i = left; i < right; i++) // 前半轮,将最大元素放到后面
+ {
+ if (array[i] > array[i + 1]) {
+ SortUtil.swap(array, i, i + 1);
+ }
+ }
+ right--;
+ for (int i = right; i > left; i--) // 后半轮,将最小元素放到前面
+ {
+ if (array[i - 1] > array[i]) {
+ SortUtil.swap(array, i - 1, i);
+ }
+ }
+ left++;
+ }
+ }
+
+
}
--
Gitee
From 45cab725bfbd8191890722b0f6bc0f33eb438039 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Tue, 10 Mar 2020 14:52:41 +0800
Subject: [PATCH 15/46] =?UTF-8?q?=E8=AE=A1=E6=95=B0=E6=8E=92=E5=BA=8F?=
=?UTF-8?q?=EF=BC=8C=E5=9F=BA=E6=95=B0=E6=8E=92=E5=BA=8F=EF=BC=8C=E6=A1=B6?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/sort/BucketSort.java | 86 ++++++++++++++
src/main/java/com/lin/sort/CountingSort.java | 66 +++++++++++
src/main/java/com/lin/sort/RadixSort.java | 107 ++++++++++++++++++
.../java/com/lin/TestSort/BubbleSortTest.java | 2 +-
.../java/com/lin/TestSort/BucketSortTest.java | 17 +++
.../com/lin/TestSort/CountingSortTest.java | 17 +++
.../java/com/lin/TestSort/HeapSortTest.java | 4 +-
.../com/lin/TestSort/InsertionSortTest.java | 4 +-
.../java/com/lin/TestSort/MergeSortTest.java | 4 +-
.../com/lin/TestSort/QuickSortedTest.java | 2 +-
.../java/com/lin/TestSort/RadixSortTest.java | 20 ++++
.../com/lin/TestSort/SelectionSortTest.java | 2 +-
.../java/com/lin/TestSort/ShellSortTest.java | 8 +-
13 files changed, 325 insertions(+), 14 deletions(-)
create mode 100644 src/main/java/com/lin/sort/BucketSort.java
create mode 100644 src/main/java/com/lin/sort/CountingSort.java
create mode 100644 src/main/java/com/lin/sort/RadixSort.java
create mode 100644 src/test/java/com/lin/TestSort/BucketSortTest.java
create mode 100644 src/test/java/com/lin/TestSort/CountingSortTest.java
create mode 100644 src/test/java/com/lin/TestSort/RadixSortTest.java
diff --git a/src/main/java/com/lin/sort/BucketSort.java b/src/main/java/com/lin/sort/BucketSort.java
new file mode 100644
index 0000000..9dab05f
--- /dev/null
+++ b/src/main/java/com/lin/sort/BucketSort.java
@@ -0,0 +1,86 @@
+package com.lin.sort;
+
+import com.lin.enums.StatusCode;
+
+import java.util.Arrays;
+
+public class BucketSort {
+
+
+ /**
+ * 桶排序
+ *
+ * @param sourceArray
+ * @return
+ * @throws Exception
+ */
+ public static int[] bucketSorted(int[] sourceArray) {
+ // 对 arr 进行拷贝,不改变参数内容
+ int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
+
+ return bucketSort(arr, 5);
+ }
+
+ /**
+ * 桶排序
+ *
+ * @param arr
+ * @param bucketSize
+ * @return
+ * @throws Exception
+ */
+ private static int[] bucketSort(int[] arr, int bucketSize) {
+ if (arr.length == 0) {
+ return arr;
+ }
+
+ int minValue = arr[0];
+ int maxValue = arr[0];
+ for (int value : arr) {
+ if (value < minValue) {
+ minValue = value;
+ } else if (value > maxValue) {
+ maxValue = value;
+ }
+ }
+
+ int bucketCount = (int) Math.floor((maxValue - minValue) / bucketSize) + 1;
+ int[][] buckets = new int[bucketCount][0];
+
+ // 利用映射函数将数据分配到各个桶中
+ for (int i = 0; i < arr.length; i++) {
+ int index = (int) Math.floor((arr[i] - minValue) / bucketSize);
+ buckets[index] = arrAppend(buckets[index], arr[i]);
+ }
+
+ int arrIndex = 0;
+ for (int[] bucket : buckets) {
+ if (bucket.length <= 0) {
+ continue;
+ }
+ // 对每个桶进行排序,这里使用了插入排序
+
+ InsertionSort.insertionSorted(bucket, StatusCode.MIX_SORT);
+
+// bucket = insertSort.sort(bucket);
+ for (int value : bucket) {
+ arr[arrIndex++] = value;
+ }
+ }
+
+ return arr;
+ }
+
+ /**
+ * 自动扩容,并保存数据
+ *
+ * @param arr
+ * @param value
+ */
+ private static int[] arrAppend(int[] arr, int value) {
+ arr = Arrays.copyOf(arr, arr.length + 1);
+ arr[arr.length - 1] = value;
+ return arr;
+ }
+
+}
diff --git a/src/main/java/com/lin/sort/CountingSort.java b/src/main/java/com/lin/sort/CountingSort.java
new file mode 100644
index 0000000..f000a3a
--- /dev/null
+++ b/src/main/java/com/lin/sort/CountingSort.java
@@ -0,0 +1,66 @@
+package com.lin.sort;
+
+import java.util.Arrays;
+
+/**
+ * 计数排序
+ */
+public class CountingSort {
+
+
+ /**
+ * 计数排序
+ *
+ * @param sourceArray
+ * @return 返回排序后的数组
+ */
+ public static int[] countingsorted(int[] sourceArray) {
+ // 对 arr 进行拷贝,不改变参数内容
+ int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
+
+ int maxValue = getMaxValue(arr);
+
+ return sort(arr, maxValue);
+ }
+
+ /**
+ * 统计排序
+ *
+ * @param arr
+ * @param maxValue
+ * @return
+ */
+ private static int[] sort(int[] arr, int maxValue) {
+ int bucketLen = maxValue + 1;
+ int[] bucket = new int[bucketLen];
+
+ for (int value : arr) {
+ bucket[value]++;
+ }
+
+ int sortedIndex = 0;
+ for (int j = 0; j < bucketLen; j++) {
+ while (bucket[j] > 0) {
+ arr[sortedIndex++] = j;
+ bucket[j]--;
+ }
+ }
+ return arr;
+ }
+
+ /**
+ * 获取最大值
+ *
+ * @param arr
+ * @return
+ */
+ private static int getMaxValue(int[] arr) {
+ int maxValue = arr[0];
+ for (int value : arr) {
+ if (maxValue < value) {
+ maxValue = value;
+ }
+ }
+ return maxValue;
+ }
+}
diff --git a/src/main/java/com/lin/sort/RadixSort.java b/src/main/java/com/lin/sort/RadixSort.java
new file mode 100644
index 0000000..6c66253
--- /dev/null
+++ b/src/main/java/com/lin/sort/RadixSort.java
@@ -0,0 +1,107 @@
+package com.lin.sort;
+
+import java.util.Arrays;
+
+/**
+ * 基数排序
+ */
+public class RadixSort {
+
+
+ /**
+ * 基数排序
+ *
+ * @param sourceArray
+ * @return
+ */
+ public static int[] radixSorted(int[] sourceArray) {
+ // 对 arr 进行拷贝,不改变参数内容
+ int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
+
+ int maxDigit = getMaxDigit(arr);
+ return radixSort(arr, maxDigit);
+ }
+
+ /**
+ * 获取最高位数
+ *
+ * @param arr
+ * @return
+ */
+ private static int getMaxDigit(int[] arr) {
+ int maxValue = getMaxValue(arr);
+ return getNumLenght(maxValue);
+ }
+
+ /**
+ * 获取最大值
+ *
+ * @param arr
+ * @return
+ */
+ private static int getMaxValue(int[] arr) {
+ int maxValue = arr[0];
+ for (int value : arr) {
+ if (maxValue < value) {
+ maxValue = value;
+ }
+ }
+ return maxValue;
+ }
+
+ protected static int getNumLenght(long num) {
+ if (num == 0) {
+ return 1;
+ }
+ int lenght = 0;
+ for (long temp = num; temp != 0; temp /= 10) {
+ lenght++;
+ }
+ return lenght;
+ }
+
+ /**
+ * 基数排序
+ *
+ * @param arr
+ * @param maxDigit
+ * @return
+ */
+ private static int[] radixSort(int[] arr, int maxDigit) {
+ int mod = 10;
+ int dev = 1;
+
+ for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
+ // 考虑负数的情况,这里扩展一倍队列数,其中 [0-9]对应负数,[10-19]对应正数 (bucket + 10)
+ int[][] counter = new int[mod * 2][0];
+
+ for (int j = 0; j < arr.length; j++) {
+ int bucket = ((arr[j] % mod) / dev) + mod;
+ counter[bucket] = arrayAppend(counter[bucket], arr[j]);
+ }
+
+ int pos = 0;
+ for (int[] bucket : counter) {
+ for (int value : bucket) {
+ arr[pos++] = value;
+ }
+ }
+ }
+
+ return arr;
+ }
+
+ /**
+ * 自动扩容,并保存数据
+ *
+ * @param arr
+ * @param value
+ */
+ private static int[] arrayAppend(int[] arr, int value) {
+ arr = Arrays.copyOf(arr, arr.length + 1);
+ arr[arr.length - 1] = value;
+ return arr;
+ }
+
+
+}
diff --git a/src/test/java/com/lin/TestSort/BubbleSortTest.java b/src/test/java/com/lin/TestSort/BubbleSortTest.java
index d20ecbb..ba6176f 100644
--- a/src/test/java/com/lin/TestSort/BubbleSortTest.java
+++ b/src/test/java/com/lin/TestSort/BubbleSortTest.java
@@ -9,7 +9,7 @@ public class BubbleSortTest {
@Test
public void bubbleSortedTest(){
- int[] a = {1,6,8,2,4,5,7,1,0};
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
BubbleSort.bubbleSorted(a, StatusCode.MAX_SORT);
for (int i : a) {
System.out.println(i);
diff --git a/src/test/java/com/lin/TestSort/BucketSortTest.java b/src/test/java/com/lin/TestSort/BucketSortTest.java
new file mode 100644
index 0000000..41abc9e
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/BucketSortTest.java
@@ -0,0 +1,17 @@
+package com.lin.TestSort;
+
+import com.lin.sort.BucketSort;
+import org.junit.Test;
+
+public class BucketSortTest {
+
+
+ @Test
+ public void bucketSortedTest() {
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
+ int[] countingsorted = BucketSort.bucketSorted(a);
+ for (int i : countingsorted) {
+ System.out.println(i);
+ }
+ }
+}
diff --git a/src/test/java/com/lin/TestSort/CountingSortTest.java b/src/test/java/com/lin/TestSort/CountingSortTest.java
new file mode 100644
index 0000000..ee183b3
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/CountingSortTest.java
@@ -0,0 +1,17 @@
+package com.lin.TestSort;
+
+import com.lin.sort.CountingSort;
+import org.junit.Test;
+
+public class CountingSortTest {
+
+
+ @Test
+ public void countingSortedTest() {
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
+ int[] countingsorted = CountingSort.countingsorted(a);
+ for (int i : countingsorted) {
+ System.out.println(i);
+ }
+ }
+}
diff --git a/src/test/java/com/lin/TestSort/HeapSortTest.java b/src/test/java/com/lin/TestSort/HeapSortTest.java
index 8a57c96..fe9a2fe 100644
--- a/src/test/java/com/lin/TestSort/HeapSortTest.java
+++ b/src/test/java/com/lin/TestSort/HeapSortTest.java
@@ -9,8 +9,8 @@ public class HeapSortTest {
@Test
public void heapSrotTest() {
- int[] a = {1,6,8,2,4,5,7,1,0};
- HeapSort.heapSorted(a,StatusCode.MAX_SORT);
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
+ HeapSort.heapSorted(a, StatusCode.MAX_SORT);
for (int i : a) {
System.out.println(i);
}
diff --git a/src/test/java/com/lin/TestSort/InsertionSortTest.java b/src/test/java/com/lin/TestSort/InsertionSortTest.java
index 543c400..267f324 100644
--- a/src/test/java/com/lin/TestSort/InsertionSortTest.java
+++ b/src/test/java/com/lin/TestSort/InsertionSortTest.java
@@ -9,7 +9,7 @@ public class InsertionSortTest {
@Test
public void insertionSortedtest() {
- int[] a = {1,6,8,2,4,5,7,1,0};
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
InsertionSort.insertionSorted(a, StatusCode.MIX_SORT);
for (int i : a) {
System.out.println(i);
@@ -18,7 +18,7 @@ public class InsertionSortTest {
@Test
public void insertionSortedDichotomyTest() {
- int[] a = {1,6,8,2,4,5,7,1,0};
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
InsertionSort.insertionSortedDichotomy(a, StatusCode.MIX_SORT);
for (int i : a) {
System.out.println(i);
diff --git a/src/test/java/com/lin/TestSort/MergeSortTest.java b/src/test/java/com/lin/TestSort/MergeSortTest.java
index 180110c..8e88977 100644
--- a/src/test/java/com/lin/TestSort/MergeSortTest.java
+++ b/src/test/java/com/lin/TestSort/MergeSortTest.java
@@ -10,14 +10,14 @@ public class MergeSortTest {
@Test
public void mergeSortedRecursionTest() {
int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
- MergeSort.mergeSortedRecursion(a, StatusCode.MAX_SORT, 0, a.length-1);
+ MergeSort.mergeSortedRecursion(a, StatusCode.MAX_SORT, 0, a.length - 1);
for (int i : a) {
System.out.println(i);
}
}
@Test
- public void mergeSortedIterationTest(){
+ public void mergeSortedIterationTest() {
int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
MergeSort.mergeSortedIteration(a, StatusCode.MIX_SORT);
for (int i : a) {
diff --git a/src/test/java/com/lin/TestSort/QuickSortedTest.java b/src/test/java/com/lin/TestSort/QuickSortedTest.java
index a163556..2127f63 100644
--- a/src/test/java/com/lin/TestSort/QuickSortedTest.java
+++ b/src/test/java/com/lin/TestSort/QuickSortedTest.java
@@ -10,7 +10,7 @@ public class QuickSortedTest {
@Test
public void quickSortedTest() {
int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
- QuickSort.quickSorted(a, 0, a.length - 1,StatusCode.MAX_SORT);
+ QuickSort.quickSorted(a, 0, a.length - 1, StatusCode.MAX_SORT);
for (int i : a) {
System.out.println(i);
}
diff --git a/src/test/java/com/lin/TestSort/RadixSortTest.java b/src/test/java/com/lin/TestSort/RadixSortTest.java
new file mode 100644
index 0000000..bc424bd
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/RadixSortTest.java
@@ -0,0 +1,20 @@
+package com.lin.TestSort;
+
+import com.lin.sort.RadixSort;
+import org.junit.Test;
+
+public class RadixSortTest {
+
+
+ @Test
+ public void radixSortedTest() {
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
+ int[] countingsorted = RadixSort.radixSorted(a);
+ for (int i : countingsorted) {
+ System.out.println(i);
+ }
+ }
+
+
+
+}
diff --git a/src/test/java/com/lin/TestSort/SelectionSortTest.java b/src/test/java/com/lin/TestSort/SelectionSortTest.java
index a53ff6f..3981aa6 100644
--- a/src/test/java/com/lin/TestSort/SelectionSortTest.java
+++ b/src/test/java/com/lin/TestSort/SelectionSortTest.java
@@ -10,7 +10,7 @@ public class SelectionSortTest {
@Test
public void selectionSortedTest() {
int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
- SelectionSort.selectionSorted(a,StatusCode.MIX_SORT);
+ SelectionSort.selectionSorted(a, StatusCode.MIX_SORT);
for (int i : a) {
System.out.println(i);
}
diff --git a/src/test/java/com/lin/TestSort/ShellSortTest.java b/src/test/java/com/lin/TestSort/ShellSortTest.java
index f138176..86807ae 100644
--- a/src/test/java/com/lin/TestSort/ShellSortTest.java
+++ b/src/test/java/com/lin/TestSort/ShellSortTest.java
@@ -7,15 +7,13 @@ import org.junit.Test;
public class ShellSortTest {
@Test
- public void shellSortTest(){
- int[] a = {1,6,8,2,4,5,7,1,0};
- ShellSort.shellSorted(a,StatusCode.MIX_SORT);
+ public void shellSortTest() {
+ int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0};
+ ShellSort.shellSorted(a, StatusCode.MIX_SORT);
for (int i : a) {
System.out.println(i);
}
}
-
-
}
--
Gitee
From dd80115ec4d8de80f080c21b673b71d9a0adc02a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Wed, 11 Mar 2020 20:52:42 +0800
Subject: [PATCH 16/46] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?=
=?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/App.java | 7 -------
1 file changed, 7 deletions(-)
diff --git a/src/main/java/com/lin/App.java b/src/main/java/com/lin/App.java
index 1133952..0b491be 100644
--- a/src/main/java/com/lin/App.java
+++ b/src/main/java/com/lin/App.java
@@ -9,13 +9,6 @@ import java.util.Map;
public class App {
public static void main(String[] args) {
- System.out.println("Hello Utils");
- Map m = new HashMap<>();
- m.put("ss", "ghjk");
- m.put("ss", 23456);
-
- Object s = m.get("s");
-
}
}
--
Gitee
From f5bb3c17f02b758cd0d69b91dd98a9172e7c889a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Thu, 12 Mar 2020 23:32:48 +0800
Subject: [PATCH 17/46] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BA=BF=E7=A8=8B?=
=?UTF-8?q?=E6=B1=A0=E9=AA=8C=E8=AF=81=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/demo/ThreadPool.java | 4 ++++
1 file changed, 4 insertions(+)
create mode 100644 src/main/java/com/lin/demo/ThreadPool.java
diff --git a/src/main/java/com/lin/demo/ThreadPool.java b/src/main/java/com/lin/demo/ThreadPool.java
new file mode 100644
index 0000000..bfa6d9d
--- /dev/null
+++ b/src/main/java/com/lin/demo/ThreadPool.java
@@ -0,0 +1,4 @@
+package com.lin.demo;
+
+public class ThreadPool {
+}
--
Gitee
From 48d9ec8d735bacf95acaaf941fd3ebd7a02df1c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Fri, 13 Mar 2020 09:47:53 +0800
Subject: [PATCH 18/46] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/App.java | 7 +-
src/main/java/com/lin/demo/ThreadPool.java | 140 +++++++++++++++++++++
2 files changed, 143 insertions(+), 4 deletions(-)
diff --git a/src/main/java/com/lin/App.java b/src/main/java/com/lin/App.java
index 0b491be..9120aab 100644
--- a/src/main/java/com/lin/App.java
+++ b/src/main/java/com/lin/App.java
@@ -1,13 +1,12 @@
package com.lin;
-import java.util.HashMap;
-import java.util.Map;
-
/**
* Hello Utils!
*/
public class App {
- public static void main(String[] args) {
+
+
+ public static void main(String[] args) throws InterruptedException {
}
diff --git a/src/main/java/com/lin/demo/ThreadPool.java b/src/main/java/com/lin/demo/ThreadPool.java
index bfa6d9d..47a123b 100644
--- a/src/main/java/com/lin/demo/ThreadPool.java
+++ b/src/main/java/com/lin/demo/ThreadPool.java
@@ -1,4 +1,144 @@
package com.lin.demo;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
public class ThreadPool {
+
+ /**
+ * 缓存线程池
+ */
+ public void newCachedThreadPool() {
+ ExecutorService executorService = Executors.newCachedThreadPool();
+
+ for (int i = 0; i < 5; i++) {
+ final int index = i;
+ executorService.execute(new Runnable() {
+ public void run() {
+ try {
+ SimpleDateFormat sdf = new SimpleDateFormat(
+ "HH:mm:ss");
+ System.out.println("运行时间: " +
+ sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ }
+ executorService.shutdown();
+ }
+
+ /**
+ * 单线程线程池
+ */
+ public void newSingleThreadExecutor() {
+ ExecutorService executorService = Executors.newSingleThreadExecutor();
+
+ for (int i = 0; i < 5; i++) {
+ final int index = i;
+ executorService.execute(new Runnable() {
+ public void run() {
+ try {
+ SimpleDateFormat sdf = new SimpleDateFormat(
+ "HH:mm:ss");
+ System.out.println("运行时间: " +
+ sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ }
+ executorService.shutdown();
+ }
+
+ /**
+ * 定长线程池
+ */
+ public void newFixedThreadPool() {
+ ExecutorService executorService = Executors.newFixedThreadPool(3);
+
+ for (int i = 0; i < 5; i++) {
+ final int index = i;
+ executorService.execute(new Runnable() {
+ public void run() {
+ try {
+ SimpleDateFormat sdf = new SimpleDateFormat(
+ "HH:mm:ss");
+ System.out.println("运行时间: " +
+ sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ }
+ }
+
+ /**
+ * 定时执行的线程池
+ */
+ public void newScheduledThreadPool() {
+ ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
+ SimpleDateFormat sdf = new SimpleDateFormat(
+ "HH:mm:ss");
+ for (int i = 0; i < 5; i++) {
+ final int index = i;
+ System.out.println("提交时间: " + sdf.format(new Date()));
+ executorService.schedule(new Runnable() {
+ public void run() {
+ try {
+
+ System.out.println("运行时间: " +
+ sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }, 3, TimeUnit.SECONDS);
+
+ }
+ }
+
+ /**
+ * 周期执行的线程池
+ *
+ * @throws InterruptedException
+ */
+ public void newScheduledThreadPool2() throws InterruptedException {
+ ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
+ SimpleDateFormat sdf = new SimpleDateFormat(
+ "HH:mm:ss");
+// for (int i = 0; i < 5; i++) {
+ final int index = 1;
+ System.out.println("提交时间: " + sdf.format(new Date()));
+ executorService.scheduleAtFixedRate(new Runnable() {
+ public void run() {
+ try {
+
+ System.out.println("运行时间: " +
+ sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
+ Thread.sleep(2000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }, 1, 3, TimeUnit.SECONDS);
+ //主线程等待10秒钟后关闭
+ Thread.sleep(10000);
+// }
+
+ executorService.shutdown();
+ }
+
+
}
--
Gitee
From 5ebdcf71d4a90c33b44e589990db352ea1b2652b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Fri, 13 Mar 2020 17:34:44 +0800
Subject: [PATCH 19/46] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/demo/JUC.java | 8 ++++++++
1 file changed, 8 insertions(+)
create mode 100644 src/main/java/com/lin/demo/JUC.java
diff --git a/src/main/java/com/lin/demo/JUC.java b/src/main/java/com/lin/demo/JUC.java
new file mode 100644
index 0000000..bb59723
--- /dev/null
+++ b/src/main/java/com/lin/demo/JUC.java
@@ -0,0 +1,8 @@
+package com.lin.demo;
+
+public class JUC {
+
+
+
+
+}
--
Gitee
From bb97b15a2004d84bcbaaa4f79a74c042a52afbf7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Sun, 15 Mar 2020 22:54:38 +0800
Subject: [PATCH 20/46] =?UTF-8?q?=E5=8A=A0=E6=B3=A8=E9=87=8A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/demo/ThreadPool.java | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/main/java/com/lin/demo/ThreadPool.java b/src/main/java/com/lin/demo/ThreadPool.java
index 47a123b..48abc41 100644
--- a/src/main/java/com/lin/demo/ThreadPool.java
+++ b/src/main/java/com/lin/demo/ThreadPool.java
@@ -7,6 +7,9 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
+/**
+ * 线程池
+ */
public class ThreadPool {
/**
--
Gitee
From 5101736c76715ea8c1242d301758c7f43cbf0337 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 16 Mar 2020 18:13:30 +0800
Subject: [PATCH 21/46] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/demo/JUC.java | 1 +
src/main/java/com/lin/demo/Tree.java | 15 +++++++++++++++
2 files changed, 16 insertions(+)
create mode 100644 src/main/java/com/lin/demo/Tree.java
diff --git a/src/main/java/com/lin/demo/JUC.java b/src/main/java/com/lin/demo/JUC.java
index bb59723..0dd343c 100644
--- a/src/main/java/com/lin/demo/JUC.java
+++ b/src/main/java/com/lin/demo/JUC.java
@@ -5,4 +5,5 @@ public class JUC {
+
}
diff --git a/src/main/java/com/lin/demo/Tree.java b/src/main/java/com/lin/demo/Tree.java
new file mode 100644
index 0000000..e9c0359
--- /dev/null
+++ b/src/main/java/com/lin/demo/Tree.java
@@ -0,0 +1,15 @@
+package com.lin.demo;
+
+
+/**
+ * 二叉树
+ */
+public class Tree {
+
+
+
+
+
+
+
+}
--
Gitee
From 296967d88b2bf204e2ca7859197d43b92d11203b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 16 Mar 2020 23:09:37 +0800
Subject: [PATCH 22/46] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/App.java | 14 +++--
src/test/java/com/lin/Other/one.java | 78 ++++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 5 deletions(-)
create mode 100644 src/test/java/com/lin/Other/one.java
diff --git a/src/main/java/com/lin/App.java b/src/main/java/com/lin/App.java
index 9120aab..a0e9c12 100644
--- a/src/main/java/com/lin/App.java
+++ b/src/main/java/com/lin/App.java
@@ -1,13 +1,17 @@
package com.lin;
+
+import org.apache.commons.codec.binary.StringUtils;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
/**
* Hello Utils!
*/
public class App {
- public static void main(String[] args) throws InterruptedException {
-
-
- }
-}
+}
\ No newline at end of file
diff --git a/src/test/java/com/lin/Other/one.java b/src/test/java/com/lin/Other/one.java
new file mode 100644
index 0000000..9a425c5
--- /dev/null
+++ b/src/test/java/com/lin/Other/one.java
@@ -0,0 +1,78 @@
+package com.lin.Other;
+
+
+import org.apache.commons.codec.binary.StringUtils;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Hello Utils!
+ */
+public class one {
+
+
+ public static void main(String[] args) throws InterruptedException {
+ Map integerObjectMap = pageNumExpressionToArray("1,2{3},3");
+ System.out.println(integerObjectMap.toString());
+
+ }
+
+
+ public static Map pageNumExpressionToArray(String expression) {
+
+ Map returnvalue = new HashMap<>();
+
+ Map hashmap = new HashMap<>();
+ List pageList = new ArrayList<>();
+
+// if (StringUtils.isEmpty(expression)) {
+// return new ArrayList<>();
+// }
+ String[] arrays = expression.split(",");
+ for (String exp : arrays) {
+// if (StringUtils.isEmpty(exp)) continue;
+ if (exp.contains("{")) {
+ String i = exp.substring(exp.indexOf("{") + 1, exp.indexOf("}"));
+ exp = exp.substring(0, exp.indexOf("{"));
+ hashmap.put(Integer.valueOf(exp), Integer.valueOf(i));
+// pageList.add(Integer.valueOf(exp));
+ }
+ if (exp.contains("-")) {
+ String[] startEndArray = exp.split("-");
+ int startPageNo = Integer.valueOf(startEndArray[0]);
+ int endPageNo = Integer.valueOf(startEndArray[1]);
+ for (int i = startPageNo; i <= endPageNo; i++) {
+ pageList.add(i);
+ }
+ } else {
+ pageList.add(Integer.valueOf(exp));
+
+ }
+ }
+ returnvalue.put(1, pageList);
+ returnvalue.put(2, hashmap);
+ return returnvalue;
+
+
+
+ }
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
--
Gitee
From 16a52f797ae5e856da10941555d74afafe10fc07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Tue, 17 Mar 2020 16:06:55 +0800
Subject: [PATCH 23/46] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E6=A1=88?=
=?UTF-8?q?=E5=88=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/demo/Tree.java | 175 ++++++++++++++++++++++++++-
1 file changed, 173 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/lin/demo/Tree.java b/src/main/java/com/lin/demo/Tree.java
index e9c0359..6c90790 100644
--- a/src/main/java/com/lin/demo/Tree.java
+++ b/src/main/java/com/lin/demo/Tree.java
@@ -1,15 +1,186 @@
package com.lin.demo;
+import lombok.Data;
+
+import java.util.Queue;
+import java.util.Stack;
+import java.util.concurrent.LinkedBlockingQueue;
+
/**
- * 二叉树
+ * 普通二叉树
*/
-public class Tree {
+public class Tree {
+
+ /**
+ * 递归实现前序遍历
+ *
+ * @param node
+ */
+ public void preOrderTraverse(Node node) {
+ if (node != null) {
+ System.out.println(node.getValue());
+ preOrderTraverse(node.getLeft());
+ preOrderTraverse(node.getRight());
+ }
+ }
+
+ /**
+ * 非递归实现的前序遍历
+ *
+ * @param root
+ */
+ public void nrPreOrderTraverse(Node root) {
+ //借助栈实现
+ Stack> stack = new Stack>();
+ Node node = root;
+ while (node != null || !stack.isEmpty()) {
+
+ while (node != null) {
+ System.out.println(node.getValue());
+ stack.push(node);
+ node = node.getLeft();
+ }
+ node = stack.pop();
+ node = node.getRight();
+ }
+
+ }
+
+ /**
+ * 递归实现的中序遍历
+ *
+ * @param node
+ */
+ public void inOrderTraverse(Node node) {
+ if (node != null) {
+ inOrderTraverse(node.getLeft());
+ System.out.println(node.getValue());
+ inOrderTraverse(node.getRight());
+ }
+ }
+
+ /**
+ * 非递归实现的中序遍历
+ *
+ * @param root
+ */
+ public void nrInOrderTraverse(Node root) {
+ Stack> stack = new Stack>();
+ Node node = root;
+ while (node != null || !stack.isEmpty()) {
+ while (node != null) {
+ stack.push(node);
+ node = node.getLeft();
+ }
+ node = stack.pop();
+ System.out.println(node.getValue());
+ node = node.getRight();
+ }
+ }
+
+ /**
+ * 递归实现的后续遍历
+ *
+ * @param node
+ */
+ public void postOrderTraverse(Node node) {
+ if (node != null) {
+ postOrderTraverse(node.getLeft());
+ postOrderTraverse(node.getRight());
+ System.out.println(node.getValue());
+ }
+ }
+
+ /**
+ * 非递归实现的后续遍历
+ *
+ * @param root
+ */
+ public void nrPostOrderTraverse(Node root) {
+
+ Stack> stack = new Stack>();
+ Node node = root;
+ Node preNode = null;//表示最近一次访问的节点
+
+ while (node != null || !stack.isEmpty()) {
+ while (node != null) {
+ stack.push(node);
+ node = node.getLeft();
+ }
+
+ node = stack.peek();
+
+ if (node.getRight() == null || node.getRight() == preNode) {
+ System.out.println(node.getValue());
+ node = stack.pop();
+ preNode = node;
+ node = null;
+ } else {
+ node = node.getRight();
+ }
+ }
+ }
+
+ /**
+ * 借助队列实现的层序遍历
+ *
+ * @param node
+ */
+ public void levelTraverse(Node node) {
+ //借助队列实现
+ Queue> queue = new LinkedBlockingQueue>();
+ queue.add(node);
+ while (!queue.isEmpty()) {
+ Node temp = queue.poll();
+ if (temp != null) {
+ System.out.println(temp.getValue());
+ queue.add(temp.getLeft());
+ queue.add(temp.getRight());
+ }
+ }
+ }
+ /**
+ * 获取树的深度
+ *
+ * @param node
+ * @return
+ */
+ private Integer getHeight(Node node) {
+ if (node == null)
+ return 0;
+ else {
+ int left = getHeight(node.getLeft());
+ int right = getHeight(node.getRight());
+ return left > right ? left + 1 : right + 1;//左子树 右子树最深的,再加上父节点本身深度1
+ }
+ }
+ /**
+ * 获取节点数量
+ *
+ * @param node
+ * @return
+ */
+ private Integer getSize(Node node) {
+ if (node == null)
+ return 0;
+ else {
+ int leftSize = getSize(node.getLeft());
+ int rightSize = getSize(node.getRight());
+ return leftSize + rightSize + 1;
+ }
+ }
+ @Data
+ class Node {
+ Node left;
+ Node right;
+ T value;
+ }
}
--
Gitee
From 57e23d69d904d6d69206c56e5d9dbcf2a6572e2b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Tue, 17 Mar 2020 16:08:26 +0800
Subject: [PATCH 24/46] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E6=A1=88?=
=?UTF-8?q?=E5=88=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/{demo => tree}/Tree.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
rename src/main/java/com/lin/{demo => tree}/Tree.java (99%)
diff --git a/src/main/java/com/lin/demo/Tree.java b/src/main/java/com/lin/tree/Tree.java
similarity index 99%
rename from src/main/java/com/lin/demo/Tree.java
rename to src/main/java/com/lin/tree/Tree.java
index 6c90790..3f090bf 100644
--- a/src/main/java/com/lin/demo/Tree.java
+++ b/src/main/java/com/lin/tree/Tree.java
@@ -1,4 +1,4 @@
-package com.lin.demo;
+package com.lin.tree;
import lombok.Data;
--
Gitee
From 87453b0caafd7a7e2fd2aeeb7059a544e79b01c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Wed, 18 Mar 2020 14:28:55 +0800
Subject: [PATCH 25/46] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E4=B8=8E?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=9F=A5=E6=89=BE=E6=A0=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/tree/BSTree.java | 312 +++++++++++++++++++++++++
src/main/java/com/lin/tree/Tree.java | 2 +-
2 files changed, 313 insertions(+), 1 deletion(-)
create mode 100644 src/main/java/com/lin/tree/BSTree.java
diff --git a/src/main/java/com/lin/tree/BSTree.java b/src/main/java/com/lin/tree/BSTree.java
new file mode 100644
index 0000000..dc72144
--- /dev/null
+++ b/src/main/java/com/lin/tree/BSTree.java
@@ -0,0 +1,312 @@
+package com.lin.tree;
+
+import lombok.Data;
+
+/**
+ * 二叉搜索树
+ *
+ * @param
+ */
+public class BSTree> {
+
+
+ private BSTNode mRoot;
+
+
+ @Data
+ public class BSTNode> {
+ T key;
+ BSTNode left;
+ BSTNode right;
+ BSTNode parent;
+
+ public BSTNode() {
+ super();
+ }
+
+ public BSTNode(T key, BSTNode left, BSTNode right, BSTNode parent) {
+ this.key = key;
+ this.left = left;
+ this.right = right;
+ this.parent = parent;
+ }
+ }
+
+ /**
+ * 二叉搜索树递归查找
+ *
+ * @param x
+ * @param key
+ * @return
+ */
+ private BSTNode search(BSTNode x, T key) {
+ if (x == null)
+ return x;
+
+ int cmp = key.compareTo(x.key);
+ if (cmp < 0)
+ return search(x.left, key);
+ else if (cmp > 0)
+ return search(x.right, key);
+ else
+ return x;
+ }
+
+ public BSTNode search(T key) {
+ return search(mRoot, key);
+ }
+
+ /**
+ * 二叉搜索树非递归查找
+ *
+ * @param x
+ * @param key
+ * @return
+ */
+ private BSTNode iterativeSearch(BSTNode x, T key) {
+ while (x != null) {
+ int cmp = key.compareTo(x.key);
+
+ if (cmp < 0)
+ x = x.left;
+ else if (cmp > 0)
+ x = x.right;
+ else
+ return x;
+ }
+
+ return x;
+ }
+
+ public BSTNode iterativeSearch(T key) {
+ return iterativeSearch(mRoot, key);
+ }
+
+ /**
+ * 查找最大节点
+ *
+ * @param tree
+ * @return
+ */
+ private BSTNode maximum(BSTNode tree) {
+ if (tree == null)
+ return null;
+
+ while (tree.right != null)
+ tree = tree.right;
+ return tree;
+ }
+
+ public T maximum() {
+ BSTNode p = maximum(mRoot);
+ if (p != null)
+ return p.key;
+
+ return null;
+ }
+
+ /**
+ * 查找最小节点
+ *
+ * @param tree
+ * @return
+ */
+ private BSTNode minimum(BSTNode tree) {
+ if (tree == null)
+ return null;
+
+ while (tree.left != null)
+ tree = tree.left;
+ return tree;
+ }
+
+ public T minimum() {
+ BSTNode p = minimum(mRoot);
+ if (p != null)
+ return p.key;
+
+ return null;
+ }
+
+ /**
+ * 查找前驱节点
+ *
+ * @param x
+ * @return
+ */
+ public BSTNode predecessor(BSTNode x) {
+ // 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。
+ if (x.left != null)
+ return maximum(x.left);
+
+ // 如果x没有左孩子。则x有以下两种可能:
+ // (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。
+ // (01) x是"一个左孩子",则查找"x的最低的父结点,并且该父结点要具有右孩子",找到的这个"最低的父结点"就是"x的前驱结点"。
+ BSTNode y = x.parent;
+ while ((y != null) && (x == y.left)) {
+ x = y;
+ y = y.parent;
+ }
+
+ return y;
+ }
+
+ /**
+ * 查找后继节点
+ *
+ * @param x
+ * @return
+ */
+ public BSTNode successor(BSTNode x) {
+ // 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"。
+ if (x.right != null)
+ return minimum(x.right);
+
+ // 如果x没有右孩子。则x有以下两种可能:
+ // (01) x是"一个左孩子",则"x的后继结点"为 "它的父结点"。
+ // (02) x是"一个右孩子",则查找"x的最低的父结点,并且该父结点要具有左孩子",找到的这个"最低的父结点"就是"x的后继结点"。
+ BSTNode y = x.parent;
+ while ((y != null) && (x == y.right)) {
+ x = y;
+ y = y.parent;
+ }
+
+ return y;
+ }
+
+ /**
+ * 将节点插入二叉树
+ *
+ * @param bst
+ * @param z
+ */
+ private void insert(BSTree bst, BSTNode z) {
+ int cmp;
+ BSTNode y = null;
+ BSTNode x = bst.mRoot;
+
+ // 查找z的插入位置
+ while (x != null) {
+ y = x;
+ cmp = z.key.compareTo(x.key);
+ if (cmp < 0)
+ x = x.left;
+ else
+ x = x.right;
+ }
+
+ z.parent = y;
+ if (y == null)
+ bst.mRoot = z;
+ else {
+ cmp = z.key.compareTo(y.key);
+ if (cmp < 0)
+ y.left = z;
+ else
+ y.right = z;
+ }
+ }
+
+ public void insert(T key) {
+ BSTNode z = new BSTNode(key, null, null, null);
+
+ // 如果新建结点失败,则返回。
+ if (z != null)
+ insert(this, z);
+ }
+
+ /**
+ * 删除节点并返回
+ *
+ * @param bst
+ * @param z
+ * @return
+ */
+ private BSTNode remove(BSTree bst, BSTNode z) {
+ BSTNode x = null;
+ BSTNode y = null;
+
+ if ((z.left == null) || (z.right == null))
+ y = z;
+ else
+ y = successor(z);
+
+ if (y.left != null)
+ x = y.left;
+ else
+ x = y.right;
+
+ if (x != null)
+ x.parent = y.parent;
+
+ if (y.parent == null)
+ bst.mRoot = x;
+ else if (y == y.parent.left)
+ y.parent.left = x;
+ else
+ y.parent.right = x;
+
+ if (y != z)
+ z.key = y.key;
+
+ return y;
+ }
+
+ public void remove(T key) {
+ BSTNode z, node;
+
+ if ((z = search(mRoot, key)) != null)
+ if ((node = remove(this, z)) != null)
+ node = null;
+ }
+
+ /**
+ * 打印二叉搜索树
+ *
+ * @param tree
+ * @param key
+ * @param direction
+ */
+ private void print(BSTNode tree, T key, int direction) {
+
+ if (tree != null) {
+
+ if (direction == 0) // tree是根节点
+ System.out.printf("%2d is root\n", tree.key);
+ else // tree是分支节点
+ System.out.printf("%2d is %2d's %6s child\n", tree.key, key, direction == 1 ? "right" : "left");
+
+ print(tree.left, tree.key, -1);
+ print(tree.right, tree.key, 1);
+ }
+ }
+
+ public void print() {
+ if (mRoot != null)
+ print(mRoot, mRoot.key, 0);
+ }
+
+
+ /**
+ * 销毁二叉树
+ *
+ * @param tree
+ */
+ private void destroy(BSTNode tree) {
+ if (tree == null)
+ return;
+
+ if (tree.left != null)
+ destroy(tree.left);
+ if (tree.right != null)
+ destroy(tree.right);
+
+ tree = null;
+ }
+
+ public void clear() {
+ destroy(mRoot);
+ mRoot = null;
+ }
+
+}
diff --git a/src/main/java/com/lin/tree/Tree.java b/src/main/java/com/lin/tree/Tree.java
index 3f090bf..2d62e66 100644
--- a/src/main/java/com/lin/tree/Tree.java
+++ b/src/main/java/com/lin/tree/Tree.java
@@ -176,7 +176,7 @@ public class Tree {
@Data
- class Node {
+ public class Node {
Node left;
Node right;
T value;
--
Gitee
From ab8ca8d018f96def3bd03dbd55484bcc3a0b9458 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Wed, 18 Mar 2020 14:30:54 +0800
Subject: [PATCH 26/46] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=8C=85=E5=90=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/{Util => util}/ExcelExportUtil.java | 2 +-
src/main/java/com/lin/{Util => util}/ExcelImportUtil.java | 2 +-
src/main/java/com/lin/{Util => util}/FileReadUtil.java | 2 +-
src/main/java/com/lin/{Util => util}/FileWriteUtil.java | 2 +-
.../java/com/lin/{Util => util}/HttpConnectionUtil.java | 2 +-
.../java/com/lin/{Util => util}/HttpUrlConnectionUtil.java | 2 +-
src/main/java/com/lin/{Util => util}/InvokeUtil.java | 5 +----
src/main/java/com/lin/{Util => util}/XmlParseUtil.java | 2 +-
src/test/java/com/lin/TestUtil/ExcelTest.java | 6 +++---
src/test/java/com/lin/TestUtil/FileTest.java | 4 ++--
src/test/java/com/lin/TestUtil/HttpConnectionTest.java | 4 ++--
src/test/java/com/lin/TestUtil/InvokeTest.java | 4 ++--
src/test/java/com/lin/TestUtil/XmlTest.java | 7 +------
13 files changed, 18 insertions(+), 26 deletions(-)
rename src/main/java/com/lin/{Util => util}/ExcelExportUtil.java (99%)
rename src/main/java/com/lin/{Util => util}/ExcelImportUtil.java (99%)
rename src/main/java/com/lin/{Util => util}/FileReadUtil.java (98%)
rename src/main/java/com/lin/{Util => util}/FileWriteUtil.java (99%)
rename src/main/java/com/lin/{Util => util}/HttpConnectionUtil.java (99%)
rename src/main/java/com/lin/{Util => util}/HttpUrlConnectionUtil.java (99%)
rename src/main/java/com/lin/{Util => util}/InvokeUtil.java (96%)
rename src/main/java/com/lin/{Util => util}/XmlParseUtil.java (99%)
diff --git a/src/main/java/com/lin/Util/ExcelExportUtil.java b/src/main/java/com/lin/util/ExcelExportUtil.java
similarity index 99%
rename from src/main/java/com/lin/Util/ExcelExportUtil.java
rename to src/main/java/com/lin/util/ExcelExportUtil.java
index 9a2957a..bd26336 100644
--- a/src/main/java/com/lin/Util/ExcelExportUtil.java
+++ b/src/main/java/com/lin/util/ExcelExportUtil.java
@@ -1,4 +1,4 @@
-package com.lin.Util;
+package com.lin.util;
import lombok.extern.slf4j.Slf4j;
diff --git a/src/main/java/com/lin/Util/ExcelImportUtil.java b/src/main/java/com/lin/util/ExcelImportUtil.java
similarity index 99%
rename from src/main/java/com/lin/Util/ExcelImportUtil.java
rename to src/main/java/com/lin/util/ExcelImportUtil.java
index f423a8d..c9da58c 100644
--- a/src/main/java/com/lin/Util/ExcelImportUtil.java
+++ b/src/main/java/com/lin/util/ExcelImportUtil.java
@@ -1,4 +1,4 @@
-package com.lin.Util;
+package com.lin.util;
import lombok.extern.slf4j.Slf4j;
diff --git a/src/main/java/com/lin/Util/FileReadUtil.java b/src/main/java/com/lin/util/FileReadUtil.java
similarity index 98%
rename from src/main/java/com/lin/Util/FileReadUtil.java
rename to src/main/java/com/lin/util/FileReadUtil.java
index ba5c237..2e33e8f 100644
--- a/src/main/java/com/lin/Util/FileReadUtil.java
+++ b/src/main/java/com/lin/util/FileReadUtil.java
@@ -1,4 +1,4 @@
-package com.lin.Util;
+package com.lin.util;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
diff --git a/src/main/java/com/lin/Util/FileWriteUtil.java b/src/main/java/com/lin/util/FileWriteUtil.java
similarity index 99%
rename from src/main/java/com/lin/Util/FileWriteUtil.java
rename to src/main/java/com/lin/util/FileWriteUtil.java
index b3cbe26..ca6a705 100644
--- a/src/main/java/com/lin/Util/FileWriteUtil.java
+++ b/src/main/java/com/lin/util/FileWriteUtil.java
@@ -1,4 +1,4 @@
-package com.lin.Util;
+package com.lin.util;
import lombok.extern.slf4j.Slf4j;
diff --git a/src/main/java/com/lin/Util/HttpConnectionUtil.java b/src/main/java/com/lin/util/HttpConnectionUtil.java
similarity index 99%
rename from src/main/java/com/lin/Util/HttpConnectionUtil.java
rename to src/main/java/com/lin/util/HttpConnectionUtil.java
index ee82de7..911d3cc 100644
--- a/src/main/java/com/lin/Util/HttpConnectionUtil.java
+++ b/src/main/java/com/lin/util/HttpConnectionUtil.java
@@ -1,4 +1,4 @@
-package com.lin.Util;
+package com.lin.util;
import lombok.extern.slf4j.Slf4j;
diff --git a/src/main/java/com/lin/Util/HttpUrlConnectionUtil.java b/src/main/java/com/lin/util/HttpUrlConnectionUtil.java
similarity index 99%
rename from src/main/java/com/lin/Util/HttpUrlConnectionUtil.java
rename to src/main/java/com/lin/util/HttpUrlConnectionUtil.java
index b9406d9..520aa75 100644
--- a/src/main/java/com/lin/Util/HttpUrlConnectionUtil.java
+++ b/src/main/java/com/lin/util/HttpUrlConnectionUtil.java
@@ -1,4 +1,4 @@
-package com.lin.Util;
+package com.lin.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
diff --git a/src/main/java/com/lin/Util/InvokeUtil.java b/src/main/java/com/lin/util/InvokeUtil.java
similarity index 96%
rename from src/main/java/com/lin/Util/InvokeUtil.java
rename to src/main/java/com/lin/util/InvokeUtil.java
index fd309e8..ec34cd5 100644
--- a/src/main/java/com/lin/Util/InvokeUtil.java
+++ b/src/main/java/com/lin/util/InvokeUtil.java
@@ -1,11 +1,8 @@
-package com.lin.Util;
+package com.lin.util;
-import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
-import javax.swing.*;
import java.lang.reflect.Field;
-import java.lang.reflect.Type;
import java.util.Map;
/**
diff --git a/src/main/java/com/lin/Util/XmlParseUtil.java b/src/main/java/com/lin/util/XmlParseUtil.java
similarity index 99%
rename from src/main/java/com/lin/Util/XmlParseUtil.java
rename to src/main/java/com/lin/util/XmlParseUtil.java
index 0456574..0d13287 100644
--- a/src/main/java/com/lin/Util/XmlParseUtil.java
+++ b/src/main/java/com/lin/util/XmlParseUtil.java
@@ -1,4 +1,4 @@
-package com.lin.Util;
+package com.lin.util;
import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.Document;
diff --git a/src/test/java/com/lin/TestUtil/ExcelTest.java b/src/test/java/com/lin/TestUtil/ExcelTest.java
index 316469f..f13923a 100644
--- a/src/test/java/com/lin/TestUtil/ExcelTest.java
+++ b/src/test/java/com/lin/TestUtil/ExcelTest.java
@@ -1,9 +1,9 @@
package com.lin.TestUtil;
import com.lin.Dto.BookDto;
-import com.lin.Util.ExcelExportUtil;
-import com.lin.Util.ExcelImportUtil;
-import com.lin.Util.FileWriteUtil;
+import com.lin.util.ExcelExportUtil;
+import com.lin.util.ExcelImportUtil;
+import com.lin.util.FileWriteUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
diff --git a/src/test/java/com/lin/TestUtil/FileTest.java b/src/test/java/com/lin/TestUtil/FileTest.java
index 505ace7..a24d1cb 100644
--- a/src/test/java/com/lin/TestUtil/FileTest.java
+++ b/src/test/java/com/lin/TestUtil/FileTest.java
@@ -2,8 +2,8 @@ package com.lin.TestUtil;
import com.alibaba.fastjson.JSON;
import com.lin.Dto.SenseCallbackDto;
-import com.lin.Util.FileReadUtil;
-import com.lin.Util.FileWriteUtil;
+import com.lin.util.FileReadUtil;
+import com.lin.util.FileWriteUtil;
import org.junit.Test;
import java.io.IOException;
diff --git a/src/test/java/com/lin/TestUtil/HttpConnectionTest.java b/src/test/java/com/lin/TestUtil/HttpConnectionTest.java
index e1eb409..4e26514 100644
--- a/src/test/java/com/lin/TestUtil/HttpConnectionTest.java
+++ b/src/test/java/com/lin/TestUtil/HttpConnectionTest.java
@@ -1,7 +1,7 @@
package com.lin.TestUtil;
-import com.lin.Util.HttpConnectionUtil;
-import com.lin.Util.HttpUrlConnectionUtil;
+import com.lin.util.HttpConnectionUtil;
+import com.lin.util.HttpUrlConnectionUtil;
import org.junit.Test;
/**
diff --git a/src/test/java/com/lin/TestUtil/InvokeTest.java b/src/test/java/com/lin/TestUtil/InvokeTest.java
index 56eb3f4..17438ff 100644
--- a/src/test/java/com/lin/TestUtil/InvokeTest.java
+++ b/src/test/java/com/lin/TestUtil/InvokeTest.java
@@ -3,8 +3,8 @@ package com.lin.TestUtil;
import com.lin.Dto.BookDto;
import com.lin.Dto.SenseCallbackDto;
import com.lin.Dto.TestDto;
-import com.lin.Util.InvokeUtil;
-import com.lin.Util.XmlParseUtil;
+import com.lin.util.InvokeUtil;
+import com.lin.util.XmlParseUtil;
import org.junit.Test;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
diff --git a/src/test/java/com/lin/TestUtil/XmlTest.java b/src/test/java/com/lin/TestUtil/XmlTest.java
index e962f22..9228256 100644
--- a/src/test/java/com/lin/TestUtil/XmlTest.java
+++ b/src/test/java/com/lin/TestUtil/XmlTest.java
@@ -1,16 +1,11 @@
package com.lin.TestUtil;
-import com.lin.Util.FileReadUtil;
-import com.lin.Util.XmlParseUtil;
+import com.lin.util.XmlParseUtil;
import org.junit.Test;
import org.xml.sax.SAXException;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
/**
* Xml相关测试工具
--
Gitee
From 0eb90bb664c0255046a72384a0f358c73f0e9f98 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Wed, 18 Mar 2020 17:29:40 +0800
Subject: [PATCH 27/46] =?UTF-8?q?=E4=B8=A4=E7=A7=8D=E4=B8=8D=E5=90=8C?=
=?UTF-8?q?=E7=9A=84AVL=E6=A0=91=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/com/lin/tree/avltree/AVLTree1.java | 316 ++++++++++++++++++
.../java/com/lin/tree/avltree/AVLTree2.java | 199 +++++++++++
.../java/com/lin/TestTree/AVLTree1Test.java | 39 +++
3 files changed, 554 insertions(+)
create mode 100644 src/main/java/com/lin/tree/avltree/AVLTree1.java
create mode 100644 src/main/java/com/lin/tree/avltree/AVLTree2.java
create mode 100644 src/test/java/com/lin/TestTree/AVLTree1Test.java
diff --git a/src/main/java/com/lin/tree/avltree/AVLTree1.java b/src/main/java/com/lin/tree/avltree/AVLTree1.java
new file mode 100644
index 0000000..88a436c
--- /dev/null
+++ b/src/main/java/com/lin/tree/avltree/AVLTree1.java
@@ -0,0 +1,316 @@
+package com.lin.tree.avltree;
+
+import java.util.Comparator;
+
+public class AVLTree1 {
+
+ private static class Node {
+ int h;
+ E element;
+ Node left;
+ Node right;
+ //由于java中不像C语言那样有二级指针的概念,所以添加一个父类的引用,方便程序编写
+ Node parent;
+
+ public Node(E element, int h, Node left, Node right, Node parent) {
+ this.element = element;
+ this.h = h;
+ this.left = left;
+ this.right = right;
+ this.parent = parent;
+ }
+ }
+
+ private Node root;//指向伪根节点的引用
+ private int size = 0;//节点个数
+ Comparator super E> cmp;//节点大小的比较器
+
+ //如果调用了不带参数的构造函数,则使用该内部类作为比较器,
+ //但此时泛型E需要继承Comparable接口,否则运行时会抛出异常
+ private static class Cmp implements Comparator {
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ @Override
+ public int compare(T e1, T e2) {
+ return ((Comparable) e1).compareTo(e2);
+ }
+
+ }
+
+ //带比较器的构造函数
+ public AVLTree1(Comparator super E> cmp) {
+ if (cmp == null) {
+ throw new IllegalArgumentException();
+ }
+ this.cmp = cmp;
+ //创建一个伪根节点,该节点的右子支才是真正的AVL树的根
+ //使用伪根节点节点的目的是,对插入和删除操作递归的形式能够统一
+ root = new Node(null, -1, null, null, null);
+ }
+
+ //不带比较器的构造函数
+ public AVLTree1() {
+ this.cmp = new Cmp();
+ root = new Node(null, -1, null, null, null);
+ }
+
+ //如果树中节点有变动,从底向上逐级调用该函数,可以更新节点的高度
+ private int getHight(Node x) {
+ if (x == null) {
+ return 0;
+ } else {
+ return x.h;
+ }
+ }
+
+ //求某个节点作为根时,该子树的最小值
+ private E treeMin(Node x) {
+ while (x.left != null) {
+ x = x.left;
+ }
+ return x.element;
+ }
+
+ public int size() {
+ return size;
+ }
+
+ //先根遍历,调试时使用
+ public void preorderTraverse() {
+ if (root != null) {
+ preorderTraverse0(root.right);
+ }
+ }
+
+ private void preorderTraverse0(Node x) {
+ if (x != null) {
+ System.out.print(x.element + " ");
+ if (x.left != null) {
+ System.out.print(x.left.element + " ");
+ } else {
+ System.out.print("null ");
+ }
+
+ if (x.right != null) {
+ System.out.print(x.right.element + " ");
+ } else {
+ System.out.print("null ");
+ }
+ System.out.println();
+ preorderTraverse0(x.left);
+ preorderTraverse0(x.right);
+ }
+ }
+
+ //逆时针旋转(左旋),参数表示轴节点
+ private void antiClockwiseRotate(Node X) {
+ Node P = X.parent;
+ Node XR = X.right;
+ if (P.left == X) {
+ P.left = XR;
+ } else {
+ P.right = XR;
+ }
+ XR.parent = P;
+
+ X.right = XR.left;
+ if (XR.left != null) {
+ XR.left.parent = X;
+ }
+
+ XR.left = X;
+ X.parent = XR;
+
+ //旋转后要更新这两个节点的高度
+ X.h = Math.max(getHight(X.left), getHight(X.right)) + 1;
+ XR.h = Math.max(getHight(XR.left), getHight(XR.right)) + 1;
+ }
+
+ //顺时针旋转(右旋),参数表示轴节点
+ private void clockwistRotate(Node X) {
+ Node P = X.parent;
+ Node XL = X.left;
+ if (P.left == X) {
+ P.left = XL;
+ } else {
+ P.right = XL;
+ }
+ XL.parent = P;
+
+ X.left = XL.right;
+ if (XL.right != null) {
+ XL.right.parent = X;
+ }
+
+ XL.right = X;
+ X.parent = XL;
+
+ //旋转后要更新这两个节点的高度
+ X.h = Math.max(getHight(X.left), getHight(X.right)) + 1;
+ XL.h = Math.max(getHight(XL.left), getHight(XL.right)) + 1;
+ }
+
+ //
+ public void insert(E e) {
+ insert0(root.right, e);
+ }
+
+ private void insert0(Node x, E e) {
+ if (x == null) {
+ root.right = new Node(e, 1, null, null, root);//根节点
+ size++;
+ return;
+ }
+
+ if (cmp.compare(e, x.element) > 0) {
+ if (x.right != null) {
+ insert0(x.right, e);
+ int lh = getHight(x.left);
+ int rh = getHight(x.right);
+ if (rh - lh == 2) {
+ if (cmp.compare(e, x.right.element) > 0) {
+ antiClockwiseRotate(x);
+ } else {
+ clockwistRotate(x.right);
+ antiClockwiseRotate(x);
+ }
+ }
+ } else {
+ size++;
+ x.right = new Node(e, 1, null, null, x);
+ }
+ } else if (cmp.compare(e, x.element) < 0) {
+ if (x.left != null) {
+ insert0(x.left, e);
+ int lh = getHight(x.left);
+ int rh = getHight(x.right);
+ if (lh - rh == 2) {
+ if (cmp.compare(e, x.left.element) < 0) {
+ clockwistRotate(x);
+ } else {
+ antiClockwiseRotate(x.left);
+ clockwistRotate(x);
+ }
+ }
+ } else {
+ size++;
+ x.left = new Node(e, 1, null, null, x);
+ }
+ } else {
+ //元素已存在,我们用新的元素更新旧,
+ //compare返回值等于0,并不表示两个对象完全相等
+ x.element = e;
+ }
+ x.h = Math.max(getHight(x.left), getHight(x.right)) + 1;
+ }
+
+ public boolean delete(E e) {
+ return delete0(root.right, e);
+ }
+
+ //返回值表示是否删除成功
+ private boolean delete0(Node x, E e) {
+ if (x == null) {//没有找到待删除的元素
+ return false;
+ }
+
+ if (cmp.compare(e, x.element) > 0) {
+ boolean reval = delete0(x.right, e);
+ if (reval == false) {
+ return false;
+ }
+
+ int lh = getHight(x.left);
+ int rh = getHight(x.right);
+ if (lh - rh == 2) {
+ if (getHight(x.left.left) > getHight(x.left.right)) {
+ clockwistRotate(x);
+ } else {
+ antiClockwiseRotate(x.left);
+ clockwistRotate(x);
+ }
+ }
+ } else if (cmp.compare(e, x.element) < 0) {
+ boolean reval = delete0(x.left, e);
+ if (reval == false) {
+ return false;
+ }
+
+ int lh = getHight(x.left);
+ int rh = getHight(x.right);
+ if (rh - lh == 2) {
+ if (getHight(x.right.right) > getHight(x.right.left)) {
+ antiClockwiseRotate(x);
+ } else {
+ clockwistRotate(x.right);
+ antiClockwiseRotate(x);
+ }
+ }
+ } else {//找到待删除的元素
+ Node P = x.parent;
+
+ if (x.left == null) {//左子支为空,可直接删除,在这一层一定不需要旋转
+ size--;
+ if (P.left == x) {
+ P.left = x.right;
+ if (P.left != null) {
+ P.left.parent = P;
+ }
+ } else {
+ P.right = x.right;
+ if (P.right != null) {
+ P.right.parent = P;
+ }
+ }
+ } else if (x.right == null) {//右子支为空,可直接删除,在这一层一定不需要旋转
+ size--;
+ if (P.left == x) {
+ P.left = x.left;
+ if (P.left != null) {
+ P.left.parent = P;
+ }
+ } else {
+ P.right = x.left;
+ if (P.right != null) {
+ P.right.parent = P;
+ }
+ }
+ } else {//找到待删除的节点,用后继节点代替,然后删除后继节点
+ E nextVal = treeMin(x.right);
+ x.element = nextVal;
+ delete0(x.right, nextVal);
+ int lh = getHight(x.left);
+ int rh = getHight(x.right);
+ if (lh - rh == 2) {
+ if (getHight(x.left.left) > getHight(x.left.right)) {
+ clockwistRotate(x);
+ } else {
+ antiClockwiseRotate(x.left);
+ clockwistRotate(x);
+ }
+ }
+ }
+ }
+ x.h = Math.max(getHight(x.left), getHight(x.right)) + 1;
+ return true;
+ }
+
+// public static void main(String[] args) {
+// AVLTree1 avl = new AVLTree1<>();
+// /*可自行添加插入,删除操作进行测试*/
+// avl.insert(3);
+// avl.insert(5);
+// avl.insert(6);
+// avl.insert(7);
+// avl.insert(8);
+// avl.insert(9);
+// avl.preorderTraverse();
+// System.out.println();
+// System.out.println(avl.size());
+//
+// avl.delete(7);
+// avl.delete(8);
+// avl.preorderTraverse();
+// System.out.println();
+// System.out.println(avl.size());
+// }
+}
diff --git a/src/main/java/com/lin/tree/avltree/AVLTree2.java b/src/main/java/com/lin/tree/avltree/AVLTree2.java
new file mode 100644
index 0000000..da40769
--- /dev/null
+++ b/src/main/java/com/lin/tree/avltree/AVLTree2.java
@@ -0,0 +1,199 @@
+package com.lin.tree.avltree;
+
+//AVL树的节点类
+class AVLNode {
+ T element;
+ AVLNode left;
+ AVLNode right;
+ int height;//记录高度
+
+ //构造器
+ public AVLNode(T theElement) {
+ this(theElement, null, null);
+ element = theElement;
+ }
+
+ public AVLNode(T theElement, AVLNode left, AVLNode right) {
+ this.element = theElement;
+ this.left = left;
+ this.right = right;
+ this.height = 0;
+ }
+}
+
+//AVL树
+public class AVLTree2 {
+ //私有属性
+ private AVLNode root;//根节点
+
+ private int height(AVLNode t) {
+ return t == null ? -1 : t.height;
+ }
+
+ //构造方法
+ AVLTree2() {
+ this.root = null;
+ }
+
+ //插入操作
+ public void insert(T x) {
+ this.root = insert(x, root);
+ }
+
+ //删除操作
+ public void remove(T x) {
+ this.root = remove(x, root);
+ }
+
+ //中序打印操作
+ public void infPrintTree() {
+ infPrintTree(root);
+ System.out.println();
+ }
+
+ //判断是否是AVL树
+ public boolean isAVL() {
+ return Math.abs(maxDeep(root.right) - maxDeep(root.left)) < 2;
+ }
+
+ public AVLNode findMax(AVLNode t) {
+ if (t == null)
+ throw new RuntimeException("this AVLNode is Empty");
+ if (t.right == null)
+ return t;
+ return findMax(t.right);
+ }
+
+ public AVLNode insert(T x, AVLNode t) {
+ if (t == null)//传入节点为空,则将该节点作为根节点返回
+ return new AVLNode<>(x, null, null);
+ int compareResult = x.compareTo(t.element);
+ if (compareResult < 0)//传入元素x小于t
+ t.left = insert(x, t.left);
+ else if (compareResult > 0)//传入元素x大于t
+ t.right = insert(x, t.right);
+ return balance(t);
+ }
+
+ //重新进行平衡
+ public AVLNode balance(AVLNode t) {
+ if (t == null)
+ return t;
+ if (height(t.left) - height(t.right) > 1) {//左插入
+ if (height(t.left.left) >= height(t.left.right)) //左左插入 情形1
+ t = singleRightRotate(t);//右旋
+ else //左右插入 情形2
+ t = doubleLeftRightRotate(t);//左右双旋
+ }
+ if (height(t.right) - height(t.left) > 1) {//右插入
+ if (height(t.right.right) >= height(t.right.left)) //右右插入 情形3
+ t = singleLeftRotate(t);//左旋
+ else //右左插入 情形4
+ t = doubleRightLeftRotate(t);//右左双旋
+ }
+ t.height = height(t.right) > height(t.left) ? height(t.right) + 1 : height(t.left) + 1;
+ return t;
+ }
+
+ //右单旋
+ private AVLNode singleRightRotate(AVLNode t) {
+ System.out.println("进行右旋转==============>>>");
+ AVLNode r = t.left;
+ t.left = r.right;
+ r.right = t;
+ t.height = height(t.left) > height(t.right) ? height(t.left) + 1 : height(t.right) + 1;
+ r.height = height(t.right) > height(r) ? height(t.right) + 1 : height(r) + 1;
+ return r;
+ }
+
+ //左单旋
+ private AVLNode singleLeftRotate(AVLNode t) {
+ System.out.println("进行左旋转==============>>>");
+ AVLNode r = t.right;
+ t.right = r.left;
+ r.left = t;
+ t.height = height(t.left) > height(t.right) ? height(t.left) + 1 : height(t.right) + 1;
+ r.height = height(t.left) > height(r) ? height(t.left) + 1 : height(r) + 1;
+ return r;
+ }
+
+ //右左双旋
+ private AVLNode doubleRightLeftRotate(AVLNode t) {
+ System.out.println("进行右左双旋==============>>>");
+ t.right = singleRightRotate(t.right);
+ return singleLeftRotate(t);
+ }
+
+ //左右双旋
+ private AVLNode doubleLeftRightRotate(AVLNode t) {
+ System.out.println("进行左右双旋==============>>>");
+ t.left = singleLeftRotate(t.left);
+ return singleRightRotate(t);
+ }
+
+ public AVLNode remove(T x, AVLNode t) {
+ if (t == null)//传入节点为空
+ return t;
+ int compareResult = x.compareTo(t.element);
+ if (compareResult < 0)
+ t.left = remove(x, t.left);
+ else if (compareResult > 0)
+ t.right = remove(x, t.right);
+ else if (t.left != null && t.right != null) { //有两个儿子
+ t.element = findMax(t.left).element;
+ remove(t.element, t.left);
+ } else //单儿子情形
+ t = t.left == null ? t.right : t.left;
+ if (t != null)
+ t.height = height(t.right) > height(t.left) ? height(t.right) + 1 : height(t.left) + 1;
+ return balance(t);
+ }
+
+ public int maxDeep(AVLNode t) {
+ int dl, dr;//记录左右树的深度
+ if (t == null)
+ return 0;
+ else {
+ dl = maxDeep(t.left);
+ dr = maxDeep(t.right);
+ }
+ return dl > dr ? dl + 1 : dr + 1;
+ }
+
+
+ public void infPrintTree(AVLNode t) {
+ if (t == null)
+ return;
+ infPrintTree(t.left);
+ System.out.print(t.element + " ");
+ infPrintTree(t.right);
+ }
+
+// public static void main(String[] args) {
+// AVLTree2 avlT = createAVLTree();
+// System.out.println("是否是AVL树:" + avlT.isAVL());
+// System.out.println("中序打印出该树===>>>");
+// avlT.infPrintTree();
+// avlT.remove(60);
+// avlT.insert(35);
+// }
+
+ /**
+ * 50
+ * / \
+ * 30 60
+ * / \
+ * 20 40
+ *
+ * @return
+ */
+ private static AVLTree2 createAVLTree() {
+ AVLTree2 avlT = new AVLTree2();
+ avlT.insert(50);
+ avlT.insert(60);
+ avlT.insert(30);
+ avlT.insert(40);
+ avlT.insert(20);
+ return avlT;
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/lin/TestTree/AVLTree1Test.java b/src/test/java/com/lin/TestTree/AVLTree1Test.java
new file mode 100644
index 0000000..f1f71eb
--- /dev/null
+++ b/src/test/java/com/lin/TestTree/AVLTree1Test.java
@@ -0,0 +1,39 @@
+package com.lin.TestTree;
+
+public class AVLTree1Test {
+
+ private static int arr[] = {3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9};
+
+ public static void main(String[] args) {
+// int i;
+// AVLTree1 tree = new AVLTree1();
+//
+// System.out.printf("== 依次添加: ");
+// for (i = 0; i < arr.length; i++) {
+// System.out.printf("%d ", arr[i]);
+// tree.insert(arr[i]);
+// }
+// System.out.printf("\n== 前序遍历: ");
+// tree.preOrder();
+// System.out.printf("\n== 中序遍历: ");
+// tree.inOrder();
+// System.out.printf("\n== 后序遍历: ");
+// tree.postOrder();
+// System.out.printf("\n");
+// System.out.printf("== 高度: %d\n", tree.height());
+// System.out.printf("== 最小值: %d\n", tree.minimum());
+// System.out.printf("== 最大值: %d\n", tree.maximum());
+// System.out.printf("== 树的详细信息: \n");
+// tree.print();
+// i = 8;
+// System.out.printf("\n== 删除根节点: %d", i);
+// tree.remove(i);
+// System.out.printf("\n== 高度: %d", tree.height());
+// System.out.printf("\n== 中序遍历: ");
+// tree.inOrder();
+// System.out.printf("\n== 树的详细信息: \n");
+// tree.print();
+// // 销毁二叉树
+// tree.destroy();
+ }
+}
--
Gitee
From 8d8e256e214e4b603499b86ff182ee0ad598c5e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Thu, 19 Mar 2020 23:23:46 +0800
Subject: [PATCH 28/46] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BA=A2=E9=BB=91?=
=?UTF-8?q?=E6=A0=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/tree/RBTree.java | 7 +++++++
1 file changed, 7 insertions(+)
create mode 100644 src/main/java/com/lin/tree/RBTree.java
diff --git a/src/main/java/com/lin/tree/RBTree.java b/src/main/java/com/lin/tree/RBTree.java
new file mode 100644
index 0000000..f31f35f
--- /dev/null
+++ b/src/main/java/com/lin/tree/RBTree.java
@@ -0,0 +1,7 @@
+package com.lin.tree;
+
+/**
+ * 红黑树
+ */
+public class RBTree {
+}
--
Gitee
From cef340c5cd04dab7c6dd916fc64fcede0ce095de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Fri, 20 Mar 2020 20:41:15 +0800
Subject: [PATCH 29/46] =?UTF-8?q?=E6=97=A0=E8=81=8A=E7=9A=84=E6=8F=90?=
=?UTF-8?q?=E4=BA=A4=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/tree/RBTree.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/main/java/com/lin/tree/RBTree.java b/src/main/java/com/lin/tree/RBTree.java
index f31f35f..c09c2ae 100644
--- a/src/main/java/com/lin/tree/RBTree.java
+++ b/src/main/java/com/lin/tree/RBTree.java
@@ -4,4 +4,5 @@ package com.lin.tree;
* 红黑树
*/
public class RBTree {
-}
+
+}
\ No newline at end of file
--
Gitee
From f46aee5a9feb469cf188f3b0bdba84a168d9a40b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Sat, 21 Mar 2020 21:03:36 +0800
Subject: [PATCH 30/46] =?UTF-8?q?=E6=97=A0=E8=81=8A=E7=9A=84=E6=8F=90?=
=?UTF-8?q?=E4=BA=A4=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/tree/RBTree.java | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/src/main/java/com/lin/tree/RBTree.java b/src/main/java/com/lin/tree/RBTree.java
index c09c2ae..ceca2e4 100644
--- a/src/main/java/com/lin/tree/RBTree.java
+++ b/src/main/java/com/lin/tree/RBTree.java
@@ -5,4 +5,10 @@ package com.lin.tree;
*/
public class RBTree {
+
+}
+
+
+class Node {
+
}
\ No newline at end of file
--
Gitee
From ee9c3156c15960f669db4264abefbba3431c020c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Sun, 22 Mar 2020 18:58:43 +0800
Subject: [PATCH 31/46] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BC=A0=E8=BE=93?=
=?UTF-8?q?=E4=B9=8BIO?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/io/AIO.java | 4 ++++
src/main/java/com/lin/io/IO.java | 7 +++++++
src/main/java/com/lin/io/NIO.java | 4 ++++
3 files changed, 15 insertions(+)
create mode 100644 src/main/java/com/lin/io/AIO.java
create mode 100644 src/main/java/com/lin/io/IO.java
create mode 100644 src/main/java/com/lin/io/NIO.java
diff --git a/src/main/java/com/lin/io/AIO.java b/src/main/java/com/lin/io/AIO.java
new file mode 100644
index 0000000..d44a6f9
--- /dev/null
+++ b/src/main/java/com/lin/io/AIO.java
@@ -0,0 +1,4 @@
+package com.lin.io;
+
+public class AIO {
+}
diff --git a/src/main/java/com/lin/io/IO.java b/src/main/java/com/lin/io/IO.java
new file mode 100644
index 0000000..b61ba50
--- /dev/null
+++ b/src/main/java/com/lin/io/IO.java
@@ -0,0 +1,7 @@
+package com.lin.io;
+
+/**
+ * 普通的IO流
+ */
+public class IO {
+}
diff --git a/src/main/java/com/lin/io/NIO.java b/src/main/java/com/lin/io/NIO.java
new file mode 100644
index 0000000..a9de064
--- /dev/null
+++ b/src/main/java/com/lin/io/NIO.java
@@ -0,0 +1,4 @@
+package com.lin.io;
+
+public class NIO {
+}
--
Gitee
From fed13977711f758d2148ac41271145399f1b69e2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Sun, 22 Mar 2020 19:00:03 +0800
Subject: [PATCH 32/46] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BC=A0=E8=BE=93?=
=?UTF-8?q?=E4=B9=8BIO?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/tree/RBTree.java | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/src/main/java/com/lin/tree/RBTree.java b/src/main/java/com/lin/tree/RBTree.java
index ceca2e4..8e7477a 100644
--- a/src/main/java/com/lin/tree/RBTree.java
+++ b/src/main/java/com/lin/tree/RBTree.java
@@ -6,6 +6,14 @@ package com.lin.tree;
public class RBTree {
+
+
+
+
+
+
+
+
}
--
Gitee
From ffdd68f7ee370b3f423848e6df150c72714b3005 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 23 Mar 2020 20:39:12 +0800
Subject: [PATCH 33/46] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8B=B7=E8=B4=9D?=
=?UTF-8?q?=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/io/FileCopy.java | 251 +++++++++++++++++++++++++
1 file changed, 251 insertions(+)
create mode 100644 src/main/java/com/lin/io/FileCopy.java
diff --git a/src/main/java/com/lin/io/FileCopy.java b/src/main/java/com/lin/io/FileCopy.java
new file mode 100644
index 0000000..1c86a8c
--- /dev/null
+++ b/src/main/java/com/lin/io/FileCopy.java
@@ -0,0 +1,251 @@
+package com.lin.io;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+public class FileCopy {
+
+ /*如果操作文件是纯文本则使用字符流,如果不是纯文本则使用字节流*/
+ public static void main(String[] args) {
+ //向文件中写入数据(会把原来的数据覆盖掉)
+ //writeFile();
+ //按照单个字符读取
+ //readByCharacter();
+ //按照字符组读取
+ //readByCharacterArray();
+ //对已存在的文件进行续写(不会覆盖原来的数据但是,只能写一次)
+ //writeFileContinue();
+ //将F盘的文件拷贝到D盘
+ //copyFileFromFtoD();
+ //字符缓冲流的读取
+ /*缓冲区的出现时为了提高流的操作效率而出现的.
+ 需要被提高效率的流作为参数传递给缓冲区的构造函数
+ 在缓冲区中封装了一个数组,存入数据后一次取出*/
+ /*读取的内容是:
+ 窗前明月光,疑是地上霜。
+ 举头望明月,低头思故乡。
+ --李白*/
+ //bufferedReader();
+ //字符缓冲流的写
+ //bufferedWriter();
+ //媒体流的时候就会用到字节流
+ //将F盘的图片拷贝到D盘
+ //copyPictureFromDtoF();
+ //将F盘的音乐复制到D盘
+ //copyMP3FromFtoD();
+ }
+
+ private static void copyMP3FromFtoD() {
+ FileInputStream fi = null;
+ FileOutputStream fo = null;
+ try {
+ fi = new FileInputStream("F:\\aa\\guoge.mp3");
+ fo = new FileOutputStream("D:/guoge_copy.mp3");
+ byte[] buf = new byte[1024];
+ int n = 0;
+ while ((n = (fi.read(buf))) != -1) {
+ fo.write(buf, 0, n);
+ }
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ fo.close();
+ fi.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void copyPictureFromDtoF() {
+ FileInputStream fi = null;
+ FileOutputStream fo = null;
+ try {
+ fi = new FileInputStream("F:\\aa\\004.png");
+ fo = new FileOutputStream("D:\\004_copy.png");
+ byte[] buf = new byte[1024];
+ int n = 0;
+ while ((n = fi.read(buf)) != -1) {
+ fo.write(buf, 0, n);
+ }
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ fo.close();
+ fi.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void bufferedWriter() {
+ FileWriter file = null;
+ BufferedWriter bw = null;
+ try {
+ file = new FileWriter("F:\\aa\\bb.txt", true);
+ bw = new BufferedWriter(file);
+ //跨平台的换行符
+ bw.newLine();
+ bw.write("天行健,君子以自强不息;");
+ bw.newLine();
+ bw.write("地势坤,君子以厚德载物。");
+ bw.newLine();
+ //缓冲区的写必须有刷新
+ bw.flush();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ bw.close();
+ file.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void bufferedReader() {
+ FileReader file = null;
+ try {
+ file = new FileReader("F:\\aa\\bb.txt");
+ BufferedReader br = new BufferedReader(file);
+ while (true) {
+ String s;
+ try {
+ s = br.readLine();
+ if (s == null) break;
+ System.out.println(s);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ file.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void copyFileFromFtoD() {
+ FileWriter fw = null;
+ FileReader fr = null;
+ try {
+ fw = new FileWriter("D:\\test20180224.txt", true);
+ fr = new FileReader("F:\\aa\\test.txt");
+ char[] buf = new char[11];
+ int n = 0;
+ while ((n = fr.read(buf)) != -1) {
+ fw.write(new String(buf, 0, n));
+ System.out.println(new String(buf, 0, n));
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ fw.close();
+ fr.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void writeFileContinue() {
+ FileWriter file = null;
+ try {
+ file = new FileWriter("F:\\aa\\test.txt", true);
+ file.write("(这是续写内容)");
+ file.flush();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ file.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void readByCharacter() {
+ FileReader file = null;
+ try {
+ //创建FileReader并指定要读取的文件
+ file = new FileReader("F:\\aa\\test.txt");
+ int n = 0;
+ while ((n = file.read()) != -1) {
+ System.out.println((char) n);
+ }
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ file.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void readByCharacterArray() {
+ FileReader file = null;
+ try {
+ //创建FileReader并指定要读取的文件
+ file = new FileReader("F:\\aa\\test.txt");
+ char[] buf = new char[11];
+ int n = 0;
+ while ((n = file.read(buf)) != -1) {
+ System.out.println(new String(buf, 0, n));
+ }
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ file.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void writeFile() {
+ //创建一个FileWriter对象,该对象初始化的时候就要指定被操作的文件 该文件不存在就会新建一个文件
+ FileWriter file = null;
+ try {
+ file = new FileWriter("F:\\aa\\test.txt");
+ file.write("HelloWorld!");
+ //刷新缓存数据将数据写入文件
+ file.flush();
+ file.write("你好世界!");
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ file.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+}
--
Gitee
From 97ad82facddf9919e89047a3c556bdd95a4bea14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 23 Mar 2020 22:03:10 +0800
Subject: [PATCH 34/46] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=A1=88=E4=BE=8B?=
=?UTF-8?q?=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/com/lin/io/{ => nome}/FileCopy.java | 2 +-
src/main/java/com/lin/io/nome/TestIO1.java | 88 ++++++++++++++
src/main/java/com/lin/io/nome/TestIO2.java | 107 ++++++++++++++++++
3 files changed, 196 insertions(+), 1 deletion(-)
rename src/main/java/com/lin/io/{ => nome}/FileCopy.java (99%)
create mode 100644 src/main/java/com/lin/io/nome/TestIO1.java
create mode 100644 src/main/java/com/lin/io/nome/TestIO2.java
diff --git a/src/main/java/com/lin/io/FileCopy.java b/src/main/java/com/lin/io/nome/FileCopy.java
similarity index 99%
rename from src/main/java/com/lin/io/FileCopy.java
rename to src/main/java/com/lin/io/nome/FileCopy.java
index 1c86a8c..5ed9831 100644
--- a/src/main/java/com/lin/io/FileCopy.java
+++ b/src/main/java/com/lin/io/nome/FileCopy.java
@@ -1,4 +1,4 @@
-package com.lin.io;
+package com.lin.io.nome;
import java.io.BufferedReader;
import java.io.BufferedWriter;
diff --git a/src/main/java/com/lin/io/nome/TestIO1.java b/src/main/java/com/lin/io/nome/TestIO1.java
new file mode 100644
index 0000000..fd681aa
--- /dev/null
+++ b/src/main/java/com/lin/io/nome/TestIO1.java
@@ -0,0 +1,88 @@
+package com.lin.io.nome;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+public class TestIO1 {
+ public static void FileInputStreamTest() throws IOException {
+ FileInputStream fis = new FileInputStream("tmp2.txt");
+ byte[] buf = new byte[1024];
+ int hasRead = 0;
+
+ //read()返回的是单个字节数据(字节数据可以直接专程int类型),但是read(buf)返回的是读取到的字节数,真正的数据保存在buf中
+ while ((hasRead = fis.read(buf)) > 0) {
+ //每次最多将1024个字节转换成字符串,这里tmp2.txt中的字符小于1024,所以一次就读完了
+ //循环次数 = 文件字符数 除以 buf长度
+ System.out.println(new String(buf, 0, hasRead));
+ /*
+ * 将字节强制转换成字符后逐个输出,能实现和上面一样的效果。但是如果源文件是中文的话可能会乱码
+
+ for (byte b : buf) {
+ char ch = (char)b;
+ if (ch != '\r')
+ System.out.print(ch);
+ }
+ */
+ }
+ //在finally块里close更安全
+ fis.close();
+ }
+
+ public static void FileReaderTest() throws IOException {
+
+ try (
+ // 在try() 中打开的文件, JVM会自动关闭
+ FileReader fr = new FileReader("tmp2.txt")) {
+ char[] buf = new char[32];
+ int hasRead = 0;
+ // 每个char都占两个字节,每个字符或者汉字都是占2个字节,因此无论buf长度为多少,总是能读取中文字符长度的整数倍,不会乱码
+ while ((hasRead = fr.read(buf)) > 0) {
+ // 如果buf的长度大于文件每行的长度,就可以完整输出每行,否则会断行。
+ // 循环次数 = 文件字符数 除以 buf长度
+ System.out.println(new String(buf, 0, hasRead));
+ // 跟上面效果一样
+ // System.out.println(buf);
+ }
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ public static void FileOutputStreamTest() throws FileNotFoundException, IOException {
+ try (
+ //在try()中打开文件会在结尾自动关闭
+ FileInputStream fis = new FileInputStream("tmp2.txt");
+ FileOutputStream fos = new FileOutputStream("tmp3.txt");
+ ) {
+ byte[] buf = new byte[4];
+ int hasRead = 0;
+ while ((hasRead = fis.read(buf)) > 0) {
+ //每读取一次就写一次,读多少就写多少
+ fos.write(buf, 0, hasRead);
+ }
+ System.out.println("write success");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static void FileWriterTest() throws IOException {
+ try (FileWriter fw = new FileWriter("tmp4.txt")) {
+ fw.write("天王盖地虎\r\n");
+ fw.write("宝塔镇河妖\r\n");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+ //FileInputStreamTest();
+ //FileReaderTest();
+ //FileOutputStreamTest();
+ FileWriterTest();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/lin/io/nome/TestIO2.java b/src/main/java/com/lin/io/nome/TestIO2.java
new file mode 100644
index 0000000..9a6ac93
--- /dev/null
+++ b/src/main/java/com/lin/io/nome/TestIO2.java
@@ -0,0 +1,107 @@
+package com.lin.io.nome;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.io.PushbackReader;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+public class TestIO2 {
+ public static void printStream() throws FileNotFoundException, IOException {
+ try (
+ FileOutputStream fos = new FileOutputStream("tmp.txt");
+ PrintStream ps = new PrintStream(fos)) {
+ ps.println("普通字符串\n");
+ //输出对象
+ ps.println(new TestIO2());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ System.out.println("输出完成");
+
+ }
+
+ public static void stringNode() throws IOException {
+ String str = "天王盖地虎\n"
+ + "宝塔镇河妖\n";
+ char[] buf = new char[32];
+ int hasRead = 0;
+ //StringReader将以String字符串为节点读取数据
+ try (StringReader sr = new StringReader(str)) {
+ while ((hasRead = sr.read(buf)) > 0) {
+ System.out.print(new String(buf, 0, hasRead));
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ //由于String是一个不可变类,因此创建StringWriter时,实际上是以一个StringBuffer作为输出节点
+ try (StringWriter sw = new StringWriter()) {
+ sw.write("黑夜给了我黑色的眼睛\n");
+ sw.write("我却用它寻找光明\n");
+ //toString()返回sw节点内的数据
+ System.out.println(sw.toString());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static void keyIn() throws IOException {
+ try (
+ //InputStreamReader是从byte转成char的桥梁
+ InputStreamReader reader = new InputStreamReader(System.in);
+ //BufferedReader(Reader in)是char类型输入的包装类
+ BufferedReader br = new BufferedReader(reader);
+ ) {
+ String line = null;
+ while ((line = br.readLine()) != null) {
+ if (line.equals("exit")) {
+ //System.exit(1);
+ break;
+ }
+ System.out.println(line);
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static void pushback() throws FileNotFoundException, IOException {
+ try (PushbackReader pr = new PushbackReader(new FileReader("C:/PROJECT/JavaBasic/PROJECT_JavaBasic/src/io/TestIO.java"), 64)) {
+ char[] buf = new char[32];
+ String lastContent = "";
+ int hasRead = 0;
+ while ((hasRead = pr.read(buf)) > 0) {
+ String content = new String(buf, 0, hasRead);
+ int targetIndex = 0;
+ if ((targetIndex = (lastContent + content).indexOf("targetIndex = (lastContent + content)")) > 0) {
+ pr.unread((lastContent + content).toCharArray());
+ if (targetIndex > 32) {
+ buf = new char[targetIndex];
+ }
+ pr.read(buf, 0, targetIndex);
+ System.out.println(new String(buf, 0, targetIndex));
+ System.exit(0);
+ } else {
+ System.out.println(lastContent);
+ lastContent = content;
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+ printStream();
+ //stringNode();
+ //keyIn();
+ //pushback();
+ }
+}
\ No newline at end of file
--
Gitee
From 9ef3623b9a116a4eae0612bbdca67151592dc29a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Mon, 23 Mar 2020 22:04:31 +0800
Subject: [PATCH 35/46] =?UTF-8?q?=E5=B7=A5=E7=A8=8B=E9=87=8D=E6=9E=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/tree/{ => nome}/BSTree.java | 2 +-
src/main/java/com/lin/tree/{ => nome}/RBTree.java | 2 +-
src/main/java/com/lin/tree/{ => nome}/Tree.java | 2 +-
src/main/java/com/lin/tree/{ => nome}/avltree/AVLTree1.java | 2 +-
src/main/java/com/lin/tree/{ => nome}/avltree/AVLTree2.java | 2 +-
5 files changed, 5 insertions(+), 5 deletions(-)
rename src/main/java/com/lin/tree/{ => nome}/BSTree.java (99%)
rename src/main/java/com/lin/tree/{ => nome}/RBTree.java (73%)
rename src/main/java/com/lin/tree/{ => nome}/Tree.java (99%)
rename src/main/java/com/lin/tree/{ => nome}/avltree/AVLTree1.java (99%)
rename src/main/java/com/lin/tree/{ => nome}/avltree/AVLTree2.java (99%)
diff --git a/src/main/java/com/lin/tree/BSTree.java b/src/main/java/com/lin/tree/nome/BSTree.java
similarity index 99%
rename from src/main/java/com/lin/tree/BSTree.java
rename to src/main/java/com/lin/tree/nome/BSTree.java
index dc72144..362584c 100644
--- a/src/main/java/com/lin/tree/BSTree.java
+++ b/src/main/java/com/lin/tree/nome/BSTree.java
@@ -1,4 +1,4 @@
-package com.lin.tree;
+package com.lin.tree.nome;
import lombok.Data;
diff --git a/src/main/java/com/lin/tree/RBTree.java b/src/main/java/com/lin/tree/nome/RBTree.java
similarity index 73%
rename from src/main/java/com/lin/tree/RBTree.java
rename to src/main/java/com/lin/tree/nome/RBTree.java
index 8e7477a..03f78d6 100644
--- a/src/main/java/com/lin/tree/RBTree.java
+++ b/src/main/java/com/lin/tree/nome/RBTree.java
@@ -1,4 +1,4 @@
-package com.lin.tree;
+package com.lin.tree.nome;
/**
* 红黑树
diff --git a/src/main/java/com/lin/tree/Tree.java b/src/main/java/com/lin/tree/nome/Tree.java
similarity index 99%
rename from src/main/java/com/lin/tree/Tree.java
rename to src/main/java/com/lin/tree/nome/Tree.java
index 2d62e66..91694a4 100644
--- a/src/main/java/com/lin/tree/Tree.java
+++ b/src/main/java/com/lin/tree/nome/Tree.java
@@ -1,4 +1,4 @@
-package com.lin.tree;
+package com.lin.tree.nome;
import lombok.Data;
diff --git a/src/main/java/com/lin/tree/avltree/AVLTree1.java b/src/main/java/com/lin/tree/nome/avltree/AVLTree1.java
similarity index 99%
rename from src/main/java/com/lin/tree/avltree/AVLTree1.java
rename to src/main/java/com/lin/tree/nome/avltree/AVLTree1.java
index 88a436c..27d5698 100644
--- a/src/main/java/com/lin/tree/avltree/AVLTree1.java
+++ b/src/main/java/com/lin/tree/nome/avltree/AVLTree1.java
@@ -1,4 +1,4 @@
-package com.lin.tree.avltree;
+package com.lin.tree.nome.avltree;
import java.util.Comparator;
diff --git a/src/main/java/com/lin/tree/avltree/AVLTree2.java b/src/main/java/com/lin/tree/nome/avltree/AVLTree2.java
similarity index 99%
rename from src/main/java/com/lin/tree/avltree/AVLTree2.java
rename to src/main/java/com/lin/tree/nome/avltree/AVLTree2.java
index da40769..4b2cc0e 100644
--- a/src/main/java/com/lin/tree/avltree/AVLTree2.java
+++ b/src/main/java/com/lin/tree/nome/avltree/AVLTree2.java
@@ -1,4 +1,4 @@
-package com.lin.tree.avltree;
+package com.lin.tree.nome.avltree;
//AVL树的节点类
class AVLNode {
--
Gitee
From c8d059d441af9ec913b7ac16194d803a7889d614 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Tue, 24 Mar 2020 20:12:19 +0800
Subject: [PATCH 36/46] =?UTF-8?q?=E7=BA=A2=E9=BB=91=E6=A0=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/tree/nome/RBTree.java | 675 +++++++++++++++++++-
1 file changed, 672 insertions(+), 3 deletions(-)
diff --git a/src/main/java/com/lin/tree/nome/RBTree.java b/src/main/java/com/lin/tree/nome/RBTree.java
index 03f78d6..727a689 100644
--- a/src/main/java/com/lin/tree/nome/RBTree.java
+++ b/src/main/java/com/lin/tree/nome/RBTree.java
@@ -2,21 +2,690 @@ package com.lin.tree.nome;
/**
* 红黑树
+ * @param
*/
-public class RBTree {
+public class RBTree> {
+ private RBTNode mRoot; // 根结点
+ private static final boolean RED = false;
+ private static final boolean BLACK = true;
+ public class RBTNode> {
+ boolean color; // 颜色
+ T key; // 关键字(键值)
+ RBTNode left; // 左孩子
+ RBTNode right; // 右孩子
+ RBTNode parent; // 父结点
+ public RBTNode(T key, boolean color, RBTNode parent, RBTNode left, RBTNode right) {
+ this.key = key;
+ this.color = color;
+ this.parent = parent;
+ this.left = left;
+ this.right = right;
+ }
+ public T getKey() {
+ return key;
+ }
+ public String toString() {
+ return ""+key+(this.color==RED?"(R)":"B");
+ }
+ }
+ public RBTree() {
+ mRoot=null;
+ }
+ private RBTNode parentOf(RBTNode node) {
+ return node!=null ? node.parent : null;
+ }
+ private boolean colorOf(RBTNode node) {
+ return node!=null ? node.color : BLACK;
+ }
+ private boolean isRed(RBTNode node) {
+ return ((node!=null)&&(node.color==RED)) ? true : false;
+ }
+ private boolean isBlack(RBTNode node) {
+ return !isRed(node);
+ }
+ private void setBlack(RBTNode node) {
+ if (node!=null)
+ node.color = BLACK;
+ }
+ private void setRed(RBTNode node) {
+ if (node!=null)
+ node.color = RED;
+ }
+ private void setParent(RBTNode node, RBTNode parent) {
+ if (node!=null)
+ node.parent = parent;
+ }
+ private void setColor(RBTNode node, boolean color) {
+ if (node!=null)
+ node.color = color;
+ }
+ /**
+ * 前序遍历"红黑树"
+ */
+ private void preOrder(RBTNode tree) {
+ if(tree != null) {
+ System.out.print(tree.key+" ");
+ preOrder(tree.left);
+ preOrder(tree.right);
+ }
+ }
-}
+ public void preOrder() {
+ preOrder(mRoot);
+ }
+ /**
+ * 中序遍历"红黑树"
+ */
+ private void inOrder(RBTNode tree) {
+ if(tree != null) {
+ inOrder(tree.left);
+ System.out.print(tree.key+" ");
+ inOrder(tree.right);
+ }
+ }
-class Node {
+ public void inOrder() {
+ inOrder(mRoot);
+ }
+
+ /**
+ * 后序遍历"红黑树"
+ */
+ private void postOrder(RBTNode tree) {
+ if(tree != null)
+ {
+ postOrder(tree.left);
+ postOrder(tree.right);
+ System.out.print(tree.key+" ");
+ }
+ }
+
+ public void postOrder() {
+ postOrder(mRoot);
+ }
+
+
+ /**
+ * (递归实现)查找"红黑树x"中键值为key的节点
+ */
+ private RBTNode search(RBTNode x, T key) {
+ if (x==null)
+ return x;
+
+ int cmp = key.compareTo(x.key);
+ if (cmp < 0)
+ return search(x.left, key);
+ else if (cmp > 0)
+ return search(x.right, key);
+ else
+ return x;
+ }
+
+ public RBTNode search(T key) {
+ return search(mRoot, key);
+ }
+
+ /**
+ * (非递归实现)查找"红黑树x"中键值为key的节点
+ */
+ private RBTNode iterativeSearch(RBTNode x, T key) {
+ while (x!=null) {
+ int cmp = key.compareTo(x.key);
+
+ if (cmp < 0)
+ x = x.left;
+ else if (cmp > 0)
+ x = x.right;
+ else
+ return x;
+ }
+
+ return x;
+ }
+
+ public RBTNode iterativeSearch(T key) {
+ return iterativeSearch(mRoot, key);
+ }
+
+ /**
+ * 查找最小结点:返回tree为根结点的红黑树的最小结点。
+ */
+ private RBTNode minimum(RBTNode tree) {
+ if (tree == null)
+ return null;
+
+ while(tree.left != null)
+ tree = tree.left;
+ return tree;
+ }
+
+ public T minimum() {
+ RBTNode p = minimum(mRoot);
+ if (p != null)
+ return p.key;
+
+ return null;
+ }
+
+ /**
+ * 查找最大结点:返回tree为根结点的红黑树的最大结点。
+ */
+ private RBTNode maximum(RBTNode tree) {
+ if (tree == null)
+ return null;
+
+ while(tree.right != null)
+ tree = tree.right;
+ return tree;
+ }
+
+ public T maximum() {
+ RBTNode p = maximum(mRoot);
+ if (p != null)
+ return p.key;
+
+ return null;
+ }
+
+ /**
+ * 找结点(x)的后继结点。即,查找"红黑树中数据值大于该结点"的"最小结点"。
+ */
+ public RBTNode successor(RBTNode x) {
+ // 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"。
+ if (x.right != null)
+ return minimum(x.right);
+
+ // 如果x没有右孩子。则x有以下两种可能:
+ // (01) x是"一个左孩子",则"x的后继结点"为 "它的父结点"。
+ // (02) x是"一个右孩子",则查找"x的最低的父结点,并且该父结点要具有左孩子",找到的这个"最低的父结点"就是"x的后继结点"。
+ RBTNode y = x.parent;
+ while ((y!=null) && (x==y.right)) {
+ x = y;
+ y = y.parent;
+ }
+
+ return y;
+ }
+
+ /**
+ * 找结点(x)的前驱结点。即,查找"红黑树中数据值小于该结点"的"最大结点"。
+ */
+ public RBTNode predecessor(RBTNode x) {
+ // 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。
+ if (x.left != null)
+ return maximum(x.left);
+
+ // 如果x没有左孩子。则x有以下两种可能:
+ // (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。
+ // (01) x是"一个左孩子",则查找"x的最低的父结点,并且该父结点要具有右孩子",找到的这个"最低的父结点"就是"x的前驱结点"。
+ RBTNode y = x.parent;
+ while ((y!=null) && (x==y.left)) {
+ x = y;
+ y = y.parent;
+ }
+
+ return y;
+ }
+
+ /**
+ * 对红黑树的节点(x)进行左旋转
+ *
+ * 左旋示意图(对节点x进行左旋):
+ * px px
+ * / /
+ * x y
+ * / \ --(左旋)-. / \ #
+ * lx y x ry
+ * / \ / \
+ * ly ry lx ly
+ *
+ *
+ */
+ private void leftRotate(RBTNode x) {
+ // 设置x的右孩子为y
+ RBTNode y = x.right;
+
+ // 将 “y的左孩子” 设为 “x的右孩子”;
+ // 如果y的左孩子非空,将 “x” 设为 “y的左孩子的父亲”
+ x.right = y.left;
+ if (y.left != null)
+ y.left.parent = x;
+
+ // 将 “x的父亲” 设为 “y的父亲”
+ y.parent = x.parent;
+
+ if (x.parent == null) {
+ this.mRoot = y; // 如果 “x的父亲” 是空节点,则将y设为根节点
+ } else {
+ if (x.parent.left == x)
+ x.parent.left = y; // 如果 x是它父节点的左孩子,则将y设为“x的父节点的左孩子”
+ else
+ x.parent.right = y; // 如果 x是它父节点的左孩子,则将y设为“x的父节点的左孩子”
+ }
+
+ // 将 “x” 设为 “y的左孩子”
+ y.left = x;
+ // 将 “x的父节点” 设为 “y”
+ x.parent = y;
+ }
+
+ /**
+ * 对红黑树的节点(y)进行右旋转
+ *
+ * 右旋示意图(对节点y进行左旋):
+ * py py
+ * / /
+ * y x
+ * / \ --(右旋)-. / \ #
+ * x ry lx y
+ * / \ / \ #
+ * lx rx rx ry
+ *
+ */
+ private void rightRotate(RBTNode y) {
+ // 设置x是当前节点的左孩子。
+ RBTNode x = y.left;
+
+ // 将 “x的右孩子” 设为 “y的左孩子”;
+ // 如果"x的右孩子"不为空的话,将 “y” 设为 “x的右孩子的父亲”
+ y.left = x.right;
+ if (x.right != null)
+ x.right.parent = y;
+
+ // 将 “y的父亲” 设为 “x的父亲”
+ x.parent = y.parent;
+
+ if (y.parent == null) {
+ this.mRoot = x; // 如果 “y的父亲” 是空节点,则将x设为根节点
+ } else {
+ if (y == y.parent.right)
+ y.parent.right = x; // 如果 y是它父节点的右孩子,则将x设为“y的父节点的右孩子”
+ else
+ y.parent.left = x; // (y是它父节点的左孩子) 将x设为“x的父节点的左孩子”
+ }
+
+ // 将 “y” 设为 “x的右孩子”
+ x.right = y;
+
+ // 将 “y的父节点” 设为 “x”
+ y.parent = x;
+ }
+
+ /**
+ * 红黑树插入修正函数
+ *
+ * 在向红黑树中插入节点之后(失去平衡),再调用该函数;
+ * 目的是将它重新塑造成一颗红黑树。
+ *
+ * 参数说明:
+ * node 插入的结点 // 对应《算法导论》中的z
+ */
+ private void insertFixUp(RBTNode node) {
+ RBTNode parent, gparent;
+
+ // 若“父节点存在,并且父节点的颜色是红色”
+ while (((parent = parentOf(node))!=null) && isRed(parent)) {
+ gparent = parentOf(parent);
+
+ //若“父节点”是“祖父节点的左孩子”
+ if (parent == gparent.left) {
+ // Case 1条件:叔叔节点是红色
+ RBTNode uncle = gparent.right;
+ if ((uncle!=null) && isRed(uncle)) {
+ setBlack(uncle);
+ setBlack(parent);
+ setRed(gparent);
+ node = gparent;
+ continue;
+ }
+
+ // Case 2条件:叔叔是黑色,且当前节点是右孩子
+ if (parent.right == node) {
+ RBTNode tmp;
+ leftRotate(parent);
+ tmp = parent;
+ parent = node;
+ node = tmp;
+ }
+
+ // Case 3条件:叔叔是黑色,且当前节点是左孩子。
+ setBlack(parent);
+ setRed(gparent);
+ rightRotate(gparent);
+ } else { //若“z的父节点”是“z的祖父节点的右孩子”
+ // Case 1条件:叔叔节点是红色
+ RBTNode uncle = gparent.left;
+ if ((uncle!=null) && isRed(uncle)) {
+ setBlack(uncle);
+ setBlack(parent);
+ setRed(gparent);
+ node = gparent;
+ continue;
+ }
+
+ // Case 2条件:叔叔是黑色,且当前节点是左孩子
+ if (parent.left == node) {
+ RBTNode tmp;
+ rightRotate(parent);
+ tmp = parent;
+ parent = node;
+ node = tmp;
+ }
+
+ // Case 3条件:叔叔是黑色,且当前节点是右孩子。
+ setBlack(parent);
+ setRed(gparent);
+ leftRotate(gparent);
+ }
+ }
+
+ // 将根节点设为黑色
+ setBlack(this.mRoot);
+ }
+
+ /**
+ * 将结点插入到红黑树中
+ *
+ * 参数说明:
+ * node 插入的结点 // 对应《算法导论》中的node
+ */
+ private void insert(RBTNode node) {
+ int cmp;
+ RBTNode y = null;
+ RBTNode x = this.mRoot;
+
+ // 1. 将红黑树当作一颗二叉查找树,将节点添加到二叉查找树中。
+ while (x != null) {
+ y = x;
+ cmp = node.key.compareTo(x.key);
+ if (cmp < 0)
+ x = x.left;
+ else
+ x = x.right;
+ }
+
+ node.parent = y;
+ if (y!=null) {
+ cmp = node.key.compareTo(y.key);
+ if (cmp < 0)
+ y.left = node;
+ else
+ y.right = node;
+ } else {
+ this.mRoot = node;
+ }
+
+ // 2. 设置节点的颜色为红色
+ node.color = RED;
+
+ // 3. 将它重新修正为一颗二叉查找树
+ insertFixUp(node);
+ }
+
+ /**
+ * 新建结点(key),并将其插入到红黑树中
+ *
+ * 参数说明:
+ * key 插入结点的键值
+ */
+ public void insert(T key) {
+ RBTNode node=new RBTNode(key,BLACK,null,null,null);
+
+ // 如果新建结点失败,则返回。
+ if (node != null)
+ insert(node);
+ }
+
+
+ /**
+ * 红黑树删除修正函数
+ *
+ * 在从红黑树中删除插入节点之后(红黑树失去平衡),再调用该函数;
+ * 目的是将它重新塑造成一颗红黑树。
+ *
+ * 参数说明:
+ * node 待修正的节点
+ */
+ private void removeFixUp(RBTNode node, RBTNode parent) {
+ RBTNode other;
+
+ while ((node==null || isBlack(node)) && (node != this.mRoot)) {
+ if (parent.left == node) {
+ other = parent.right;
+ if (isRed(other)) {
+ // Case 1: x的兄弟w是红色的
+ setBlack(other);
+ setRed(parent);
+ leftRotate(parent);
+ other = parent.right;
+ }
+
+ if ((other.left==null || isBlack(other.left)) &&
+ (other.right==null || isBlack(other.right))) {
+ // Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的
+ setRed(other);
+ node = parent;
+ parent = parentOf(node);
+ } else {
+
+ if (other.right==null || isBlack(other.right)) {
+ // Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
+ setBlack(other.left);
+ setRed(other);
+ rightRotate(other);
+ other = parent.right;
+ }
+ // Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
+ setColor(other, colorOf(parent));
+ setBlack(parent);
+ setBlack(other.right);
+ leftRotate(parent);
+ node = this.mRoot;
+ break;
+ }
+ } else {
+
+ other = parent.left;
+ if (isRed(other)) {
+ // Case 1: x的兄弟w是红色的
+ setBlack(other);
+ setRed(parent);
+ rightRotate(parent);
+ other = parent.left;
+ }
+
+ if ((other.left==null || isBlack(other.left)) &&
+ (other.right==null || isBlack(other.right))) {
+ // Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的
+ setRed(other);
+ node = parent;
+ parent = parentOf(node);
+ } else {
+
+ if (other.left==null || isBlack(other.left)) {
+ // Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
+ setBlack(other.right);
+ setRed(other);
+ leftRotate(other);
+ other = parent.left;
+ }
+
+ // Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
+ setColor(other, colorOf(parent));
+ setBlack(parent);
+ setBlack(other.left);
+ rightRotate(parent);
+ node = this.mRoot;
+ break;
+ }
+ }
+ }
+
+ if (node!=null)
+ setBlack(node);
+ }
+
+ /**
+ * 删除结点(node),并返回被删除的结点
+ *
+ * 参数说明:
+ * node 删除的结点
+ */
+ private void remove(RBTNode node) {
+ RBTNode child, parent;
+ boolean color;
+
+ // 被删除节点的"左右孩子都不为空"的情况。
+ if ( (node.left!=null) && (node.right!=null) ) {
+ // 被删节点的后继节点。(称为"取代节点")
+ // 用它来取代"被删节点"的位置,然后再将"被删节点"去掉。
+ RBTNode replace = node;
+
+ // 获取后继节点
+ replace = replace.right;
+ while (replace.left != null)
+ replace = replace.left;
+
+ // "node节点"不是根节点(只有根节点不存在父节点)
+ if (parentOf(node)!=null) {
+ if (parentOf(node).left == node)
+ parentOf(node).left = replace;
+ else
+ parentOf(node).right = replace;
+ } else {
+ // "node节点"是根节点,更新根节点。
+ this.mRoot = replace;
+ }
+
+ // child是"取代节点"的右孩子,也是需要"调整的节点"。
+ // "取代节点"肯定不存在左孩子!因为它是一个后继节点。
+ child = replace.right;
+ parent = parentOf(replace);
+ // 保存"取代节点"的颜色
+ color = colorOf(replace);
+
+ // "被删除节点"是"它的后继节点的父节点"
+ if (parent == node) {
+ parent = replace;
+ } else {
+ // child不为空
+ if (child!=null)
+ setParent(child, parent);
+ parent.left = child;
+
+ replace.right = node.right;
+ setParent(node.right, replace);
+ }
+
+ replace.parent = node.parent;
+ replace.color = node.color;
+ replace.left = node.left;
+ node.left.parent = replace;
+
+ if (color == BLACK)
+ removeFixUp(child, parent);
+
+ node = null;
+ return ;
+ }
+
+ if (node.left !=null) {
+ child = node.left;
+ } else {
+ child = node.right;
+ }
+
+ parent = node.parent;
+ // 保存"取代节点"的颜色
+ color = node.color;
+
+ if (child!=null)
+ child.parent = parent;
+
+ // "node节点"不是根节点
+ if (parent!=null) {
+ if (parent.left == node)
+ parent.left = child;
+ else
+ parent.right = child;
+ } else {
+ this.mRoot = child;
+ }
+
+ if (color == BLACK)
+ removeFixUp(child, parent);
+ node = null;
+ }
+
+ /**
+ * 删除结点(z),并返回被删除的结点
+ *
+ * 参数说明:
+ * tree 红黑树的根结点
+ * z 删除的结点
+ */
+ public void remove(T key) {
+ RBTNode node;
+
+ if ((node = search(mRoot, key)) != null)
+ remove(node);
+ }
+
+ /**
+ * 销毁红黑树
+ */
+ private void destroy(RBTNode tree) {
+ if (tree==null)
+ return ;
+
+ if (tree.left != null)
+ destroy(tree.left);
+ if (tree.right != null)
+ destroy(tree.right);
+
+ tree=null;
+ }
+
+ public void clear() {
+ destroy(mRoot);
+ mRoot = null;
+ }
+
+ /**
+ * 打印"红黑树"
+ *
+ * key -- 节点的键值
+ * direction -- 0,表示该节点是根节点;
+ * -1,表示该节点是它的父结点的左孩子;
+ * 1,表示该节点是它的父结点的右孩子。
+ */
+ private void print(RBTNode tree, T key, int direction) {
+
+ if(tree != null) {
+
+ if(direction==0) // tree是根节点
+ System.out.printf("%2d(B) is root\n", tree.key);
+ else // tree是分支节点
+ System.out.printf("%2d(%s) is %2d's %6s child\n", tree.key, isRed(tree)?"R":"B", key, direction==1?"right" : "left");
+
+ print(tree.left, tree.key, -1);
+ print(tree.right,tree.key, 1);
+ }
+ }
+
+ public void print() {
+ if (mRoot != null)
+ print(mRoot, mRoot.key, 0);
+ }
}
\ No newline at end of file
--
Gitee
From ac0b276b2a4950b1b566b0a7ec329c1c83ef7b89 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Wed, 25 Mar 2020 22:36:22 +0800
Subject: [PATCH 37/46] Executors
---
src/main/java/com/lin/demo/Executors.java | 4 ++++
1 file changed, 4 insertions(+)
create mode 100644 src/main/java/com/lin/demo/Executors.java
diff --git a/src/main/java/com/lin/demo/Executors.java b/src/main/java/com/lin/demo/Executors.java
new file mode 100644
index 0000000..21dc918
--- /dev/null
+++ b/src/main/java/com/lin/demo/Executors.java
@@ -0,0 +1,4 @@
+package com.lin.demo;
+
+public class Executors {
+}
--
Gitee
From 199b8779100ba6aa7c52dfc3e8f12c80912594ab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Wed, 25 Mar 2020 23:03:01 +0800
Subject: [PATCH 38/46] =?UTF-8?q?=E7=BA=A2=E9=BB=91=E6=A0=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../nome/{RBTree.java => rbtree/RBTree1.java} | 6 +-
.../com/lin/tree/nome/rbtree/RBTree2.java | 440 ++++++++++++++++++
2 files changed, 443 insertions(+), 3 deletions(-)
rename src/main/java/com/lin/tree/nome/{RBTree.java => rbtree/RBTree1.java} (99%)
create mode 100644 src/main/java/com/lin/tree/nome/rbtree/RBTree2.java
diff --git a/src/main/java/com/lin/tree/nome/RBTree.java b/src/main/java/com/lin/tree/nome/rbtree/RBTree1.java
similarity index 99%
rename from src/main/java/com/lin/tree/nome/RBTree.java
rename to src/main/java/com/lin/tree/nome/rbtree/RBTree1.java
index 727a689..0905b71 100644
--- a/src/main/java/com/lin/tree/nome/RBTree.java
+++ b/src/main/java/com/lin/tree/nome/rbtree/RBTree1.java
@@ -1,10 +1,10 @@
-package com.lin.tree.nome;
+package com.lin.tree.nome.rbtree;
/**
* 红黑树
* @param
*/
-public class RBTree> {
+public class RBTree1> {
private RBTNode mRoot; // 根结点
@@ -35,7 +35,7 @@ public class RBTree> {
}
}
- public RBTree() {
+ public RBTree1() {
mRoot=null;
}
diff --git a/src/main/java/com/lin/tree/nome/rbtree/RBTree2.java b/src/main/java/com/lin/tree/nome/rbtree/RBTree2.java
new file mode 100644
index 0000000..2d90c90
--- /dev/null
+++ b/src/main/java/com/lin/tree/nome/rbtree/RBTree2.java
@@ -0,0 +1,440 @@
+package com.lin.tree.nome.rbtree;
+
+class NodeColor {
+ public static String Red = "red";
+ public static String Black = "black";
+}
+
+
+class RedBlackTreeNode {
+ private String color;
+ private int key;
+ private RedBlackTreeNode left;
+ private RedBlackTreeNode right;
+ private RedBlackTreeNode parent;
+
+ //构造Nil结点
+ public RedBlackTreeNode() {
+ this.color = NodeColor.Black;
+ this.key = 0;
+ this.left = null;
+ this.right = null;
+ this.parent = null;
+ }
+
+ //构造结点
+ public RedBlackTreeNode(String color, int key, RedBlackTreeNode left, RedBlackTreeNode right, RedBlackTreeNode parent) {
+ this.color = color;
+ this.key = key;
+ this.left = left;
+ this.right = right;
+ this.parent = parent;
+ }
+
+ //获取颜色
+ public String getColor() {
+ return color;
+ }
+
+ //设置颜色
+ public void setColor(String color) {
+ this.color = color;
+ }
+
+ //获取值
+ public int getKey() {
+ return key;
+ }
+
+ //设置值
+ public void setKey(int key) {
+ this.key = key;
+ }
+
+ //获取左子树
+ public RedBlackTreeNode getLeft() {
+ return left;
+ }
+
+ //设置左子树
+ public void setLeft(RedBlackTreeNode left) {
+ this.left = left;
+ }
+
+ //获取右子树
+ public RedBlackTreeNode getRight() {
+ return right;
+ }
+
+ //设置右子树
+ public void setRight(RedBlackTreeNode right) {
+ this.right = right;
+ }
+
+ //获取父结点
+ public RedBlackTreeNode getParent() {
+ return parent;
+ }
+
+ //设置父结点
+ public void setParent(RedBlackTreeNode parent) {
+ this.parent = parent;
+ }
+}
+
+
+public class RBTree2 {
+ private static RedBlackTreeNode nil = new RedBlackTreeNode();
+ private RedBlackTreeNode root = new RedBlackTreeNode();
+
+ //构造空红黑树
+ public RBTree2() {
+ root = nil;
+ }
+
+ //创建结点
+ public RedBlackTreeNode RB_NODE(int key) {
+ RedBlackTreeNode node = new RedBlackTreeNode(NodeColor.Red, key, nil, nil, nil);
+ return node;
+ }
+
+ //判断是否为Nil结点
+ public boolean IsNil(RedBlackTreeNode node) {
+ if (node == nil) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ //获取根结点
+ public RedBlackTreeNode getRoot() {
+ return root;
+ }
+
+ //设置根结点
+ public void setRoot(RedBlackTreeNode root) {
+ this.root = root;
+ }
+
+ //插入结点
+ public void RB_INSERT(RBTree2 T, RedBlackTreeNode z) {
+ //临时变量结点y,存储临时结点,默认为Nil
+ RedBlackTreeNode y = T.nil;
+ //x初试为根结点
+ RedBlackTreeNode x = T.getRoot();
+ //循环二查找合适的插入点
+ while (IsNil(x) == false) {
+ //保存当前结点,作为结果的根结点
+ y = x;
+ if (z.getKey() < x.getKey()) {
+ //查找左子树
+ x = x.getLeft();
+ } else {
+ //查找右子树
+ x = x.getRight();
+ }
+ }
+ //临时结点y设置为插入点的父结点
+ z.setParent(y);
+
+ if (IsNil(y) == true) {
+ //空树时设置z为根结点
+ T.setRoot(z);
+ } else if (z.getKey() < y.getKey()) {
+ //插入为左子树
+ y.setLeft(z);
+ } else {
+ //插入为右子树
+ y.setRight(z);
+ }
+ //将插入结点的左右子树设为Nil,颜色为红色,已经在构造时设置过,可以省略
+ z.setLeft(T.nil);
+ z.setRight(T.nil);
+ z.setColor(NodeColor.Red);
+ //插入调整
+ RB_INSERT_FIXUP(T, z);
+ }
+
+ //插入调整
+ public void RB_INSERT_FIXUP(RBTree2 T, RedBlackTreeNode z) {
+ //当z的父结点为红色时,插入结点和父结点同为红色,需要调整
+ while (z.getParent().getColor() == NodeColor.Red) {
+ //插入结点的父结点是祖父节点的左子树
+ if (z.getParent() == z.getParent().getParent().getLeft()) {
+ //y定义为叔叔结点
+ RedBlackTreeNode y = z.getParent().getParent().getRight();
+ //case1:如果叔叔结点为红色,上层父结点和叔叔结点设为黑色,祖父节点设为红色
+ if (y.getColor() == NodeColor.Red) {
+ z.getParent().setColor(NodeColor.Black);
+ y.setColor(NodeColor.Black);
+ z.getParent().getParent().setColor(NodeColor.Red);
+ //祖父结点设为z
+ z = z.getParent().getParent();
+ }
+ //case2:插入结点为父结点的右子树,(叔叔结点一定为黑色),父结点设为z,对z左旋
+ else if (z == z.getParent().getRight()) {
+ //对父结点左旋
+ z = z.getParent();
+ LEFT_ROTATE(T, z);
+ }
+ //case3:插入结点为父结点的左子树,(叔叔结点一定为黑色),父结点设为黑色,祖父结点设为红色,对祖父结点右旋
+ z.getParent().setColor(NodeColor.Black);
+ z.getParent().getParent().setColor(NodeColor.Red);
+ RIGHT_ROTATE(T, z.getParent().getParent());
+ }
+ //插入结点的父结点是祖父节点的右子树,同理,方向交换
+ else {
+ RedBlackTreeNode y = z.getParent().getParent().getLeft();
+ if (y.getColor() == NodeColor.Red) {
+ z.getParent().setColor(NodeColor.Black);
+ y.setColor(NodeColor.Black);
+ z.getParent().getParent().setColor(NodeColor.Red);
+ z = z.getParent().getParent();
+ } else if (z == z.getParent().getLeft()) {
+ z = z.getParent();
+ RIGHT_ROTATE(T, z);
+ }
+ z.getParent().setColor(NodeColor.Black);
+ z.getParent().getParent().setColor(NodeColor.Red);
+ LEFT_ROTATE(T, z.getParent().getParent());
+ }
+ }
+ T.getRoot().setColor(NodeColor.Black);
+ }
+
+ //删除结点子函数,把根结点为u的子树替换为根结点为v的子树
+ public void RB_TRANSPLANT(RBTree2 T, RedBlackTreeNode u, RedBlackTreeNode v) {
+ //u为根结点
+ if (IsNil(u.getParent())) {
+ T.root = v;
+ }
+ //u为左子树
+ else if (u == u.getParent().getLeft()) {
+ u.getParent().setLeft(v);
+ }
+ //u为右子树
+ else {
+ u.getParent().setRight(v);
+ }
+ //父结点交换
+ v.setParent(u.getParent());
+ }
+
+ //查找后继
+ public RedBlackTreeNode TREE_MINIMUM(RedBlackTreeNode x) {
+ //不断查找左子树
+ while (IsNil(x.getLeft()) == false) {
+ x = x.getLeft();
+ }
+ return x;
+ }
+
+ //删除结点
+ public void RB_DELETE(RBTree2 T, RedBlackTreeNode z) {
+ //临时结点y保存即将删除的结点z信息
+ RedBlackTreeNode y = z;
+ //在结点删除或者移动前必须保存结点的颜色
+ String yOriginColor = y.getColor();
+ //临时结点x,用于记录y的位置
+ RedBlackTreeNode x = null;
+ //case1:z无左子树,直接将右子树置于z位置
+ if (IsNil(z.getLeft()) == true) {
+ x = z.getRight();
+ RB_TRANSPLANT(T, z, z.getRight());
+ }
+ //case2:z无右子树,直接将左子树置于z位置
+ else if (IsNil(z.getRight()) == true) {
+ x = z.getLeft();
+ RB_TRANSPLANT(T, z, z.getLeft());
+ }
+ //case3:z有左右子树
+ else {
+ //找到右子树中最小的结点,即z的后继
+ y = TREE_MINIMUM(z.getRight());
+ //删除的实际是y的位置的结点,要记录y的颜色
+ yOriginColor = y.getColor();
+ //y可能有右孩子,一定无左孩子,保存右孩子
+ x = y.getRight();
+ //若y为z的右孩子,直接相连
+ if (y.getParent() == z) {
+ x.setParent(y);
+ }
+ //若不相连
+ else {
+ RB_TRANSPLANT(T, y, y.getRight());
+ y.setRight(z.getRight());
+ y.getRight().setParent(y);
+ }
+ RB_TRANSPLANT(T, z, y);
+ y.setLeft(z.getLeft());
+ y.getLeft().setParent(y);
+ y.setColor(z.getColor());
+ }
+ //删除结点为黑色时需要调整
+ if (yOriginColor == NodeColor.Black) {
+ RB_DELETE_FIXUP(T, x);
+ }
+ }
+
+ //删除调整
+ public void RB_DELETE_FIXUP(RBTree2 T, RedBlackTreeNode x) {
+ //临时结点
+ RedBlackTreeNode w = null;
+ //非根结点且为黑色
+ while (x != T.getRoot() && x.getColor() == NodeColor.Black) {
+ //x为父结点左孩子
+ if (x == x.getParent().getLeft()) {
+ //w为兄弟结点
+ w = x.getParent().getRight();
+ //case1:w兄弟结点为红色
+ if (w.getColor() == NodeColor.Red) {
+ //w设为黑色
+ w.setColor(NodeColor.Black);
+ //被删结点的父结点设为黑色
+ x.getParent().setColor(NodeColor.Red);
+ //对x的父结点左旋
+ LEFT_ROTATE(T, x.getParent());
+ //更新x的兄弟结点
+ w = x.getParent().getRight();
+ }
+ //case2:w兄弟结点和两个孩子结点都为黑
+ if (w.getLeft().getColor() == NodeColor.Black && w.getRight().getColor() == NodeColor.Black) {
+ //w设为黑色
+ w.setColor(NodeColor.Red);
+ //重设x为x的父结点
+ x = x.getParent();
+ }
+ //case3:w兄弟结点为黑,w的左孩子为红,右孩子为黑
+ else if (w.getRight().getColor() == NodeColor.Black) {
+ //w的左孩子设为黑
+ w.getLeft().setColor(NodeColor.Black);
+ //w设为红
+ w.setColor(NodeColor.Red);
+ //右旋
+ RIGHT_ROTATE(T, w);
+ //更新w
+ w = x.getParent().getRight();
+ }
+ //case4:w兄弟结点为黑,w的右孩子为红
+ w.setColor(x.getParent().getColor());
+ x.getParent().setColor(NodeColor.Black);
+ w.getRight().setColor(NodeColor.Black);
+ LEFT_ROTATE(T, x.getParent());
+ x = T.getRoot();
+ }
+ //x为父结点右孩子
+ else {
+ w = x.getParent().getLeft();
+ if (w.getColor() == NodeColor.Red) {
+ w.setColor(NodeColor.Black);
+ x.getParent().setColor(NodeColor.Red);
+ RIGHT_ROTATE(T, x.getParent());
+ w = x.getParent().getLeft();
+ }
+ if (w.getRight().getColor() == NodeColor.Black && w.getLeft().getColor() == NodeColor.Black) {
+ w.setColor(NodeColor.Red);
+ x = x.getParent();
+ } else if (w.getLeft().getColor() == NodeColor.Black) {
+ w.getRight().setColor(NodeColor.Black);
+ w.setColor(NodeColor.Red);
+ LEFT_ROTATE(T, w);
+ w = x.getParent().getLeft();
+ }
+ w.setColor(x.getParent().getColor());
+ x.getParent().setColor(NodeColor.Black);
+ w.getLeft().setColor(NodeColor.Black);
+ RIGHT_ROTATE(T, x.getParent());
+ x = T.getRoot();
+ }
+ }
+ x.setColor(NodeColor.Black);
+ }
+
+ //左旋
+ public void LEFT_ROTATE(RBTree2 T, RedBlackTreeNode x) {
+ //左旋结点右子树不能为空
+ if (IsNil(x.getRight()) == true)
+ return;
+ //定义y结点
+ RedBlackTreeNode y = x.getRight();
+ //y左子树 -> x右子树
+ x.setRight(y.getLeft());
+ //x -> y左子树父结点
+ y.getLeft().setParent(x);
+ //x父结点 -> y父结点
+ y.setParent(x.getParent());
+ //y -> x父结点左/右子树或根结点
+ if (IsNil(x.getParent()) == true) {
+ //x为根结点,y设为根结点
+ T.setRoot(y);
+ } else if (x.getParent().getLeft() == x) {
+ //x为左子树,y设为左子树
+ x.getParent().setLeft(y);
+ } else {
+ //x为右子树,y设为右子树
+ x.getParent().setRight(y);
+ }
+ //x -> y左子树
+ y.setLeft(x);
+ //y -> x父结点
+ x.setParent(y);
+ }
+
+ //右旋
+ public void RIGHT_ROTATE(RBTree2 T, RedBlackTreeNode x) {
+ //右旋结点父结点不能为空
+ if (IsNil(x.getParent()) == true)
+ return;
+ //定义y结点
+ RedBlackTreeNode y = x.getParent();
+ //x右子树 -> y左子树
+ y.setLeft(x.getRight());
+ //y -> x右子树父结点
+ x.getRight().setParent(y);
+ //y父结点 -> x父结点
+ x.setParent(y.getParent());
+ //x -> y父结点左/右子树或根结点
+ if (IsNil(y.getParent()) == true) {
+ //y为根结点,x设为根结点
+ T.setRoot(x);
+ } else if (y.getParent().getLeft() == y) {
+ //y为左子树,x设为左子树
+ y.getParent().setLeft(x);
+ } else {
+ //y为右子树,x设为右子树
+ y.getParent().setRight(x);
+ }
+ //y -> x右子树
+ x.setRight(y);
+ //x -> y父结点
+ y.setParent(x);
+ }
+
+ //前序遍历
+ public void preorder(RedBlackTreeNode t) {
+ if (IsNil(t) == false) {
+ System.out.println(t.getKey());
+ preorder(t.getLeft());
+ preorder(t.getRight());
+ }
+ }
+
+ //中序遍历
+ public void midorder(RedBlackTreeNode t) {
+ if (IsNil(t) == false) {
+ midorder(t.getLeft());
+ System.out.println(t.getKey());
+ midorder(t.getRight());
+ }
+ }
+
+ //后序遍历
+ public void postorder(RedBlackTreeNode t) {
+ if (IsNil(t) == false) {
+ postorder(t.getLeft());
+ postorder(t.getRight());
+ System.out.println(t.getKey());
+ }
+ }
+}
--
Gitee
From 2b7706af96cd16e9bf6dd9c2a0b7f0a72633bbf8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Wed, 25 Mar 2020 23:08:37 +0800
Subject: [PATCH 39/46] =?UTF-8?q?=E9=9D=99=E6=80=81=E5=86=85=E9=83=A8?=
=?UTF-8?q?=E7=B1=BB=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/lin/tree/nome/rbtree/RBTree2.java | 136 +++++++++---------
.../java/com/lin/TestTree/RBTree2Test.java | 22 +++
2 files changed, 90 insertions(+), 68 deletions(-)
create mode 100644 src/test/java/com/lin/TestTree/RBTree2Test.java
diff --git a/src/main/java/com/lin/tree/nome/rbtree/RBTree2.java b/src/main/java/com/lin/tree/nome/rbtree/RBTree2.java
index 2d90c90..8c8d765 100644
--- a/src/main/java/com/lin/tree/nome/rbtree/RBTree2.java
+++ b/src/main/java/com/lin/tree/nome/rbtree/RBTree2.java
@@ -1,89 +1,89 @@
package com.lin.tree.nome.rbtree;
-class NodeColor {
- public static String Red = "red";
- public static String Black = "black";
-}
+public class RBTree2 {
-class RedBlackTreeNode {
- private String color;
- private int key;
- private RedBlackTreeNode left;
- private RedBlackTreeNode right;
- private RedBlackTreeNode parent;
-
- //构造Nil结点
- public RedBlackTreeNode() {
- this.color = NodeColor.Black;
- this.key = 0;
- this.left = null;
- this.right = null;
- this.parent = null;
+ public static class NodeColor {
+ public static String Red = "red";
+ public static String Black = "black";
}
- //构造结点
- public RedBlackTreeNode(String color, int key, RedBlackTreeNode left, RedBlackTreeNode right, RedBlackTreeNode parent) {
- this.color = color;
- this.key = key;
- this.left = left;
- this.right = right;
- this.parent = parent;
- }
+ public static class RedBlackTreeNode {
+ private String color;
+ private int key;
+ private RedBlackTreeNode left;
+ private RedBlackTreeNode right;
+ private RedBlackTreeNode parent;
+
+ //构造Nil结点
+ public RedBlackTreeNode() {
+ this.color = NodeColor.Black;
+ this.key = 0;
+ this.left = null;
+ this.right = null;
+ this.parent = null;
+ }
- //获取颜色
- public String getColor() {
- return color;
- }
+ //构造结点
+ public RedBlackTreeNode(String color, int key, RedBlackTreeNode left, RedBlackTreeNode right, RedBlackTreeNode parent) {
+ this.color = color;
+ this.key = key;
+ this.left = left;
+ this.right = right;
+ this.parent = parent;
+ }
- //设置颜色
- public void setColor(String color) {
- this.color = color;
- }
+ //获取颜色
+ public String getColor() {
+ return color;
+ }
- //获取值
- public int getKey() {
- return key;
- }
+ //设置颜色
+ public void setColor(String color) {
+ this.color = color;
+ }
- //设置值
- public void setKey(int key) {
- this.key = key;
- }
+ //获取值
+ public int getKey() {
+ return key;
+ }
- //获取左子树
- public RedBlackTreeNode getLeft() {
- return left;
- }
+ //设置值
+ public void setKey(int key) {
+ this.key = key;
+ }
- //设置左子树
- public void setLeft(RedBlackTreeNode left) {
- this.left = left;
- }
+ //获取左子树
+ public RedBlackTreeNode getLeft() {
+ return left;
+ }
- //获取右子树
- public RedBlackTreeNode getRight() {
- return right;
- }
+ //设置左子树
+ public void setLeft(RedBlackTreeNode left) {
+ this.left = left;
+ }
- //设置右子树
- public void setRight(RedBlackTreeNode right) {
- this.right = right;
- }
+ //获取右子树
+ public RedBlackTreeNode getRight() {
+ return right;
+ }
- //获取父结点
- public RedBlackTreeNode getParent() {
- return parent;
- }
+ //设置右子树
+ public void setRight(RedBlackTreeNode right) {
+ this.right = right;
+ }
- //设置父结点
- public void setParent(RedBlackTreeNode parent) {
- this.parent = parent;
- }
-}
+ //获取父结点
+ public RedBlackTreeNode getParent() {
+ return parent;
+ }
+ //设置父结点
+ public void setParent(RedBlackTreeNode parent) {
+ this.parent = parent;
+ }
+ }
-public class RBTree2 {
private static RedBlackTreeNode nil = new RedBlackTreeNode();
private RedBlackTreeNode root = new RedBlackTreeNode();
diff --git a/src/test/java/com/lin/TestTree/RBTree2Test.java b/src/test/java/com/lin/TestTree/RBTree2Test.java
new file mode 100644
index 0000000..ffbd302
--- /dev/null
+++ b/src/test/java/com/lin/TestTree/RBTree2Test.java
@@ -0,0 +1,22 @@
+package com.lin.TestTree;
+
+import com.lin.tree.nome.rbtree.RBTree2;
+
+
+public class RBTree2Test {
+
+
+ public static void main(String[] args) {
+ RBTree2 T = new RBTree2();
+ RBTree2.RedBlackTreeNode node1 = T.RB_NODE(10);
+ T.RB_INSERT(T, node1);
+ RBTree2.RedBlackTreeNode node2 = T.RB_NODE(20);
+ T.RB_INSERT(T, node2);
+ RBTree2.RedBlackTreeNode node3 = T.RB_NODE(30);
+ T.RB_INSERT(T, node3);
+ T.preorder(T.getRoot());
+ }
+
+
+
+}
--
Gitee
From ada86b30df580d67fec0721201f54c3fc659a08f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Thu, 26 Mar 2020 23:12:48 +0800
Subject: [PATCH 40/46] =?UTF-8?q?=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/test/java/com/lin/TestTree/AVLTree1Test.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/test/java/com/lin/TestTree/AVLTree1Test.java b/src/test/java/com/lin/TestTree/AVLTree1Test.java
index f1f71eb..88bbd03 100644
--- a/src/test/java/com/lin/TestTree/AVLTree1Test.java
+++ b/src/test/java/com/lin/TestTree/AVLTree1Test.java
@@ -36,4 +36,5 @@ public class AVLTree1Test {
// // 销毁二叉树
// tree.destroy();
}
+
}
--
Gitee
From 9e7564eceb868e1b9d62a5c3e70a65c7f059e21d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Fri, 27 Mar 2020 23:01:17 +0800
Subject: [PATCH 41/46] =?UTF-8?q?=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/test/java/com/lin/TestTree/AVLTree1Test.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/test/java/com/lin/TestTree/AVLTree1Test.java b/src/test/java/com/lin/TestTree/AVLTree1Test.java
index 88bbd03..e77a7cf 100644
--- a/src/test/java/com/lin/TestTree/AVLTree1Test.java
+++ b/src/test/java/com/lin/TestTree/AVLTree1Test.java
@@ -37,4 +37,5 @@ public class AVLTree1Test {
// tree.destroy();
}
+
}
--
Gitee
From b517dc691750c50eb9e47a2f665e5acaeb929776 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Fri, 12 Jun 2020 14:25:58 +0800
Subject: [PATCH 42/46] =?UTF-8?q?avl=E6=A0=91=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/lin/tree/nome/avltree/AVLTree1.java | 19 -------
.../java/com/lin/TestTree/AVLTree1Test.java | 51 +++++++------------
2 files changed, 19 insertions(+), 51 deletions(-)
diff --git a/src/main/java/com/lin/tree/nome/avltree/AVLTree1.java b/src/main/java/com/lin/tree/nome/avltree/AVLTree1.java
index 27d5698..4a13540 100644
--- a/src/main/java/com/lin/tree/nome/avltree/AVLTree1.java
+++ b/src/main/java/com/lin/tree/nome/avltree/AVLTree1.java
@@ -294,23 +294,4 @@ public class AVLTree1 {
return true;
}
-// public static void main(String[] args) {
-// AVLTree1 avl = new AVLTree1<>();
-// /*可自行添加插入,删除操作进行测试*/
-// avl.insert(3);
-// avl.insert(5);
-// avl.insert(6);
-// avl.insert(7);
-// avl.insert(8);
-// avl.insert(9);
-// avl.preorderTraverse();
-// System.out.println();
-// System.out.println(avl.size());
-//
-// avl.delete(7);
-// avl.delete(8);
-// avl.preorderTraverse();
-// System.out.println();
-// System.out.println(avl.size());
-// }
}
diff --git a/src/test/java/com/lin/TestTree/AVLTree1Test.java b/src/test/java/com/lin/TestTree/AVLTree1Test.java
index e77a7cf..09bd06c 100644
--- a/src/test/java/com/lin/TestTree/AVLTree1Test.java
+++ b/src/test/java/com/lin/TestTree/AVLTree1Test.java
@@ -1,40 +1,27 @@
package com.lin.TestTree;
-public class AVLTree1Test {
+import com.lin.tree.nome.avltree.AVLTree1;
- private static int arr[] = {3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9};
+public class AVLTree1Test {
public static void main(String[] args) {
-// int i;
-// AVLTree1 tree = new AVLTree1();
-//
-// System.out.printf("== 依次添加: ");
-// for (i = 0; i < arr.length; i++) {
-// System.out.printf("%d ", arr[i]);
-// tree.insert(arr[i]);
-// }
-// System.out.printf("\n== 前序遍历: ");
-// tree.preOrder();
-// System.out.printf("\n== 中序遍历: ");
-// tree.inOrder();
-// System.out.printf("\n== 后序遍历: ");
-// tree.postOrder();
-// System.out.printf("\n");
-// System.out.printf("== 高度: %d\n", tree.height());
-// System.out.printf("== 最小值: %d\n", tree.minimum());
-// System.out.printf("== 最大值: %d\n", tree.maximum());
-// System.out.printf("== 树的详细信息: \n");
-// tree.print();
-// i = 8;
-// System.out.printf("\n== 删除根节点: %d", i);
-// tree.remove(i);
-// System.out.printf("\n== 高度: %d", tree.height());
-// System.out.printf("\n== 中序遍历: ");
-// tree.inOrder();
-// System.out.printf("\n== 树的详细信息: \n");
-// tree.print();
-// // 销毁二叉树
-// tree.destroy();
+ AVLTree1 avl = new AVLTree1<>();
+ /*可自行添加插入,删除操作进行测试*/
+ avl.insert(3);
+ avl.insert(5);
+ avl.insert(6);
+ avl.insert(7);
+ avl.insert(8);
+ avl.insert(9);
+ avl.preorderTraverse();
+ System.out.println();
+ System.out.println(avl.size());
+
+ avl.delete(7);
+ avl.delete(8);
+ avl.preorderTraverse();
+ System.out.println();
+ System.out.println(avl.size());
}
--
Gitee
From 4eadc5a352da0b0bebbc2f3696474b2bf843867b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Fri, 12 Jun 2020 14:27:21 +0800
Subject: [PATCH 43/46] =?UTF-8?q?=E5=88=A0=E9=99=A4io=E5=8C=85?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/io/AIO.java | 4 -
src/main/java/com/lin/io/IO.java | 7 -
src/main/java/com/lin/io/NIO.java | 4 -
src/main/java/com/lin/io/nome/FileCopy.java | 251 --------------------
src/main/java/com/lin/io/nome/TestIO1.java | 88 -------
src/main/java/com/lin/io/nome/TestIO2.java | 107 ---------
6 files changed, 461 deletions(-)
delete mode 100644 src/main/java/com/lin/io/AIO.java
delete mode 100644 src/main/java/com/lin/io/IO.java
delete mode 100644 src/main/java/com/lin/io/NIO.java
delete mode 100644 src/main/java/com/lin/io/nome/FileCopy.java
delete mode 100644 src/main/java/com/lin/io/nome/TestIO1.java
delete mode 100644 src/main/java/com/lin/io/nome/TestIO2.java
diff --git a/src/main/java/com/lin/io/AIO.java b/src/main/java/com/lin/io/AIO.java
deleted file mode 100644
index d44a6f9..0000000
--- a/src/main/java/com/lin/io/AIO.java
+++ /dev/null
@@ -1,4 +0,0 @@
-package com.lin.io;
-
-public class AIO {
-}
diff --git a/src/main/java/com/lin/io/IO.java b/src/main/java/com/lin/io/IO.java
deleted file mode 100644
index b61ba50..0000000
--- a/src/main/java/com/lin/io/IO.java
+++ /dev/null
@@ -1,7 +0,0 @@
-package com.lin.io;
-
-/**
- * 普通的IO流
- */
-public class IO {
-}
diff --git a/src/main/java/com/lin/io/NIO.java b/src/main/java/com/lin/io/NIO.java
deleted file mode 100644
index a9de064..0000000
--- a/src/main/java/com/lin/io/NIO.java
+++ /dev/null
@@ -1,4 +0,0 @@
-package com.lin.io;
-
-public class NIO {
-}
diff --git a/src/main/java/com/lin/io/nome/FileCopy.java b/src/main/java/com/lin/io/nome/FileCopy.java
deleted file mode 100644
index 5ed9831..0000000
--- a/src/main/java/com/lin/io/nome/FileCopy.java
+++ /dev/null
@@ -1,251 +0,0 @@
-package com.lin.io.nome;
-
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-
-public class FileCopy {
-
- /*如果操作文件是纯文本则使用字符流,如果不是纯文本则使用字节流*/
- public static void main(String[] args) {
- //向文件中写入数据(会把原来的数据覆盖掉)
- //writeFile();
- //按照单个字符读取
- //readByCharacter();
- //按照字符组读取
- //readByCharacterArray();
- //对已存在的文件进行续写(不会覆盖原来的数据但是,只能写一次)
- //writeFileContinue();
- //将F盘的文件拷贝到D盘
- //copyFileFromFtoD();
- //字符缓冲流的读取
- /*缓冲区的出现时为了提高流的操作效率而出现的.
- 需要被提高效率的流作为参数传递给缓冲区的构造函数
- 在缓冲区中封装了一个数组,存入数据后一次取出*/
- /*读取的内容是:
- 窗前明月光,疑是地上霜。
- 举头望明月,低头思故乡。
- --李白*/
- //bufferedReader();
- //字符缓冲流的写
- //bufferedWriter();
- //媒体流的时候就会用到字节流
- //将F盘的图片拷贝到D盘
- //copyPictureFromDtoF();
- //将F盘的音乐复制到D盘
- //copyMP3FromFtoD();
- }
-
- private static void copyMP3FromFtoD() {
- FileInputStream fi = null;
- FileOutputStream fo = null;
- try {
- fi = new FileInputStream("F:\\aa\\guoge.mp3");
- fo = new FileOutputStream("D:/guoge_copy.mp3");
- byte[] buf = new byte[1024];
- int n = 0;
- while ((n = (fi.read(buf))) != -1) {
- fo.write(buf, 0, n);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- fo.close();
- fi.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private static void copyPictureFromDtoF() {
- FileInputStream fi = null;
- FileOutputStream fo = null;
- try {
- fi = new FileInputStream("F:\\aa\\004.png");
- fo = new FileOutputStream("D:\\004_copy.png");
- byte[] buf = new byte[1024];
- int n = 0;
- while ((n = fi.read(buf)) != -1) {
- fo.write(buf, 0, n);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- fo.close();
- fi.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private static void bufferedWriter() {
- FileWriter file = null;
- BufferedWriter bw = null;
- try {
- file = new FileWriter("F:\\aa\\bb.txt", true);
- bw = new BufferedWriter(file);
- //跨平台的换行符
- bw.newLine();
- bw.write("天行健,君子以自强不息;");
- bw.newLine();
- bw.write("地势坤,君子以厚德载物。");
- bw.newLine();
- //缓冲区的写必须有刷新
- bw.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- bw.close();
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private static void bufferedReader() {
- FileReader file = null;
- try {
- file = new FileReader("F:\\aa\\bb.txt");
- BufferedReader br = new BufferedReader(file);
- while (true) {
- String s;
- try {
- s = br.readLine();
- if (s == null) break;
- System.out.println(s);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private static void copyFileFromFtoD() {
- FileWriter fw = null;
- FileReader fr = null;
- try {
- fw = new FileWriter("D:\\test20180224.txt", true);
- fr = new FileReader("F:\\aa\\test.txt");
- char[] buf = new char[11];
- int n = 0;
- while ((n = fr.read(buf)) != -1) {
- fw.write(new String(buf, 0, n));
- System.out.println(new String(buf, 0, n));
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- fw.close();
- fr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private static void writeFileContinue() {
- FileWriter file = null;
- try {
- file = new FileWriter("F:\\aa\\test.txt", true);
- file.write("(这是续写内容)");
- file.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private static void readByCharacter() {
- FileReader file = null;
- try {
- //创建FileReader并指定要读取的文件
- file = new FileReader("F:\\aa\\test.txt");
- int n = 0;
- while ((n = file.read()) != -1) {
- System.out.println((char) n);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private static void readByCharacterArray() {
- FileReader file = null;
- try {
- //创建FileReader并指定要读取的文件
- file = new FileReader("F:\\aa\\test.txt");
- char[] buf = new char[11];
- int n = 0;
- while ((n = file.read(buf)) != -1) {
- System.out.println(new String(buf, 0, n));
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private static void writeFile() {
- //创建一个FileWriter对象,该对象初始化的时候就要指定被操作的文件 该文件不存在就会新建一个文件
- FileWriter file = null;
- try {
- file = new FileWriter("F:\\aa\\test.txt");
- file.write("HelloWorld!");
- //刷新缓存数据将数据写入文件
- file.flush();
- file.write("你好世界!");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-}
diff --git a/src/main/java/com/lin/io/nome/TestIO1.java b/src/main/java/com/lin/io/nome/TestIO1.java
deleted file mode 100644
index fd681aa..0000000
--- a/src/main/java/com/lin/io/nome/TestIO1.java
+++ /dev/null
@@ -1,88 +0,0 @@
-package com.lin.io.nome;
-
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-
-public class TestIO1 {
- public static void FileInputStreamTest() throws IOException {
- FileInputStream fis = new FileInputStream("tmp2.txt");
- byte[] buf = new byte[1024];
- int hasRead = 0;
-
- //read()返回的是单个字节数据(字节数据可以直接专程int类型),但是read(buf)返回的是读取到的字节数,真正的数据保存在buf中
- while ((hasRead = fis.read(buf)) > 0) {
- //每次最多将1024个字节转换成字符串,这里tmp2.txt中的字符小于1024,所以一次就读完了
- //循环次数 = 文件字符数 除以 buf长度
- System.out.println(new String(buf, 0, hasRead));
- /*
- * 将字节强制转换成字符后逐个输出,能实现和上面一样的效果。但是如果源文件是中文的话可能会乱码
-
- for (byte b : buf) {
- char ch = (char)b;
- if (ch != '\r')
- System.out.print(ch);
- }
- */
- }
- //在finally块里close更安全
- fis.close();
- }
-
- public static void FileReaderTest() throws IOException {
-
- try (
- // 在try() 中打开的文件, JVM会自动关闭
- FileReader fr = new FileReader("tmp2.txt")) {
- char[] buf = new char[32];
- int hasRead = 0;
- // 每个char都占两个字节,每个字符或者汉字都是占2个字节,因此无论buf长度为多少,总是能读取中文字符长度的整数倍,不会乱码
- while ((hasRead = fr.read(buf)) > 0) {
- // 如果buf的长度大于文件每行的长度,就可以完整输出每行,否则会断行。
- // 循环次数 = 文件字符数 除以 buf长度
- System.out.println(new String(buf, 0, hasRead));
- // 跟上面效果一样
- // System.out.println(buf);
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
-
- public static void FileOutputStreamTest() throws FileNotFoundException, IOException {
- try (
- //在try()中打开文件会在结尾自动关闭
- FileInputStream fis = new FileInputStream("tmp2.txt");
- FileOutputStream fos = new FileOutputStream("tmp3.txt");
- ) {
- byte[] buf = new byte[4];
- int hasRead = 0;
- while ((hasRead = fis.read(buf)) > 0) {
- //每读取一次就写一次,读多少就写多少
- fos.write(buf, 0, hasRead);
- }
- System.out.println("write success");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static void FileWriterTest() throws IOException {
- try (FileWriter fw = new FileWriter("tmp4.txt")) {
- fw.write("天王盖地虎\r\n");
- fw.write("宝塔镇河妖\r\n");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) throws IOException {
- //FileInputStreamTest();
- //FileReaderTest();
- //FileOutputStreamTest();
- FileWriterTest();
- }
-}
\ No newline at end of file
diff --git a/src/main/java/com/lin/io/nome/TestIO2.java b/src/main/java/com/lin/io/nome/TestIO2.java
deleted file mode 100644
index 9a6ac93..0000000
--- a/src/main/java/com/lin/io/nome/TestIO2.java
+++ /dev/null
@@ -1,107 +0,0 @@
-package com.lin.io.nome;
-
-import java.io.BufferedReader;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PrintStream;
-import java.io.PushbackReader;
-import java.io.StringReader;
-import java.io.StringWriter;
-
-public class TestIO2 {
- public static void printStream() throws FileNotFoundException, IOException {
- try (
- FileOutputStream fos = new FileOutputStream("tmp.txt");
- PrintStream ps = new PrintStream(fos)) {
- ps.println("普通字符串\n");
- //输出对象
- ps.println(new TestIO2());
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("输出完成");
-
- }
-
- public static void stringNode() throws IOException {
- String str = "天王盖地虎\n"
- + "宝塔镇河妖\n";
- char[] buf = new char[32];
- int hasRead = 0;
- //StringReader将以String字符串为节点读取数据
- try (StringReader sr = new StringReader(str)) {
- while ((hasRead = sr.read(buf)) > 0) {
- System.out.print(new String(buf, 0, hasRead));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- //由于String是一个不可变类,因此创建StringWriter时,实际上是以一个StringBuffer作为输出节点
- try (StringWriter sw = new StringWriter()) {
- sw.write("黑夜给了我黑色的眼睛\n");
- sw.write("我却用它寻找光明\n");
- //toString()返回sw节点内的数据
- System.out.println(sw.toString());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static void keyIn() throws IOException {
- try (
- //InputStreamReader是从byte转成char的桥梁
- InputStreamReader reader = new InputStreamReader(System.in);
- //BufferedReader(Reader in)是char类型输入的包装类
- BufferedReader br = new BufferedReader(reader);
- ) {
- String line = null;
- while ((line = br.readLine()) != null) {
- if (line.equals("exit")) {
- //System.exit(1);
- break;
- }
- System.out.println(line);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static void pushback() throws FileNotFoundException, IOException {
- try (PushbackReader pr = new PushbackReader(new FileReader("C:/PROJECT/JavaBasic/PROJECT_JavaBasic/src/io/TestIO.java"), 64)) {
- char[] buf = new char[32];
- String lastContent = "";
- int hasRead = 0;
- while ((hasRead = pr.read(buf)) > 0) {
- String content = new String(buf, 0, hasRead);
- int targetIndex = 0;
- if ((targetIndex = (lastContent + content).indexOf("targetIndex = (lastContent + content)")) > 0) {
- pr.unread((lastContent + content).toCharArray());
- if (targetIndex > 32) {
- buf = new char[targetIndex];
- }
- pr.read(buf, 0, targetIndex);
- System.out.println(new String(buf, 0, targetIndex));
- System.exit(0);
- } else {
- System.out.println(lastContent);
- lastContent = content;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) throws IOException {
- printStream();
- //stringNode();
- //keyIn();
- //pushback();
- }
-}
\ No newline at end of file
--
Gitee
From ed032e1000775a9d6715d0c201c3e1235242192f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Fri, 12 Jun 2020 14:39:28 +0800
Subject: [PATCH 44/46] =?UTF-8?q?=E6=B7=BB=E5=8A=A03.0=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E6=96=87=E6=A1=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/test/java/com/lin/TestSort/v3.0.md | 9 +++++++++
1 file changed, 9 insertions(+)
create mode 100644 src/test/java/com/lin/TestSort/v3.0.md
diff --git a/src/test/java/com/lin/TestSort/v3.0.md b/src/test/java/com/lin/TestSort/v3.0.md
new file mode 100644
index 0000000..4305f78
--- /dev/null
+++ b/src/test/java/com/lin/TestSort/v3.0.md
@@ -0,0 +1,9 @@
+# V3.0
+
+1. 冒泡排序工具
+2. 选择排序
+3. 插入排序
+4. 选择排序
+5. 归并排序
+6. 希尔排序
+7. 堆排序
\ No newline at end of file
--
Gitee
From d11493e8bf440025018e02049e5fabe5e8f384fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Fri, 12 Jun 2020 14:43:17 +0800
Subject: [PATCH 45/46] =?UTF-8?q?=E6=B7=BB=E5=8A=A04.0=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E6=96=87=E6=A1=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/test/java/com/lin/TestTree/v4.0.md | 8 ++++++++
1 file changed, 8 insertions(+)
create mode 100644 src/test/java/com/lin/TestTree/v4.0.md
diff --git a/src/test/java/com/lin/TestTree/v4.0.md b/src/test/java/com/lin/TestTree/v4.0.md
new file mode 100644
index 0000000..f3f3f47
--- /dev/null
+++ b/src/test/java/com/lin/TestTree/v4.0.md
@@ -0,0 +1,8 @@
+# V4.0
+
+1. AVL树实现算法1
+2. AVL树实现算法2
+3. 红黑树实现算法1
+4. 红黑树实现算法2
+5. 二叉平衡树实现算法
+6. 普通二叉树6
\ No newline at end of file
--
Gitee
From 9b5b145e67c566a1f37952ee34ee134aed35bb77 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B9=9D=E5=88=86=E7=9F=B3=E4=BA=BA?= <1915803787@qq.com>
Date: Fri, 12 Jun 2020 14:45:44 +0800
Subject: [PATCH 46/46] =?UTF-8?q?=E6=B7=BB=E5=8A=A04.0=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E6=96=87=E6=A1=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/lin/demo/Executors.java | 4 -
src/main/java/com/lin/demo/JUC.java | 9 --
src/main/java/com/lin/demo/ThreadPool.java | 147 ---------------------
src/test/java/com/lin/Other/one.java | 78 -----------
4 files changed, 238 deletions(-)
delete mode 100644 src/main/java/com/lin/demo/Executors.java
delete mode 100644 src/main/java/com/lin/demo/JUC.java
delete mode 100644 src/main/java/com/lin/demo/ThreadPool.java
delete mode 100644 src/test/java/com/lin/Other/one.java
diff --git a/src/main/java/com/lin/demo/Executors.java b/src/main/java/com/lin/demo/Executors.java
deleted file mode 100644
index 21dc918..0000000
--- a/src/main/java/com/lin/demo/Executors.java
+++ /dev/null
@@ -1,4 +0,0 @@
-package com.lin.demo;
-
-public class Executors {
-}
diff --git a/src/main/java/com/lin/demo/JUC.java b/src/main/java/com/lin/demo/JUC.java
deleted file mode 100644
index 0dd343c..0000000
--- a/src/main/java/com/lin/demo/JUC.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package com.lin.demo;
-
-public class JUC {
-
-
-
-
-
-}
diff --git a/src/main/java/com/lin/demo/ThreadPool.java b/src/main/java/com/lin/demo/ThreadPool.java
deleted file mode 100644
index 48abc41..0000000
--- a/src/main/java/com/lin/demo/ThreadPool.java
+++ /dev/null
@@ -1,147 +0,0 @@
-package com.lin.demo;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
-
-/**
- * 线程池
- */
-public class ThreadPool {
-
- /**
- * 缓存线程池
- */
- public void newCachedThreadPool() {
- ExecutorService executorService = Executors.newCachedThreadPool();
-
- for (int i = 0; i < 5; i++) {
- final int index = i;
- executorService.execute(new Runnable() {
- public void run() {
- try {
- SimpleDateFormat sdf = new SimpleDateFormat(
- "HH:mm:ss");
- System.out.println("运行时间: " +
- sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- }
- executorService.shutdown();
- }
-
- /**
- * 单线程线程池
- */
- public void newSingleThreadExecutor() {
- ExecutorService executorService = Executors.newSingleThreadExecutor();
-
- for (int i = 0; i < 5; i++) {
- final int index = i;
- executorService.execute(new Runnable() {
- public void run() {
- try {
- SimpleDateFormat sdf = new SimpleDateFormat(
- "HH:mm:ss");
- System.out.println("运行时间: " +
- sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- }
- executorService.shutdown();
- }
-
- /**
- * 定长线程池
- */
- public void newFixedThreadPool() {
- ExecutorService executorService = Executors.newFixedThreadPool(3);
-
- for (int i = 0; i < 5; i++) {
- final int index = i;
- executorService.execute(new Runnable() {
- public void run() {
- try {
- SimpleDateFormat sdf = new SimpleDateFormat(
- "HH:mm:ss");
- System.out.println("运行时间: " +
- sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
-
- /**
- * 定时执行的线程池
- */
- public void newScheduledThreadPool() {
- ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
- SimpleDateFormat sdf = new SimpleDateFormat(
- "HH:mm:ss");
- for (int i = 0; i < 5; i++) {
- final int index = i;
- System.out.println("提交时间: " + sdf.format(new Date()));
- executorService.schedule(new Runnable() {
- public void run() {
- try {
-
- System.out.println("运行时间: " +
- sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }, 3, TimeUnit.SECONDS);
-
- }
- }
-
- /**
- * 周期执行的线程池
- *
- * @throws InterruptedException
- */
- public void newScheduledThreadPool2() throws InterruptedException {
- ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
- SimpleDateFormat sdf = new SimpleDateFormat(
- "HH:mm:ss");
-// for (int i = 0; i < 5; i++) {
- final int index = 1;
- System.out.println("提交时间: " + sdf.format(new Date()));
- executorService.scheduleAtFixedRate(new Runnable() {
- public void run() {
- try {
-
- System.out.println("运行时间: " +
- sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }, 1, 3, TimeUnit.SECONDS);
- //主线程等待10秒钟后关闭
- Thread.sleep(10000);
-// }
-
- executorService.shutdown();
- }
-
-
-}
diff --git a/src/test/java/com/lin/Other/one.java b/src/test/java/com/lin/Other/one.java
deleted file mode 100644
index 9a425c5..0000000
--- a/src/test/java/com/lin/Other/one.java
+++ /dev/null
@@ -1,78 +0,0 @@
-package com.lin.Other;
-
-
-import org.apache.commons.codec.binary.StringUtils;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Hello Utils!
- */
-public class one {
-
-
- public static void main(String[] args) throws InterruptedException {
- Map integerObjectMap = pageNumExpressionToArray("1,2{3},3");
- System.out.println(integerObjectMap.toString());
-
- }
-
-
- public static Map pageNumExpressionToArray(String expression) {
-
- Map returnvalue = new HashMap<>();
-
- Map hashmap = new HashMap<>();
- List pageList = new ArrayList<>();
-
-// if (StringUtils.isEmpty(expression)) {
-// return new ArrayList<>();
-// }
- String[] arrays = expression.split(",");
- for (String exp : arrays) {
-// if (StringUtils.isEmpty(exp)) continue;
- if (exp.contains("{")) {
- String i = exp.substring(exp.indexOf("{") + 1, exp.indexOf("}"));
- exp = exp.substring(0, exp.indexOf("{"));
- hashmap.put(Integer.valueOf(exp), Integer.valueOf(i));
-// pageList.add(Integer.valueOf(exp));
- }
- if (exp.contains("-")) {
- String[] startEndArray = exp.split("-");
- int startPageNo = Integer.valueOf(startEndArray[0]);
- int endPageNo = Integer.valueOf(startEndArray[1]);
- for (int i = startPageNo; i <= endPageNo; i++) {
- pageList.add(i);
- }
- } else {
- pageList.add(Integer.valueOf(exp));
-
- }
- }
- returnvalue.put(1, pageList);
- returnvalue.put(2, hashmap);
- return returnvalue;
-
-
-
- }
-
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
--
Gitee