乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java實(shí)現(xiàn)二叉樹啟遍歷的算法

       鳳舞天煌 2007-09-23
      java實(shí)現(xiàn)二叉樹啟遍歷的算法
       
      public class TreeNode1 { //二叉樹的結(jié)點(diǎn)類
      public String data; //數(shù)據(jù)元數(shù)
      public TreeNode1 left,right; //指向左,右孩子結(jié)點(diǎn)的鏈

      public TreeNode1(){
      this("?");
      }

      public TreeNode1(String d){ //構(gòu)造有值結(jié)點(diǎn)
      data = d;
      left = right = null;
      }

      public void preorder(TreeNode1 p){ //先根次序遍歷二叉樹
      if(p!=null){
      System.out.print(p.data+" ");
      preorder(p.left);
      preorder(p.right);
      }
      }

      public void inorder(TreeNode1 p){ //中根次序遍歷二叉樹
      if(p!=null){
      inorder(p.left);
      System.out.print(p.data+" ");
      inorder(p.right);
      }
      }

      public void postorder(TreeNode1 p){ //后根次序遍歷二叉樹
      if(p!=null){
      postorder(p.left);
      postorder(p.right);
      System.out.print(p.data+" ");
      }
      }
      }
       
      下面來個更為完整詳細(xì)的:
      public class BinaryNode {
      Object element;
      BinaryNode left;
      BinaryNode right;

      }


      import java.util.*;

      public class Queue {

      protected LinkedList list;

      // Postcondition: this Queue object has been initialized.
      public Queue() {

      list = new LinkedList();

      } // default constructor

      // Postcondition: the number of elements in this Queue object has been
      // returned.
      public int size() {

      return list.size();

      } // method size

      // Postcondition: true has been returned if this Queue object has no
      // elements. Otherwise, false has been returned.
      public boolean isEmpty() {

      return list.isEmpty();

      } // method isEmpty

      // Postconditon: A copy of element has been inserted at the back of this
      // Queue object. The averageTime (n) is constant and
      // worstTime (n) is O (n).
      public void enqueue(Object element) {

      list.addLast(element);

      } // method enqueue

      // Precondition: this Queue object is not empty. Otherwise,
      // NoSuchElementException will be thrown.
      // Postcondition: The element that was at the front of this Queue object -
      // just before this method was called -- has been removed
      // from this Queue object and returned.
      public Object dequeue() {

      return list.removeFirst();

      } // method dequeue

      // Precondition: this Queue object is not empty. Otherwise,
      // NoSuchElementException will be thrown.
      // Postcondition: the element at index 0 in this Queue object has been
      // returned.
      public Object front() {

      return list.getFirst();

      } // method front

      } // Queue class


      import java.io.IOException;


      public class BinaryTree {
      BinaryNode root;


      public BinaryTree() {
      super();
      // TODO 自動生成構(gòu)造函數(shù)存根
      root=this.createPre();
      }

      public BinaryNode createPre()
      //按照先序遍歷的輸入方法,建立二叉樹
      {
      BinaryNode t=null;
      char ch;
      try {
      ch = (char)System.in.read();

      if(ch==‘ ‘)
      t=null;
      else
      {
      t=new BinaryNode();
      t.element=(Object)ch;
      t.left=createPre();
      t.right=createPre();
      }
      } catch (IOException e) {
      // TODO 自動生成 catch 塊
      e.printStackTrace();
      }
      return t;
      }

      public void inOrder()
      {
      this.inOrder(root);
      }

      public void inOrder(BinaryNode t)
      //中序遍歷二叉樹
      {
      if(t!=null)
      {
      inOrder(t.left);
      System.out.print(t.element);
      inOrder(t.right);
      }
      }

      public void postOrder()
      {
      this.postOrder(root);
      }

      public void postOrder(BinaryNode t)
      //后序遍歷二叉樹
      {
      if(t!=null)
      {
      postOrder(t.left);
      System.out.print(t.element);
      postOrder(t.right);
      }
      }

      public void preOrder()
      {
      this.preOrder(root);
      }
      public void preOrder(BinaryNode t)
      //前序遍歷二叉樹
      {
      if(t!=null)
      {
      System.out.print(t.element);
      preOrder(t.left);
      preOrder(t.right);
      }
      }

      public void breadthFirst()
      {
      Queue treeQueue=new Queue();
      BinaryNode p;
      if(root!=null)
      treeQueue.enqueue(root);
      while(!treeQueue.isEmpty())
      {
      System.out.print(((BinaryNode)(treeQueue.front())).element);
      p=(BinaryNode)treeQueue.dequeue();
      if(p.left!=null)
      treeQueue.enqueue(p.left);
      if(p.right!=null)
      treeQueue.enqueue(p.right);
      }
      }
      }


      public class BinaryTreeTest {

      /**
      * @param args
      */
      public static void main(String[] args) {
      // TODO 自動生成方法存根
      BinaryTree tree = new BinaryTree();

      System.out.println("先序遍歷:");
      tree.preOrder();
      System.out.println();

      System.out.println("中序遍歷:");
      tree.inOrder();
      System.out.println();

      System.out.println("后序遍歷:");
      tree.postOrder();
      System.out.println();

      System.out.println("層次遍歷:");
      tree.breadthFirst();
      System.out.println();
      }

      }

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多