jjjj


public interface IStackForPriorExamination<E extends Comparable<E>> {
 public void push(E e);
 public E pop();
 public E peekMedian();
 public E peekMaximum();
 public E peekMinimum();
 public int size();

}

 

/* the algorithm is based on the AVL tree,the worst time for peekMedian() peekMaximum() peekMinimum() are O(logn). The class StackNode is used to record
 *  the sequence the elements were pushed into the stack, and the class Node is a AVL tree. In fact, there is no need to creat the class StackNode, record
 *  the  elements' pushed sequence is better. And there is much space to let the algorithm be faster.*/

public class StackForPriorExaminationImpl<E extends Comparable<E>>implements IStackForPriorExamination<E> {
 int count;
 Node tree;
 int tall;
 int low;
 StackNode top;
 Node tempNode;
public StackForPriorExaminationImpl(){
 
}

 private void lRotate(Node t) {

  Node p = t.rChild;

  E temp = p.data;
  p.data = t.data;
  t.data = temp;
  t.rChild = p.rChild;
  p.rChild = p.lChild;
  p.lChild = t.lChild;
  t.lChild = p;

  t.right = p.right;
  p.right = p.left;
  p.left = t.left;
  t.left += p.right + p.self;
 }

 private void rRotate(Node t) {
  Node p = t.lChild;

  E temp = t.data;
  t.data = p.data;
  p.data = temp;
  t.lChild = p.lChild;
  p.lChild = p.rChild;
  p.rChild = t.rChild;
  t.rChild = p;

  t.left = p.left;
  p.left = p.right;
  p.right = t.right;
  t.right += p.left + p.self;

 }
@Override
 public void push(E e) {
  count++;
  Node p = new Node();
  p.rChild = this.tree;
  insert(this.tree, p, 1, e);
  this.tree = p.rChild;
 }
@Override
 public E peekMinimum() {
  tempNode = this.tree;
  while (tempNode.lChild != null)
   tempNode = tempNode.lChild;

  return tempNode.data;
 }
@Override
 public E peekMaximum() {
  tempNode = this.tree;
  while (tempNode.rChild != null)
   tempNode = tempNode.rChild;
  return tempNode.data;
 }
@Override
 public E peekMedian() {
  return getMed(this.tree, count / 2 + 1, 0);
 }

 public E getMed(Node t, int s, int sum) {

  if (t.left + sum < s && t.left + sum + t.self >= s) {
   return t.data;
  } else if (t.left + sum + t.self < s) {

   return getMed(t.rChild, s, t.left + sum + t.self);
  } else if (t.left + sum >= s) {
   return getMed(t.lChild, s, sum);
  }
  return null;
 }

 private boolean insert(Node t, Node p, int d, E s) {

  if (t == null) {
   switch (d) {
   case 0:
    p.lChild = new Node(s);
    StackNode s1 = new StackNode(s);
    s1.next = top;
    top = s1;

    break;
   case 1:
    p.rChild = new Node(s);
    StackNode s2 = new StackNode(s);
    s2.next = top;
    top = s2;
    break;
   }
   tall = 1;
   return true;// ==================
  } else {
   if (t.data.compareTo(s) == 0) {
    tall = 0;
    t.self++;
    StackNode s1 = new StackNode(s);
    s1.next = top;
    top = s1;
    return false;
   }// ----------------t.self++
   if (t.data.compareTo(s) > 0) {
    t.left++;// ==========================
    if (insert(t.lChild, t, 0, s)) {

     if (tall == 1) {
      switch (t.bf) {
      case 0:
       tall = 1;
       t.bf = 1;
       break;
      case 1:
       tall = 0;
       leftBalance(t);
      case -1:
       tall = 0;
       t.bf = 0;
       break;
      }
     }

     return true;
    }
   }
   if (t.data.compareTo(s) < 0) {
    t.right++;// ========================
    if (insert(t.rChild, t, 1, s)) {

     if (tall == 1) {
      switch (t.bf) {
      case 0:
       tall = 1;
       t.bf = -1;
       break;
      case 1:
       tall = 0;
       t.bf = 0;
      case -1:
       tall = 0;
       rightBalance(t);
      }
     }

     return true;
    }
   }

   return false;
  }
 }

