-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeNode.java
86 lines (84 loc) · 1.54 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
public class TreeNode {
//fields with key1 < key2
private int key1;
private int key2;
private TreeNode parent;
private TreeNode leftChildX;
private TreeNode leftChild;
private TreeNode rightChild;
private TreeNode midChild;
//for identify the node type with potential value of 2 or 3
private int degree;
//constructor
public TreeNode(int kl, int kr){
key1 = kl;
key2 = kr;
init();
}
//for convenience
public void init(){
parent = null;
leftChildX = null;
leftChild = null;
rightChild = null;
midChild = null;
degree = 2;
}
//set method
public void setKey1(int k1){
key1 = k1;
}
public void setKey2(int k2){
key2 = k2;
}
public void setDegree(int d){
degree = d;
}
public void setParent(TreeNode p){
parent = p;
}
public void setLeftChildX(TreeNode x){
leftChildX = x;
}
public void setLeftChild(TreeNode l){
leftChild = l;
}
public void setRightChild(TreeNode r){
rightChild = r;
}
public void setMidChild(TreeNode m){
midChild = m;
}
//get method
public int getKey1(){
return key1;
}
public int getKey2(){
return key2;
}
public int getDegree(){
return degree;
}
public TreeNode getParent(){
return parent;
}
public TreeNode getLeftChildX(){
return leftChildX;
}
public TreeNode getLeftChild(){
return leftChild;
}
public TreeNode getRightChild(){
return rightChild;
}
public TreeNode getMidChild(){
return midChild;
}
//to string
public String toString(){
if (key2 == Integer.MAX_VALUE)
return "(" + key1 + ")";
else
return "(" + key1 + "," + key2 + ")";
}
}