[PHP data structure] tree and binary tree

Time:2021-10-15

The concept of tree is actually very broad and common. Don’t be alarmed when you see this word, because you can see the application of tree structure in our life every day. For example, the organizational structure of the company:

https://imgs.developpaper.com/imgs/1575433-20210725112949870-1944718477.png

In addition, our family tree, or our family structure, is also a typical tree structure. In addition, in the computer field, the [folders] we deal with every day and the data we store in the database are typical applications of trees. Today we are going to learn the definitions of tree and binary tree in comparative partial theory and some of their properties.

tree

From the above examples in real life, we can see that the tree structure can summarize some of its characteristics.

A tree is a finite set of n (n > 0) nodes, which is either an empty tree (n = 0); Or non empty tree t.

In this definition, we need to clarify two problems: one is that the tree must have nodes, and the other is that it can be divided into empty tree and non empty tree according to the number of nodes. However, this is only the most basic definition. It also has some features.

There is and only one node called root.

In other words, the tree must be extended from a node, which is the same as the tree root. From it, branches and leaves spread outward.

Other nodes except the root node can be divided into m (M > 0) disjoint finite sets T1, T2,…, TM, in which each set itself is a tree and is called the subtree of the root

This paragraph may not be easy to understand. In fact, it means that each node has only one parent node and cannot have multiple parent nodes. Similarly, there can be no connection between peer nodes, but it can have multiple child nodes.

For the definition of tree, we can see the following figure.

https://imgs.developpaper.com/imgs/1575433-20210725112950143-813830112.png

The above figure simply lists what standard trees and non-standard trees look like. Of which:

  • (a) , is a tree with only one root node. As long as there is one node, it can be called a tree structure

  • (b) , is a standard tree structure

  • (c) Note that there is a connecting line between its C and H nodes. This is not a tree. A node with only one parent node is called a tree. In fact, this is the [graph] we will learn in the future

Terms related to trees

Compared with the stack pressing (in) and out of the stack, and the queue in and out of the queue, the related terms of the tree are much more complex. Anyway, first of all, you have to remember these terms, otherwise the use of those terms will only make you more dizzy. However, it doesn’t matter if we can’t remember it for a moment. First, we have a general impression. If we encounter it in the later learning process, we will come back to review. In this way, the impression will be more profound.

  • Node: a node may contain a set of data or branches to other nodes, which can be regarded as the place where the branches branch, (b) a, B, C, D, e, etc. in the figure are nodes

  • Degree of node: the number of subtrees owned by a node is called the degree of node. In fact, it is the degree of its lower child nodes. (b) in the figure, the degree of node C is 1 and the degree of node D is 3

  • Degree of tree: the maximum degree of each node in the tree is the degree of the tree with the most child nodes. (b) the degree of the tree in the graph is 3

  • Leaf: the node with degree 0, that is, the node without child nodes, (b) k, l, F, G, m, I and j in the graph are the leaf nodes of the tree

  • Parents and children: the child of a node is its child; For these child nodes, the current node is its parents. (b) in the figure, D’s children are h, I and j, and h, I and J’s parents are d

  • Level: from the root node, the root node is the first level, and the children of the root are the second level, and so on. (b) the level of node g in the figure is 3, (a) all levels of the figure are only 1

  • Depth (height) of the tree: the maximum level of the current tree. Obviously, (b) the depth of the graph is 4

  • Brothers, ancestors and descendants: brother node is that the parents of these nodes are the same node; The ancestor node is all the nodes passing on the road from the root node to a specified node; Descendants are all the nodes on the way from a certain node to the target node. (b) In the figure, e and F are brothers, e’s ancestors are a and B, and E’s descendants are k or L

  • Cousins: all nodes on the same floor with different parents are cousins. Similarly, in figure (b), the cousins of G include E and F, and h, I and j are also cousins

Binary tree

After we have a certain understanding of the concept of tree, we will further learn another concept, which is also the most important form of tree in data structure and algorithm: binary tree.

Generally speaking, the shape of a tree can vary greatly. For example, a node can have three child nodes, while another node may have 300 child nodes. In fact, the operation of a tree without any rules will be very troublesome, and the definition of a binary tree is much simpler. In addition to the nature of the tree, it also has one more content: each node of the binary tree has only two child nodes at most, that is, the degree of the whole binary tree must be 2, and the degree of all nodes will not exceed 2. As for why binary trees are easy to operate, we will explain it in detail in the properties of binary trees in the next section. All tree structures can be transformed into binary trees through certain rules.

We also show what a binary tree is through a diagram.

https://imgs.developpaper.com/imgs/1575433-20210725112950368-1314156153.png

