Java基於列表實現的元素迭代器、位置迭代器、樹迭代器(演算法源碼)

來源:互聯網
上載者:User

/* 高手之作,本人謹以收藏者身份共用源碼,供大家參考之! */

 

/*
 * 迭代器ADT介面
 */

package dsa;

public interface Iterator {
 boolean hasNext();//檢查迭代器中是否還有剩餘的元素
 Object getNext();//返回迭代器中的下一元素
}

 

 

/*
 * 基於列表實現的元素迭代器
 */

package dsa;

public class IteratorElement implements Iterator {
 private List list;//列表
 private Position nextPosition;//當前(下一個)元素的位置

//預設構造方法
 public IteratorElement() { list = null; }

//構造方法
 public IteratorElement(List L) {
  list = L;
  if (list.isEmpty())//若列表為空白,則
   nextPosition = null;//當前元素置空
  else//否則
   nextPosition = list.first();//從第一個元素開始
 }

//檢查迭代器中是否還有剩餘的元素
 public boolean hasNext() { return (null != nextPosition); }

//返回迭代器中的下一元素
 public Object getNext() throws ExceptionNoSuchElement {
  if (!hasNext()) throw new ExceptionNoSuchElement("意外:沒有下一元素");
  Position currentPosition = nextPosition;
  if (currentPosition == list.last())//若已到達尾元素,則
   nextPosition = null;//不再有下一元素
  else//否則
   nextPosition = list.getNext(currentPosition);//轉向下一元素
  return currentPosition.getElem();
 }
}

 

 

/*
 * 基於列表實現的位置迭代器
 */

package dsa;

public class IteratorPosition implements Iterator {
 private List list;//列表
 private Position nextPosition;//當前(下一個)位置

//預設構造方法
 public IteratorPosition() { list = null; }

//構造方法
 public IteratorPosition(List L) {
  list = L;
  if (list.isEmpty())//若列表為空白,則
   nextPosition = null;//當前位置置空
  else//否則
   nextPosition = list.first();//從第一個位置開始
 }

//檢查迭代器中是否還有剩餘的位置
 public boolean hasNext() { return (nextPosition != null); }

//返回迭代器中的下一位置
 public Object getNext() throws ExceptionNoSuchElement {
  if (!hasNext()) throw new ExceptionNoSuchElement("意外:沒有下一位置");
  Position currentPosition = nextPosition;
  if (currentPosition == list.last())//若已到達尾位置,則
   nextPosition = null;//不再有下一個位置
  else//否則
   nextPosition = list.getNext(currentPosition);//轉向下一位置
  return currentPosition;
 }
}

 

 

/**
 * 基於列表實現的樹迭代器
 */

package dsa;

public class IteratorTree implements Iterator {
 private List list;//列表
 private Position nextPosition;//當前(下一個)元素的位置

//預設構造方法
 public IteratorTree() { list = null; }

//前序走訪
 public void elementsPreorderIterator(TreeLinkedList T) {
  if (null == T) return;//遞迴基
  list.insertLast(T);//首先輸出當前節點
  TreeLinkedList subtree = T.getFirstChild();//從當前節點的長子開始
  while (null != subtree) {//依次對當前節點的各個孩子
   this.elementsPreorderIterator(subtree);//做前序走訪
   subtree = subtree.getNextSibling();
  }
 }

//後序遍曆
 public void elementsPostorderIterator(TreeLinkedList T) {
  if (null == T) return;//遞迴基
  TreeLinkedList subtree = T.getFirstChild();//從當前節點的長子開始
  while (null != subtree) {//依次對當前節點的各個孩子
   this.elementsPostorderIterator(subtree);//做後序遍曆
   subtree = subtree.getNextSibling();
  }
  list.insertLast(T);//當所有後代都訪問過後,最後才訪問當前節點
 }

//層次遍曆
 public void levelTraversalIterator(TreeLinkedList T) {
  if (null == T) return;
  Queue_List Q = new Queue_List();//空隊
  Q.enqueue(T);//根節點入隊
  while (!Q.isEmpty()) {//在隊列重新變空之前
   TreeLinkedList tree = (TreeLinkedList) (Q.dequeue());//取出隊列首節點
   list.insertLast(tree);//將新出隊的節點接入迭代器中
   TreeLinkedList subtree = tree.getFirstChild();//從tree的第一個孩子起
   while (null != subtree) {//依次找出所有孩子,並
    Q.enqueue(subtree);//將其加至隊列中
    subtree = subtree.getNextSibling();
   }
  }
 }

//檢查迭代器中是否還有剩餘的元素
 public boolean hasNext() { return (null != nextPosition); }

//返回迭代器中的下一元素
 public Object getNext() throws ExceptionNoSuchElement {
  if (!hasNext()) throw new ExceptionNoSuchElement("No next position");
  Position currentPosition = nextPosition;
  if (currentPosition == list.last())//若已到達尾元素,則
   nextPosition = null;//不再有下一元素
  else//否則
   nextPosition = list.getNext(currentPosition);//轉向下一元素
  return currentPosition.getElem();
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.