Search algorithm series (1) basis of binary tree

Time:2021-2-26

WeChat official account: Xiao Chao said

This isSearch algorithmThe first article in the series helps you get started with binary trees

What is a tree?

Let’s start with some pictures:

Search algorithm series (1) basis of binary tree

Among them, the first, second and fourth are all trees, and the third is not. The characteristics of trees are very obvious!

Each element is called “node“; it is used to connect the relationship between adjacent nodes, we call it “parent-child relationship”. For example, in Figure 1, node a is the base of node BParent nodeNode B is the node of node aChild nodeAt the same time, B node and Q node are the children of the same parent node a, so they become each otherBrother node. We call a node without a parent nodeRoot nodeWhich is node a in Figure 1. We call nodes without children nodesLeaf nodeFor example, nodes D, e, F and G in Figure 1. These concepts are obvious, but they are the most basic.

Binary tree

A binary tree is naturally a tree in which each node has at most two branches, that is, two sub nodes. The two branches are calledZuozishuandRight subtree. For example, figure 1, figure 2 and Figure 4 are all binary trees, because each node has at most two child nodes. Among them, figure 1 is also known asFull binary treeFigure 4 is also calledComplete binary tree. And the reason for thatComplete binary treeThe concept of binary tree is based on the physical storage of binary tree.

Storage method of binary tree

  • Chain storage method based on linked list
  • Sequential storage method based on array

Chain storage method:

We create one for each nodeNode object:

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

Each node is a node object, which contains the data we need to store, the reference to the left child node, and the reference to the right child node. It is like a linked list to string the whole tree. If the node has no left child, thenNode.left==nullperhapsNode.right==null.

Sequential storage method

We store the root node in thei=1The left child node is stored in the2*i=2The right child node is stored in the2*i+1=2The location of the car. And so on, complete the storage of the tree. With the help of subscript operation, we can easily jump from the parent node to the left and right child nodes, or find its parent node from any child node. If the position of X is I, then the subscripts of its two child nodes are2iand2i+1The location of its parent node isi/2(here the result is rounded down).

The details are shown in the figure below: it can be found that only the full binary tree storage has the highest efficiency and the most memory saving

Search algorithm series (1) basis of binary tree

Traversal of binary tree

There are three main traversal operations of binary tree

  • Preorder traversal
  • Middle order traversal
  • Postorder traversal

Preorder traversal means that for any node in the tree, first print the node, then print its left subtree, and finally print its right subtree.

Middle order traversal means that for any node in the tree, first print its left subtree, then print itself, and finally print its right subtree.

Postorder 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.

Notice, there’s something about itrecursionThe taste of

Take the picture as an example

Search algorithm series (1) basis of binary tree

  • Preorder traversal: a – > b – > D – > e – > C – > F
  • Middle order traversal: D – > b – > e – > A – > F – > C
  • Postorder traversal: D – > e – > b – > F – > C – > A

The specific code implementation (write recursion can be): 1

public void preOrder(Node root){
    if(root==null) return;
    System.out.println ( root.data ); // print the value of the root node
    preOrder(root.left);
    preOrder(root.right);
}

public void inOrder(Node root){
    if(root==null) return;
    inOrder(root.left);
    Systrm.out.println(root.data);
    inOrder(root.right);
}

public void inOrder(Node root){
    if(root==null) return;
    inOrder(root.left)
    inOrder(root.right);
    Systrm.out.println(root.data);
}

The time complexity of binary tree traversal is O (n), which is because each node can be accessed at most twice (recursive function in and out of the stack), so the number of traversal operations is directly proportional to the number of nodes n, that is to say, the time complexity of binary tree traversal is O (n).

expectation

After the above introduction, we have a basic understanding of binary tree, then, what is the significance of binary tree? What efficient algorithms can we design based on this data structure? Next time we’ll talk about itBinary search treeWe will define a data structure and maintain its properties.

Digression: for algorithm beginners, recommend a very nice bookAlgorithm Fourth EditionAll kinds of pictures in it are very detailed. If you need an electronic file, reply backAlgorithm 4You can get the download link. Backstage replyAlgorithm 01Send you an algorithm and data structure mind map. Finally, I hope we can make progress and grow together!

Search algorithm series (1) basis of binary tree

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]