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:
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==null
perhapsNode.right==null
.
Sequential storage method:
We store the root node in thei=1
The left child node is stored in the2*i=2
The right child node is stored in the2*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 are2i
and2i+1
The 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
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
- 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!