Skip to content

Commit 287cc4b

Browse files
committed
change
1 parent f80b7c8 commit 287cc4b

File tree

6 files changed

+192
-190
lines changed

6 files changed

+192
-190
lines changed

src/com/duweri/interview/datastructure/ReBuildBinaryTree.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
/**
5-
* 已知前序遍历和后续遍历重建二叉树
6-
* @author 杜伟
5+
* 已知前序遍历和后续遍历重建二叉树
6+
* @author 杜伟
77
*/
88
public class ReBuildBinaryTree {
99

@@ -18,23 +18,23 @@ public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
1818
TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
1919
return root;
2020
}
21-
//前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
21+
//前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
2222
private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
2323

2424
if(startPre>endPre||startIn>endIn)
2525
return null;
26-
TreeNode root=new TreeNode(pre[startPre]);//前序遍历的第一个结点
26+
TreeNode root=new TreeNode(pre[startPre]);//前序遍历的第一个结点
2727

28-
for(int i=startIn;i<=endIn;i++){//遍历中序数组
29-
if(in[i]==pre[startPre]){ //如果中序的某节点和前序一样
30-
//递归左边
28+
for(int i=startIn;i<=endIn;i++){//遍历中序数组
29+
if(in[i]==pre[startPre]){ //如果中序的某节点和前序一样
30+
//递归左边
3131
root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
32-
//递归右边
32+
//递归右边
3333
root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
3434
}
35-
}//循环完数组结束
35+
}//循环完数组结束
3636

37-
return root;//返回根节点
37+
return root;//返回根节点
3838
}
3939

4040

src/com/duweri/interview/datastructure/Sort.java

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.duweri.interview.datastructure;
22

