Sequential storage binary tree

Sequential storage binary tree

Basic instructions

From the point of view of data storage, and storage array storage interchangeable tree

       数组    《------》    树

Requirements: When traversing the array ARR, still before preorder, preorder and postorder traversal way to accomplish nodes.

concept

  1. Binary Tree is usually stored in order to consider only complete binary tree
  2. The left child node of the n-th element is 2 * n + 1;
  3. The right child node of the n-th element is 2 * n + 2;
  4. Parent node of the n-th (n - 1) / 2;
  5. n is represented as a binary tree of several elements (Note: number of starts counting from 0)

Icon:
Icon

Applications

Eight heap sort algorithm to sort, use is sequentially stored as a tree.

Implemented by sequentially stored binary tree traversal three kinds

  1. Preorder traversal:
  • Analysis of ideas:
    • Determine whether the tree is empty
    • First output root node,
    • In determining the left child is empty, the left child node performing recursive output
    • Final decision right child node is empty, output is performed recursively
    • No return on
  • Code:
public void preOrder(){
    this.preOrder(0);
}
//重载方法,是程序看起来更加干净简洁
/**\
 *
 * @param index  标识为数组的下标
 *
 */
public void preOrder(int index){
    if (arr == null || arr.length == 0){
        //有没有数组长度为0,还不为空的数组
        //经过实验发现,确实存在如此蛋疼的数组
        //两种情况,数组未初始化;已经初始化了,但是长度为0
        System.out.println("树为空,无法遍历");
        return;
    }
    System.out.println(arr[index]);
    if (2 * index + 1 < arr.length){
        //下标小于数组的长度,说明没有越界
        preOrder(2 * index + 1);
    }
    if (2 * index + 2 < arr.length){
        preOrder(2 * index + 2);
    }

}
  1. Preorder
  • Ideas analysis
    • First it is determined whether the tree is empty, comprising two situations: the length of the array is initialized or not initialized to 0
    • Determine whether the child node left empty, without recursive traversal is empty
    • Output current node
    • In determining whether a right child node is empty, not empty during recursive traversal
    • Completely exit the current statement did not return
  • Code:
 public void midOrder(){
        this.midOrder(0);
    }
public void midOrder(int index){
        //判定树是否存在或者是未初始化
        if (arr == null || arr.length == 0){
            System.out.println("the tree is empty");
            return;
        }
        if (2 * index + 1 < arr.length){
            midOrder(2 * index + 1);
        }
        System.out.print(arr[index]  + "---");
        if(2 * index + 2 < arr.length){
            midOrder(2 * index + 2);
        }
        return;
    }
  1. Postorder
  • Ideas analysis
    • First it is determined whether the array is empty or initialized to zero length
    • Left child node is empty, is not empty, recursive calls
    • Right child node is empty, is not empty, recursive calls
    • Output current node
  • Code:
 public void postOrder(){
        this.postOrder(0);
    }
  public void postOrder(int index){
        if(arr == null || arr.length == 0){
            System.out.println("树为空,无法遍历");
            return;
        }
        if (2 * index + 1 < arr.length){
            postOrder(2 * index + 1);
        }
        if (2 * index + 2 < arr.length){
            postOrder(2 * index + 2);
        }
        System.out.print(arr[index] + "---");
    }

Analysis and summary

  1. Familiar with basic binary tree is sequentially stored based on a relationship between the lower left on the table proficient
  2. Will be used to return the return value is no way to set the terminal outlet
  3. Three kinds of recursive tree traversal contrast, is the use of this.left! = Null, if left child using an empty basis to determine whether a leaf node. Sequential storage binary tree structure array subscripts used to determine whether the bounds
  4. A binary tree structure is determined sequential storage array is empty, two special cases, one is uninitialized array, another array is initialized, but the length is 0
Published 36 original articles · won praise 3 · Views 1770

Guess you like

Origin blog.csdn.net/Blackoutdragon/article/details/104045171