# Tree structure series (I): from ordinary tree to binary search tree

Time：2021-9-14 The article begins with “Chen Shu Yi” official account and personal blog shuyi.tech.

Tree structure is a very important type of data structure. This paper will start with the most basic common tree structure, extend to binary tree, and then to binary lookup tree. Through this idea, let us build the most basic knowledge link about the tree.

## Common tree

Tree is a nonlinear data structure. It is a structure in which data elements are organized according to branch relationships, much like trees in nature. The official definition of a tree is: a tree is a finite set of n (n > 0) elements, where:

1. Each element is called a node.
2. There is a specific node, called the root node or root.
3. Except for the root node, the other nodes are divided into m (M > = 0) disjoint finite sets, and each subset is a tree (called the subtree of the original tree).

There are several important concepts about trees, which are briefly introduced here:

• degree

Degree is the number of branches of a node. For example, node a in the tree above has three child nodes, so we call the degree of node a 3.

• arrangement

The hierarchy of nodes indicates the position of nodes in the book. The level of the root node is 1, and the level of other nodes is equal to the level times of its parent node plus 1. For example, the hierarchy of node f in the above figure is 3.

• depth

The depth of the tree is the maximum level of each node of the tree. For example, if the maximum level of the node in the above figure is K / L / m, the depth of the tree is any level of K / L / m, that is, the depth of the tree is 4.

• route

For any two different nodes in a subtree, if you start from one node and follow branches from top to bottom, there is a path between them. The path can be represented by the sequence of nodes that the path passes through. The length of the path is equal to the number of nodes on the path minus 1.

For example, the path from a to L in the above figure is: a > b > e > L, the number of path nodes is 4, and its length is 3.

## Binary tree

In fact, binary tree is based on the ordinary tree and the degree limit of the tree, that is, each node can only have two child nodes at most.Binary trees have five basic forms:

1. Empty binary tree – as shown in Figure a.
2. A binary tree with only one root node — as shown in Figure B.
3. Only the left subtree — as shown in Figure C.
4. Only the right subtree — as shown in Figure D.
5. Complete binary tree — as shown in Figure E. Binary tree is a simple and important tree. Many other types of trees are based on binary tree. According to the different situations of leaf nodes, we can subdivide the binary tree into: complete binary tree, complete binary tree and full binary tree.

Perfect binary tree, which means that the degree of all non leaf nodes is 2 (only if you have children, you must have two children). Complete binary tree, refers to a binary tree with depth K and N nodes if and only if each node corresponds to the nodes numbered from 1 to N in the full binary tree with depth K. In short, a complete binary tree is a subset of a full binary tree.In short, a complete binary tree is a non leaf node that has two child nodes and must be in the order from left to right and from top to bottom. Full binary tree, which means that a binary tree has only nodes with degree 0 and degree 2, and the nodes with degree 0 are on the same layer, then the binary tree is a full binary tree.In short, all leaf nodes are on the same layer, and each layer is covered with nodes. ## Binary lookup tree

I have learned so much about tree structure, but there is no practical application. I can only say that I am laying the foundation and understanding the basic concepts. Binary search tree can be said to be a very practical data structure, which can be used to quickly find elements.

Binary search tree (BST), some call it binary sort tree. Its average search efficiency is O (logn), and its worst search efficiency is O (n) (degenerated into linked list). The main reason why it can achieve such a fast search speed is its limitation on the order of data storage.

Binary lookup tree is defined as:

1. If the left subtree is not empty, the values of all nodes on the left subtree are less than the values of its root node.
2. If the right subtree is not empty, the values of all nodes on the right subtree are greater than those of its root node.
3. The left and right subtrees are also binary sort trees
4. There are no nodes with equal key values

According to the definition of binary search tree above, it is not difficult for us to draw the following binary search tree. You can see that the 7 elements of the root node are larger than the 3 elements on the left and smaller than the 11 elements on the right. The left and right subtrees 3 and 11 also conform to this law. According to such element sorting, to find an element, you only need to find the depth of the tree at most. For example, to find element 5, our search path is: 7 > 3 > 5, which only needs to be searched 3 times. If we want to find 4, our search path is: 7 > 3 > 5 > 4. We only need to find 4 times. According to the relationship between the depth of the binary tree and the number of nodes, we can calculate that the depth of the binary lookup tree is O (logn), that is, the time complexity of the binary lookup tree is O (logn).

The efficiency of binary search tree in query is greatly improved, which is tens of thousands of times higher than that of linked list. Especially when the amount of data is increasing, its improved performance is more terrible. For example, we have 100 million elements. If we use linked list storage, it needs to be compared 50 million times on average. Using binary search tree storage, we only need to compare it 27 times. 50 million times compared with 27 times, the difference is 1.85 million times!

But binary lookup tree also has a problem, that is, it will degenerate into a linked list in extreme cases. For example, we have such a group of numbers: 30, 20, 10 and 1. After they form a binary lookup tree, they will degenerate into an ordinary linked list. So how to make up for the defect of binary search tree? This involves the concept of tree balance. According to the idea of tree balance, the sages created the concept of balanced binary tree and created classic data structures such as AVL tree and red black tree. Let’s continue to talk about balanced binary tree next time.

## summary

Today we introduce the common tree structure and its related basic concepts. Then we introduce the very basic binary tree structure, and then extend it to complete binary tree, complete binary tree and full binary tree. Finally, the binary search tree structure and its existing problems are introduced. From today’s article, we can draw some conclusions:

• Binary tree is a special tree structure, which means that it has only two nodes at most.
• A complete binary tree is a binary tree with 2 nodes for non leaf nodes.
• The complete binary tree is based on the complete binary tree, with the restriction of nodes from left to right and from top to bottom.
• The full binary tree is based on the complete binary tree, plus the restriction that the nodes of each layer are full.
• Binary search tree can realize the time complexity of O (logn), but it will degenerate into a linked list.

So far, we have built the tree structure we have learned, and we can draw the following tree structure Avenue. ## OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]