BST 二叉搜素树, 排序算法一种

1. 二叉

2. 根大约整个左字数, 小于整个右子数

class Node{
    int num;
    Node left;
    Node right;
}

public class BST {

    public static void bstSort(Node node, int item){
        if(node.num < item){
            if(node.right == null){
                Node nodeTemp = new Node();
                nodeTemp.num = item;
                node.right = nodeTemp;
                return;
            }
            node = node.right;
            bstSort(node, item);
        }else {
            if(node.left == null){
                Node nodeTemp = new Node();
                nodeTemp.num = item;
                node.left = nodeTemp;
                return;
            }
            node = node.left;
            bstSort(node, item);
        }
    }

    public static void walkNode(Node node){
        if(node.left != null){
            walkNode(node.left);
        }
        System.out.print(node.num + " ");
        if(node.right != null){
            walkNode(node.right);
        }
    }

    public static void main(String args[]){
        int[] test = {1,2,3,1,4,3,1,5,7,8};
        Node node = new Node();
        node.num = test[0];
        for(int i = 1; i < test.length; i ++){
            bstSort(node, test[i]);
        }
        walkNode(node);
    }
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页