This article mainly uses java to implement the creation of complete binary tree and the recursive traversal algorithm of binary tree.
Focus on the creation of complete binary trees, recursive algorithms are easier.
Creation of a Complete Binary Tree
Before you create a complete binary tree, you first need to understand some of its properties.
Property: If a node of a complete binary tree with n nodes is numbered hierarchically, there is a value for node i (0<=i<=n) at either level (note the value of i)
1. If i=0, the node is the root of a binary tree without parents. If i>0, the parent node is [i/2], rounding down
2. If 2i+1>n then node i has no left child, otherwise its left child is 2i+1
3. If 2i+2>n then the node has no right child, otherwise the right child is 2i+2
Node Creation
//Create nodes from a single class public class BTNode { //Define a node of a binary tree private int data; private BTNode lchild; private BTNode rchild; //Mainly for the initial purpose of the constructor public BTNode(){ } //New constructors, which can also be abbreviated public BTNode(int data ,BTNode lchild ,BTNode rchild){ this.data=data; this.lchild=lchild; this.rchild=rchild; } //Here is the set, get function for data lchild rchild public void setdata(int data){ this.data=data; } public int getdata(){ return data; } public void setlchild(BTNode lchild ){ this.lchild=lchild; } public BTNode getlchild(){ return this.lchild; } public void setrchild(BTNode rchild ){ this.rchild=rchild; } public BTNode getrchild(){ return this.rchild; }
Creation of a Complete Binary Tree
//What is the difference between List and Array List? public static List<BTNode> list=new ArrayList<BTNode>(); //Method of Array Tree public void creatTree(int[] array){ for(int i=0;i<array.length;i++){ BTNode btnode =new BTNode(array[i],null,null); list.add(btnode); } //Start with a list longer than 1 if(list.size()>0){ //The for loop is clever. Note the number of for loops. i represents each node with a child and 0 represents the root node. for(int i=0;i<array.length/2;i++){ if(2*i+1<list.size())//This cannot be equal because the length of the list must be greater than 2*i+1 or the array will go out of bounds list.get(i).setlchild(list.get(2 * i + 1)); if(2*i+2<list.size())// It can't be equal here either, because the array is out of bounds list.get(i).setrchild(list.get(2 * i + 2)); } } }
Access operation
//Operations to access arrays, which are just printing operations, or output public static void visit(BTNode btnode){ if(btnode!=null) System.out.print(btnode.getdata() + " ,"); }
Recursive traversal
//Recursive operation of sequential traversal public static void preorder(BTNode btnode) { if(btnode!=null){ visit(btnode); preorder(btnode.getlchild()); preorder(btnode.getrchild()); } } //Recursive operation of intermediate traversal public static void inorder(BTNode btnode){ if(btnode!=null){ inorder(btnode.getlchild()); visit(btnode); inorder(btnode.getrchild()); } } //Recursive operation of postorder traversal public static void afterorder(BTNode btnode){ if(btnode!=null){ afterorder(btnode.getlchild()); afterorder(btnode.getrchild()); visit(btnode); } }
Test Code
public static void main(String[] args) { int[] array = { 0,1, 2, 3, 4, 5, 6, 7, 8,9,10,11,12}; BuildTree demo = new BuildTree(); demo.creatTree(array); System.out.print("Binary trees are sorted in order:"); preorder(list.get(0)); System.out.println(); System.out.print("The middle order of a binary tree is:"); inorder(list.get(0)); System.out.println(); System.out.print("Binary trees are sorted in the following order:"); afterorder(list.get(0)); }
summary
I've coded all the ways I created them and the test results are correct for me to run all over again.
The core idea of creating a binary tree in this paper is to first have the data in a tree node and place the tree nodes in the arraylist array.Because of the nature of a complete binary tree, we can manipulate the nodes in the arraylist array, that is, separate the left and right nodes.