-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTreeNode.java
84 lines (80 loc) · 1.96 KB
/
TreeNode.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
public class TreeNode {
int value;
TreeNode leftNode;
TreeNode rightNode;
public void setValue(int value) {
this.value = value;
}
public TreeNode(int value){
this.value = value;
}
public void setLeftNode(TreeNode leftNode) {
this.leftNode = leftNode;
}
public void setRightNode(TreeNode rightNode) {
this.rightNode = rightNode;
}
public void frontShow(){
System.out.println(value);
if(leftNode!=null){
leftNode.frontShow();
}
if(rightNode!=null){
rightNode.frontShow();
}
}
public void midShow(){
if(leftNode!=null){
leftNode.midShow();
}
System.out.println(value);
if(rightNode!=null){
rightNode.midShow();
}
}
public void afterShow(){
if(leftNode!=null){
leftNode.afterShow();
}
if(rightNode!=null){
rightNode.afterShow();
}
System.out.println(value);
}
public TreeNode frontSearch(int i){
TreeNode target = null;
if(this.value==i){
return this;
}else{
if(leftNode!=null){
target = leftNode.frontSearch(i);
}
if(target!=null){
return target;
}
if(rightNode!=null){
target = rightNode.frontSearch(i);
}
}
return target;
}
public void delete(int i){
TreeNode parent = this;
if(parent.leftNode!=null&&parent.leftNode.value==i){
parent.leftNode=null;
return;
}
if(parent.rightNode!=null&&parent.rightNode.value==i){
parent.rightNode=null;
return;
}
parent=leftNode;
if(parent!=null){
parent.delete(i);
}
parent=rightNode;
if(parent!=null){
parent.delete(i);
}
}
}