Structure and Algorithm (05): Binary Tree and Multitree

Source code of this article: GitHub·click here || GitEE·click here

1. Tree structure

1. Arrays and linked lists

Array structure

Array storage is to access elements through subscripts, and the query speed is fast. If the array elements are ordered, binary search can also be used to improve the retrieval speed; adding new elements may cause multiple subscripts to move, which is inefficient;

Linked list structure

Linked list storage elements are highly efficient for element addition and deletion, but each time traversing elements need to start from the beginning node, which is particularly inefficient;

The tree structure can relatively improve the efficiency of data storage and reading at the same time.

2. Tree structure concept

  • Root node: the root of the tree, the node without a parent node, as shown in the above figure A node;
  • Sibling nodes: child nodes that have the same parent node. Figure B and C points;
  • Leaf node: A node without child nodes. As shown in DEFG node;
  • The height of the tree: the maximum number of layers, as shown in the figure is 3 layers;
  • Path: Find the route of the specified node from the root node;

The tree structure is a level of nesting structure. The outer and inner layers of a tree structure have similar structures, so this structure can be expressed recursively. The various tree diagrams in the classic data structure are a typical tree structure: a tree can be simply represented as a root, a left subtree, and a right subtree. The left subtree and the right subtree have their own subtrees.

Two, the binary tree model

There are many types of trees. Binary Tree is an important type of tree structure. Each node can only have two child nodes at most. One form is called binary tree. The child nodes of binary tree are divided into left and right nodes. The data structure abstracted from actual problems is often in the form of a binary tree.

Complete binary tree

All the leaf nodes of the binary tree are in the last layer or the penultimate layer, and 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.

Full binary tree

When all the leaf nodes of the binary tree are in the last layer, and the total number of nodes = 2^n -1, n is the number of layers, it is called a full binary tree.

Balanced binary tree

A balanced binary tree means that the absolute value of the height difference of the subtree of any node is less than or equal to 1, and the left and right subtrees are both a balanced binary tree. The common ones that conform to the balanced tree are, B-tree (multiple balanced search tree) ), AVL tree (binary balanced search tree), etc.

Binary search tree

Binary Search Tree (BinarySearchTree) is not only a binary tree, but also satisfies a certain order: the left child of the node is smaller than itself, and the right child of the node is larger than itself.

Three, binary tree coding

1. Basic code

Node code

class TreeNode {
    
    
    private String num ;
    private TreeNode leftNode ;
    private TreeNode rightNode ;
    public TreeNode(String num) {
    
    
        this.num = num;
    }
    @Override
    public String toString() {
    
    
        return "TreeNode{num=" + num +'}';
    }
}

Tree structure code

class BinaryTree01 {
    
    
    private TreeNode root ;
}

2. Traverse and search

Preorder traversal search

Process the data of the current node first, and then recursively traverse the left subtree and the right subtree in turn;

public void prevTraverse() {
    
    
    // 输出父结点
    System.out.println(this);
    // 向左子树递归前序遍历
    if(this.leftNode != null) {
    
    
        this.leftNode.prevTraverse();
    }
    // 向右子树递归前序遍历
    if(this.rightNode != null) {
    
    
        this.rightNode.prevTraverse();
    }
}
public TreeNode prevSearch(String num) {
    
    
    //比较当前结点
    if(this.num.equals(num)) {
    
    
        return this ;
    }
    // 递归遍历左子树查找
    TreeNode findNode = null;
    if(this.leftNode != null) {
    
    
        findNode = this.leftNode.prevSearch(num);
    }
    // 左子树遍历命中
    if(findNode != null) {
    
    
        return findNode ;
    }
    // 递归遍历右子树查找
    if(this.rightNode != null) {
    
    
        findNode = this.rightNode.prevSearch(num);
    }
    return findNode ;
}

In-order traversal search

First recursively traverse the left subtree, then process the parent node, and then recursively traverse the right subtree;

