回答編集履歴
2
コード編集
test
CHANGED
@@ -38,11 +38,15 @@
|
|
38
38
|
|
39
39
|
|
40
40
|
|
41
|
-
public
|
41
|
+
public String indent() { return parent_ == null ? "" : parent_.indent() + " "; }
|
42
42
|
|
43
|
-
System.out.println(name_);
|
44
43
|
|
44
|
+
|
45
|
+
public void print() { if ( left() != null ) { System.out.print(indent()); left().print(); }
|
46
|
+
|
47
|
+
System.out.print(indent()); System.out.println(name_);
|
48
|
+
|
45
|
-
if ( right() != null ) right().print(); }
|
49
|
+
if ( right() != null ) { System.out.print(indent()); right().print(); } }
|
46
50
|
|
47
51
|
|
48
52
|
|
@@ -52,9 +56,9 @@
|
|
52
56
|
|
53
57
|
|
54
58
|
|
55
|
-
root.setLeft(new BNode("left") )
|
59
|
+
root.setLeft( new BNode("left") )
|
56
60
|
|
57
|
-
.setLeft( new BNode("left-left") )
|
61
|
+
.setLeft( new BNode("left-left") )
|
58
62
|
|
59
63
|
.parent().setRight(new BNode("left-right"));
|
60
64
|
|
@@ -62,9 +66,9 @@
|
|
62
66
|
|
63
67
|
root.setRight(new BNode("right"))
|
64
68
|
|
65
|
-
|
69
|
+
.setRight(new BNode("right-right"))
|
66
70
|
|
67
|
-
.parent().setLeft( new BNode("right-left")
|
71
|
+
.parent().setLeft( new BNode("right-left"));
|
68
72
|
|
69
73
|
|
70
74
|
|
1
コード修正
test
CHANGED
@@ -12,9 +12,11 @@
|
|
12
12
|
|
13
13
|
public class BNode {
|
14
14
|
|
15
|
-
private BNode left_ = null;
|
15
|
+
private BNode left_ = null;
|
16
16
|
|
17
17
|
private BNode right_ = null;
|
18
|
+
|
19
|
+
private BNode parent_ = null;
|
18
20
|
|
19
21
|
|
20
22
|
|
@@ -24,13 +26,15 @@
|
|
24
26
|
|
25
27
|
public BNode(String name) { name_ = name; }
|
26
28
|
|
27
|
-
public BNode right() { return right_; }
|
29
|
+
public BNode right() { return right_; }
|
28
30
|
|
29
|
-
public BNode left() { return left_; }
|
31
|
+
public BNode left() { return left_; }
|
30
32
|
|
31
|
-
public BNode
|
33
|
+
public BNode parent() { return parent_; }
|
32
34
|
|
35
|
+
public BNode setRight(BNode node) { right_ = node; node.parent_ = this; return node; }
|
36
|
+
|
33
|
-
public BNode setLeft(BNode node) { left_ = node; return node; }
|
37
|
+
public BNode setLeft(BNode node) { left_ = node; node.parent_ = this; return node; }
|
34
38
|
|
35
39
|
|
36
40
|
|
@@ -46,9 +50,23 @@
|
|
46
50
|
|
47
51
|
BNode root = new BNode("root");
|
48
52
|
|
49
|
-
root.setLeft(new BNode("left")).setLeft(new BNode("left-left"));
|
50
53
|
|
54
|
+
|
55
|
+
root.setLeft(new BNode("left") )
|
56
|
+
|
57
|
+
.setLeft( new BNode("left-left") )
|
58
|
+
|
51
|
-
|
59
|
+
.parent().setRight(new BNode("left-right"));
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
root.setRight(new BNode("right"))
|
64
|
+
|
65
|
+
.setRight(new BNode("right-right"))
|
66
|
+
|
67
|
+
.parent().setLeft( new BNode("right-left") );
|
68
|
+
|
69
|
+
|
52
70
|
|
53
71
|
root.print();
|
54
72
|
|