Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/JumbleGC/Tree.java
41159 views
/*1* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package gc.gctests.JumbleGC;2425import java.util.Vector ;2627class treeNode {28static int memorySinkSize = 100;29int info;30treeNode left;31treeNode right;32int [] memory_sink;3334public treeNode(int info) {35this.info = info;36memory_sink = new int[memorySinkSize];37}38}394041public class Tree {4243private treeNode TreeRoot; // root of Tree44private int elementCount; // number of elements in Tree45Vector TreeValues; // all the nodal values in the tree46// duplicated in this array.47private int TreeValueIndex; // Where to store next Tree value484950Tree(int TreeSize) { TreeValues = new Vector(TreeSize); }5152synchronized void addElement(int o) {53treeNode p,q;5455treeNode newnode = new treeNode(o);56p = TreeRoot;57q = null;5859while(p != null){60q = p;61if(newnode.info <= p.info)62p = p.left;63else64p = p.right;65}6667if ( q == null ){68TreeRoot = newnode;69return;70}71if (newnode.info <= q.info )72q.left = newnode;73else74q.right = newnode;75elementCount++;76TreeValues.addElement(Integer.valueOf(o));77}787980int getTreeValue(int index) {81Integer num;82num = (Integer) TreeValues.elementAt(index);83TreeValues.removeElementAt(index);84return num.intValue();85}868788int vectorSize(){ return TreeValues.size(); }899091synchronized void PrettyPrint(){92Print(TreeRoot, "");93}9495private void Print( treeNode root, String indent) {96if(root == null){97return;98}99Print(root.right, indent + " ");100System.out.println(indent + root.info);101Print(root.left, indent + " ");102}103104105synchronized int getNodeNumber(){return elementCount; }106107synchronized private treeNode findNode(int o) {108treeNode p, q;109p = TreeRoot;110while(p != null && p.info != o){111q = p;112if (o < p.info )113p = p.left;114else if(o > p.info)115p = p.right;116}117return p;118}119120// destroy subtree rooted at treeNode containing int o121// creating a subtree of garbage rooted at treeNode containing int o122123void destroySubTree(int o) {124treeNode p,q;125126// find treeNode containing p.127p = TreeRoot;128q = null;129while(p != null && p.info != o){130q = p;131if (o < p.info )132p = p.left;133else if(o > p.info)134p = p.right;135}136137if (p == null){ // couldnt find treeNode138return;139}140141// decrease elementCount of tree by the number of treeNodes142// in sub-tree rooted at p143144elementCount -= getCount(p);145if (q == null){ // destroy the whole tree146TreeRoot = null;147return;148}149150if (p.info > q.info ) // deleting right child151q.right = null;152else153q.left = null;154}155156157synchronized void deleteElement(int o){158treeNode p,q;159treeNode rc, sub_node, leftmost, leftmost_parent,s;160161p = TreeRoot;162q = null;163sub_node = null;164165while(p != null && p.info != o){166q = p;167if (o < p.info )168p = p.left;169else if(o > p.info)170p = p.right;171}172173if ( p == null) // couldnt find treeNode174return;175176rc = p.right;177178if (rc == null){179sub_node = p.left;180} else if (rc.left == null) {181rc.left = p.left;182sub_node = p.right;183}else if ( rc.left != null && rc.right != null) {184s = rc;185leftmost_parent = null;186leftmost = null;187while ( s != null){188leftmost_parent = leftmost;189leftmost = s;190s = s.left;191}192leftmost_parent.left = leftmost.right;193leftmost.left = p.left;194leftmost.right= p.right;195sub_node = leftmost;196}197198if ( q == null ){199TreeRoot = sub_node;200return;201}202203if (p.info > q.info ) // deleting right child204q.right = sub_node;205else206q.left = sub_node;207208return;209}210211212private int getCount( treeNode root) {213if (root == null )214return 0;215if (root.left == null && root.right == null)216return 1;217else218return getCount(root.left) + getCount(root.right) + 1;219}220}221222223