标题:Java实现广度遍历二叉树
public class TestMyTreeBroad12 {
public void boradorder(Queue
<IntNode> q
) {
if(q
.isEmpty()) {
return ;
}else {
IntNode p
= q
.peek();
System
.out
.print(p
.val
+ " ");
q
.poll();
if(p
.left
!= null
) {
q
.offer(p
.left
);
}
if(p
.right
!= null
) {
q
.offer(p
.right
);
}
this.boradorder(q
);
}
}
public void hh(IntNode p1
) {
Queue
<IntNode> q
= new LinkedList<IntNode>();
q
.offer(p1
);
this.boradorder(q
);
}
public void boradorder03( Queue
<IntNode> q
, int count
, List
<List
<IntNode>> blist
, List
<IntNode> slist
) {
if(q
.isEmpty()) {
blist
.add(slist
);
return ;
}else {
if(count
== 0) {
blist
.add(slist
);
slist
= new ArrayList<IntNode>();
count
= q
.size();
}
IntNode node
= q
.poll();
count
--;
System
.out
.print(node
.val
+ " ");
slist
.add(node
);
if(node
.left
!= null
) {
q
.offer(node
.left
);
}
if(node
.right
!= null
) {
q
.offer(node
.right
);
}
this.boradorder03(q
, count
, blist
, slist
);
}
}
public void hh03(IntNode p
) {
if(p
== null
) {
return ;
}
Queue
<IntNode> q
= new LinkedList<>();
int count
= 1;
List
<List
<IntNode>> blist
= new ArrayList<>();
List
<IntNode> slist
= new ArrayList<>();
q
.offer(p
);
this.boradorder03(q
, count
, blist
, slist
);
System
.out
.println();
System
.out
.println("输出list中的值");
this.printListList(blist
);
}
public List
<List
<IntNode>> broadorder02(IntNode p
) {
Queue
<IntNode> q
= new LinkedList<>();
List
<List
<IntNode>> blist
= new ArrayList<>();
List
<IntNode> mlist
= new ArrayList<IntNode>();
if(p
== null
) {
return null
;
}
q
.offer(p
);
mlist
.add(p
);
blist
.add(mlist
);
while(!q
.isEmpty()) {
int size
= q
.size();
List
<IntNode> mlist02
= new ArrayList<IntNode>();
for(int i
= 0; i
< size
; i
++) {
IntNode node
= q
.poll();
System
.out
.print(node
.val
+ " ");
if(node
.left
!= null
) {
q
.offer(node
.left
);
mlist02
.add(node
.left
);
}
if(node
.right
!= null
) {
q
.offer(node
.right
);
mlist02
.add(node
.right
);
}
}
if(mlist02
.size() != 0) {
blist
.add(mlist02
);
}
}
return blist
;
}
public void printListList(List
<List
<IntNode>> list
) {
System
.out
.println("\n开始输出:");
for(List
<IntNode> lis
: list
) {
for(IntNode node
: lis
) {
System
.out
.print(node
.val
+ " ");
}
System
.out
.println();
}
System
.out
.println();
}
@Test
public void test02() {
IntNode p1
= new IntNode('a');
IntNode p2
= new IntNode('b');
IntNode p3
= new IntNode('c');
IntNode p4
= new IntNode('d');
IntNode p5
= new IntNode('e');
IntNode p6
= new IntNode('f');
IntNode p7
= new IntNode('g');
IntNode p8
= new IntNode('h');
IntNode p9
= new IntNode('i');
IntNode p10
= new IntNode('j');
IntNode p11
= new IntNode('k');
p1
.left
= p2
;
p1
.right
= p3
;
p2
.left
= p4
;
p2
.right
= p5
;
p4
.right
= p6
;
p5
.left
= p7
;
p5
.right
= p8
;
p3
.left
= p9
;
p3
.right
= p10
;
p10
.right
=p11
;
System
.out
.println("使用递归的广度遍历");
this.hh(p1
);
System
.out
.println();
System
.out
.println("使用递归的广度遍历,【保存到了ListList中】");
this.hh03(p1
);
System
.out
.println("不使用递归的广度遍历");
List
<List
<IntNode>> list
= this.broadorder02(p1
);
this.printListList(list
);
}
}
转载请注明原文地址: https://lol.8miu.com/read-37025.html