Data Structure Java Edition Binary Tree (Basic Knowledge)

What tree is this?

            binary tree in nature 

Let's take a look at what's going on with this tree

content

1. Tree

1. Related concepts

2. Representation of the tree

2. Binary tree

1. Concept

2. Two special binary trees

3. Properties of Binary Trees

4. Binary tree storage

5. Basic operations of binary tree

① Preamble

② Traversal of binary tree

(1) Pre-middle-post-order traversal

(2) Layer order traversal


1. Tree

1. Related concepts

1. The concept of tree

A tree is a nonlinear data structure, which consists of n (n>=0) finite nodes to form a set with hierarchical relationship. It is called a tree because it looks like an upside-down tree, which means it has the roots up and the leaves down.

Its characteristics (you can think about it in combination with the following figure):

①There is a special node, called the root node, the root node has no predecessor node

②Except the root node, the remaining nodes are divided into M (M > 0) sets T1, T2, ..., Tm that do not intersect with each other, each of which is set Ti (1 <= i <= m) Another tree-like subtree. The root node of each subtree has one and only one predecessor, and can have 0 or more successors

③ The tree is defined recursively.

2. The degree of a node: the number of subtrees contained in a node; (as shown in the figure above, the degree of the node of A is: 3)

3. The degree of the tree: In a tree, the maximum value of the degree of all nodes is called the degree of the tree; (as shown in the figure above, the degree of the tree is: 3)

4. Leaf node (terminal node): node with degree 0; (as shown in the figure above, J, F, K, L, H, I are all leaf nodes)

5. Parent node (parent node): If a node contains a child node, this node is called the parent node of its child node; (As shown above, A is the parent node of B, C, D ) 

6. Child node (child node): The root node of the subtree contained in a node is called the child node of the node; (as shown above, B, C, D are the child nodes of A)

7. Root node: a node in a tree without a parent node; (as shown in the figure above, A)

8. The level of nodes: starting from the definition of the root, the root is the first level, the child nodes of the root are the second level, and so on (as shown in the figure above, the level of B is 2) 

9. The height (depth) of the tree: the maximum level of nodes in the tree; (as shown above, the height of the tree is 4)

2. Representation of the tree

   The tree structure is more complicated than the linear table, and it is more troublesome to store and represent. In practice, there are many representations of trees, such as: parent representation, child representation, child parent representation, child sibling representation, etc. Here we simply understand the most commonly used child brother notation .

class Node {
int value; // 树中存储的数据
Node firstChild; // 第一个孩子引用
Node nextBrother; // 下一个兄弟引用
}

The structure diagram is shown in the following figure:

2. Binary tree

1. Concept

   Binary tree: A binary tree is a finite set of nodes, the set: ① or empty

                 ②. Or it consists of a root node plus two binary trees called left subtree and right subtree. 

Features: ① The degree of any node in the binary tree does not exceed 2

           ②The binary tree is an ordered tree with left and right subtrees (as shown in the figure above, the left subtree and right subtree of the binary tree can be seen respectively)

           ③ Schematic diagram of five situations of any binary tree

 2. Two special binary trees

①Full binary tree: A binary tree, if the number of nodes in each layer reaches the maximum value, then the binary tree is a full binary tree. That is, if a binary tree has K levels and the total number of nodes is , then it is a full binary tree.

②Complete binary tree: A complete binary tree is a highly efficient data structure, and a complete binary tree is derived from a full binary tree. For a binary tree of depth K with n nodes, it is called complete if and only if each node corresponds to a node numbered from 0 to n-1 in a full binary tree of depth K. Binary tree. It should be noted that a full binary tree is a special kind of complete binary tree

 3. Properties of binary trees (important!)

The following properties can be derived by drawing:

①If the number of layers of the root node is specified as 1, then there are at most 2^(i-1)(i>0) nodes on the i-th layer of a non-empty binary tree

②If it is specified that the depth of a binary tree with only the root node is 1, the maximum number of nodes of a binary tree with a depth of K is 2^k-1 (k>=0)

③ For any binary tree, if the number of leaf nodes is n0 and the number of non-leaf nodes with degree 2 is n2, then n0=n2+1

④ The depth k of a complete binary tree with n nodes is log2(n+1) [base 2, logarithm of n+1] rounded up 

⑤ For a complete binary tree with n nodes, if all nodes are numbered from 0 in the order from top to bottom and left to right, then for the node with sequence number i: If i>0, the parent sequence number: ( i-1)/2; i=0, i is the root node number, if there is no parent node, 2i+1

4. Binary tree storage

   Classification of binary tree storage: sequential storage and linked storage similar to a linked list.

    The chain storage of the binary tree is referenced through one node, and the common representations include binary and ternary representations.

Binary notation:

/ 孩子表示法
class Node {
int val; // 数据域
Node left; // 左孩子的引用,常常代表左孩子为根的整棵左子树
Node right; // 右孩子的引用,常常代表右孩子为根的整棵右子树
}

Trident notation:

class Node {
int val; // 数据域
Node left; // 左孩子的引用,常常代表左孩子为根的整棵左子树
Node right; // 右孩子的引用,常常代表右孩子为根的整棵右子树
Node parent; // 当前节点的根节点
}

① Preamble

Create a simple binary tree and quickly enter the learning of binary tree operations (this is not the real creation method of binary numbers, the real creation method will be described later, the purpose here is for everyone to better understand and master)

//二叉树是用每个结点构成的,写一个结点的类来专门进行存放
class BTNode{
    public char val;
    public BTNode left;//对于左孩子的引用
    public BTNode right;//对于右孩子的引用
    public BTNode(char val){
        this.val=val;
    }
}
public class BinaryTree {
    public BTNode root;//属于二叉树的根,而不是节点的根,因此不写到上面的类
    public BTNode createTree(){
        BTNode A=new BTNode('A');
        BTNode B=new BTNode('B');
        BTNode C=new BTNode('C');
        BTNode D=new BTNode('D');
        BTNode E=new BTNode('E');
        BTNode F=new BTNode('F');
        BTNode G=new BTNode('G');
        BTNode H=new BTNode('H');
        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        E.right=H;
        return A;//因为A为根
    }

}

 ② Traversal of binary tree (emphasis!)

(1) Pre-middle-post-order traversal

 a. The principle of traversal: It means to visit each node in the tree once and only once along a certain search route .

b. Traversal method: traverse through recursion

c. Schematic diagram of traversal : 

d. Detailed description:

① Preorder traversal:

  // 前序遍历
    //先根   再左     再右
    void preOrder(BTNode root){
        if(root==null) return ;
        System.out.println(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

②In-order traversal:

 // 中序遍历
    void inOrder(BTNode root){
        if(root==null) return ;
        inOrder(root.left);
        System.out.println(root.val+" ");
        inOrder(root.right);
    }

③ Post-order traversal:

// 后序遍历
    void postOrde(BTNode root){
        if(root==null) return ;
        postOrde(root.left);
        postOrde(root.right);
       System.out.println(root.val+" ");
    }

(2) Layer order traversal

①Concept: Set the level of the root node of the binary tree to 1. The level-order traversal is to start from the root node of the binary tree, first visit the root node of the first level, and then visit the nodes on the second level from left to right, and then It is the node of the third layer, and so on. The process of accessing the nodes of the tree layer by layer from left to right is layer order traversal.

②Example: 

 

Guess you like

Origin blog.csdn.net/weixin_58850105/article/details/122547547