Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/JumbleGC/Tree.java
41159 views
1
/*
2
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package gc.gctests.JumbleGC;
25
26
import java.util.Vector ;
27
28
class treeNode {
29
static int memorySinkSize = 100;
30
int info;
31
treeNode left;
32
treeNode right;
33
int [] memory_sink;
34
35
public treeNode(int info) {
36
this.info = info;
37
memory_sink = new int[memorySinkSize];
38
}
39
}
40
41
42
public class Tree {
43
44
private treeNode TreeRoot; // root of Tree
45
private int elementCount; // number of elements in Tree
46
Vector TreeValues; // all the nodal values in the tree
47
// duplicated in this array.
48
private int TreeValueIndex; // Where to store next Tree value
49
50
51
Tree(int TreeSize) { TreeValues = new Vector(TreeSize); }
52
53
synchronized void addElement(int o) {
54
treeNode p,q;
55
56
treeNode newnode = new treeNode(o);
57
p = TreeRoot;
58
q = null;
59
60
while(p != null){
61
q = p;
62
if(newnode.info <= p.info)
63
p = p.left;
64
else
65
p = p.right;
66
}
67
68
if ( q == null ){
69
TreeRoot = newnode;
70
return;
71
}
72
if (newnode.info <= q.info )
73
q.left = newnode;
74
else
75
q.right = newnode;
76
elementCount++;
77
TreeValues.addElement(Integer.valueOf(o));
78
}
79
80
81
int getTreeValue(int index) {
82
Integer num;
83
num = (Integer) TreeValues.elementAt(index);
84
TreeValues.removeElementAt(index);
85
return num.intValue();
86
}
87
88
89
int vectorSize(){ return TreeValues.size(); }
90
91
92
synchronized void PrettyPrint(){
93
Print(TreeRoot, "");
94
}
95
96
private void Print( treeNode root, String indent) {
97
if(root == null){
98
return;
99
}
100
Print(root.right, indent + " ");
101
System.out.println(indent + root.info);
102
Print(root.left, indent + " ");
103
}
104
105
106
synchronized int getNodeNumber(){return elementCount; }
107
108
synchronized private treeNode findNode(int o) {
109
treeNode p, q;
110
p = TreeRoot;
111
while(p != null && p.info != o){
112
q = p;
113
if (o < p.info )
114
p = p.left;
115
else if(o > p.info)
116
p = p.right;
117
}
118
return p;
119
}
120
121
// destroy subtree rooted at treeNode containing int o
122
// creating a subtree of garbage rooted at treeNode containing int o
123
124
void destroySubTree(int o) {
125
treeNode p,q;
126
127
// find treeNode containing p.
128
p = TreeRoot;
129
q = null;
130
while(p != null && p.info != o){
131
q = p;
132
if (o < p.info )
133
p = p.left;
134
else if(o > p.info)
135
p = p.right;
136
}
137
138
if (p == null){ // couldnt find treeNode
139
return;
140
}
141
142
// decrease elementCount of tree by the number of treeNodes
143
// in sub-tree rooted at p
144
145
elementCount -= getCount(p);
146
if (q == null){ // destroy the whole tree
147
TreeRoot = null;
148
return;
149
}
150
151
if (p.info > q.info ) // deleting right child
152
q.right = null;
153
else
154
q.left = null;
155
}
156
157
158
synchronized void deleteElement(int o){
159
treeNode p,q;
160
treeNode rc, sub_node, leftmost, leftmost_parent,s;
161
162
p = TreeRoot;
163
q = null;
164
sub_node = null;
165
166
while(p != null && p.info != o){
167
q = p;
168
if (o < p.info )
169
p = p.left;
170
else if(o > p.info)
171
p = p.right;
172
}
173
174
if ( p == null) // couldnt find treeNode
175
return;
176
177
rc = p.right;
178
179
if (rc == null){
180
sub_node = p.left;
181
} else if (rc.left == null) {
182
rc.left = p.left;
183
sub_node = p.right;
184
}else if ( rc.left != null && rc.right != null) {
185
s = rc;
186
leftmost_parent = null;
187
leftmost = null;
188
while ( s != null){
189
leftmost_parent = leftmost;
190
leftmost = s;
191
s = s.left;
192
}
193
leftmost_parent.left = leftmost.right;
194
leftmost.left = p.left;
195
leftmost.right= p.right;
196
sub_node = leftmost;
197
}
198
199
if ( q == null ){
200
TreeRoot = sub_node;
201
return;
202
}
203
204
if (p.info > q.info ) // deleting right child
205
q.right = sub_node;
206
else
207
q.left = sub_node;
208
209
return;
210
}
211
212
213
private int getCount( treeNode root) {
214
if (root == null )
215
return 0;
216
if (root.left == null && root.right == null)
217
return 1;
218
else
219
return getCount(root.left) + getCount(root.right) + 1;
220
}
221
}
222
223