Data structure and algorithm analysis----binary tree + preorder + inorder + postorder traversal

Binary tree overview

Know the singly linked list, each node of the singly linked list can only have one successor node. A linked list, assuming that each node can have no more than two successor nodes, then it can be called a binary tree. At this time, the nodes in this binary tree are called nodes, and the successor nodes of the binary tree are called It is called a child node, and it has left and right points. The left child node of a node is the left child node, and the right child node is the right child node.
like:
insert image description hereFrom Baidu Encyclopedia
Each letter here represents a node
A binary tree does not have to be implemented by a linked list. It is a data structure and a structure. As long as it has this structure, it can be called a binary tree.
Baidu Encyclopedia:
Binary tree (Binary tree) is an important type of tree structure. The data structure abstracted from many practical problems is often in the form of a binary tree. Even a general tree can be easily converted into a binary tree, and the storage structure and algorithm of the binary tree are relatively simple, so the binary tree is particularly important. The characteristic of a binary tree is that each node can only have at most two subtrees, and there are left and right points.
Related terms:
① Node: Contains a data element and some information pointing to subtree branches.
② Degree of a node: The number of subtrees a node has is called the degree of a node.
③Leaf node: also known as a terminal node, a node without a subtree or a node with zero degree.
④Branch node: also known as non-terminal node, the node whose degree is not zero is called non-terminal node.
⑤ Degree of the tree: the maximum value of the degrees of all nodes in the tree.
⑥The level of nodes: starting from the root node, assuming that the root node is the first layer, the child nodes of the root node are the second layer, and so on, if a node is located in the L layer, its child nodes Located on level L+1.
⑦The depth of the tree: also known as the height of the tree, the maximum level of all nodes in the tree is called the depth of the tree.
⑧Ordered tree: If the order of each subtree in the tree is sequential, the tree is called an ordered tree.
⑨Unordered tree: If the order of each subtree in the tree is not in order, the tree is called an unordered tree.
10Forest: A forest is composed of m (m≥0) disjoint trees. If the root node of a non-empty tree is deleted, the tree becomes a forest, and the trees in the forest are composed of the subtrees of the original root node.

Two special binary trees:
1. Full binary tree: If a binary tree has only nodes with degree 0 and nodes with degree 2, and the nodes with degree 0 are on the same level, then the binary tree is a full binary tree.
2. Complete binary tree: A binary tree with a depth of k and n nodes is called complete if and only if each node corresponds to the nodes numbered from 1 to n in a full binary tree with a depth of k. binary tree.
The characteristic of a complete binary tree is that leaf nodes can only appear on the two largest layers of the sequence, and the maximum sequence of descendants under the left branch of a node is equal to or greater than the maximum sequence of descendants under the right branch by 1.


To put it simply, a complete binary tree: the second-to-last layer of nodes is full, and there must be a node on the far left of the first-to-last layer, which is continuous from left to right. The so-called continuity means that there must be no empty space between the two leaf nodes. A binary tree is a complete binary tree, and a complete binary tree is not necessarily a full binary tree
insert image description hereFrom Baidu Encyclopedia
The definition of full binary tree in China is a little different from the definition of full binary tree in foreign countries, don’t worry about it

traverse

Traversal is one of the most basic operations on trees. The so-called traversal of a binary tree is to walk through all the nodes of the binary tree according to certain rules and order, so that each node is visited once and only once. Since the binary tree is a nonlinear structure, the traversal of the tree is essentially to convert each node of the binary tree into a linear sequence to represent.
There are three methods of traversal, preorder, inorder, and follow-up:
the difference between the three traversal methods lies in when the parent node is printed.
If the parent node is output first, it is the preorder.
If the input order is: child node, parent node, child node, inorder
If the input order is: child node, child node, parent node, then order
Here we use recursion to implement the traversal of the binary tree
The recursion of these traversals is really not easy to understand. Why can a whole tree be traversed? You can go through it with debug to understand.

First create a binary tree

We need to create a node class:
insert image description here
we create a slightly more complex tree, which is more impressive to traverse
Create a tree with this structure:
insert image description here
create it like this:
insert image description here

preorder traversal

What is preorder traversal?
In the process of recursion, first print the current node, then recurse to the left sub-node, the left recursion is completed, and insert image description here
then recurse to the right sub-node , print the current node, then recurse to the left child node, go to the left until the end of the left child node, that is, node==null, then start backtracking, exit this recursion, and then recurse to the right, until The right child node goes to the end, then backtracks, backtracks to the parent nodes of these nodes, and then recurses left and right under the root of the parent node, until finally exiting the entire recursive body, that is, the traversal is complete Print results: the
order
insert image description hereinsert image description here
is : 0, 1, 3, 4, 7, 9, 10, 11, 12, 13, 2, 5, 6, 8
indeed correct

