This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
169 lines (136 loc) · 3.19 KB
/
Node.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Representação genérica de um nó da árvore binária.
*
* @author André Matteucci - 32273541
* @author Felipe Ribeiro - 32212720
* @author Enzo Koji - 32273754
*
* @see BinaryTree
* @see OperandNode
* @see OperatorNode
*/
public class Node {
protected Node parent;
protected Node left;
protected Node right;
protected Object data;
public Node() {
this.parent = null;
this.left = null;
this.right = null;
this.data = '\0';
}
public Node(Node parent) {
this.parent = parent;
this.left = null;
this.right = null;
this.data = '\0';
}
public Node(Node left, Node right) {
this.parent = null;
this.left = left;
this.right = right;
this.data = '\0';
}
public Node(Node parent, Node left, Node right) {
this.parent = parent;
this.left = left;
this.right = right;
this.data = '\0';
}
public Node(char data) {
this.parent = null;
this.left = null;
this.right = null;
this.data = data;
}
public Node(Node parent, char data) {
this.parent = parent;
this.left = null;
this.right = null;
this.data = data;
}
public Node(Node parent, char data, Node left, Node right) {
this.parent = parent;
this.left = left;
this.right = right;
this.data = data;
}
public Node(char data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
public void setParent(Node parent) {
this.parent = parent;
}
public void setLeft(Node left) {
this.left = left;
left.setParent(this);
}
public void setRight(Node right) {
this.right = right;
right.setParent(this);
}
public void setData(char data) {
this.data = data;
}
public Node getParent() {
return this.parent;
}
public Node getLeft() {
return this.left;
}
public Node getRight() {
return this.right;
}
public Object getData() {
return this.data;
}
public boolean hasLeft() {
return this.left != null;
}
public boolean hasRight() {
return this.right != null;
}
public boolean isRoot() {
return this.parent == null;
}
public boolean isLeaf() {
return this.left == null && this.right == null;
}
public int getDegree() {
int degree = 0;
if (hasLeft())
++degree;
if (hasRight())
++degree;
return degree;
}
public int getLevel() {
if (isRoot()) {
return 0;
}
return parent.getLevel() + 1;
}
public int getHeight() {
if (isLeaf()) {
return 0;
}
int height = 0;
if (hasLeft())
height = Math.max(height, left.getHeight());
if (hasRight())
height = Math.max(height, right.getHeight());
return height + 1;
}
/**
* Método utilizado para visitar um nó da árvore.
* No caso particular do nó genérico, sempre retorna Float.NaN.
*
* @return Float.NaN
*/
public float visitar() {
return Float.NaN;
}
}