Data structure and algorithm: the basic part of the tree structure

Preface

This article mainly explains binary trees, sequential storage binary trees, and threaded binary trees


Data structure and algorithm article list

Data structure and algorithm article list: click here to jump to view


table of Contents

Insert picture description here


(1) Binary tree

(1) Why use trees

1) Analyze the advantages and disadvantages of the array.
Advantages : fast to query elements according to the index, can store a large amount of data, convenient to traverse the array according to the index,
disadvantages : slow to find elements according to the content, the size of the array cannot be changed once determined, the array can only store one type Data, adding, and deleting elements are slow, and no methods are encapsulated. All operations need to be defined by the user.
Array diagram:
Insert picture description here

2) Analyze the advantages and disadvantages of the linked list.
Advantages : It is convenient to add and delete.
Disadvantages : When looking for a value, it is necessary to traverse from the head node, which is relatively inefficient.
Operation diagram:
Insert picture description here

3) Analysis of tree storage methods
can improve the efficiency of data storage and reading.
Example: [7, 3, 10, 1, 5, 9, 12]
Insert picture description here


(2) Tree diagram

Insert picture description here
Common terms of tree (combined with schematic understanding):

  1. node
  2. Root node
  3. Parent node
  4. Child node
  5. Leaf nodes (nodes without child nodes)
  6. Node weight (node ​​value)
  7. Path (Find the route of the node from the root node)
  8. Floor
  9. Subtree
  10. Tree height (maximum number of layers)
  11. Forest: Multiple subtrees form a forest

(3) The concept of binary tree

  1. The ordered tree whose node degree is not greater than 2 is called a binary tree.
  2. The child nodes of the binary tree are divided into left and right nodes.
    Schematic diagram:
    Insert picture description here
  3. If a binary tree has only nodes with degree 0 and nodes with degree 2, and the nodes with degree 0 are on the same level, we call it a full binary tree.
    Insert picture description here
  4. A binary tree with a depth of k and n nodes is called a complete binary tree if and only if each of its nodes corresponds to the nodes numbered from 1 to n in the full binary tree of depth k.
    Insert picture description here

(4) Description of binary tree traversal

  1. Preorder traversal: output the parent node first, then traverse the left and right subtrees
  2. Mid-order traversal: first traverse the left subtree, then output the parent node, and then traverse the right subtree
  3. Post-order traversal: first traverse the left subtree, then traverse the right subtree, and finally output the parent node
  4. Summary: Look at the order of the output parent nodes to determine whether it is the pre-order, middle-order or post-order

(5) Binary tree traversal application examples (pre-order, middle-order, post-order)

Description and ideas of application examples
Insert picture description here
Binary tree traversal code (click the link below to view the source code)
Binary tree traversal application examples (pre-order, middle-order, post-order)


(6) Binary tree-find the specified node (pre-order, middle-order, post-order)

Claim:

  1. Please write the methods of pre-order search, middle-order search and post-order search.
  2. And use three search methods respectively to find nodes with heroNO = 5
  3. And analyze the various search methods, how many times have been compared
  4. Idea analysis diagram
    Insert picture description here
    Code (click the link below to view the source code):
    Binary tree-find the specified node (preorder, middle order, postorder)

(7) Binary tree-delete node

Claim:

  1. If the deleted node is a leaf node, delete the node
  2. If the deleted node is a non-leaf node, delete the subtree.
  3. Test, delete the No. 5 leaf node and No. 3 subtree.
  4. Complete the delete idea analysis
    Insert picture description here
    code (click the link below to view the source code):
    Binary Tree-Delete Node

(Two) sequential storage of binary trees

(1) The concept of sequential storage of binary trees

Basic instructions:
From the perspective of data storage, the array storage method and the tree storage method can be converted to each other, that is, an array can be converted into a tree, and a tree can also be converted into an array. See the schematic diagram on the right.
Insert picture description here
Claim:

  1. The node of the binary tree on the right requires an array to store arr: [1, 2, 3, 4, 5, 6, 6]
  2. It is required that when traversing the array arr, the nodes can still be traversed in the way of pre-order traversal, middle-order traversal and post-order traversal.

The characteristics of sequential storage binary tree:

  1. Sequential binary trees usually only consider complete binary trees
  2. The left child node of the nth element is 2 * n + 1
  3. The right child node of the nth element is 2 * n + 2
  4. The parent node of the nth element is (n-1) / 2
  5. n: Indicates the number of elements in the binary tree (start numbering with 0 as shown in the figure)

