Java implements binary insertion sorting tree insertion and in-order traversal (new breadth-first traversal, adding program code comments)

Yesterday, I saw the knowledge points about binary trees when I was writing questions at HackeRrank, and today I made a simple algorithm implementation. The insertion and traversal of the binary sorting tree are mainly implemented by recursive calls.

  • Binary sorting tree: a special form of binary tree, the left fork of the node is less than or equal to the node, and the right fork is greater than the number of nodes; the main advantage of this is that when performing in-order traversal, it can directly output from small to large. Order. (About the binary tree, you can go to Baidu search by yourself)
public class Tree {

    Tree treeLeft;
    Tree treeRight;//Creates two branches of the tree, declares the type of its own tree, the purpose is that each new branch is still a node of the tree
    int data;

    Tree(int data){
        this.data = data;//data here is the value of the tree node (or branch)
        treeLeft = treeRight = null;
    }  
}
public class TreeMain {

    /**
     * Binary sorted tree; the left is less than or equal to, and the right is greater than the root node.
     * @param root
     * @param data
     * @return
     */
    public static Tree insert(Tree root, int data) {

        if (root == null) {
            return new Tree(data);//New tree node
        }else {
            Tree cur;
            if (data <= root.data) {//Small on the left
                cur = insert(root.treeLeft,data);//Recursively until the root is empty, call the first IF to create a new tree node
                root.treeLeft = cur;
            }else {//The big one is placed on the right
                cur = insert(root.treeRight,data);
                root.treeRight = cur;
            }

            return root;
        }

    }


    /**
     * Recursive method to achieve in-order traversal, and print
     * @param root
     */
    public static void inOrder(Tree root){
        if(root == null) return;
        inOrder(root.treeLeft);//Use recursive method to find the leftmost side of the tree
        System.out.print(root.data + " ");
        inOrder(root.treeRight);
    }

    /**
     * The column method realizes the breadth-first traversal of the binary tree, and the queue follows the first-in-first-out rule, which is suitable for this method
     * @param root
     */
    public static void levelOrer(Tree root){
        Queue<Tree> queue = new LinkedList<Tree>();//New queue

        if(root != null){
            queue.add(root);//Add the root node to the queue
        }

        while (!queue.isEmpty()){
            Tree cur = queue.peek();//The purpose of creating cur is to bring the tree layer by layer during the while loop. If root is used directly, it will result in only one level tree output
            System.out.print(cur.data + " ");
            queue.remove();
            if (cur.treeLeft != null){
                queue.add(cur.treeLeft);//Add the left branch to the queue first, then output first
            }
            if(cur.treeRight != null){
                queue.add(cur.treeRight);
            }
        }

    }

    /**
     * main function, input and output, traversal
     * @param args
     */
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the number of numbers: ");
        int T = sc.nextInt();
        int[] t = new int[T];
        System.out.println("Please enter numbers, separated by spaces: ");
        for (int i = 0; i < T; i++) {
            t[i] = sc.nextInt();
        }

        Tree root = null;
        for (int i = 0; i < T; i++) {
            root = insert(root,t[i]);
        }

        inOrder(root);
        System.out.println("\n");
        levelOrer(root);

    }
}

The results are as follows:

Xiaobai got started, please bear with me!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325725353&siteId=291194637