3-
import javax.management.Descriptor;
4-
53
public class Sort {
64

75
public static void main(String[] args) {
@@ -20,39 +18,39 @@ public static void display(int[] arr){
2018
}
2119

2220
/**
23-
* 插入排序
24-
* 关键:1、临时变量保存待排序的数字
25-
* 2、边比较边后移
26-
* 适用情况:序列基本有序的时候
21+
* 插入排序
22+
* 关键:1、临时变量保存待排序的数字
23+
* 2、边比较边后移
24+
* 适用情况:序列基本有序的时候
2725
*/
2826
public static void insertSort(int[] arr){
2927
for (int i = 1; i < arr.length; i++) {
30-
int temp = arr[i];//临时保存待插入序列
31-
int j;//下面先保证J大于0再去访问arr[j]!!!!
32-
for (j = i-1;j >= 0 && temp > arr[j]; j--) {//带插入序列大于当前序列就前移--》从大到小排列
28+
int temp = arr[i];//临时保存待插入序列
29+
int j;//下面先保证J大于0再去访问arr[j]!!!!
30+
for (j = i-1;j >= 0 && temp > arr[j]; j--) {//带插入序列大于当前序列就前移--》从大到小排列
3331
arr[j+1] = arr[j];
3432
}
3533
arr[j+1]=temp;
3634
}
3735
}
3836
/**
39-
* 希尔排序
37+
* 希尔排序
4038
*/
4139
public static void shellSort(int[] arr){
4240

43-
for (int i = arr.length/2; i >= 1; i=i/2) {//子序列的划分
41+
for (int i = arr.length/2; i >= 1; i=i/2) {//子序列的划分
4442

4543
}
4644

4745
}
4846
/**
49-
* 冒泡排序
50-
* 关键:1、外循环标识无序区和有序区
51-
* 2、内循环每次将一个无序区的(最大)数字排到最前面
47+
* 冒泡排序
48+
* 关键:1、外循环标识无序区和有序区
49+
* 2、内循环每次将一个无序区的(最大)数字排到最前面
5250
*/
5351
public static void bubbleSort(int[] arr){
54-
for (int i = 0; i <arr.length-1; i++) {//从后往前推小的
55-
for (int j = arr.length-1; j>i; j--) {//从最后一个开始遍历
52+
for (int i = 0; i <arr.length-1; i++) {//从后往前推小的
53+
for (int j = arr.length-1; j>i; j--) {//从最后一个开始遍历
5654
if (arr[j]>arr[j-1]) {
5755
int temp = arr[j];//swap
5856
arr[j] = arr[j-1];
@@ -63,10 +61,10 @@ public static void bubbleSort(int[] arr){
6361
}
6462

6563
/**
66-
* 快速排序
67-
* 关键: 1、选轴值
68-
* 2、一次划分
69-
* 3、递归快排
64+
* 快速排序
65+
* 关键: 1、选轴值
66+
* 2、一次划分
67+
* 3、递归快排
7068
*/
7169
public static void quickSort(int[] arr,int start,int end){
7270
if(start<end){
@@ -76,11 +74,11 @@ public static void quickSort(int[] arr,int start,int end){
7674
}
7775
}
7876
/**
79-
* 以arr[0]为基准进行一次划分算法,左边都小于arr[0],右边都大于arr[0]
77+
* 以arr[0]为基准进行一次划分算法,左边都小于arr[0],右边都大于arr[0]
8078
*/
8179
private static int partition(int[] arr,int first,int end){
8280
while(first < end){
83-
//先扫描右端
81+
//先扫描右端
8482
while(first < end && arr[first]<arr[end]){
8583
end--;
8684
}
@@ -90,7 +88,7 @@ private static int partition(int[] arr,int first,int end){
9088
arr[first] = temp;
9189
first++;
9290
}
93-
//扫描左边
91+
//扫描左边
9492
while(first < end && arr[first]<arr[end]){
9593
first++;
9694
}
@@ -123,19 +121,19 @@ private static int partition2(int[] arr,int left,int right){
123121

124122

125123
/**
126-
* 选择排序: O(n^2)
127-
* 每次遍历无序区选出最小的
124+
* 选择排序: O(n^2)
125+
* 每次遍历无序区选出最小的
128126
*/
129127
public static void selectSort(int[] arr){
130128
int minIndex = 0;
131129
for (int i = 0; i < arr.length-1; i++) {
132-
minIndex = i;//指向最小的值的角标
133-
for (int j = i+1; j < arr.length; j++) {//遍历无序区
130+
minIndex = i;//指向最小的值的角标
131+
for (int j = i+1; j < arr.length; j++) {//遍历无序区
134132
if(arr[j] < arr[minIndex]){
135133
minIndex = j;
136134
}
137135
}
138-
if (minIndex != i) { //无序区遍历完了,找到最小的角标了,交换
136+
if (minIndex != i) { //无序区遍历完了,找到最小的角标了,交换
139137
int temp = arr[minIndex];
140138
arr[minIndex] = arr[i];
141139
arr[i] = temp;

src/com/duweri/interview/datastructure/TraversalBinaryTree.java

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,33 @@ private void printNode(Node node) {
1111
}
1212

1313
/**
14-
* 先序遍历---递归
14+
* 先序遍历---递归
1515
*/
1616
public void theFirstTraversal(Node root) {
17-
printNode(root); // 根
18-
if (root.getLeftNode() != null) { // 左
17+
printNode(root); // 根
18+
if (root.getLeftNode() != null) { // 左
1919
theFirstTraversal(root.getLeftNode());
2020
}
21-
if (root.getRightNode() != null) { // 右
21+
if (root.getRightNode() != null) { // 右
2222
theFirstTraversal(root.getRightNode());
2323
}
2424
}
2525

2626
/**
27-
* 中序遍历---递归
27+
* 中序遍历---递归
2828
*/
2929
public void theInOrderTraversal(Node root) {
30-
if (root.getLeftNode() != null) { // 左
30+
if (root.getLeftNode() != null) { // 左
3131
theInOrderTraversal(root.getLeftNode());
3232
}
33-
printNode(root); // 中
33+
printNode(root); // 中
3434
if (root.getRightNode() != null) {
35-
theInOrderTraversal(root.getRightNode());// 右
35+
theInOrderTraversal(root.getRightNode());// 右
3636
}
3737
}
3838

3939
/**
40-
* 后序遍历---递归
40+
* 后序遍历---递归
4141
*/
4242
public void thePostOrderTraversal(Node root) {
4343
if (root.getLeftNode() != null) {
@@ -49,45 +49,45 @@ public void thePostOrderTraversal(Node root) {
4949
printNode(root);
5050
}
5151
/**
52-
* 前序遍历---非递归算法
52+
* 前序遍历---非递归算法
5353
*/
5454
public void theFirstTraversal_Stack(Node root) {
55-
Stack<Node> stack = new Stack<Node>();//初始化栈
56-
Node node = root; //遍历过程中移动此节点
57-
while (node != null || stack.size() > 0) { // 将所有左孩子压栈
58-
if (node != null) { //节点不为空,
59-
printNode(node);// 输出该节点
60-
stack.push(node);//节点入栈
61-
node = node.getLeftNode();//指向左孩子
62-
} else { //节点为空
63-
node = stack.pop();//出栈返回,去遍历右孩子
55+
Stack<Node> stack = new Stack<Node>();//初始化栈
56+
Node node = root; //遍历过程中移动此节点
57+
while (node != null || stack.size() > 0) { // 将所有左孩子压栈
58+
if (node != null) { //节点不为空,
59+
printNode(node);// 输出该节点
60+
stack.push(node);//节点入栈
61+
node = node.getLeftNode();//指向左孩子
62+
} else { //节点为空
63+
node = stack.pop();//出栈返回,去遍历右孩子
6464
node = node.getRightNode();
6565
}
6666
}
6767
}
6868
/**
69-
* 中序遍历---非递归算法
69+
* 中序遍历---非递归算法
7070
*/
7171
public void theInOrderTraversal_Stack(Node root) {
72-
Stack<Node> stack = new Stack<Node>();//初始化栈
73-
Node node = root; //临时指针节点
74-
while (node != null || stack.size() > 0) {//栈不空或者节点不空
75-
if (node != null) { //节点不空的情况
72+
Stack<Node> stack = new Stack<Node>();//初始化栈
73+
Node node = root; //临时指针节点
74+
while (node != null || stack.size() > 0) {//栈不空或者节点不空
75+
if (node != null) { //节点不空的情况
7676
stack.push(node);
7777
node = node.getLeftNode();
78-
} else { //栈不空的情况
78+
} else { //栈不空的情况
7979
node = stack.pop();
8080
printNode(node);
8181
node = node.getRightNode();
8282
}
8383
}
8484
}
8585
/**
86-
* 后序遍历---非递归算法
86+
* 后序遍历---非递归算法
8787
*/
8888
public void thePostOrderTraversal_Stack(Node root) {
8989
Stack<Node> stack = new Stack<Node>();
90-
Stack<Node> output = new Stack<Node>();// 构造一个中间栈来存储逆后序遍历的结果
90+
Stack<Node> output = new Stack<Node>();// 构造一个中间栈来存储逆后序遍历的结果
9191
Node node = root;
9292
while (node != null || stack.size() > 0) {
9393
if (node != null) {
@@ -106,27 +106,27 @@ public void thePostOrderTraversal_Stack(Node root) {
106106
}
107107

108108
/**
109-
* 深度优先遍历二叉树
110-
* @param root 根节点
109+
* 深度优先遍历二叉树
110+
* @param root 根节点
111111
*/
112112
public void depthFirstSearch(Node root){
113113
Stack<Node> stack = new Stack<Node>();
114114
stack.push(root);
115115
Node node = root;
116116
while(!stack.empty()){
117117
node = stack.pop();
118-
printNode(node); //遍历根结点
118+
printNode(node); //遍历根结点
119119
if(node.getRightNode() != null){
120-
stack.push(node.getRightNode()); //先将右子树压栈
120+
stack.push(node.getRightNode()); //先将右子树压栈
121121
}
122122
if(node.getLeftNode() != null){
123-
stack.push(node.getLeftNode()); //再将左子树压栈
123+
stack.push(node.getLeftNode()); //再将左子树压栈
124124
}
125125
}
126126
}
127127
/**
128-
* 广度优先遍历二叉树
129-
* @param node 根节点
128+
* 广度优先遍历二叉树
129+
* @param node 根节点
130130
*/
131131
public void breadthFirst(Node node) {
132132
Deque<Node> nodeDeque = new ArrayDeque();
@@ -147,18 +147,18 @@ public void breadthFirst(Node node) {
147147
public static void main(String[] args) {
148148
TraversalBinaryTree tree = new TraversalBinaryTree();
149149
Node root = tree.init();
150-
System.out.println("先序遍历");
150+
System.out.println("先序遍历");
151151
tree.theFirstTraversal(root);
152152
System.out.println("");
153-
System.out.println("中序遍历");
153+
System.out.println("中序遍历");
154154
tree.theInOrderTraversal(root);
155155
System.out.println("");
156-
System.out.println("后序遍历");
156+
System.out.println("后序遍历");
157157
tree.thePostOrderTraversal(root);
158158
System.out.println("");
159159
}
160160

161-
public Node init() {// 注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错
161+
public Node init() {// 注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错
162162
Node J = new Node(8, null, null);
163163
Node H = new Node(4, null, null);
164164
Node G = new Node(2, null, null);
@@ -168,7 +168,7 @@ public static void main(String[] args) {
168168
Node C = new Node(9, F, null);
169169
Node B = new Node(3, D, E);
170170
Node A = new Node(6, B, C);
171-
return A; // 返回根节点
171+
return A; // 返回根节点
172172
}
173173

174174
public class Node {

0 commit comments

Comments
 (0)