In a binary tree, the left child node and its descendants can be regarded as the left child tree of the current node. The right node and its descendants can also be regarded as the right subtree of the current node. According to the child nodes of the node, there are several binary tree forms in the above figure.

  • (a) A tree is a tree with only one node, or a binary tree with only one node

  • (b) A tree is a binary tree with only one left node

  • (c) A tree is a binary tree with only one right node

  • (d) A tree is a binary tree with left and right nodes

Properties of binary tree

Property 1: there are at most 2 on the i-th layer of binary treei-1Nodes (I > = 1)

Property 2: a binary tree with depth K has at most 2k-1 node (k > = 1)

Property 3: for any binary tree T, if the number of terminal nodes is n0, the number of nodes with degree 2 is n2, then n0 = n2 + 1

Property 4: the depth of a complete binary tree with n nodes is log2n + 1

Property 5: if for a complete binary tree with n nodes (its depth is log)2The nodes of N + 1) are numbered according to sequence (from layer 1 to layer 2)2N + 1 layers, each layer from left to right), then for any node i (1 < = I < = n), there are:

  1. If I = 1, node I is the root of binary tree and has no parents; If I > 1, its parents are node i / 2

  2. If 2I > N, node i has no left child (node I is a leaf node); Otherwise, the left child is node 2I

  3. If 2I + 1 > N, node i has no right child; Otherwise, its right child is node 2I + 1

We won’t elaborate on the mathematical proof of the above five properties of binary tree. After all, the purpose of our series of articles is to let you learn the essence of data structure and algorithm through simple examples, rather than simply and roughly using mathematical formulas to deduce and prove directly, so let’s count directly on the graph.

https://imgs.developpaper.com/imgs/1575433-20210725112950640-876925034.png

  • For property 1, according to the formula, the current binary tree can only have 2 at most on layer 33-1Four nodes, that is, four nodes. There can be at most 2 on the 4th floor4-1, which is 23=8 nodes

  • For property 2, the depth of the tree in the current graph is 4, that is, there are at most 24-1 = 15 nodes

  • In property 3, we first count the number of nodes with degree 2. In this graph, there are 7 nodes with degree 2, that is, a, B, C, D, e, F and g. the nodes in layer 4 have no child nodes, that is, they are all 0 degrees, also known as terminal nodes (leaf nodes). The number of these nodes is 8 in total. Now n2=7. According to the property formula, n can be obtained0 = n2 + 1 = 7 + 1 = 8

  • The number of nodes in the graph is 15, and the log can be obtained by applying the formula of property 42n + 1 = log215 + 1 = 3.91 (rounded down) + 1 = 3 + 1 = 4, the depth of the current tree is 4, and property 4 and property 2 can be regarded as complementary

  • For property 5, please pay attention to the number on the edge of each node, and we will select e node as an example. Node e is currently 5, so its parents are 5 / 2 = 2 (rounded down); The left child of E is 2I, that is, 2 * 5 = 10, and the right child of E is 2I + 1, that is, 2 * 5 + 1 = 11; The definition of property 5 is more abstract, and leaf nodes are used to explain it. It is aimed at the whole binary tree, but the meaning is the same as what we explain here. You can take other nodes to verify it. Property 5 is very important for us to use sequential structure to store binary tree!

Please be sure to master and remember these five properties and their meanings of binary tree, because in the later learning, you may come into contact with the contents of the above five properties, whether it is the order of binary tree, chain storage structure, or the traversal of binary tree. It can be said that they are the most basic souls in binary tree learning.

forest

Finally, let’s simply understand what is “forest”. Many trees put together to form a “forest”. Just like the explanation diagram of binary tree above, (a) (b) (c) (d) put together and look at them as a whole, it is a “forest”, in which there are (a) (b) (c) (d) these four trees respectively. There is no connection between trees in the forest. If we want to operate or traverse a forest, we often turn the forest into a tree. The specific algorithms and steps are not the focus of our study, so let’s have a look. Students who want to study deeply can search relevant contents or consult relevant teaching materials.

summary

When you move from the stack and queue to the tree, do you suddenly feel that you have taken a big step? A little confused? It doesn’t matter. Today’s content is actually some basic theoretical content. If you can understand it, you can understand it. If you can’t understand it, you can continue to study and then come back to today’s concepts. Learning does not constantly repeat the process of progress. Of course, everything should be based on the foundation. After you understand the data structure of the tree and some simple traversal algorithms, come back and deeply understand these concepts and recite them. I believe that the questions related to the tree in the general interview are nothing to say. Let’s work together!

reference material:

Data structure, Second Edition, Yan Weimin

Data structure, Second Edition, Chen Yue

High score notes on data structure, 2020 edition, tianqin postgraduate entrance examination

===============

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532