Advertisement
wandrake

Untitled

Jun 28th, 2013
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. public class mTreeS<A> : extends mTree<A> {
  2.     protected int outDegree;
  3.  
  4.     public void newEmpty() {
  5.         super.newEmpty();
  6.         outDegree = 0;
  7.     }
  8.  
  9.     public void newVertex(A r) {
  10.         super.newVertex(r);
  11.         outDegree = 0;
  12.     }
  13.  
  14.     public void addEdge(A r, A s) throws Exception {
  15.         super.addEdge(r, s);
  16.         int sons = super.sons(r).size();
  17.         if (sons > outDegree) outDegree = sons;
  18.     }
  19.  
  20.     public int fanOut() { return outDegree; }
  21. }
  22.  
  23. public class mTreeR<A> : extends mTreeS<A> {
  24.     private class Pair<A> {
  25.         public A fst;
  26.         public A snd;
  27.         public Pair(A x, A y) {
  28.             this.fst = x;
  29.             this.snd = y;
  30.         }
  31.     }
  32.  
  33.     private class PairDegree<A> {
  34.         public A node;
  35.         public int degree;
  36.         public PairDegree(A x, int y) {
  37.             this.node = x;
  38.             this.degree = y;
  39.         }
  40.     }
  41.  
  42.     private Vector<Pair<A>> removed;
  43.     private Vector<PairDegree<A>> degrees;
  44.  
  45.     private void checkDegree() {
  46.         int max = -1;
  47.         for (PairDegree<A> pd : degrees) {
  48.             if (pd.degree > max) max = pd.degree;
  49.         }
  50.         this.outDegree = max;
  51.     }
  52.  
  53.     public mTreeR() {
  54.         super();
  55.         removed = new Vector<Pair<A>>();
  56.         degrees = new Vector<PairDegree<A>>();
  57.     }
  58.  
  59.     public void newEmpty() {
  60.         super.newEmpty();
  61.         removed = new Vector<Pair<A>>();
  62.         degrees = new Vector<PairDegree<A>>();
  63.     }
  64.  
  65.     public void newVertex(A r) {
  66.         super.newVertex(r);
  67.         removed = new Vector<Pair<A>>();
  68.         degrees = new Vector<PairDegree<A>>();
  69.         degrees.add(new PairDegree<A>(r, 0));
  70.     }
  71.  
  72.     public void addEdge(A r, A s) throws Exception {
  73.         Pair<A> ref = null;
  74.         for (Pair<A> p : removed) {
  75.             if (p.fst.equals(r) && p.snd.equals(s)) ref = p;
  76.         }
  77.         if (ref == null) super.addEdge(r, s);
  78.         else removed.remove(ref);
  79.  
  80.         degrees.add(new PairDegree<A>(s, 0));
  81.         for (PairDegree<A> pd : degrees) {
  82.             if (pd.node.equals(r)) pd.degree++;
  83.         }
  84.  
  85.         checkDegree();
  86.     }
  87.  
  88.     public void remove(A r, A s) throws Exception {
  89.         Pair<A> ref = null;
  90.         for (Pair<A> p : removed) {
  91.             if (p.fst.equals(r) && p.snd.equals(s)) ref = p;
  92.         }
  93.         if (ref != null) throw new Exception("Error");
  94.  
  95.         boolean found = false;
  96.         for (A a : super.sons(r)) if (a.equals(s)) found = true;
  97.         if (!found) throw new Exception("Error");
  98.    
  99.         removed.add(new Pair<A>(r, s));
  100.         for (PairDegree<A> pd : degrees) {
  101.             if (pd.node.equals(r)) pd.degree--;
  102.         }
  103.         checkDegree();
  104.  
  105.     }
  106.  
  107.     public Vector<A> sons(A r) throws Exception {
  108.         Vector<A> s = super.sons();
  109.         Vector<A> res = new Vector<A>();
  110.         for (A a : s) {
  111.             boolean found = false;
  112.             for (Pair<A> p : removed) {
  113.                 if (p.fst.equals(r) && p.snd.equals(a)) found = true;
  114.             }
  115.             if (!found) res.add(a);
  116.         }
  117.         return res;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement