WeChat official account: Xiao Chao said

This is

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

## What is a tree?

Let’s start with some pictures:

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 B**Parent node**Node B is the node of node a**Child node**At the same time, B node and Q node are the children of the same parent node a, so they become each other**Brother node**. We call a node without a parent node**Root node**Which is node a in Figure 1. We call nodes without children nodes**Leaf node**For 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 called**Zuozishu**and**Right 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 as**Full binary tree**Figure 4 is also called**Complete binary tree**. And the reason for that**Complete binary tree**The 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 node`Node 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, then`Node.left==null`

perhaps`Node.right==null`

.

**Sequential storage method**：

We store the root node in the`i=1`

The left child node is stored in the`2*i=2`

The right child node is stored in the`2*i+1=2`

The 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 are`2i`

and`2i+1`

The location of its parent node is`i/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

#### 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 it**recursion**The taste of

Take the picture as an example

- 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 it**Binary search tree**We will define a data structure and maintain its properties.

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