Super hard core! An article thoroughly understands [binary tree] and the three pre-order, middle-order and post-order traversal

The concept and characteristics of trees

1 The concept of tree

  • The tree is a linear structure similar to a linked list, but it is also a typical non-linear structure; the tree has a strong hierarchy. Compared with the linear structure, the tree has lower time complexity and higher efficiency; readers can contact, Trees seen in life;

2 Tree terms

  • First look at a picture of a tree as follows, remove the arrows and related terms in the figure, the tree is a non-linear hierarchy ;

Insert picture description here
The relevant terms of the tree are as follows:

  • Root node : a node without a parent node, node A above;
  • Sibling nodes : child nodes with the same parent node; for example, F and G are sibling nodes;
  • Leaf node : ; 没有孩子节点的节点such as D, F, G, I, J;
  • Ancestors and grandchildren : If there is a path from root node A to node J, and node E appears on the path, then E is said to be the ancestor node of node J, and J is the grandchild of E; ABEH can be considered as J Ancestor node
  • Node size : The size of a node refers to the node 拥护的孙节点个数, including itself; for example, the size of the E node is 4;
  • Node depth : refers to the root node to the node 路径长度; for example (ABD), the depth of the two chains, that is, the D node is 2;
  • Node height : refers to the node to the deepest node 路径长度; for example (E-H -J), two chains, that is, the height of the E node is 2;
  • The level of the tree : the set with the same depth is called the level of the tree; for example, B and C; for example, D, E, F, G
  • The height and depth of the tree: The depth of the tree refers to the maximum depth of all nodes, and the height of the tree refers to the maximum height of all nodes;树的高度等于树的深度

3 Types of trees

  • Binary tree: If each node in a tree has 0, 1 or 2 nodes, the tree is called a binary tree; therefore, the empty tree is also a binary tree;

  • Strict binary tree: Each node of the tree has left or right children or no children;

Insert picture description here

  • Oblique tree: Each node of the oblique tree has only one child; if each node of the oblique tree has only left children, it is called a left oblique tree; if each node of the oblique tree has only right children, it is called right oblique tree;

Insert picture description here

  • Full binary tree: All parent nodes have left and right children, and all leaf nodes are on the same layer, so this kind of tree is called a full binary tree

Insert picture description here

  • Complete binary tree: For a binary tree with n nodes, it is numbered according to the hierarchy , and the left and right children are numbered according to the left child and then the right child . If the node with the number i and the node with the number i in the full binary tree of the same depth are in the binary tree The position of is exactly the same , then this binary tree is called a complete binary tree; therefore, a full binary tree must be a complete binary tree, otherwise it is not true

Insert picture description here

2.4 The nature of full binary trees

  • The number of nodes of the full fork tree: Assuming that the level of the full binary tree is k, according to the mathematical induction method and the formula of the proportional sequence, we can get 2 ^ 0 + 2 ^ 1 +… + 2 ^ k = 2 ^ (k + 1)- 1; The derivation process is as follows;

  • The number of leaf nodes of a full binary tree: According to the structure of a full binary tree, the k-th layer is the layer where the leaf nodes are located, so the number of leaf nodes is 2 ^ k

Insert picture description here

Binary tree implementation

The structure of a binary tree

  • According to the structure of the binary tree, each node can be assumed to have left and right children, which can correspond to left and right pointers, and each node can store values; therefore, after abstraction through object-oriented programming ideas The class is as follows
/**
 * <p>二叉树的结构 </p>
 */
public class TreeNode {

    // 左孩子
    private TreeNode leftNode;
    // 右孩子
    private TreeNode rightNode;
    // 存储值
    private Object value;
    // 构造方法
    TreeNode(Object value){
    this.value = value;
    }
   // 省略 set get 
}   
  • Now we need to implement the following full binary tree;

Insert picture description here

