-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeNode.java
135 lines (114 loc) · 3.47 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.util.ArrayList;
import java.util.List;
public class TreeNode<T> {
/**
* Node has:
* 1. Parent
* 2. List of children
* 3. data - here it will be Block
* */
public TreeNode<T> parent = null;
public List<TreeNode<T>> children = new ArrayList<TreeNode<T>>();
public T data = null;
public TreeNode(T data) {
this.data = data;
this.parent = null;
}
public TreeNode(T data, TreeNode<T> parent) {
this.data = data;
this.parent = parent;
parent.children.add(this);
}
public List<TreeNode<T>> getChildren() {
return children;
}
public void setParent(TreeNode<T> parent) {
parent.addChild(this.getData());
this.parent = parent;
}
public void addChild(T data) {
TreeNode<T> child = new TreeNode<T>(data);
child.setParent(this);
this.children.add(child);
}
public void addChild(TreeNode<T> child) {
child.setParent(this);
this.children.add(child);
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public boolean isRoot() {
return (this.parent == null);
}
public boolean isLeaf() {
if(this.children.size() == 0)
return true;
else
return false;
}
public void removeParent() {
this.parent = null;
}
/**
* Find the node in the subtree rooted at node that contains the data d
* */
public TreeNode<T> findNode(T d){
if(this.data.equals(d)){
return this;
}
TreeNode<T> tmp=null;
for(int i=0;i<this.children.size();i++){
tmp = children.get(i).findNode(d);
if(tmp!=null){
return tmp;
}
}
return null;
}
/**
* Find the node in the subtree rooted at node that contains the data d
* */
public TreeNode<T> findNodeByBlockID(String id){
if(((Block)this.data).matchBlockID(id)){
return this;
}
TreeNode<T> tmp=null;
for(int i=0;i<this.children.size();i++){
tmp = children.get(i).findNodeByBlockID(id);
if(tmp!=null){
return tmp;
}
}
return null;
}
public void printNode(String ident){
System.out.println(ident+"********************************************");
((Block) this.data).printBlock(ident);
System.out.println(ident+"Parent Node data:");
((Block) this.parent.data).printBlock(ident);
System.out.println(ident+"Children Nodes data:");
for(int i=0;i<this.children.size();i++){
System.out.println(ident+"Child #"+(i+1));
((Block) this.children.get(i).data).printBlock(ident);
System.out.println();
}
System.out.println(ident+"********************************************");
}
public void printOnlyNode(String ident){
System.out.println(ident+"********************************************");
((Block) this.data).printBlock(ident);
// System.out.println(ident+"Parent Node data:");
// ((Block) this.parent.data).printBlock(ident);
System.out.println(ident+"********************************************");
}
public void printSubTree(String ident){
this.printOnlyNode(ident);
for(int i=0;i<this.children.size();i++){
this.children.get(i).printSubTree(ident+"\t");
}
}
}