Binary tree of Java data structure and its source code implementation

Tree

Our data structure contains one-to-one, one-to-many, and many-to-many storage relationships.
Our commonly used one-to-one data structures: arrays and linked lists; one-to-many data structures: trees; many-to-many: graphs.
This article mainly introduces the commonly used structure of the tree, the binary tree.

binary tree

Basic characteristics of binary tree

  • have at most one root node
  • Each node can have at most two child nodes
  • Any two left and right subtrees of each node are binary trees

Binary tree contains three special forms: oblique tree, full binary tree, complete binary tree

inclined tree

  • When all the nodes of the binary tree have only the left subtree, the binary tree is called a left oblique tree ;
  • When all the nodes of the binary tree have only the right subtree, the binary tree is called a right oblique tree ;

full binary tree

  • Assuming that the depth of a binary tree is N, in this N layer, the nodes of each layer reach the maximum value, which is a full binary tree
  • The number of nodes in a full binary tree satisfies 2^n-1;

complete binary tree

  • Assuming that the depth of a binary tree is N, in the N-1 layer, the nodes of each layer have reached the maximum value, and in the Nth layer, all the nodes are located on the leftmost and continuous, it is called a complete binary tree

Code

//二叉树

class JZ26TreeNode{
    
    

    int val;//本结点存储数据
    JZ26TreeNode left;//存储左子树地址
    JZ26TreeNode right;//存储右子树地址

    //构造方法 传入结点值
    public JZ26TreeNode(int x){
    
    
        this.val = x;
    }

}

generate binary tree

public class JZ26 {
    
    
    public static void main(String[] args) {
    
    
        //二叉树 根节点
        JZ26TreeNode root = new JZ26TreeNode(3);
        //根节点左子树
        JZ26TreeNode left01 = new JZ26TreeNode(9);
        //根结点右子树
        JZ26TreeNode right01 = new JZ26TreeNode(20);
        //将根节点左右子树分别与根节点相关联
        root.left = left01;
        root.right = right01;
        
        //根节点右子树的左子树
        JZ26TreeNode right01Left  = new JZ26TreeNode(15);
        //根节点右子树的右子树
        JZ26TreeNode right01Right =  new JZ26TreeNode(7);
   
        //给根节点右子树分别关联
        right01.left = right01Left;
        right01.right = right01Right;
    }
}

Everyone should make up the binary tree in the picture above, here I will draw it for you

insert image description here

Print binary tree (breadth-first traversal)

After viewing various printing methods on the Internet, they are all implemented with the help of containers. First store and then print. It should be the most convenient way to use a queue. The queue is first in, first out.

//借助queue打印二叉树
    public static void main(String[] args) {
    
    
        //二叉树 根节点
        JZ26TreeNode root = new JZ26TreeNode(3);
        //根节点左子树
        JZ26TreeNode left01 = new JZ26TreeNode(9);
        //根结点右子树
        JZ26TreeNode right01 = new JZ26TreeNode(20);
        //将根节点左右子树分别与根节点相关联
        root.left = left01;
        root.right = right01;

        //根节点右子树的左子树
        JZ26TreeNode right01Left  = new JZ26TreeNode(15);
        //根节点右子树的右子树
        JZ26TreeNode right01Right =  new JZ26TreeNode(7);

        //给根节点右子树分别赋值
        right01.left = right01Left;
        right01.right = right01Right;

        binaryTree(root);

    }

//打印二叉树 广度优先遍历
    public static void binaryTree(JZ26TreeNode root){
    
    
        //生成队列 将根节点存储至队列
        Queue<JZ26TreeNode> queue = new LinkedList();
        queue.add(root);
        //queue为空 跳出循环
        while (!queue.isEmpty()){
    
    
            JZ26TreeNode poll = queue.poll();
            //队列头部元素取出 打印
            System.out.println("二叉树元素为 :"+poll.val);
            //将头部元素的左右结点分别添加至队列 (非空情况)
            if(poll.left!=null) queue.add(poll.left);
            if(poll.right!=null) queue.add(poll.right);
        }
    }

The printed result is:

insert image description here

Algorithm question

The basic binary tree is generated. Here we will do a binary tree algorithm problem; (Leetcode Jianzhi offer question 32)

从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回:

[3,9,20,15,7]

来源:力扣(LeetCode
链接:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Smart students have discovered that the binary tree of this question is what we have realized above. Let's start coding.

/*
先说一下思路 题目要求每层从上至下 从左到右顺序存储元素。
根据二叉树的存储规则 我们只能从根节点开始,每次拿到结点的左右结点(非空情况)
但是拿到的左右结点也有可能分别有子左右结点。故采取队列协助打印。
题目要求返回int[]  
上面我们已经学会打印了 那么这道题把打印变成存储int[] 即可。

*/  
    public static void main(String[] args) {
    
    
        //二叉树 根节点
        JZ26TreeNode root = new JZ26TreeNode(3);
        //根节点左子树
        JZ26TreeNode left01 = new JZ26TreeNode(9);
        //根结点右子树
        JZ26TreeNode right01 = new JZ26TreeNode(20);
        //将根节点左右子树分别与根节点相关联
        root.left = left01;
        root.right = right01;

        //根节点右子树的左子树
        JZ26TreeNode right01Left  = new JZ26TreeNode(15);
        //根节点右子树的右子树
        JZ26TreeNode right01Right =  new JZ26TreeNode(7);

        //给根节点右子树分别赋值
        right01.left = right01Left;
        right01.right = right01Right;

        int[] ints = binaryTree(root);
        for(int i: ints){
    
    
            System.out.println("数组元素为:"+i);
        }

    }

    public static int[] binaryTree(JZ26TreeNode root){
    
    
        //生成队列 将根节点存储至队列
        Queue<JZ26TreeNode> queue = new LinkedList();
        queue.add(root);
        List<Integer> res = new ArrayList<>();
        //queue为空 跳出循环
        while (!queue.isEmpty()){
    
    
            JZ26TreeNode poll = queue.poll();
            //取出头元素添加至list
            res.add(poll.val);

            //将头部元素的左右结点分别添加至队列 (非空情况)
            if(poll.left!=null) queue.add(poll.left);
            if(poll.right!=null) queue.add(poll.right);
        }
        int[] tar = new int[res.size()];
        //循环列表添加至数组
        for (int i = 0; i < res.size(); i++) {
    
    
            tar[i] = res.get(i);
        }
        return tar;
    }



Guess you like

Origin blog.csdn.net/pgcdnameming/article/details/124481715