Basics of Binary Tree (Part 1)

  • First of all, what is a "tree"? No matter how complete the definition is, there is no intuitive picture. So I drew a few "trees" in the picture. Come and see, what are the characteristics of these "trees"?
    Insert picture description here
  • Each element in the tree is called a "node"; it is used to connect the relationship between adjacent nodes, and we call it a "parent-child relationship".
  • For example, in the picture below, node A is the parent node of node B, and node B is the child node of node A. The parent nodes of the three nodes B, C, and D are the same node, so they are called sibling nodes . We call the node without a parent node the root node , which is the node E in the graph. We call nodes without child nodes as leaf nodes or leaf nodes. For example, G, H, I, J, K, and L in the graph are all leaf nodes .
    Insert picture description here
  • In addition, there are three similar concepts regarding "trees": Height, Depth, and Level. Their definition is as follows:
    (1) The height of the node = the longest path from the node to the leaf node
    (2) The depth of the node = the number of edges experienced by the root node to this node
    (3) The number of layers of the node = the node The depth of +1
    (4) The height of the tree = the height of the root node
    Insert picture description here

Binary tree

Insert picture description here

  • Binary tree, as the name implies, each node has at most two child nodes, namely the left child node and the right child node. However, the binary tree does not require every node to have two children. Some nodes have only the left child node, and some have only the right child node.
  • In the above figure, in the binary tree numbered 2, all the leaf nodes are at the bottom. Except for the leaf nodes, each node has two left and right child nodes. This kind of binary tree is called a full binary tree.
  • In the binary tree number 3, the leaf nodes are in the bottom two layers, and the leaf nodes of the last layer are arranged to the left, and except for the last layer, the number of nodes in other layers must reach the maximum. This kind of binary tree is called a complete binary tree. .

Binary tree storage

To store a binary tree, there are two methods, one is the binary chain storage method based on pointers or references, and the other is the sequential storage method based on arrays.

  • Let's first look at the relatively simple and intuitive chain storage method. From the figure, you should be able to clearly see that each node has three fields, one of which stores data, and the other two are pointers to the left and right child nodes. As long as we hold the root node, we can string the entire tree through the pointers of the left and right child nodes. This storage method is more commonly used. Most of the binary tree code is implemented through this structure
    Insert picture description here

  • Let's look at the sequential storage method based on arrays. We store the root node at the subscript i = 1, the left child node is stored at the subscript 2 * i = 2, and the right child node is stored at 2 * i + 1 = 3. By analogy, the left child node of node B is stored at 2 * i = 2 * 2 = 4, and the right child node is stored at 2 * i + 1 = 2 * 2 + 1 = 5.
    Insert picture description here

  • However, the example I just gave is a complete binary tree, so only a storage location with a subscript of 0 is "waste". If it is an incomplete binary tree, it will actually waste a lot of array storage space, such as the following example.
    Insert picture description here

  • Array storage is more suitable for complete binary trees, and other types of binary trees using array storage will waste storage space.

Traversal of binary tree

  • Preorder traversal means that, for any node in the tree, print this node first, then print its left subtree, and finally print its right subtree.
  • In-order traversal means that for any node in the tree, first print its left subtree, then print itself, and finally print its right subtree.
  • Post-order traversal means that for any node in the tree, first print its left subtree, then print its right subtree, and finally print the node itself.
    Insert picture description here
  • The very important operations in the binary tree are pre-, middle, and post-order traversal operations. The time complexity of traversal is O(n).

Binary tree traversal code implementation

前序遍历的递推公式: preOrder(r) = print r->preOrder(r->left)->preOrder(r->right)

中序遍历的递推公式: inOrder(r) = inOrder(r->left)->print r->inOrder(r->right) 

后序遍历的递推公式: postOrder(r) = postOrder(r->left)->postOrder(r->right)->print r
void preOrder(Node* root) {
    
      
   if (root == null) return;  print root // 此处为伪代码,表示打印root节点  
   preOrder(root->left);  
   preOrder(root->right); 
} 
void inOrder(Node* root) {
    
      
   if (root == null) return;  
   inOrder(root->left);  
   print root // 此处为伪代码,表示打印root节点  
   inOrder(root->right); 
} 
void postOrder(Node* root) {
    
      
   if (root == null) return;  
   postOrder(root->left);  
   postOrder(root->right);  
   print root // 此处为伪代码,表示打印root节点 
} 

Guess you like

Origin blog.csdn.net/qq_36828822/article/details/104138896