public void midTraverse() {
    
    
    // 向左子树递归中序遍历
    if(this.leftNode != null) {
    
    
        this.leftNode.midTraverse();
    }
    // 输出父结点
    System.out.println(this);
    // 向右子树递归中序遍历
    if(this.rightNode != null) {
    
    
        this.rightNode.midTraverse();
    }
}
public TreeNode midSearch(String num) {
    
    
    // 递归遍历左子树查找
    TreeNode findNode = null;
    if(this.leftNode != null) {
    
    
        findNode = this.leftNode.midSearch(num);
    }
    if(findNode != null) {
    
    
        return findNode ;
    }
    // 比较当前结点
    if(this.num.equals(num)) {
    
    
        return this ;
    }
    // 递归遍历右子树查找
    if(this.rightNode != null) {
    
    
        findNode = this.rightNode.midSearch(num);
    }
    return findNode ;
}

Post-order traversal search

First recursively traverse the left subtree, then recursively traverse the right subtree, and finally process the parent node;

public void lastTraverse() {
    
    
    // 向左子树递归后序遍历
    if(this.leftNode != null) {
    
    
        this.leftNode.lastTraverse();
    }
    // 向右子树递归后序遍历
    if(this.rightNode != null) {
    
    
        this.rightNode.lastTraverse();
    }
    // 输出父结点
    System.out.println(this);
}
public TreeNode lastSearch(String num) {
    
    
    // 递归遍历左子树查找
    TreeNode findNode = null;
    if(this.leftNode != null) {
    
    
        findNode = this.leftNode.lastSearch(num);
    }
    if(findNode != null) {
    
    
        return findNode ;
    }
    // 递归遍历右子树查找
    if(this.rightNode != null) {
    
    
        findNode = this.rightNode.lastSearch(num);
    }
    if(findNode != null) {
    
    
        return findNode ;
    }
    // 比较当前结点
    if(this.num.equals(num)) {
    
    
        return this ;
    }
    return null ;
}

3. Delete the node

If the currently deleted node is a leaf node, you can delete the node directly; if the deleted node is a non-leaf node, delete the node tree.

public void deleteNode(String num) {
    
    
    // 判断左节点是否删除
    if(this.leftNode != null && this.leftNode.num.equals(num)) {
    
    
        this.leftNode = null ;
        return ;
    }
    // 判断右节点是否删除
    if(this.rightNode != null && this.rightNode.num.equals(num)) {
    
    
        this.rightNode = null;
        return ;
    }
    // 向左子树遍历进行递归删除
    if(this.leftNode != null) {
    
    
        this.leftNode.deleteNode(num);
    }
    // 向右子树遍历进行递归删除
    if(this.rightNode != null) {
    
    
        this.rightNode.deleteNode(num);
    }
}

Four, polytree

A multi-tree means that a parent node can have multiple child nodes, but a child node still follows the law of one parent node. Generally, the actual application height of the binary tree is too high, and the description of the data relationship can be simplified through the multi-tree.

For example: Linux file system, organizational structure relationship, role menu authority management system, etc., are usually described based on a polytree.

Five, source code address

GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent

Recommended reading: finishing programming system

Recommended items

Serial number project name GitHub address GitEE address Recommended
01 Java describes design patterns, algorithms, and data structures GitHub·click here GitEE·Click here ☆☆☆☆☆
02 Java foundation, concurrency, object-oriented, web development GitHub·click here GitEE·Click here ☆☆☆☆
03 Detailed explanation of SpringCloud microservice basic component case GitHub·click here GitEE·Click here ☆☆☆
04 SpringCloud microservice architecture actual combat comprehensive case GitHub·click here GitEE·Click here ☆☆☆☆☆
05 Getting started with SpringBoot framework basic application to advanced GitHub·click here GitEE·Click here ☆☆☆☆
06 SpringBoot framework integrates and develops common middleware GitHub·click here GitEE·Click here ☆☆☆☆☆
07 Basic case of data management, distribution, architecture design GitHub·click here GitEE·Click here ☆☆☆☆☆
08 Big data series, storage, components, computing and other frameworks GitHub·click here GitEE·Click here ☆☆☆☆☆

Guess you like

Origin blog.csdn.net/cicada_smile/article/details/108752573