-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
BinaryNode.java
102 lines (84 loc) ยท 2.46 KB
/
BinaryNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.crossoverjie.algorithm;
import java.util.LinkedList;
/**
* Function:
*
* @author crossoverJie
* Date: 04/01/2018 18:26
* @since JDK 1.8
*/
public class BinaryNode {
private Object data ;
private BinaryNode left ;
private BinaryNode right ;
public BinaryNode() {
}
public BinaryNode(Object data, BinaryNode left, BinaryNode right) {
this.data = data;
this.left = left;
this.right = right;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public BinaryNode getLeft() {
return left;
}
public void setLeft(BinaryNode left) {
this.left = left;
}
public BinaryNode getRight() {
return right;
}
public void setRight(BinaryNode right) {
this.right = right;
}
public BinaryNode createNode(){
BinaryNode node = new BinaryNode("1",null,null) ;
BinaryNode left2 = new BinaryNode("2",null,null) ;
BinaryNode left3 = new BinaryNode("3",null,null) ;
BinaryNode left4 = new BinaryNode("4",null,null) ;
BinaryNode left5 = new BinaryNode("5",null,null) ;
BinaryNode left6 = new BinaryNode("6",null,null) ;
node.setLeft(left2) ;
left2.setLeft(left4);
left2.setRight(left6);
node.setRight(left3);
left3.setRight(left5) ;
return node ;
}
@Override
public String toString() {
return "BinaryNode{" +
"data=" + data +
", left=" + left +
", right=" + right +
'}';
}
/**
* ไบๅๆ ็ๅฑๅบ้ๅ ๅๅฉไบ้ๅๆฅๅฎ็ฐ ๅๅฉ้ๅ็ๅ
่ฟๅ
ๅบ็็นๆง
*
* ้ฆๅ
ๅฐๆ น่็นๅ
ฅ้ๅ ็ถๅ้ๅ้ๅใ
* ้ฆๅ
ๅฐๆ น่็นๆๅฐๅบๆฅ๏ผๆฅ็ๅคๆญๅทฆ่็นๆฏๅฆไธบ็ฉบ ไธไธบ็ฉบๅๅ ๅ
ฅ้ๅ
* @param node
*/
public void levelIterator(BinaryNode node){
LinkedList<BinaryNode> queue = new LinkedList<>() ;
//ๅ
ๅฐๆ น่็นๅ
ฅ้
queue.offer(node) ;
BinaryNode current ;
while (!queue.isEmpty()){
current = queue.poll();
System.out.print(current.data+"--->");
if (current.getLeft() != null){
queue.offer(current.getLeft()) ;
}
if (current.getRight() != null){
queue.offer(current.getRight()) ;
}
}
}
}