Inorder traversal

What is in-order traversal? In the process of recursion, first recurse to the left, the left recursion is completed, then print the current node, and then recurse to the right child node
insert image description here
First recurse to the left, until the left node reaches the end, then print the current node, then recurse to the right on the basis of the current node, and then go back to the end, which is the same as the above preorder traversal, which is recursive Backtrack
insert image description hereinsert image description here
Print order: 3, 1, 9, 7, 11, 10, 13, 12, 4, 0, 5, 2, 6, 8, no problem

Subsequent traversal

What is post-order traversal? In the process of recursion, first recurse to the left, the left recursion is completed, recurse to the right, and the right recursion is completed to print the current node
insert image description here
It relies on printing during the backtracking process
First recurse to the left, go to the end, then recurse to the right based on the current node, print the current node at the end, and then backtrack
insert image description hereinsert image description here
Sequence: 3, 9, 11, 13, 12, 10, 7, 4, 1, 5, 8, 6, 2, 0

the code

package com.LYH.structure.Nonlinearity.Tree;

public class BinaryTreeTraversal {
    
    
    public static void main(String[] args) {
    
    
        BinaryTree tree=new BinaryTree();

//        创建14个结点
        Node[] Nodes=new Node[14];
        for (int i = 0; i < 14; i++) {
    
    
            Nodes[i]=new Node(i,"牛蛙"+i+"号");
        }
//        构建结构
        Nodes[0].setLeft(Nodes[1]);
        Nodes[0].setRight(Nodes[2]);
        Nodes[1].setLeft(Nodes[3]);
        Nodes[1].setRight(Nodes[4]);
        Nodes[2].setLeft(Nodes[5]);
        Nodes[2].setRight(Nodes[6]);
        Nodes[4].setLeft(Nodes[7]);
        Nodes[6].setRight(Nodes[8]);
        Nodes[7].setLeft(Nodes[9]);
        Nodes[7].setRight(Nodes[10]);
        Nodes[10].setLeft(Nodes[11]);
        Nodes[10].setRight(Nodes[12]);
        Nodes[12].setLeft(Nodes[13]);
        tree.setRoot(Nodes[0]);
        tree.Traversal();
}
class BinaryTree{
    
    
    private Node root;

    public Node getRoot() {
    
    
        return root;
    }

    public void setRoot(Node root) {
    
    
        this.root = root;
    }
//    三种遍历方式的区别在于父节点什么时候打印,
//    若先输出父节点,则是前序。
//    若输入顺序是:子节点 父节点 子节点   则中序
//    若输入顺序是:子节点 子节点 父节点   则后序

//    遍历
    public void Traversal(){
    
    
        if (root==null){
    
    
            System.out.println("树为空");
        }else {
    
    
//            PreorderTraversal(root); //前序
//            PostorderTraversal(root);//后序
            InfixTraversal(root); //中序
        }
    }
//    前序
    private void PreorderTraversal(Node node){
    
    
        if (node!=null){
    
    
            System.out.println(node);
            PreorderTraversal(node.getLeft());
            PreorderTraversal(node.getRight());
        }
    }

//    中序
    private void InfixTraversal(Node node){
    
    
        if (node!=null){
    
    
            InfixTraversal(node.getLeft());
            System.out.println(node);
            InfixTraversal(node.getRight());
        }
    }

//    后序
    private void PostorderTraversal(Node node){
    
    
        if (node!=null){
    
    
            PostorderTraversal(node.getLeft());
            PostorderTraversal(node.getRight());
            System.out.println(node);
        }
    }
}
class Node{
    
      //假设我们这个结点类存储用户的id和姓名(name)
    private int id;
    private String name;
    private Node left;  //左子结点
    private Node right;  //右子结点

    public Node(int id, String name) {
    
    
        this.id = id;
        this.name = name;
    }

    public Node() {
    
    
    }
    public void setId(int id) {
    
    
        this.id = id;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public void setLeft(Node left) {
    
    
        this.left = left;
    }
    public void setRight(Node right) {
    
    
        this.right = right;
    }
    public int getId() {
    
    
        return id;
    }
    public String getName() {
    
    
        return name;
    }
    public Node getLeft() {
    
    
        return left;
    }
    public Node getRight() {
    
    
        return right;
    }
    @Override
    public String toString() {
    
    
        return "Node{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Guess you like

Origin blog.csdn.net/qq_45821251/article/details/121453954