# Tree ring foundation: binary tree

Welcome to my WeChat official account: Tree definition:

Tree is a very important structure in data structure and algorithm analysis and design. It is a hierarchical model composed of N nodes.
It has the following characteristics:
1. There is a root node, commonly known as the root node
2. Each element is called a node
3. Except for the root node, all other nodes are divided into n disjoint sets (subtrees)

Basic terms of tree structure:
Node: Elements in tree structure
Subtree: when the node is greater than 1, the rest of the nodes are divided into disjoint sets called subtrees
Degree: the number of subtrees owned by a node is called degree of node
Leaf: node with degree 0
Child: the root of a subtree of a node is called a child node
Parents: corresponding to children's nodes
Brother: the same parent node
Depth: the maximum level of the node is called the depth of the tree, which is used to calculate the time complexity
Forest: N disjoint trees are called forest

Binary tree:
A special tree structure, each node has at most two subtrees.
On the nth layer of a binary tree, there are at most 2^(N-1) nodes,

For example, the second layer has at most 2 nodes, and the third layer has at most 4 nodes.
Full binary tree:
If the depth of the tree is m, there will be 2^M-1 node binary tree.

For example, the depth of a tree is 3, and the number of nodes of a full binary tree is 2 ^ 3-1 = 7

Three kinds of traversal of binary tree (emphasis): Preorder traversal: root node - > left subtree - > right subtree
Middle order traversal: left subtree - > root node - > right subtree
Post order traversal: left subtree - > right subtree - > root node

Traversal starts from the root node and outputs when the root node is encountered.

We take the middle order traversal as an example:
First find a. A has left subtree. Find B.
Take B as a subtree, find the left subtree of B, do not return B, output B.
Then find the right subtree of B and find C.
Take C as a subtree, find the left subtree of C, and find D.
D has no subtree, so output D.
At this time, the left subtree of C ends, returns C, and outputs C.
At this time, all the left subtrees of A end, return A, and output A.
Next, we start to traverse the right subtree of A.
Like the above steps, do not repeat.
Therefore, the middle order traversal results in:

B -> D -> C -> A -> E -> H -> G -> K -> F

Therefore, the results of three traversal methods are as follows:

Preorder traversal: ABCDEFGHK
Middle order traversal: BDCAEHGKF
Subsequent traversal: DCBHKGFEA

Let's see how the code is implemented:

```package com.monkey.springdemo.utils.sort;

class Node{
private String data;
private Node leftNode;
private Node rightNode;

public Node(String data, Node leftNode, Node rightNode) {
this.data = data;
this.leftNode = leftNode;
this.rightNode = rightNode;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

public Node getLeftNode() {
return leftNode;
}

public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}

public Node getRightNode() {
return rightNode;
}

public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
}

public class BinaryTree {

public static void main(String[] args) {
Node D = new Node("D",null,null);
Node H = new Node("H",null,null);
Node K = new Node("K",null,null);
Node C = new Node("C",D,null);
Node B = new Node("B",null,C);
Node G = new Node("G",H,K);
Node F = new Node("F",G,null);
Node E = new Node("E",null,F);
Node A = new Node("A",B,E);//Root node

BinaryTree binaryTree = new BinaryTree();
System.out.print("Preorder traversal:");
binaryTree.pre(A);
System.out.println();
System.out.print("Middle order traversal:");
binaryTree.in(A);
System.out.println();
System.out.print("Subsequent traversal:");
binaryTree.post(A);
}

public void print(Node node){
System.out.print(node.getData());
}

//Preorder traversal: root (output) - > left - > right
public void pre(Node root){
//Traversal root
print(root);
//Traverse left subtree
if (root.getLeftNode() != null){
pre(root.getLeftNode());
}
//Traversal right subtree
if (root.getRightNode() != null){
pre(root.getRightNode());
}
}

//Middle order traversal: left - > root (output) - > right
public void in(Node root){
//Traverse left subtree
if (root.getLeftNode() != null){
in(root.getLeftNode());
}
//Traversal root
print(root);
//Traversal right subtree
if (root.getRightNode() != null){
in(root.getRightNode());
}
}

//Backward traversal: left - > right - > root (output)
public void post(Node root){
//Traverse left subtree
if (root.getLeftNode() != null){
post(root.getLeftNode());
}
//Traversal right subtree
if (root.getRightNode() != null){
post(root.getRightNode());
}
//Traversal root
print(root);
}

}
```

Traversal mode of front, middle and back,

The main difference lies in the traversal order of nodes and subtrees.

If you understand this order, you will naturally understand the traversal method.

Tags: REST

Posted on Sat, 06 Jun 2020 05:09:17 -0400 by Altair