# Seven commonly used sorting algorithms -- implementation of binary tree sorting

Introduction to binary sort tree

Binary sort tree is also called binary search tree. Simply speaking, the storage of data should conform to the principle: the nodes on the left should be smaller than the root node, and the nodes on the right should be larger than the root node, so as to avoid the same (the same can be placed left and right).

Learning binary sorting tree and realizing "add, delete, modify and query" can be completed with high efficiency.

Implementation ideas:

1. Create Node (define three variables, an adding method and a middle order traversal method (the results sorted before and after are not ordered))

2. Create a binary tree (define a root node, add adding method, add traversal method)

3. Create an instance, add an array recursively, and use the middle order traversal method output

Test code:

public class BinarySortTree {

public static void main(String[] args) {
// TODO Auto-generated method stub
int arr [] = {2,5,3,7,4,8,9,1,0};
BinaryTree binaryTree = new BinaryTree();

//Add array recursively
for(int i = 0; i < arr.length; i++) {
binaryTree.add(new TreeNode(arr[i]));
}

//Ergodic output
System.out.println("Middle order ergodic output~~");
binaryTree.postOrder();

}

}
//Create a binary tree
class BinaryTree{

private TreeNode root;

//	public BinaryTree(Node root) {
//		this.root = root;
//	}

//Add method
public void add(TreeNode node) {
if(root == null) {
root = node;
} else {
this.root.add(node);
}
}

//traversal method
public void postOrder() {
if(root != null) {
this.root.postOrder();
} else {
System.out.println("Binary tree is empty, unable to traverse sorting");
}
}
}

//Create node
class TreeNode{
private int value;
private TreeNode left;
private TreeNode right;

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

@Override
public String toString() {
return "TreeNode [value=" + value + "]";
}

//Add method
public void add(TreeNode node) {
//Determine the relationship between the incoming value and the value of the current node
//If the value passed in is null, it will return directly
if(node == null) {
return;
}
//If the value passed in is less than the current value
if(node.value < this.value) {
//Judge whether the left child number of the current node is empty. If it is empty, the value less than the current node will be placed in the left node
if(this.left == null) {
this.left = node;
//If the left sub node of the current node is not empty, the left sub tree will be judged by the cabinet lifting
} else {
this.left.add(node);
}
//If the value passed in is not less than the current value, it means it is greater than or equal to the current value, then it will be added to the right side of the current node
} else {
if(this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
}

//Postorder ergodic
public void postOrder() {

if(this.left != null) {
this.left.postOrder();
}
System.out.println(this);
if(this.right != null) {
this.right.postOrder();
}

}
}

Tags: less

Posted on Sat, 23 May 2020 10:53:04 -0400 by chanw1