 private void leftBalance(Node t) {
  Node p1 = t.lChild;

  switch (p1.bf) {
  case 1:
   rRotate(t);
   t.bf = 0;
   t.rChild.bf = 0;
   break;
  case -1:
   int tt = p1.rChild.bf;
   lRotate(p1);
   rRotate(t);
   t.bf = 0;
   switch (tt) {
   case 0:
    t.lChild.bf = t.rChild.bf = 0;
    break;
   case 1:
    t.lChild.bf = 0;
    t.rChild.bf = -1;
    break;
   case -1:
    t.lChild.bf = 1;
    t.rChild.bf = 0;
    break;
   }
  }
 }

 private void rightBalance(Node t) {
  Node p1 = t.rChild;
  switch (p1.bf) {
  case -1:
   lRotate(t);
   t.bf = 0;
   t.lChild.bf = 0;
   break;
  case 1:
   int tt = p1.lChild.bf;
   System.out.println("p1" + p1.data);
   rRotate(p1);
   lRotate(t);
   t.bf = 0;
   switch (tt) {
   case 0:
    t.lChild.bf = t.rChild.bf = 0;
    break;
   case 1:
    t.lChild.bf = 0;
    t.rChild.bf = -1;
    break;
   case -1:
    t.lChild.bf = 1;
    t.rChild.bf = 0;
    break;
   }
  }
 }
 @Override
 public E pop() {
  count--;
  Node p = new Node();
  p.rChild = this.tree;
  delete(this.tree, p, 1, top.data);
  E del = top.data;
  top = top.next;
  this.tree = p.rChild;
  return del;
 }

 private boolean delete(Node t, Node p, int d, E s) {
  if (t == null) {
   low = 0;
   return false;
  }
  if (t.data.compareTo(s) == 0) {
   if (t.self > 1) {// ====================
    t.self--;
    low = 0;
    return false;
   } else if (t.lChild != null && t.rChild != null) {
    Node t1 = t.lChild;
    while (t1.rChild != null) {
     t1 = t1.rChild;
    }
    E temp = t.data;
    t.data = t1.data;
    t1.data = temp;
    delete(t.lChild, t, 0, s);
    if (low == 1) {
     switch (t.bf) {
     case 0:
      t.bf = -1;
      low = 0;
      break;
     case 1:
      t.bf = 0;
      low = 1;
      break;
     case -1:
      low = 1;
      rightBalance(t);
     }
    }
   } else {
    Node t0 = null;
    if (t.lChild == null)
     t0 = t.rChild;
    if (t.rChild == null)
     t0 = t.lChild;
    switch (d) {
    case 0:
     p.lChild = t0;
     break;
    case 1:
     p.rChild = t0;
     break;
    }
    low = 1;
   }
   return true;
  }
  if (t.data.compareTo(s) > 0) {
   t.left--;
   if (!delete(t.lChild, t, 0, s)) {

    return false;
   }
   if (low == 1) {
    switch (t.bf) {
    case 0:
     t.bf = -1;
     low = 0;
     break;
    case 1:
     t.bf = 0;
     low = 1;
     break;
    case -1:
     low = 1;
     rightBalance(t);
    }
   }
  }
  if (t.data.compareTo(s) < 0) {
   t.right--;
   if (!delete(t.rChild, t, 1, s)) {

    return false;
   }
   if (low == 1) {
    switch (t.bf) {
    case 0:
     t.bf = 1;
     low = 0;
     break;
    case -1:
     t.bf = 0;
     low = 1;
     break;
    case 1:
     low = 1;
     leftBalance(t);
    }
   }
  }
  return false;
 }
 @Override
 public int size() {
  return count;
 }

 public static void main(String[] args) {
  StackForPriorExaminationImpl<Integer> avl = new StackForPriorExaminationImpl<Integer>();
  int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 8 };
  for (int i = 0; i < 10; i++)
   avl.push(a[i]);
  System.out.println(avl.size());
  for (int i = 0; i < 10; i++) {
   System.out.println("------------------the med number: "
     + avl.peekMedian());
   System.out.println("------------------the max number: "
     + avl.peekMaximum());
   System.out.println("------------------the min number: "
     + avl.peekMinimum());
   avl.pop();
   System.out.println("****");
  }
 }

 class StackNode {
  E data;
  StackNode next;

  StackNode(E data) {
   this.data = data;
  }
 }

 class Node {
  E data;
  Node lChild;
  Node rChild;
  int left;
  int right;
  int self = 1;
  int bf;

  Node(E data) {
   this.data = data;
  }

  Node() {
  }
 }

}

posted on 2011-09-26 22:02  梦逆飞  阅读(733)  评论(0编辑  收藏  举报

导航