Java version sequential storage binary tree

Posted by mrbill501 on Sun, 27 Feb 2022 15:23:59 +0100

PS: This article is a reprint of the article. It will be more readable to read the original text. There is a link to the original text at the end of the article

catalogue

1. Sequential storage binary tree

1. Sequential storage binary tree

From the perspective of data storage, array storage mode and tree storage mode can be converted to each other, that is, array can be converted into tree and tree can also be converted into array; The sequential storage binary tree we are talking about here is usually a complete binary tree. Therefore, if we want to store an ordinary binary tree in sequence, we need to convert the ordinary binary tree into a complete binary tree in advance; For the definition of complete binary tree, you can see the data structure and algorithm of Java version (3); Sequential storage of binary tree refers to the use of sequential table (array) to store binary tree. The sequential storage of complete binary tree only needs to start from the root node and store the nodes in the tree into the array according to the hierarchy; OK, let's draw a binary tree that can be stored sequentially, as shown in Figure 1:

picture

Note: the white number indicates the value of the node, and the blue number indicates the number of the node.

The sequential storage of binary tree refers to the use of sequential table (array) to store binary tree. The sequential storage of complete binary tree only needs to start from the root node and store the nodes in the tree into the array according to the hierarchy? Take the complete binary tree in Figure 1 as an example. If we use an array to store the binary tree, the elements of the array are stored as array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}. Look, the white numbers are the elements of the array, and the blue numbers are the subscripts of the array. If the layer of the root node is the first layer and the child nodes of the root node are the second layer, Then the array will store the first layer, then the second layer, and then the next layer, and so on. Finally, the elements stored in the array are traversed in order. At this time, you should understand the text in double quotation marks.

Sequential storage binary tree has the following characteristics:

Note: n represents the first few elements in the binary tree (n starts from 0 and cannot be less than 0, and N represents the number of nodes, as shown in Figure 1)

1) Sequential binary trees usually only consider complete binary trees.

2) The left child node of the nth element is 2*n+ 1.

3) The right child node of the nth element is 2*n+ 2.

4) The parent node of the nth element is (n-1)/2.

Here we use code to realize one: that is, when the nodes of the sequential storage binary tree in Figure 1 are stored in the array, and then the array elements are output according to a certain law, we can also realize the pre order traversal, middle order traversal and post order traversal of the binary tree in Figure 1; For the traversal of binary tree, you can see the data structure and algorithm of Java version (IV).

(1) Write an ArrayBinaryTree class that converts an array into a binary tree traversal (pre order, middle order and post order):

public class ArrayBinaryTree {
private int[] array;// An array that stores data nodes
public ArrayBinaryTree(int[] array) {

 this.array = array;

}

public void preOrder() {

 preOrder(0);

}

//The output order of the array is transformed into the preorder traversal of the corresponding binary tree
private void preOrder(int index) {

 if (array == null || array.length == 0) {
   System.out.println("The array is empty and cannot be traversed before the binary tree");
   return;
 }
 
 //Output the current element
 System.out.println(array[index]);
 
 //Left recursive traversal
 if(index * 2 + 1 < array.length) {
   preOrder(index * 2 + 1);
 }
 
 //Right recursive traversal
 if(index * 2 + 2 < array.length) {
   preOrder(index * 2 + 2);
 }

}

public void middleOrder() {

 middleOrder(0);

}

//The output order of the array is transformed into the middle order traversal of the corresponding binary tree
private void middleOrder(int index) {

 if (array == null || array.length == 0) {
   System.out.println("The array is empty and cannot be traversed before the binary tree");
   return;
 }
 
 //Left recursive traversal
 if(index * 2 + 1 < array.length) {
   middleOrder(index * 2 + 1);
 }
 
 //Output the current element
 System.out.println(array[index]);
 
 //Right recursive traversal
 if(index * 2 + 2 < array.length) {
   middleOrder(index * 2 + 2);
 }

}

public void postOrder() {

 postOrder(0);

}

//The output order of the array is transformed into the post order traversal of the corresponding binary tree
private void postOrder(int index) {

 if (array == null || array.length == 0) {
   System.out.println("The array is empty and cannot be traversed before the binary tree");
   return;
 }
 
 //Left recursive traversal
 if(index * 2 + 1 < array.length) {
   postOrder(index * 2 + 1);
 }
 
 //Right recursive traversal
 if(index * 2 + 2 < array.length) {
   postOrder(index * 2 + 2);
 }
 
 //Output the current element
 System.out.println(array[index]);

}
}

Pre order traversal entry call test:

public static void main(String[] args) {

    int[] array = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    ArrayBinaryTree abt = new ArrayBinaryTree(array);
    System.out.println("Preorder traversal");
    abt.preOrder();

}

Log printing is as follows:

picture

Call test for traversing the entry in the middle order:

public static void main(String[] args) {

    int[] array = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    ArrayBinaryTree abt = new ArrayBinaryTree(array);
    System.out.println("Middle order traversal");
    abt.middleOrder();

}

Print the log as follows:

picture

Post order traversal entry call test:

public static void main(String[] args) {

    int[] array = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    ArrayBinaryTree abt = new ArrayBinaryTree(array);
    System.out.println("Postorder traversal");
    abt.postOrder();

}

Log printing is as follows:

picture

Topics: Java