Java中二叉樹資料結構的實現樣本_java

來源:互聯網
上載者:User

來看一個具體的習題實踐:

題目
根據二叉樹前序走訪序列例如:7,-7,8,#,#,-3,6,#,9,#,#,#,-5,#,#,構建二叉樹,並且用前序、中序、後序進行遍曆

代碼

 import java.util.Scanner;      public class BinaryTree {     public static String[] str;     public static int count;        /**      * 靜態內部類,定義二叉樹節點      */     static class TreeNode {       public String data;       TreeNode lchild;       TreeNode rchild;          public TreeNode(String x) {         this.data = x;       }     }        /**      * 根據前序序列遞迴構建二叉樹      *      * @return      */     public static TreeNode createBtree() {       TreeNode root = null;          if (count >= str.length || str[count++].equals("#")) {         root = null;       } else {         root = new TreeNode(str[count - 1]);         root.lchild = createBtree();         root.rchild = createBtree();       }          return root;     }        /**      * 前序走訪      *      * @param root      */     public static void preTraverse(TreeNode root) {       if (root != null) {         System.out.print(root.data + " ");         preTraverse(root.lchild);         preTraverse(root.rchild);       }     }        /**      * 中序遍曆      *      * @param root      */     public static void inTraverse(TreeNode root) {       if (root != null) {         inTraverse(root.lchild);         System.out.print(root.data + " ");         inTraverse(root.rchild);       }     }        /**      * 後序遍曆      *      * @param root      */     public static void postTraverse(TreeNode root) {       if (root != null) {         postTraverse(root.lchild);         postTraverse(root.rchild);         System.out.print(root.data + " ");       }     }        public static void main(String args[]) {       Scanner cin = new Scanner(System.in);          while (cin.hasNext()) {         String s = cin.nextLine();         str = s.split(",");            count = 0;            TreeNode root = createBtree();            // 前序走訪         preTraverse(root);         System.out.println();            // 中序遍曆         inTraverse(root);         System.out.println();            // 後序遍曆         postTraverse(root);         System.out.println();       }     }   }

二叉樹的深度

下面是是實現二叉樹的遞迴演算法的實現,其思想就是,若為空白,則其深度為0,否則,其深度等於左子樹和右子樹的深度的最大值加1:

class Node{ String name; Node left; Node right; public Node(String name) { this.name = name; } @Override public String toString() { return name; }}//定義二叉樹class BinaryTree{ Node root;  public BinaryTree(){ root = null; } //為了方便起見,我就直接寫個初始化的二叉樹,詳細的可以見以前的日誌 public void initTree(){  Node node1 = new Node("a"); Node node2 = new Node("b"); Node node3 = new Node("c"); Node node4 = new Node("d"); Node node5 = new Node("e"); root = node1; node1.left = node2; node2.right = node3; node1.right = node4; node3.left = node5; } //求二叉樹的深度 int length(Node root){ int depth1; int depth2; if(root == null) return 0; //左子樹的深度 depth1 = length(root.right); //右子樹的深度 depth2 = length(root.left); if(depth1>depth2)  return depth1+1; else  return depth2+1; }}public class TestMatch{ public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.initTree(); System.out.println(tree.length(tree.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.