java中二叉樹遍曆(遞迴) 程式碼

來源:互聯網
上載者:User

測試二叉樹遍曆,遞迴演算法

 代碼如下 複製代碼

 

    public class TestBinaryTree {
    public static void main(String[] args) {
    Node<String> g = new Node<String>("G", null, null);
    Node<String> e = new Node<String>("E", null, null);
    Node<String> f = new Node<String>("F", null, null);
    Node<String> d = new Node<String>("D", null, g);
    Node<String> b = new Node<String>("B", d, e);
    Node<String> c = new Node<String>("C", null, f);
    Node<String> a = new Node<String>("A", b, c);
    System.out.println("產生的二叉樹:");
    System.out.println("            A");
    System.out.println("            |     ");
    System.out.println("       |---------|");
    System.out.println("       B         C");
    System.out.println("       |         |");
    System.out.println("  |---------|     -----|");
    System.out.println("  D         E          F");
    System.out.println("  |");
    System.out.println("   ----|");
    System.out.println("       G");
    System.out.println("二叉樹深度:" + BinaryTree.getDepth(a));
    System.out.print("前序走訪:");
    BinaryTree.priorderTraversal(a);
    System.out.println();
    System.out.print("中序遍曆:");
    BinaryTree.inorderTraversal(a);
    System.out.println();
    System.out.print("後序遍曆:");
    BinaryTree.postorderTraversal(a);
    System.out.println();
    }
    }
    // 二叉樹
    class BinaryTree {
    // 前序走訪
    static <T> void priorderTraversal(Node<T> node) {
    if (node != null) {
    visitNode(node);
    priorderTraversal(node.getLeftChild());
    priorderTraversal(node.getRightChild());
    }
    }
    // 中序遍曆
    static <T> void inorderTraversal(Node<T> node) {
    if (node != null) {
    inorderTraversal(node.getLeftChild());
    visitNode(node);
    inorderTraversal(node.getRightChild());
    }
    }
    // 後序遍曆
    static <T> void postorderTraversal(Node<T> node) {
    if (node != null) {
    postorderTraversal(node.getLeftChild());
    postorderTraversal(node.getRightChild());
    visitNode(node);
    }
    }
    // 二叉樹深度
    static <T> int getDepth(Node<T> node) {
    if (node == null) {
    return 0;
    }
    int leftDepth = 0;
    int rightDepth = 0;
    leftDepth = getDepth(node.getLeftChild());
    rightDepth = getDepth(node.getRightChild());
    return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
    }
    // 訪問節點
    static <T> void visitNode(Node<T> node) {
    System.out.print(node.getKey() + " ");
    }
    }
    // 節點
    class Node<T> {
    private T key;
    private Node<T> leftChild;
    private Node<T> rightChild;
    public Node() {
    }
    public Node(T key, Node<T> leftChild, Node<T> rightChild) {
    super();  www.2cto.com
    this.key = key;
    this.leftChild = leftChild;
    this.rightChild = rightChild;
    }
    public T getKey() {
    return key;
    }
    public void setKey(T key) {
    this.key = key;
    }
    public Node<T> getLeftChild() {
    return leftChild;
    }
    public void setLeftChild(Node<T> leftChild) {
    this.leftChild = leftChild;
    }
    public Node<T> getRightChild() {
    return rightChild;
    }
    public void setRightChild(Node<T> rightChild) {
    this.rightChild = rightChild;
    }
    }

非遞迴演算法

當建立好二叉樹類後可以建立二叉樹執行個體,並實現二叉樹的先根遍曆,中根遍曆,後根遍曆,代碼如下:

 代碼如下 複製代碼

package package2;

public class BinaryTreePreorder {
 
 public static void preOrder(BinaryTree root){  //先根遍曆
  if(root!=null){
   System.out.print(root.data+"-");
   preOrder(root.left);
   preOrder(root.right);
  }
 }
 
 public static void inOrder(BinaryTree root){     //中根遍曆

  if(root!=null){
   inOrder(root.left);
   System.out.print(root.data+"--");
   inOrder(root.right);
  }
 }
 
 public static void postOrder(BinaryTree root){    //後根遍曆

  if(root!=null){
   postOrder(root.left);
   postOrder(root.right);
   System.out.print(root.data+"---");
  }
 }
 
 public static void main(String[] str){
  int[] array = {12,76,35,22,16,48,90,46,9,40};
  BinaryTree root = new BinaryTree(array[0]);   //建立二叉樹
  for(int i=1;i<array.length;i++){
   root.insert(root, array[i]);       //向二叉樹中插入資料
  }
  System.out.println("先根遍曆:");
  preOrder(root);
  System.out.println();
  System.out.println("中根遍曆:");
  inOrder(root);
  System.out.println();
  System.out.println("後根遍曆:");
  postOrder(root);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.