An example of a binary tree data structure in Java

  • 2020-04-01 04:06:28
  • OfStack

Let's look at a specific exercise:

The title
According to binary tree preorder traversal sequence, for example: 7, 7, 8 -, #, #, and 3, 6,, # 9, #, #, #, - 5, #, #, building a binary tree, and use before order, in sequence, after the order of traversal

code


 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; 
      } 
    } 
   
     
    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; 
    } 
   
     
    public static void preTraverse(TreeNode root) { 
      if (root != null) { 
        System.out.print(root.data + " "); 
        preTraverse(root.lchild); 
        preTraverse(root.rchild); 
      } 
    } 
   
     
    public static void inTraverse(TreeNode root) { 
      if (root != null) { 
        inTraverse(root.lchild); 
        System.out.print(root.data + " "); 
        inTraverse(root.rchild); 
      } 
    } 
   
     
    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(); 
   
        //The former sequence traversal
        preTraverse(root); 
        System.out.println(); 
   
        //In the sequence traversal
        inTraverse(root); 
        System.out.println(); 
   
        //After the sequence traversal
        postTraverse(root); 
        System.out.println(); 
      } 
    } 
  }

The depth of the binary tree

The following is the implementation of the recursive algorithm of binary tree. The idea is that if is empty, its depth is 0; otherwise, its depth is equal to the maximum value of the depth of left subtree and right subtree plus 1:


class Node{
 String name;
 Node left;
 Node right;
 public Node(String name) {
 this.name = name;
 }
 @Override
 public String toString() {
 return name;
 }
}
//Define a binary tree
class BinaryTree{
 Node root;
 
 public BinaryTree(){
 root = null;
 }
 //For the sake of convenience, I'll just write an initial binary tree and see the previous log for details
 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;
 //Depth of the left subtree
 depth1 = length(root.right);
 //Depth of the right subtree
 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));
 }
}


Related articles: