# Binary tree traversal algorithm

### Preorder traversal

Idea: first root node - > left subtree - > right subtree;
The binary tree is as follows:

```/**
* TreeSearch Brief description
* <p> TODO:Describe such responsibilities</p>
*
* @author ckmike
* @version 1.0
* @date 18-12-6 10:13 p.m.
**/
public class TreeSearch {

// Node data structure
class TreeNode{
private String value = null;
private TreeNode leftchildren = null;
private TreeNode rightchildre = null;

public TreeNode(String value, TreeNode leftchildren, TreeNode rightchildre) {
this.value = value;
this.leftchildren = leftchildren;
this.rightchildre = rightchildre;
}

public TreeNode(String value) {
this.value = value;
}

public void setValue(String value) {
this.value = value;
}

public void setLeftchildren(TreeNode leftchildren) {
this.leftchildren = leftchildren;
}

public void setRightchildre(TreeNode rightchildre) {
this.rightchildre = rightchildre;
}

public String getValue() {
return value;
}

public TreeNode getLeftchildren() {
return leftchildren;
}

public TreeNode getRightchildre() {
return rightchildre;
}
}

public TreeNode getTargetTree() {
// Leaf node
TreeNode G = new TreeNode("G");
TreeNode D = new TreeNode("D");
TreeNode E = new TreeNode("E",G,null);
TreeNode B = new TreeNode("B",D,E);
TreeNode H = new TreeNode("H");
TreeNode I = new TreeNode("I");
TreeNode F = new TreeNode("F",H,I);
TreeNode C = new TreeNode("C",null,F);
// Construct root node
TreeNode root = new TreeNode("A",B,C);
return root;
}

// First order traversal binary tree
public void preorderVistTreeNode(TreeNode node){
if(null != node){
System.out.print(node.value);
if(null != node.leftchildren){
preorderVistTreeNode(node.leftchildren);
}
if(null != node.rightchildre){
preorderVistTreeNode(node.rightchildre);
}
}
}

public static void main(String[] args) {
TreeSearch treeSearch = new TreeSearch();
TreeNode tree= treeSearch.getTargetTree();
treeSearch.preorderVistTreeNode(tree);
}
}```

Traversal result: ABDEGCFHI

### Sequential traversal

Idea: first, left subtree - > root node - > right subtree;

```    // Sequential traversal
public void inorderVistTreeNode(TreeNode node){
if(null != node){
if(null != node.leftchildren){
inorderVistTreeNode(node.leftchildren);
}
System.out.print(node.value);
if(null != node.rightchildre){
inorderVistTreeNode(node.rightchildre);
}
}
}

public static void main(String[] args) {
TreeSearch treeSearch = new TreeSearch();
TreeNode tree= treeSearch.getTargetTree();
System.out.print("Sequential traversal:");
treeSearch.inorderVistTreeNode(tree);
}```

Middle order traversal result: DBGEACHFI

### Post order traversal

Idea: first, left subtree - > right subtree - > root node;

```    // Post order traversal
public void postorderVistTreeNode(TreeNode node){
if(null != node){
if(null != node.leftchildren){
postorderVistTreeNode(node.leftchildren);
}
if(null != node.rightchildre){
postorderVistTreeNode(node.rightchildre);
}
System.out.print(node.value);
}
}

public static void main(String[] args) {
TreeSearch treeSearch = new TreeSearch();
TreeNode tree= treeSearch.getTargetTree();
System.out.print("Post order traversal:");
treeSearch.postorderVistTreeNode(tree);
}
```

Postorder traversal result: DGEBHIFCA

### level traversal

Idea: first root node, then the second layer, the third layer, and then go down in turn, (the same layer nodes output from left to right);

```// level traversal
public void levelorderVistTreeNode(TreeNode node){
if(null != node){
TreeNode currentNode;
while (!list.isEmpty()){
currentNode = list.poll();
System.out.print(currentNode.value);
if(null != currentNode.leftchildren){
}
if(null != currentNode.rightchildre){
}
}
}
}

public static void main(String[] args) {
TreeSearch treeSearch = new TreeSearch();
TreeNode tree= treeSearch.getTargetTree();
System.out.print("level traversal:");
treeSearch.levelorderVistTreeNode(tree);
}```

Sequence traversal: ABCDEFGHI

It should be noted that sequence traversal binary tree is realized by non recursive queue, which is realized by using FIFO of queue. So I've written the algorithms of first order traversal, middle order traversal, second order traversal and sequence traversal binary tree here, isn't it very simple. If you are interested, you can think about whether there is an algorithm that does not use recursion to implement first, middle, and last traversal. Write it in a language you are familiar with and remember to share it.

Tags: Java

Posted on Mon, 02 Dec 2019 21:19:20 -0500 by OsvaldoM