Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/gctest03/Tree.java
41155 views
/*1* Copyright (c) 2002, 2018, 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.gctest03;2425class DataNodeException extends Exception26{27}282930class DataNode {31int key;32int buf[];33static int dataNodeCount = 0;34static int dataNodeLimit;3536static synchronized void incDataNodeCount()37{38dataNodeCount++;39}4041static synchronized int getDataNodeCount()42{43return dataNodeCount;44}4546static synchronized void setDataNodeLimit(int newLimit)47{48dataNodeLimit = newLimit;49}5051static synchronized int getDataNodeLimit()52{53return dataNodeLimit;54}5556static synchronized void clearDataNodeCount()57{58dataNodeCount = 0;59}60DataNode(int key) throws DataNodeException61{6263incDataNodeCount();64if (getDataNodeCount() > getDataNodeLimit())65{66throw (new DataNodeException());67}6869this.key = key;70try {71buf = new int[key];72}73catch ( OutOfMemoryError e )74{75System.out.println(Thread.currentThread().getName() + " : outofmemory");76}77}7879public void print()80{81System.out.println(key);82}8384public boolean equals(DataNode d)85{86int k = d.getkey();8788return ( key == k);89}9091public boolean large(DataNode d)92{93int k = d.getkey();9495return ( key > k);96}9798public boolean less(DataNode d)99100{101int k = d.getkey();102103return ( key < k);104}105106public int getkey()107{ return key; }108}109110111class TreeNode {112DataNode data;113TreeNode parent;114TreeNode left;115TreeNode right;116117TreeNode(DataNode key)118{119this.data = key;120parent = null;121left = null;122right = null;123}124125public void print()126{127data.print();128}129130public TreeNode getleft()131{132return left;133}134135public TreeNode getright()136{137return right;138}139140public TreeNode getparent()141{142return parent;143}144145public synchronized void setleft(TreeNode left)146{147this.left = left;148}149150public synchronized void setright(TreeNode right)151{152this.right = right;153}154155public synchronized void setparent(TreeNode parent)156{157this.parent = parent;158}159160public DataNode getData()161{162return data;163}164165// print it out in order of a large one first166public void sprint()167{168//print itself169if ( left != null ) left.sprint();170System.out.println(data.getkey());171if ( right != null ) right.sprint();172}173174// print it out in order of a small one first175public void lprint()176{177if (right != null ) right.lprint();178System.out.println(data.getkey());179if ( left != null ) left.lprint();180}181182public synchronized TreeNode duplicate()183{184TreeNode tp = new TreeNode(data);185186if ( left != null )187{188tp.left = left.duplicate();189}190if ( right != null )191{192tp.right = right.duplicate();193}194return tp;195}196197public TreeNode search(DataNode d)198{199TreeNode tp = this;200DataNode k;201202while ( tp != null )203{204k = tp.getData();205if ( k.equals(d) )206{207return tp;208}209else210if ( d.large(k) )211tp = tp.getright();212else213tp = tp.getleft();214}215return null;216}217218public synchronized void insert(TreeNode t)219{220DataNode d = t.getData();221222TreeNode tp = this;223TreeNode tp1 = tp;224DataNode d0;225226while ( true )227{228d0 = tp.getData();229230if ( d.large(d0) )231{232tp1 = tp;233tp = tp.getright();234if ( tp == null )235{236tp1.setright(t);237t.setparent(tp1);238break;239}240}241else242{243tp1 = tp;244tp = tp.getleft();245if (tp == null )246{247tp1.setleft(t);248t.setparent(tp1);249break;250}251}252}253254}255256257}258259260class Tree {261TreeNode root = null;262263Tree()264{265root = null;266}267268Tree(TreeNode root)269{270this.root = root;271}272273public synchronized void insert(TreeNode t)274{275if ( root == null )276{277root = t;278return;279}280281root.insert(t);282}283284285public void sort1()286{287root.sprint();288}289290public void sort2()291{292root.lprint();293}294295public TreeNode search(DataNode d)296{297if ( root == null ) return null;298else return root.search(d);299}300301public synchronized boolean remove(DataNode d)302{303if ( root == null ) return false;304305TreeNode t = root.search(d);306307// data is not in a heap308if ( t == null ) return false;309310/*311if ( d.equals(t.getData()) == false )312{313System.out.println("failed");314return false;315}316*/317318TreeNode p = t.getparent();319TreeNode l = t.getleft();320TreeNode r = t.getright();321322// the removed node is a root323if ( p == null )324{325if ( l == null && r != null )326{327r.setparent(null);328root = r;329return true;330}331if ( l != null && r == null )332{333l.setparent(null);334root = l;335return true;336}337if ( l == null && r == null )338{339root = null;340return true;341}342343if ( l != null && r != null )344{345r.setparent(null);346r.insert(l);347root = r;348return true;349}350}351352// a leaf353if ( r == null && l == null )354{355if ( p.getright() == t )356{357/* right child */358p.setright(null);359}360else361p.setleft(null);362return true;363}364365// a node without left child366if ( r != null && l == null )367{368r.setparent(p);369if ( t == p.getright() ) p.setright(r);370if ( t == p.getleft() ) p.setleft(r);371return true;372}373374if ( r == null && l != null )375{376l.setparent(p);377if ( t == p.getright() ) p.setright(l);378if ( t == p.getleft() ) p.setleft(l);379return true;380}381382// a node with two children383r.insert(l);384r.setparent(p);385if ( t == p.getright() ) p.setright(r);386if ( t == p.getleft() ) p.setleft(r);387return true;388}389390public synchronized Tree copy()391{392if ( root == null ) return null;393394return(new Tree(root.duplicate()));395}396397public synchronized boolean isempty()398{399return ( root == null );400}401402403}404405406