<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
/*First class on university...6/10 !!!*/
package dim.ds;
import dim.ext.javax.swing.tree.Dmtn;//DefaultMutableTreeNode
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
/**
NOT return ROOT !
* <br> will be given by the following tree
* <br>0=root
* <br>--0.1=1
* <br>--0.2=2
* <br>--0.3=3
* <br>--------0.3.0=some
* <br>----------------0.3.0.0=99
* <br>----------------0.3.0.1=999
* <br>--------0.3.1=6
* <br>--0.4=7
* @author jimidimi
*/
public class PreBackOrder implements Enumeration<Dmtn>,Iterator<Dmtn>{
public static void main(String[] args) {
Dmtn t=new Dmtn("root");
//7,6,1,2,5,3,2,1
t.crad(1);
t.crad(2);
Dmtn d3=t.crad(3);
Dmtn d5=d3.crad(5);
d5.crad(2);
d5.crad(1);
Dmtn d6=d3.crad(6);
t.crad(7);
PreBackOrder pbo=new PreBackOrder(t);
while(pbo.hasMoreElements()){
Object o=pbo.nextElement();
System.out.println(pbo.counter+" : "+o+" depth = "+pbo.depth);
}
System.out.println(" -------------- second example ----------- ");
t.removeAllChildren();
//
//4,0,450,3,2,1,21
t.sbad(21).sbad(1);
t.sbad(21).sbad(2);
t.sbad(21).sbad(3);
t.sbad(450).sbad(0);
t.sbad(450).sbad(4);
pbo.clear();
while(pbo.hasMoreElements()){
Object o=pbo.nextElement();
System.out.println(pbo.counter+" : "+o+" depth = "+pbo.depth);
}
}
public Dmtn root,par,it;
/**
* The current index path
*/
public Vector<Integer> indexPathStack=new Vector<>();
/**
* The current last index of Vector
*/
public int stackIndex=-1;
/**
* The current last Value of Vector
*/
public int itIndexOnPar;
/**
* Simple enumeration step counter
*/
public int counter=-1;
/**
* The previus (from call) stack.size()=stackIndex+1
*/
int depth=0;
/**
*
* @param theRoot to iterate without ... theRoot !
* <br>To iterate also to Root use
* <br>1.Or some level up root
* <br>2.Or Dmtn temp=new Dmtn("temp");
* <br> temp.add(my_root);
* <br> new PreBackOrder(temp).iterate.....
*
*/
public PreBackOrder(Dmtn theRoot){
root=theRoot;
clear();
}
/**
* When called must be synchronized(lock) ????
*/
public void clear(){
it=root;
indexPathStack.clear();
stackIndex=-1;
int chc=it.getChildCount();
itIndexOnPar=chc;
while(chc>0){
chc--;
indexPathStack.add(chc);
itIndexOnPar=chc;
++stackIndex;
it=it.dmtn(chc);
chc=it.getChildCount();
}
par=it.pardmtn();
counter=-1;
depth=stackIndex+1;
}
@Override
public boolean hasNext() {
return it!=null;
}
@Override
public Dmtn next() {
return nextElement();
}
/**
*
* @return it!=null;
*/
@Override
public boolean hasMoreElements() {
return it!=null;
}
/**
* rolls on the iteration to next stage
* @return
*/
@Override
public Dmtn nextElement() {
++counter;//info
depth=stackIndex+1;//info
///////////////////////////////////////
Dmtn out=it;
//this missing TAB INSET is missing but class not return ROOT ????!!!???
--itIndexOnPar;
//System.out.println("lastchc : "+lastChc);
indexPathStack.setElementAt(itIndexOnPar, stackIndex);
//System.out.println(""+itIndex);
if(itIndexOnPar>=0){
//System.out.println("Taking brother");
it=par.dmtn(itIndexOnPar);
int chc=it.getChildCount();
while(chc>0){
chc--;
indexPathStack.add(chc);
itIndexOnPar=chc;
++stackIndex;
it=it.dmtn(chc);
chc=it.getChildCount();
}
par=it.pardmtn();
}
else{
//System.out.println("Finished iterate on "+par+" children");
indexPathStack.remove(stackIndex);
--stackIndex;
if(stackIndex<0){
it=null;
}
else{
it=par;
par=it.pardmtn();
itIndexOnPar=indexPathStack.get(stackIndex);
}
}
return out;
}
}
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
/*First class on university...6/10 !!!*/
package dim.ds;
import dim.ext.javax.swing.tree.Dmtn;//DefaultMutableTreeNode
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
/**
NOT return ROOT !
* A Backward Pre Order Enumeration
* <br>7, 6, 999, 99, some, 3, 2, 1* <br> will be given by the following tree
* <br>0=root
* <br>--0.1=1
* <br>--0.2=2
* <br>--0.3=3
* <br>--------0.3.0=some
* <br>----------------0.3.0.0=99
* <br>----------------0.3.0.1=999
* <br>--------0.3.1=6
* <br>--0.4=7
* @author jimidimi
*/
public class PreBackOrder implements Enumeration<Dmtn>,Iterator<Dmtn>{
public static void main(String[] args) {
Dmtn t=new Dmtn("root");
//7,6,1,2,5,3,2,1
t.crad(1);
t.crad(2);
Dmtn d3=t.crad(3);
Dmtn d5=d3.crad(5);
d5.crad(2);
d5.crad(1);
Dmtn d6=d3.crad(6);
t.crad(7);
PreBackOrder pbo=new PreBackOrder(t);
while(pbo.hasMoreElements()){
Object o=pbo.nextElement();
System.out.println(pbo.counter+" : "+o+" depth = "+pbo.depth);
}
System.out.println(" -------------- second example ----------- ");
t.removeAllChildren();
//
//4,0,450,3,2,1,21
t.sbad(21).sbad(1);
t.sbad(21).sbad(2);
t.sbad(21).sbad(3);
t.sbad(450).sbad(0);
t.sbad(450).sbad(4);
pbo.clear();
while(pbo.hasMoreElements()){
Object o=pbo.nextElement();
System.out.println(pbo.counter+" : "+o+" depth = "+pbo.depth);
}
}
public Dmtn root,par,it;
/**
* The current index path
*/
public Vector<Integer> indexPathStack=new Vector<>();
/**
* The current last index of Vector
*/
public int stackIndex=-1;
/**
* The current last Value of Vector
*/
public int itIndexOnPar;
/**
* Simple enumeration step counter
*/
public int counter=-1;
/**
* The previus (from call) stack.size()=stackIndex+1
*/
int depth=0;
/**
*
* @param theRoot to iterate without ... theRoot !
* <br>To iterate also to Root use
* <br>1.Or some level up root
* <br>2.Or Dmtn temp=new Dmtn("temp");
* <br> temp.add(my_root);
* <br> new PreBackOrder(temp).iterate.....
*
*/
public PreBackOrder(Dmtn theRoot){
root=theRoot;
clear();
}
/**
* When called must be synchronized(lock) ????
*/
public void clear(){
it=root;
indexPathStack.clear();
stackIndex=-1;
int chc=it.getChildCount();
itIndexOnPar=chc;
while(chc>0){
chc--;
indexPathStack.add(chc);
itIndexOnPar=chc;
++stackIndex;
it=it.dmtn(chc);
chc=it.getChildCount();
}
par=it.pardmtn();
counter=-1;
depth=stackIndex+1;
}
@Override
public boolean hasNext() {
return it!=null;
}
@Override
public Dmtn next() {
return nextElement();
}
/**
*
* @return it!=null;
*/
@Override
public boolean hasMoreElements() {
return it!=null;
}
/**
* rolls on the iteration to next stage
* @return
*/
@Override
public Dmtn nextElement() {
++counter;//info
depth=stackIndex+1;//info
///////////////////////////////////////
Dmtn out=it;
//this missing TAB INSET is missing but class not return ROOT ????!!!???
--itIndexOnPar;
//System.out.println("lastchc : "+lastChc);
indexPathStack.setElementAt(itIndexOnPar, stackIndex);
//System.out.println(""+itIndex);
if(itIndexOnPar>=0){
//System.out.println("Taking brother");
it=par.dmtn(itIndexOnPar);
int chc=it.getChildCount();
while(chc>0){
chc--;
indexPathStack.add(chc);
itIndexOnPar=chc;
++stackIndex;
it=it.dmtn(chc);
chc=it.getChildCount();
}
par=it.pardmtn();
}
else{
//System.out.println("Finished iterate on "+par+" children");
indexPathStack.remove(stackIndex);
--stackIndex;
if(stackIndex<0){
it=null;
}
else{
it=par;
par=it.pardmtn();
itIndexOnPar=indexPathStack.get(stackIndex);
}
}
return out;
}
}
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου