Abstract: daily life, many things can be described by trees, such as the catalog of books, the organizational structure of the work unit, and so on. The tree is a very important data structure in the computer, and the tree storage method can improve the efficiency of data storage and reading.

This article is shared from Huawei Cloud Community " Creation] If you want to know about binary trees, this article is enough ", author: liuzhen007.

Preface

In daily life, many things can be described by trees, such as the catalog of books, the organizational structure of the work unit, and so on. The tree is a very important data structure in the computer, and the tree storage method can improve the efficiency of data storage and reading.

1. Basic definition of tree

In daily life, many things can be described by trees. Trees are a very important data structure in computers. Tree storage methods can improve the efficiency of data storage and reading. For example, the use of binary sorting trees can guarantee the data Retrieval speed, at the same time, can also guarantee the speed of data insertion, deletion, and modification.
image.png

In fact, there are many types of trees, such as ordinary binary trees, complete binary trees, full binary trees, trail binary trees, Huffman trees, binary sort trees, balanced binary trees, AVL balanced binary trees, red-black trees, B trees, B+ trees, Heap and so on. The main content introduced today is the basic knowledge of binary trees and several basic types of binary trees.

Second, the related terms of the binary tree

Before the formal lecture, first introduce some professional terms and terms about binary trees, including nodes, node degrees, leaf nodes, branch nodes, node levels, tree degrees, tree depths, etc., to understand these basics The professional terms and terminology are very important for us to understand the characteristics of the binary tree.

1) Node: Contains a data element and a number of information pointing to the branch of the subtree.

2) The degree of the node: the degree of a node with the data of the subtree becomes the node.

3) Leaf nodes: also called terminal nodes, nodes without subtrees or nodes with zero degree.

4) Branch node: also called non-terminal node, the node whose degree is not zero becomes non-terminal node.

5) Node level: starting from the root node, the level of the root node is 1, the immediate successor level of the root is 2, and so on.

6) Tree degree: the maximum value of the degree of all nodes in the tree.

7) The depth of the tree: the maximum level of nodes in the tree.

1. The characteristics of the tree

As a special data structure, tree has many characteristics, such as:

1) Each node has multiple or zero sub-nodes

2) The node without the parent node becomes the root node, and the node without child nodes becomes the leaf node

3) Each non-root node has only one parent node

4) Each node and its descendants can be regarded as a tree as a whole, called a subtree with the current node as the root
image.png

2. The basic definition of a binary tree

1) A binary tree is a tree whose degree does not exceed 2, and each node has at most two child nodes

2) The nodes of the binary tree are divided into left and right nodes
image.png

3. Full Binary Tree

1) The node degree of each layer of the binary tree reaches the maximum value, then the binary tree is a full binary tree

2) A full binary tree with depth n, with 2^n-1 nodes
image.png

4. Complete Binary Tree

Leaf nodes can only appear in the lowermost layer and the next lower layer. The leaf nodes of the last layer are continuous on the left, and the leaf nodes of the penultimate layer are continuous on the right. We call it a complete binary tree.
image.png

Three, the creation of the binary tree

Next, we use code to describe the binary tree. The language uses Java as an example. The node class is defined as follows:
image.png

The binary search tree class is defined as follows:
image.png

After the relevant classes are defined, let's look at the specific method implementation, which will be introduced separately below.

1. size() method

The size() method is relatively simple. Each time you add an element, add one, and delete an element minus one. You can maintain a public variable num. The code is implemented as follows:
image.png

2. put (Key key, Value value) method

The put (Key key, Value value) method can use the overloaded method put (Node x, Key key, Value value), so the implementation is relatively simple. The first parameter only needs to be passed to the root node. The code implementation is as follows:
image.png

3. put (Node x, Key key, Value value) method

The put (Node x, Key key, Value value) method should be relatively complex to implement in the entire class. A simple analysis is given below.

In the first case, there is no node in the current tree, and the newly inserted node is directly used as the root node.
image.png

In the second case, if the current tree is not empty, start from the root node. This situation can be subdivided into three situations:

1) If the key of the new node is less than the key of the current node, continue to search for the left child node of the current node.
image.png

2) If the key of the new node is greater than the key of the current node, continue to find the right child node of the current node.
image.png

3) If the key of the new node is equal to the key of the current node, then such a node already exists in the tree, just replace the value of the node.
image.png

