Implementation example of binary tree data structure in Java _java

Source: Internet
Author: User

To see a specific exercise practice:

Topic
according to the sequence of the binary tree, such as: 7,-7,8,#,#,-3,6,#,9,#,#,#,-5,#,#, construct the binary tree, and use the preface, the middle order, the sequence to traverse

Code

 Import Java.util.Scanner; 
    public class BinaryTree {public static string[] STR; 
   
    public static int count; 
      /** * Static internal class, defined binary tree node/static class TreeNode {public String data; 
      TreeNode Lchild; 
   
      TreeNode Rchild; 
      Public TreeNode (String x) {this.data = x; 
      }/** * to construct a binary tree recursively according to the sequence of pre-sequence * * @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; 
      /** * Pre-sequence traversal * * @param root/public static void Pretraverse (TreeNode root) { 
        if (root!= null) {System.out.print (Root.data + ""); 
        Pretraverse (Root.lchild); PretraveRSE (Root.rchild); }/** * in sequence traversal * * @param root/public static void Intraverse (TreeNode root) 
        {if (root!= null) {intraverse (root.lchild); 
        System.out.print (Root.data + ""); 
      Intraverse (Root.rchild); }/** * Subsequent traversal * * @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 (); 
        Pre-sequence traversal pretraverse (root); 
   
        System.out.println (); 
        Middle sequence traversal intraverse (root); System.out.println (); 
        After the subsequent traversal posttraverse (root); 
      System.out.println ();
 } 
    } 
  }

The depth of a two-pronged tree

The following is the implementation of the two-fork tree recursive algorithm, the idea is that if it is empty, then its depth is 0, otherwise, its depth is equal to the Saozi right subtree depth of the maximum value plus 1:

 class node{String name;
 Node left;
 Node right;
 Public Node (String name) {this.name = name;
 @Override public String toString () {return name;
 
 }///define Binary tree class binarytree{Node root;
 Public BinaryTree () {root = null;
 For convenience, I'll write an initialized two-fork tree, which can be seen in detail in the previous log 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;
 }//Find the depth of the binary tree int length (Node root) {int depth1;
 int depth2;
 if (root = null) return 0;
 Zuozi depth depth1 = Length (root.right);
 The depth of the right subtree is 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)); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.