Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
GreyZeng
algorithm
提交
850e7407
A
algorithm
项目概览
GreyZeng
/
algorithm
通知
10
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
850e7407
编写于
1月 24, 2024
作者:
GreyZeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update code
上级
d6cc5b3f
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
88 addition
and
87 deletion
+88
-87
src/main/java/git/snippet/heap/Code_Heap.java
src/main/java/git/snippet/heap/Code_Heap.java
+0
-87
src/test/java/git/snippet/heap/Code_HeapTest.java
src/test/java/git/snippet/heap/Code_HeapTest.java
+88
-0
未找到文件。
src/main/java/git/snippet/heap/Code_Heap.java
浏览文件 @
850e7407
...
@@ -27,93 +27,6 @@ import java.util.List;
...
@@ -27,93 +27,6 @@ import java.util.List;
// heapify和heapInsert都是logN级别的复杂度,因为N个节点的二叉树高度是logN
// heapify和heapInsert都是logN级别的复杂度,因为N个节点的二叉树高度是logN
public
class
Code_Heap
{
public
class
Code_Heap
{
public
static
void
testHeap
()
{
int
value
=
1000
;
int
limit
=
100
;
int
testTimes
=
1000000
;
for
(
int
i
=
0
;
i
<
testTimes
;
i
++)
{
int
curLimit
=
(
int
)
(
Math
.
random
()
*
limit
)
+
1
;
MaxHeap
my
=
new
MaxHeap
(
curLimit
);
RightMaxHeap
test
=
new
RightMaxHeap
(
curLimit
);
int
curOpTimes
=
(
int
)
(
Math
.
random
()
*
limit
);
for
(
int
j
=
0
;
j
<
curOpTimes
;
j
++)
{
if
(
my
.
isEmpty
()
!=
test
.
isEmpty
())
{
System
.
out
.
println
(
"Oops!"
);
}
if
(
my
.
isFull
()
!=
test
.
isFull
())
{
System
.
out
.
println
(
"Oops!"
);
}
if
(
my
.
isEmpty
())
{
int
curValue
=
(
int
)
(
Math
.
random
()
*
value
);
my
.
push
(
curValue
);
test
.
push
(
curValue
);
}
else
if
(
my
.
isFull
())
{
if
(
my
.
pop
()
!=
test
.
pop
())
{
System
.
out
.
println
(
"Oops!"
);
}
}
else
{
if
(
Math
.
random
()
<
0.5
)
{
int
curValue
=
(
int
)
(
Math
.
random
()
*
value
);
my
.
push
(
curValue
);
test
.
push
(
curValue
);
}
else
{
if
(
my
.
pop
()
!=
test
.
pop
())
{
System
.
out
.
println
(
"Oops!"
);
}
}
}
}
}
System
.
out
.
println
(
"finish!"
);
}
// 加强堆测试
public
static
void
testGreaterHeap
()
{
int
value
=
1000
;
int
limit
=
100
;
int
testTimes
=
1000000
;
for
(
int
i
=
0
;
i
<
testTimes
;
i
++)
{
int
curLimit
=
(
int
)
(
Math
.
random
()
*
limit
)
+
1
;
HeapGreater
<
Integer
>
my
=
new
HeapGreater
<>(
new
Comparator
<
Integer
>()
{
@Override
public
int
compare
(
Integer
o1
,
Integer
o2
)
{
return
o2
-
o1
;
}
});
RightMaxHeap
test
=
new
RightMaxHeap
(
curLimit
);
int
curOpTimes
=
(
int
)
(
Math
.
random
()
*
limit
);
for
(
int
j
=
0
;
j
<
curOpTimes
;
j
++)
{
if
(
test
.
isEmpty
())
{
int
curValue
=
(
int
)
(
Math
.
random
()
*
value
);
my
.
push
(
curValue
);
test
.
push
(
curValue
);
}
else
if
(
test
.
isFull
())
{
if
(
my
.
pop
()
!=
test
.
pop
())
{
System
.
out
.
println
(
"Oops!"
);
}
}
else
{
if
(
Math
.
random
()
<
0.5
)
{
int
curValue
=
(
int
)
(
Math
.
random
()
*
value
);
my
.
push
(
curValue
);
test
.
push
(
curValue
);
}
else
{
if
(
my
.
pop
()
!=
test
.
pop
())
{
System
.
out
.
println
(
"Oops!"
);
}
}
}
}
}
System
.
out
.
println
(
"finish!"
);
}
public
static
void
main
(
String
[]
args
)
{
testHeap
();
testGreaterHeap
();
}
public
static
class
MaxHeap
{
public
static
class
MaxHeap
{
private
final
int
[]
heap
;
private
final
int
[]
heap
;
// private final int limit; limit == heap.length
// private final int limit; limit == heap.length
...
...
src/test/java/git/snippet/heap/Code_HeapTest.java
0 → 100644
浏览文件 @
850e7407
package
git.snippet.heap
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
@DisplayName
(
"堆结构测试"
)
public
class
Code_HeapTest
{
@Test
@DisplayName
(
"堆结构测试"
)
public
void
testHeap
()
{
int
value
=
1000
;
int
limit
=
100
;
int
testTimes
=
1000000
;
for
(
int
i
=
0
;
i
<
testTimes
;
i
++)
{
int
curLimit
=
(
int
)
(
Math
.
random
()
*
limit
)
+
1
;
Code_Heap
.
MaxHeap
my
=
new
Code_Heap
.
MaxHeap
(
curLimit
);
Code_Heap
.
RightMaxHeap
test
=
new
Code_Heap
.
RightMaxHeap
(
curLimit
);
int
curOpTimes
=
(
int
)
(
Math
.
random
()
*
limit
);
for
(
int
j
=
0
;
j
<
curOpTimes
;
j
++)
{
if
(
my
.
isEmpty
()
!=
test
.
isEmpty
())
{
Assertions
.
fail
();
}
if
(
my
.
isFull
()
!=
test
.
isFull
())
{
Assertions
.
fail
();
}
if
(
my
.
isEmpty
())
{
int
curValue
=
(
int
)
(
Math
.
random
()
*
value
);
my
.
push
(
curValue
);
test
.
push
(
curValue
);
}
else
if
(
my
.
isFull
())
{
if
(
my
.
pop
()
!=
test
.
pop
())
{
Assertions
.
fail
();
}
}
else
{
if
(
Math
.
random
()
<
0.5
)
{
int
curValue
=
(
int
)
(
Math
.
random
()
*
value
);
my
.
push
(
curValue
);
test
.
push
(
curValue
);
}
else
{
if
(
my
.
pop
()
!=
test
.
pop
())
{
Assertions
.
fail
();
}
}
}
}
}
}
// 加强堆测试
@Test
@DisplayName
(
"加强堆结构测试"
)
public
void
testGreaterHeap
()
{
int
value
=
1000
;
int
limit
=
100
;
int
testTimes
=
1000000
;
for
(
int
i
=
0
;
i
<
testTimes
;
i
++)
{
int
curLimit
=
(
int
)
(
Math
.
random
()
*
limit
)
+
1
;
Code_Heap
.
HeapGreater
<
Integer
>
my
=
new
Code_Heap
.
HeapGreater
<>(
(
o1
,
o2
)
->
o2
-
o1
);
Code_Heap
.
RightMaxHeap
test
=
new
Code_Heap
.
RightMaxHeap
(
curLimit
);
int
curOpTimes
=
(
int
)
(
Math
.
random
()
*
limit
);
for
(
int
j
=
0
;
j
<
curOpTimes
;
j
++)
{
if
(
test
.
isEmpty
())
{
int
curValue
=
(
int
)
(
Math
.
random
()
*
value
);
my
.
push
(
curValue
);
test
.
push
(
curValue
);
}
else
if
(
test
.
isFull
())
{
if
(
my
.
pop
()
!=
test
.
pop
())
{
Assertions
.
fail
();
}
}
else
{
if
(
Math
.
random
()
<
0.5
)
{
int
curValue
=
(
int
)
(
Math
.
random
()
*
value
);
my
.
push
(
curValue
);
test
.
push
(
curValue
);
}
else
{
if
(
my
.
pop
()
!=
test
.
pop
())
{
Assertions
.
fail
();
}
}
}
}
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录