The specific code implementation is as follows:
image.png

4. get (Key key) method

The get (Key key) method is similar to the put (Key key, Value value) method, and it can also be implemented using the overloaded method get (Node x, Key key). The code implementation is as follows:
image.png

5. get (Node x, Key key) method

The get (Node x, Key key) method implements the query method starting from the root node. If the key to be queried is less than the key of the current node, continue to find the left child node of the current node; if the key to be searched is greater than the current node Point’s key, then continue to find the right child node of the current node; if the key to be found is equal to the key of the current node, return the value of the current node.

The specific code implementation is as follows:
image.png

Through the above code, we can see that the get() method is similar to the put() method, and both are implemented through recursive calls.

6. delete (Key key) method

The delete (Key key) method is the same idea, just call the overloaded method behind, the code implementation is as follows:
image.png

7. delete (Node x, Key key) method

The implementation idea of the deletion method, taking the most complicated case as an example, first find the deleted node, find the smallest node minNode in the right subtree of the deleted node, delete the smallest node in the right subtree, and let it be deleted The left subtree of the node becomes the left subtree of the minimum node minNode, the right subtree of the deleted node becomes the right subtree of the minimum node minNode, and the parent node of the deleted node points to the minimum node minNode.

The specific code implementation is as follows:
image.png
image.png

Now that the code has been written, let's verify it through the code to see if we have written it correctly:
image.png

Answer output:

3

Li Si

2

Nice, great, the above output has proved that the program is correct.

Fourth, find the smallest and largest keys in the binary tree

For example, the binary tree is used to record the salary and employee name data of a certain company, or the ranking and name data of students in a certain class. How to quickly find the data of the highest and lowest ranked classmates?

In this case, what should we do? In fact, the following four methods can generally be sorted out, which are defined as follows:
image.png

1. min() method

The min() method is similar to the get() and put() methods mentioned above, and can also be implemented using the following overloaded methods. The specific code is as follows:

// 找出树中最小的键
public key min() {
    return min(root).key;
}

2. min (Node x) method

The min (Node x) method needs to find the smallest node in the left subtree according to the incoming node position. If it is empty, it will return empty directly. The specific code implementation is as follows:

// 找出树中最小键所在的结点
public Node min(Node x) {
    if (x == null) {
        return x;
    }
    Node minNode = x;
    while (minNode.left != null) {
        minNode = minNode.left;
    }
    return minNode;
}

3. max() method

The max() method is similar to the min() method mentioned above, and can also be implemented using the following overloaded methods. The specific code is as follows:

// 找出树中最小的键
public key max() {
    return max(root).key;
}

4. max (Node x) method

The max (Node x) method needs to find the largest node in the right subtree according to the incoming node position. If it is empty, it will directly return empty. The specific code implementation is as follows:

// 找出树中最大键所在的结点
public Node min(Node x) {
    if (x == null) {
        return x;
    }
    Node maxNode = x;
    while (maxNode.right != null) {
        maxNode = maxNode.right;
    }
    return maxNode;
}

Five, the traversal of the binary tree

There are three ways to traverse the binary tree, which are pre-order traversal, middle-order traversal, and post-order traversal.

1. Preorder traversal

Visit the root node first, then the left subtree, and finally the right subtree, such as the binary tree in the figure below. The results of the preorder traversal are as follows:

EBADCGFH。

2. Mid-order traversal

Visit the left subtree first, then the root node, and finally the right subtree, such as the binary tree in the figure below. The results of the middle-order traversal are as follows:

ABCDEFGH。

3. Post-order traversal

Visit the left subtree first, then the right subtree, and finally the root node, such as the binary tree in the figure below. The results of the post-order traversal are as follows:

ACDBFHGE。
image.png

in conclusion

The binary tree not only has very important research significance in basic data structure, but also has very important application scenarios in practical applications. It takes into account the advantages of conventional data structure arrays and linked lists, while avoiding the inherent shortcomings of the two. The data structure abstracted from many practical problems is often in the form of a binary tree, so that the storage structure and algorithm characteristics of the binary tree are used, so it is very necessary to learn the binary tree. I hope that the introduction of this article can help you understand and master the binary tree in depth.

Click to follow and learn about Huawei Cloud's fresh technology for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量