java實現(xiàn)二叉樹啟遍歷的算法
public class TreeNode1 { //二叉樹的結點類
public String data; //數(shù)據(jù)元數(shù) public TreeNode1 left,right; //指向左,右孩子結點的鏈 public TreeNode1(){ this("?"); } public TreeNode1(String d){ //構造有值結點 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+" "); } } } 下面來個更為完整詳細的:
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 自動生成構造函數(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(); } } |
|