Skip to content

Commit 1754c2e

Browse files
committed
红黑树
1 parent ce63f74 commit 1754c2e

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

src/main/java/io/github/zebinh/algorithmJava/algorithmBook/search/RedBlackBinarySearchTreeSearch03.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.LinkedList;
44
import java.util.Queue;
5+
import java.util.Random;
56
import java.util.concurrent.ArrayBlockingQueue;
67

78
/**
@@ -16,18 +17,24 @@ public static int search(int[] nums, int target) {
1617
bsTree.insert(nums[i]);
1718
}
1819
BST.Node root = bsTree.root;
19-
// Queue<BST.Node> q = new LinkedList<>();
20-
// q.offer(root);
21-
// while(q.size() > 0) {
22-
// BST.Node node = q.poll();
23-
// System.out.print(node.value + ", ");
24-
// if (node.left != null){
25-
// q.offer(node.left);
26-
// }
27-
// if (node.right != null){
28-
// q.offer(node.right);
29-
// }
30-
// }
20+
Queue<BST.Node> cur = new LinkedList<>();
21+
Queue<BST.Node> next = new LinkedList<>();
22+
cur.offer(root);
23+
while(!cur.isEmpty()) {
24+
BST.Node node = cur.poll();
25+
System.out.print(node.value + ",");
26+
if (node.left != null){
27+
next.offer(node.left);
28+
}
29+
if (node.right != null){
30+
next.offer(node.right);
31+
}
32+
if (cur.isEmpty()) {
33+
System.out.println();
34+
cur = next;
35+
next = new LinkedList<>();
36+
}
37+
}
3138

3239
while(root != null){
3340
if (root.value == target) {
@@ -109,14 +116,19 @@ public Node rotateRight(Node node){
109116
public void flipColor(Node node) {
110117
node.color = true;
111118
node.left.color = false;
112-
node.right.color = true;
119+
node.right.color = false;
113120
}
114121
}
115122

116123

117124
public static void main(String[] args) {
118125
// 测试用例
119-
int[] nums = new int[]{-9, -7, -1, 0, 1, 3, 5, 7, 8, 9, 10, 15};
120-
System.out.println(search(nums, 2));
126+
final int COUNT = 127;
127+
int [] nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
128+
// int[] nums = new int[COUNT];
129+
// for (int i = 0; i < COUNT; i++) {
130+
// nums[i] = (int)(Math.random() * 1000);
131+
// }
132+
System.out.println(search(nums, -1));
121133
}
122134
}

0 commit comments

Comments
 (0)