-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultBinaryTreeNode.java
More file actions
95 lines (84 loc) · 1.6 KB
/
DefaultBinaryTreeNode.java
File metadata and controls
95 lines (84 loc) · 1.6 KB
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
package datastructures;
/**
* This class constructs a binary tree node
*
* @author Ching2 Huang
*
* @param data
*/
public class DefaultBinaryTreeNode<T> implements BinaryTreeNode<T> {
// the left and right children
private DefaultBinaryTreeNode<T> left, right;
// the data for the node
private T data;
public DefaultBinaryTreeNode() {
//the data
this.data = null;
}
/**
* Constructor sets the data
* @param data
*/
public DefaultBinaryTreeNode(T data) {
//the data
this.data = data;
}
/**
* Get the data stored at this node.
*
* @return Object data.
*/
public T getData() {
return data;
}
/**
* Set the data stored at this node.
*/
public void setData(T data) {
this.data = data;
}
/**
* Get the left child.
*
* @return BinaryTreeNode that is left child, or null if no child.
*/
public BinaryTreeNode<T> getLeftChild() {
if (left == null) {
return null;
} else {
return left;
}
}
/**
* Get the right child.
*
* @return BinaryTreeNode that is right child, or null if no child.
*/
public BinaryTreeNode<T> getRightChild() {
if (right == null) {
return null;
} else {
return right;
}
}
/**
* Set the left child.
*/
public void setLeftChild(BinaryTreeNode<T> left) {
this.left = (DefaultBinaryTreeNode<T>) left;
}
/**
* Set the right child.
*/
public void setRightChild(BinaryTreeNode<T> right) {
this.right = (DefaultBinaryTreeNode<T>) right;
}
/**
* Tests if this node is a leaf (has no children).
*
* @return true if leaf node.
*/
public boolean isLeaf() {
return left == null && right == null;
}
}