Introduction

A tree is a data structure similar to a linked list. Unlike the linear structure of a linked list, a tree is a non-linear data structure with a hierarchical structure.

The tree is composed of many nodes, and each node can point to many nodes.

If each node in a tree has only 0, 1, or 2 child nodes, the tree is called a binary tree, if we sort the binary tree to a certain degree.

For example, for each node in a binary tree, if the elements of the left subtree node are smaller than the root node, and the elements of the right subtree node are larger than the root node, then such a tree is called a binary search tree (Binary Search Tree) Referred to as BST.

Today we will discuss the nature of BST and the basic operation of BST.

Basic properties of BST

We have just talked about the basic features of BST, now let’s summarize it:

  1. The left subtree of any node in the BST must be smaller than the value of the node
  2. The right subtree of any node in the BST must be greater than the value of the node
  3. The left and right subtrees of any node in the BST must be a BST.

Look at a picture to intuitively feel the BST:

BST construction

How to build a BST with code?

First of all, the BST is composed of nodes one by one. In addition to storing the data of the node, the Node node also needs to point to the left and right child nodes, so that our BST can be completely connected by the Node.

In addition, we also need a root node to represent the root node of the BST.

The corresponding code is as follows:

public class BinarySearchTree {

    //根节点
    Node root;

    class Node {
        int data;
        Node left;
        Node right;

        public Node(int data) {
            this.data = data;
            left = right = null;
        }
    }
}

BST search

Let's look at the BST search first. If it is the above BST, what steps should we take to search for the node 32?

First picture:

The basic steps for searching are:

  1. Starting from the root node 41, compare the size of the root node and the search value
  2. If the search value is less than the node value, then recursively search the left tree
  3. If the search value is greater than the node value, then recursively search the tree on the right
  4. If the node matches, just return directly.

The corresponding java code is as follows:

//搜索方法,默认从根节点搜索
    public Node search(int data){
        return search(root,data);
    }

    //递归搜索节点
    private Node search(Node node, int data)
    {
        // 如果节点匹配,则返回节点
        if (node==null || node.data==data)
            return node;

        // 节点数据大于要搜索的数据,则继续搜索左边节点
        if (node.data > data)
            return search(node.left, data);

        // 如果节点数据小于要搜素的数据,则继续搜索右边节点
        return search(node.right, data);
    }

BST insertion

After the search is over, let's talk about BST insertion.

First look at an animation:

In the above example, we insert two nodes 30 and 55 into the BST.

The logic of insertion is like this:

  1. Starting from the root node, compare the node data and the data to be inserted
  2. If the data to be inserted is less than the node data, recursively insert the left subtree
  3. If the data to be inserted is greater than the node data, recursively insert the right subtree
  4. If the root node is empty, insert the current data as the root node

The corresponding java code is as follows:

// 插入新节点,从根节点开始插入
    public void insert(int data) {
        root = insert(root, data);
    }

    //递归插入新节点
    private Node insert(Node node, int data) {

        //如果节点为空,则创建新的节点
        if (node == null) {
            node = new Node(data);
            return node;
        }

        //节点不为空,则进行比较,从而递归进行左侧插入或者右侧插入
        if (data < node.data)
            node.left = insert(node.left, data);
        else if (data > node.data)
            node.right = insert(node.right, data);

        //返回插入后的节点
        return node;
    }

Deletion of BST

BST deletion is a little more complicated than insertion, because insertion is always inserted into leaf nodes, and deletion may delete non-leaf nodes.

Let's first look at an example of deleting leaf nodes:

In the above example, we deleted the two nodes 30 and 55.

As you can see, deleting a leaf node is relatively simple, just delete it after finding it.

Let's look at a more complicated example, for example, we want to delete the 65 node:

You can see that you need to find the smallest one in the right subtree of the node 65, and replace the 65 node (of course, you can also find the largest one in the left subtree).

So the deletion logic is like this:

  1. Starting from the root node, compare the size of the node to be deleted and the root node
  2. If the node to be deleted is smaller than the root node, then recursively delete the left subtree
  3. If the node to be deleted is larger than the root node, then recursively delete the right subtree
  4. If the nodes match, there are two more cases
  5. If it is a unilateral node, directly return to the other side of the node
  6. If it is a bilateral node, first find the smallest value on the right as the root node, and then delete the right node after the smallest value as the right node of the root node

Look at the implementation of the code:

 // 删除新节点,从根节点开始删除
    void delete(int data)
    {
        root = delete(root, data);
    }

    //递归删除节点
    Node delete(Node node, int data)
    {
        //如果节点为空,直接返回
        if (node == null)  return node;

        //遍历左右两边的节点
        if (data < node.data)
            node.left = delete(node.left, data);
        else if (data > root.data)
            node.right = delete(node.right, data);

        //如果节点匹配
        else
        {
            //如果是单边节点,直接返回其下面的节点
            if (node.left == null)
                return node.right;
            else if (node.right == null)
                return node.left;

            //如果是双边节点,则先找出右边最小的值,作为根节点,然后将删除最小值过后的右边的节点,作为根节点的右节点
            node.data = minValue(node.right);

            // 从右边删除最小的节点
            node.right = delete(node.right, node.data);
        }
        return node;
    }

Here we use recursion to delete bilateral nodes. Can you consider whether there are other ways to delete them?

The code address of this article:

learn-algorithm

This article is included in www.flydean.com

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "programs, those things", know the technology, know you better!


flydean
890 声望432 粉丝

欢迎访问我的个人网站:www.flydean.com