java set and data structure - binary tree

1, Tree (difficulty)
1. Hierarchy: from the definition of the root, the root is the first layer, and the child node of the root is the second layer, and so on.

The level of the tree is 4.
2. Height and depth of tree
Height: the number of edges of the longest simple path from this node to the leaf node. (number of paths + 1 or number of nodes (including its own nodes))
Depth: the number of edges of the longest simple path from the root node to the node.

2, Binary tree
1. Concept: a binary tree is a finite set of nodes, which is either empty, or a root node plus two binary trees called left subtree and right subtree
Tree composition.
Characteristics of binary tree:
(1) Each node has at most two subtrees, that is, the binary tree does not have nodes with a degree greater than 2.
(2) The subtree of a binary tree can be divided into left and right, and the order of its subtrees can not be reversed. Therefore, a binary tree is an ordered tree.
2. Properties of binary tree
(1) If the number of layers of the root node is 1, there are at most 2 ^ (i-1) (I > 0) nodes on layer I of a non empty binary tree
(2) If only the depth of the root node's binary tree is 1, the maximum number of nodes of the binary tree with depth K is 2 ^ k-1 (k > = 0)
(3) If the number of leaf nodes is n0 and the number of non leaf nodes with degree 2 is n2, then n0 = n2 + 1
(4) The depth k of a complete binary tree with n nodes is rounded on log2(n+1)
(5) The root node sequence number is 0. For a node with sequence number i:
The parent node is: (i-1) / 2
Left child node: 2i+1
Right child node: 2i+2
3. Storage method of binary tree:
(1) Sequential storage: mostly used for complete binary trees
(2) Linked storage similar to a linked list: applies to any tree
4. Traversal method of binary tree:
(1) Preorder traversal
(2) Medium order traversal
(3) Postorder traversal
(4) Sequence traversal
5. Basic operations of binary tree:

// Preorder traversal
    void preOrderTraversal(BTNode root) {
        if (root == null) return;
    // Medium order traversal
    void inOrderTraversal(BTNode root) {
        if (root == null) return;
    // Postorder traversal
    void postOrderTraversal(BTNode root) {
        if (root == null) return;
    // Traversal idea - find the number of nodes
    static int size = 0;
    void getSize1(BTNode root) {
        if (root == null) return;
    // Idea of subproblem - finding the number of nodes
    int getSize2(BTNode root) {
        if (root == null) return 0;
        return getSize2(root.left)+getSize2(root.right)+1;
    // Traversal idea - find the number of leaf nodes
    static int leafSize = 0;
    void getLeafSize1(BTNode root) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
    // Idea of subproblem - finding the number of leaf nodes
    int getLeafSize2(BTNode root) {
        if (root == null) return 0;
        if (root.left==null && root.right==null) {
            return 1;
        return getLeafSize2(root.left)+getLeafSize2(root.right);
    // Sub problem idea - find the number of nodes in layer k
    int getKLevelSize(BTNode root,int k) {
        if (root == null) return 0;
        if (k == 1) {
            return 1;
        return getKLevelSize(root.left,k-1)+getKLevelSize(root.right,k-1);
    // Gets the height of the binary tree
    int getHeight(BTNode root) {
        if (root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight>rightHeight?leftHeight+1:rightHeight+1;
    // Find the node where val is located. If it is not found, null will be returned
    // Search in the order of root - > left subtree - > right subtree
    // Once found, return immediately. There is no need to continue searching in other locations
    BTNode find(BTNode root, char val) {
        if (root == null) return null;
        if (root.val == val) {
            return root;
        BTNode ret = find(root.left,val);
        if (ret != null) {
            return ret;
        ret = find(root.right,val);
        if (ret != null) {
            return ret;
        return null;

Tags: Java data structure

Posted on Thu, 28 Oct 2021 08:05:36 -0400 by mitch.craig