binary search/sort tree based on Java

Definition of BST

  1. If its left subtree is not empty, the value of all nodes in the left subtree is less than the value of its root structure.
  2. If its right subtree is not empty, the value of all nodes in the right subtree is greater than the value of its root structure.
  3. Its left and right subtrees are also binary sort trees (recursion).
  • Note: elements in the tree are repeatable, that means, they can be changed to >= or <=.
  • Why is it called a binary sort tree?
  • Because the sequence in BST is an ordered sequence, the construction of a binary sort tree for an arbitrary keyword sequence is to sort it and make it into an ordered sequence

source code

public class Bst {
    static class BSTNode{ // this is a internal class
        private int key;
        private BSTNode lc = null;
        private BSTNode rc = null;
    }
    public static void main(String[] args) {
        int[] a = new int[]{2,3,54,1,5,0,5,3,2,1};
        BSTNode b = CreateBST(a);
        DispBST(b);
        InsertBST(b, 7);
        System.out.println();
        DispBST(b);
        System.out.println();
        InOrder(b); //InOrder traversal
    }
    public static BSTNode CreateBST(int[] a){
        BSTNode bst = null;
        for(int i = 0; i < a.length; i++){
            bst = InsertBST(bst, a[i]);
        }
        return bst;
    }
    public static BSTNode InsertBST(BSTNode b, int k){ // Insert a data in bst
        if(b == null){
            b = new BSTNode();
            b.key = k;
            b.lc = null;
            b.rc = null;
            return b;
        }
        if(b.key > k) b.lc = InsertBST(b.lc, k);
        else b.rc = InsertBST(b.rc, k); 
        return b;
    }

    public static void DispBST(BSTNode bstNode){ //parenthesized notation
        if(bstNode != null){
            System.out.print(bstNode.key);
            if(bstNode.rc != null || bstNode.lc != null){
                System.out.print("(");
                DispBST(bstNode.lc);
                if(bstNode.rc != null) System.out.print(",");
                DispBST(bstNode.rc);
                System.out.print(")");
            }
        }
    }
    public static void InOrder(BSTNode bstNode){//inorder traversal
        if(bstNode != null){
            InOrder(bstNode.lc);
            System.out.print(bstNode.key + " ");
            InOrder(bstNode.rc);
        }
    }

output

2(1(0,1),3(2,54(5(3,5))))
2(1(0,1),3(2,54(5(3,5(,7)))))
0 1 1 2 2 3 3 5 5 7 54 

BST complexity analysis

  • BST is more efficient in terms of maintaining the orderliness of the table, because you don’t have to move the elements, just modify the pointer to complete the insertion and deletion of nodes
    The average execution time is O ( l o g 2 n ) O(log_2n) O(log2n)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值