-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.java
More file actions
155 lines (130 loc) · 3.26 KB
/
TreeNode.java
File metadata and controls
155 lines (130 loc) · 3.26 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Class representing a node in an AVL/BST Tree, and stores the value of the node
* and its children(left, right) and the height of the node
*
* @author Jimmy Lindstr�m
*
*/
public class TreeNode {
private int data;
private TreeNode left;
private TreeNode right;
private int height;
/**
* Constructor for the node taking one parameter, the value.
* @param data the value of the node
*/
public TreeNode(int data) {
this.data = data;
left = right = null;
height = 0;
}
/**
* Method for fetching the value of a node
* @return the data the value of the node
*/
public int getData() {
return data;
}
/**
* Method returning the left child of the node
* @return the left child of the node
*/
public TreeNode getLeft() {
return left;
}
/**
* Method for setting the value of a node taking the value as parameter
* @param data the value to be set in the node
*/
public void setData(int data) {
this.data = data;
}
/**
* Method for setting the left child of the node, taking the new node as
* parameter
* @param left the left to set as child
*/
public void setLeft(TreeNode left) {
this.left = left;
}
/**
* Method returning the right child of the node
* @return the right child of the node
*/
public TreeNode getRight() {
return right;
}
/**
* Method for setting the right child of the node, taking the new node as
* parameter
* @param right the right to set as child
*/
public void setRight(TreeNode right) {
this.right = right;
}
/**Method for getting the height of a node
*
* @return height of the node
*/
public int getHeight() {
return height;
}
/**method for setting the height of a node, calculating it based on
* the biggest height of the right and left child, and adding 1.
*/
public void setHeight() {
int heightLeft = left != null ? left.getHeight() : -1;
int heightRight = right != null ? right.getHeight() : -1;
height = Math.max(heightLeft, heightRight) +1;
}
/**Method returning the balance of a node.
*
* @return baalance of the node
*/
public int getBalance(){
int heightLeft = left != null ? left.getHeight() : -1;
int heightRight = right != null ? right.getHeight() : -1;
return heightRight - heightLeft;
}
/**
* Method for checking if the node has NO children, if it's a leaf,
* returning true if node is a leaf, else false
*/
public boolean isLeaf() {
return (right == null && left == null);
}
/**---------------------------------------
* Code to visualize the three
* ---------------------------------------
*/
public void printTree() {
if (this.right != null) {
right.printTree(true, "");
}
printNodeValue();
if (left != null) {
left.printTree(false, "");
}
}
private void printTree(boolean isRight, String indent) {
if (right != null) {
right.printTree(true, indent + (isRight ? " " : " | "));
}
System.out.print(indent);
if (isRight) {
System.out.print(" /");
} else {
System.out.print(" \\");
}
System.out.print("----- ");
printNodeValue();
if (left != null) {
left.printTree(false, indent + (isRight ? " | " : " "));
}
}
private void printNodeValue() {
System.out.print(data + "(" + this.height + ")");
System.out.print('\n');
}
}