# Introduction to binary tree

Binary tree is a kind of tree structure that each node has at most two branches (that is, the degree is no more than 2). The branches are called left subtree and right subtree. The branches of a binary tree have left and right order and cannot be reversed

The third layer of a binary tree has at most 2 I − 12 ^ {I-1} 2 I − 1 nodes; a binary tree with a depth of K K K has at most 2 K − 12^k-12k − 1 nodes (defining the depth of the root node K0 = 0k_ = 0k0 = 0)

### Special binary tree

1. Full two fork tree
A binary tree with a depth of K K K and 2k+1 − 12^{k+1}-12k+1 − 1 node is called a full binary tree. The characteristic of this tree is that the number of nodes on each layer is the maximum
2. Complete binary tree
In a binary tree, except for the last layer, if all the other layers are full, and the last layer is either full, or there is a lack of consecutive nodes on the right, then the binary tree is a complete binary tree. The depth of a complete binary tree with n n n nodes is log2n + 1LOG n + 1log2n+1. A complete binary tree with a depth of K has at least 2K − 12^{k-1}2k − 1 node and at most 2K − 12^k-12k − 1 node

### Traversal of binary tree

The traversal of binary trees can be divided into two categories:

1. Depth first traversal (pre order traversal, middle order traversal, post order traversal)

There are mainly two ways: using recursion and using stack, in which using the post order traversal of the stack is relatively complex, i.e. losing
The code is as follows:

```import java.util.Arrays;
import java.util.Stack;

public class BinaryTree {

public static TreeNode createBinaryTree(LinkedList<Integer> inputList){
TreeNode node = null;
if(inputList == null || inputList.isEmpty()){
return null;
}
Integer data = inputList.removeFirst();
if(data!=null){
node = new TreeNode(data);
node.leftChild = createBinaryTree(inputList);
node.rightChild = createBinaryTree(inputList);
}
return node;
}

/**
* Traversal of binary tree by using recursion
* @param node Binary tree node
*/
public static void preOrderTraversal(TreeNode node){
if (node==null){
return;
}
System.out.println(node.data);
preOrderTraversal(node.leftChild);
preOrderTraversal(node.rightChild);
}

/**
* Sequence traversal, using stack
* @param root Binary tree root node
*/
public static void preOrederTraversalWithStack(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
TreeNode treeNode = root;
while (treeNode!=null || !stack.isEmpty()){
while (treeNode!=null){
System.out.println(treeNode.data);
stack.push(treeNode);
treeNode = treeNode.leftChild;
}
if (!stack.isEmpty()){
treeNode = stack.pop();
treeNode = treeNode.rightChild;
}
}
}

/**
* Order traversal in binary tree, using recursion
* @param node Binary tree node
*/
public static void inOrderTraversal(TreeNode node){
if (node==null){
return;
}
inOrderTraversal(node.leftChild);
System.out.println(node.data);
inOrderTraversal(node.rightChild);
}

/**
* Middle order traversal, using stack
* @param root Binary tree root node
*/
public static void inOrderTraversalWithStack(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
TreeNode treeNode = root;
while (treeNode!=null || !stack.isEmpty()){
while (treeNode!=null){
stack.push(treeNode);
treeNode = treeNode.leftChild;
}
if (!stack.isEmpty()){
treeNode = stack.pop();
System.out.println(treeNode.data);
treeNode = treeNode.rightChild;
}
}
}

/**
* Post order traversal of binary tree, using recursion
* @param node Binary tree node
*/
public static void postOrderTraversal(TreeNode node){
if (node==null){
return;
}
postOrderTraversal(node.leftChild);
postOrderTraversal(node.rightChild);
System.out.println(node.data);
}

/**
* Post order traversal - using stack
* @param root Binary tree root node
*/
public static void postOrderTraversalWithStack(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
TreeNode currNode= root;
TreeNode preNode = null;
while (currNode != null) {
stack.push(currNode);
currNode = currNode.leftChild;
}
while (!stack.isEmpty()){
currNode = stack.pop();
if (currNode.rightChild == null || currNode.rightChild == preNode){
System.out.println(currNode.data);
preNode = currNode;
}
else {
stack.push(currNode);
currNode = currNode.rightChild;
while (currNode != null){
stack.push(currNode);
currNode = currNode.leftChild;
}
}
}
}

/**
* Node class of binary tree
*/
private static class TreeNode{
int data;
TreeNode leftChild;
TreeNode rightChild;

TreeNode(int data){
this.data = data;
}
}

public static void main(String[] args) {
new Integer[]{3,2,9,null,null,10,null,null,8,null,4}));
TreeNode treeNode = createBinaryTree(inputList);
System.out.println("Preorder traversal:");
preOrderTraversal(treeNode);
System.out.println("Middle order traversal:");
inOrderTraversal(treeNode);
System.out.println("Subsequent traversal:");
postOrderTraversal(treeNode);
System.out.println("Preorder traversal-Using stack:");
preOrederTraversalWithStack(treeNode);
System.out.println("Sequential traversal-Using stack:");
inOrderTraversalWithStack(treeNode);
System.out.println("Post order traversal-Using stack:");
postOrderTraversalWithStack(treeNode);
}
}
```
1. Breadth first traversal (hierarchical traversal)

Hierarchical traversal is implemented by queue, which is very simple. The code is as follows:

```    public static void levelOrderTraversal(TreeNode root){
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
TreeNode node = queue.poll();
System.out.println(node.data);
if (node.leftChild!=null){
queue.offer(node.leftChild);
}
if (node.rightChild!=null){
queue.offer(node.rightChild);
}
}
}
```

### Application of binary tree

Binary trees are widely used to find and maintain relative order
Binary search tree (BST - binary search tree - binary sort tree)
Self balanced binary search tree: red black tree, AVL tree, tree heap, extension tree

Published 5 original articles, won praise 3, visited 43

Tags: Java

Posted on Wed, 15 Jan 2020 00:51:26 -0500 by Shrike