(2) Sequential storage of binary tree traversal

Requirements: Give you an array {1,2,3,4,5,6,7}, which requires traversal in a binary tree pre-order traversal. The result of the preorder traversal should be 1, 2, 4, 5, 3, 6, 7

public class ArrBinaryTreeDemo {
    
    
     public static void main(String[] args) {
    
    
          int[] arr = {
    
     1, 2, 3, 4, 5, 6, 7 };
          // 创建一个 ArrBinaryTree
          ArrBinaryTree arrBinaryTree = new  ArrBinaryTree(arr);
          arrBinaryTree.preOrder(); // 1,2,4,5,3,6,7
     }
}

// 编写一个ArrayBinaryTree, 实现顺序存储二叉树遍历
class ArrBinaryTree {
    
    
     private int[] arr;// 存储数据结点的数组
     public ArrBinaryTree(int[] arr) {
    
    
          this.arr = arr;
     }

     // 重载preOrder
     public void preOrder() {
    
    
          this.preOrder(0);
     }

     // 编写一个方法,完成顺序存储二叉树的前序遍历
     /**
      *
      * @param index 数组的下标
      */
     public void preOrder(int index) {
    
    
          // 如果数组为空,或者 arr.length = 0
          if (arr == null || arr.length == 0) {
    
    
              System.out.println("数组为空,不能按照二叉树的前序遍历");
          }
          // 输出当前这个元素
          System.out.println(arr[index]);
          // 向左递归遍历
          if ((index * 2 + 1) < arr.length) {
    
    
              preOrder(2 * index + 1);
          }
          // 向右递归遍历
          if ((index * 2 + 2) < arr.length) {
    
    
              preOrder(2 * index + 2);
          }
     }
}

result:

1
2
4
5
3
6
7

(3) Sequential storage binary tree application example

The heap sort in the eight sorting algorithms will use the sequential storage binary tree


(3) Threaded Binary Tree

(1) Problem

Construct the sequence {1, 3, 6, 8, 10, 14} into a binary tree. n+1=7
Insert picture description here
problem analysis:

  1. When we traverse the above binary tree in order, the number sequence is {8, 3, 10, 1, 6, 14}
  2. However, the left and right pointers of nodes 6, 8, 10, and 14 are not fully utilized.
  3. What if we want to make full use of the left and right pointers of each node so that each node can point to its own front and back nodes?
  4. Solution-clue binary tree

(2) Basic introduction to clue binary tree

  1. The binary linked list of n nodes contains n+1 [formula 2n-(n-1)=n+1] null pointer fields. Use the null pointer field in the binary linked list to store pointers to the predecessor and successor nodes of the node in a certain traversal order (this kind of additional pointers are called "clues")
  2. This kind of binary linked list with clues is called a thread linked list, and the corresponding binary tree is called a threaded binary tree (Threaded Binary Tree). According to the nature of the clues, the clue binary tree can be divided into three types: preorder clue binary tree, middle order clue binary tree and postorder clue binary tree.
  3. The previous node of a node is called the predecessor node
  4. The next node after a node is called the successor node

(3) Cue Binary Tree Application Case

Application case description: Take the following binary tree into an in-order clue binary tree. The sequence of middle-order traversal is {8, 3, 10, 1, 14, 6}
Insert picture description here
Thinking analysis: The result of middle-order traversal: {8, 3, 10, 1, 14, 6}
Insert picture description here
Explanation: When the binary tree is clueed, Node The properties of the node left and right are as follows:

  1. left refers to the left subtree, or it may be the predecessor node. For example, ① the left subtree pointed to by the node left, and the left of the ⑩ node points to the predecessor node.
  2. right points to the right subtree, or it may point to the successor node. For example, ① node right points to the right subtree, and ⑩ node right points to the successor node

Code (click the link below to view the source code):
Clues Binary Tree Application Case


(4) Traverse the binary tree of clues

  1. Description: traverse the previous binary tree with in-order clues
  2. Analysis: Because the point of each node changes after the thread is turned, the original traversal method cannot be used. At this time, a new method is needed to traverse the threaded binary tree. Each node can be traversed in a linear manner, so there is no need to use recursive methods. Also improves the efficiency of traversal. The order of the traversal should be consistent with the in-order traversal.
  3. Code (click the link below to view the source code):
    Traverse the binary tree of clues

Guess you like

Origin blog.csdn.net/a13027629517/article/details/115263388