The idea is as follows:

  • First the root node stores 1; then the left child 2 and the right child 3 are stored respectively;
  • Secondly, the left child node is used as the parent node, and then the left child 4 and the right child 5 are stored respectively;
  • Finally, the right child node serves as the parent node and then stores the left child 6 and the right child 7 respectively;
  • The code is implemented as follows:
 public static void main(String[] args) {
        // 初始化树
        TreeNode tree = initTree();

    }
    public static TreeNode initTree(){
        // 创建7个节点
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        TreeNode treeNode6 = new TreeNode(6);
        TreeNode treeNode7 = new TreeNode(7);
        // 根据上面思路对节点进行组装
        // 组装根节点
        treeNode1.setLeftNode(treeNode2);
        treeNode1.setRightNode(treeNode3);
        // 组装左孩子
        treeNode2.setLeftNode(treeNode4);
        treeNode2.setRightNode(treeNode5);
        // 组装右孩子
        treeNode3.setLeftNode(treeNode6);
        treeNode3.setRightNode(treeNode7);
        return treeNode1;
    }

The traversal and realization of binary tree

  • The traversal of the binary tree is divided into pre-order traversal, middle-order traversal, and subsequent traversal; assuming that the current node is C (Current Node), the left node is L, and the right node is R;
前序遍历:C----->L------->R
中序遍历:L----->C------->R
后续遍历:R----->C------>L

Implementation of preorder traversal

Ideas:

  • First visit the current node;
  • Second visit the left node;
  • The last visited node;
  • Back to the previous picture, the preorder traversal CLR is 1, 2, 4, 5, 3, 6, 7;
 public static void main(String[] args) {
        // 初始化树
        TreeNode tree = initTree();
        // 调用先序遍历
        preOrderTree(tree);
    }
    /**
     * <p> 先序遍历</p>
     * @Param [rootNode]
     * @Return void
     */
    public static void preOrderTree(TreeNode rootNode){
        if (rootNode!=null){
            // 值
            System.out.println(rootNode.getValue());
            // 左孩子
            preOrderTree(rootNode.getLeftNode());
            // 右孩子
            preOrderTree(rootNode.getRightNode());
        }

    }
  • Output
1
2
4
5
3
6
7
  • The pre-order traversal implementation is a linear implementation with a time complexity of O (n)

Implementation of in-order traversal

Ideas:

  • First visit the left node
  • Second visit the current node
  • Last visit the right node
  • Back to the previous figure, the result of sequential traversal is 4, 2, 5, 1, 6, 3, 7
 public static void main(String[] args) {
        // 初始化树
        TreeNode tree = initTree();
        // 调用中序遍历
        middleOrderTree(tree);
    }
    /**
     * <p> 中序遍历</p>
     * @Param [rootNode]
     * @Return void
     */
    public static void middleOrderTree(TreeNode rootNode){
        if (rootNode!=null){
            // 左孩子
            middleOrderTree(rootNode.getLeftNode());
            // 值
            System.out.println(rootNode.getValue());
            // 右孩子
            middleOrderTree(rootNode.getRightNode());
        }

    }
  • Output
4
2
5
1
6
3
7
  • The middle-order traversal implementation is a linear implementation with a time complexity of O (n)

Implementation of subsequent traversal

Ideas:

  • First visit the left node
  • Second visit the right node
  • Last visit the current node
  • Back to the previous figure, the result of sequential traversal is 4, 5, 2, 6, 7, 3, 1
 public static void main(String[] args) {
        // 初始化树
        TreeNode tree = initTree();
        // 调用后续遍历
        postOrderTree(tree);
    }

    /**
     * <p>后续遍历 </p>
     * @Param [rootNode]
     * @Return void
     */
    public static void postOrderTree(TreeNode rootNode){
        if (rootNode!=null){
            // 左孩子
            postOrderTree(rootNode.getLeftNode());
            // 右孩子
            postOrderTree(rootNode.getRightNode());
            // 值
            System.out.println(rootNode.getValue());
        }

    }
  • Output
4
5
2
6
7
3
1
  • The post-order traversal implementation is a linear implementation with a time complexity of O (n)
Published 89 original articles · Liked145 · Visits130,000 +

Guess you like

Origin blog.csdn.net/weixin_43122090/article/details/105528557