-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLNode.java
139 lines (109 loc) · 3.1 KB
/
AVLNode.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
136
137
138
139
/**
* AVLNode class creates the nodes used to store data in AVL tree.
* Contains data for left, right, data and height of each node.
* Provides all the necessary getters and setters methods for AVL node.
* Extends comparable to make sure that generic type used in AVL node object
* implements the compare to method.
* @author Adisa Narula
* @param <T>
*/
public class AVLNode <T extends Comparable <T>> implements Comparable <AVLNode <T>> {
// initiate node data field
private T data;
private AVLNode <T> left;
private AVLNode <T> right;
int height;
// constructors
/**
* Constructor creates new AVLNode and sets data and height of node.
* @param data that is stored in the node.
*/
public AVLNode(T data) {
// set data field for node object
this.data = data;
this.left = null;
this.right = null;
this.height = 0;
}
/**
* Overloaded constructor sets data, left child, right child and height of the node.
* @param data that is stored in the node.
* @param left left subtree of the node.
* @param right right subtree of the node.
*/
public AVLNode(T data, AVLNode <T> left, AVLNode <T> right) {
// set data field for node object
this.data = data;
this.left = left;
this.right = right;
this.height = 0;
}
/**
* Setter method for data in the node. Allows user to
* change data stored in the node.
* @param data that is store in the node object.
*/
public void setData(T data) {
this.data = data;
}
/**
* Setter method to set left subtree of node.
* @param left subtree of the node.
*/
public void setLeft (AVLNode <T> left) {
this.left = left;
}
/**
* Setter method for right subtree of the node.
* @param right subtree of the node.
*/
public void setRight (AVLNode <T> right) {
this.right = right;
}
/**
* Getter method for user to be able to access
* data stored in the node.
* @return data store in the node.
*/
public T getData () {
return this.data;
}
/**
* Setter method for the height field of the node.
* @param height of node.
*/
public void setHeight(int height) {
this.height = height;
}
/**
* Getter method for user to be able to access height of node.
* @return integer representing height of node.
*/
public int getHeight() {
return this.height;
}
/**
* Getter method for user to be able to access left subtree of the node.
* @return left subtree of the node.
*/
public AVLNode <T> getLeft() {
return this.left;
}
/**
* Getter method for user to be able to access right subtree of the node.
* @return right subtree of the node.
*/
public AVLNode <T> getRight() {
return this.right;
}
/**
* Compares this object with the specified object for order.
* Returns a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object (Javadoc).
* @return integer that represents whether data is less than (-1), greater than (1) or equal to (0)
*/
@Override
public int compareTo(AVLNode<T> other) {
return this.data.compareTo(other.data);
}
}