Data structure-5 二叉搜索树 BST--Java语言实现

1. BST简介

本文部分内容参考自此处

Binary Tree : A data struc­ture in which we have nodes con­tain­ing data and two ref­er­ences to other nodes, one on the left and one on the right.

Binary Tree con­sist of Nodes

Nodes are noth­ing but objects of a class and each node has data and a
link to the left node and right node. Usu­ally we call the start­ing
node of a tree as root.
Left and right node of a Leaf node points to NULL so you will know that you have reached to the end of the tree.

Binary Search Tree:

Often we call it as BST, is a type of Binary tree which has a spe­cial
property.

Nodes smaller than root goes to the left of the root and Nodes greater
than root goes to the right of the root.
二叉搜索树:是一种特殊结构的二叉树,其左节点元素小于等于父节点,而右节点元素值大于父节点。
这里写图片描述

2. BST实现demo及相关说明

//BSTTree.java
package com.fqyuan.bst;

import org.junit.Test;

public class BSTTree {
    private Node root;

    public class Node {
        private int data;
        private Node lchild;
        private Node rchild;

        public Node(int item) {
            data = item;
        }
    }

    public BSTTree() {
        root = null;
    }

    public boolean isEmpty() {
        return root == null;
    }

    public void insert(int item) {
        if (root == null) {
            Node newNode = new Node(item);
            root = newNode;
            return;
        }

        // First find the position to insert.
        Node parent = root;
        Node currrent = root;
        while (currrent != null) {
            parent = currrent;
            if (item < currrent.data)
                currrent = currrent.lchild;
            else
                currrent = currrent.rchild;
        }
        // 'current' is the node to insert, 'parent' is the parent of 'current'.
        Node newNode = new Node(item);
        if (item < parent.data)
            parent.lchild = newNode;
        else
            parent.rchild = newNode;

    }

    // Choose the right max as the successor.
    private Node getSuccessor(Node node) {
        Node current = node.rchild;
        Node successor = null;
        // The node is to build a link with the successor.
        Node grand = null;
        while (current != null) {
            grand = successor;
            successor = current;
            current = current.lchild;
        }
        // After the loop, parent is the successor, grand is successor's father.
        if (successor != node.rchild) {
            grand.lchild = successor.rchild;
            successor.rchild = node.rchild;
        }

        return successor;
    }

    // 3 cases:
    public boolean delete(int item) {
        if (root == null)
            return false;
        // First of all, find the position to delete.
        Node current = root;
        Node parent = root;
        boolean isLeftChild = false;
        while (current.data != item) {
            parent = current;
            if (item < current.data) {
                current = current.lchild;
            } else
                current = current.rchild;
            if (current == null)
                return false;
        }
        if (parent.lchild.data == item)
            isLeftChild = true;
        else
            isLeftChild = false;

        // Case 1: if the node to be deleted is a leaf.
        if (current.lchild == null && current.rchild == null) {
            if (current == root) {
                root = null;
            } else if (isLeftChild) {
                parent.lchild = null;
            } else {
                parent.rchild = null;
            }
        }
        // Case 2: if node to be deleted has a child.
        else if (current.lchild == null) {
            if (current == root) {
                root = current.rchild;
            } else if (isLeftChild) {
                parent.lchild = current.rchild;
            } else {
                parent.rchild = current.rchild;
            }
        } else if (current.rchild == null) {
            if (current == root) {
                root = current.lchild;
            } else if (isLeftChild) {
                parent.lchild = current.lchild;
            } else {
                parent.rchild = current.lchild;
            }
        }
        // Case 3: if node to be deleted has 2 children.
        else {
            // First find the successor: the leftMax or the rightMax node to
            // replace.
            Node successor = getSuccessor(current);
            if (current == root) {
                root = successor;
            } else if (isLeftChild) {
                parent.lchild = successor;
            } else {
                parent.rchild = successor;
            }
            successor.lchild = current.lchild;

        }

        return false;
    }

    public Node search(int item) {
        Node current = root;
        while (current.data != item) {
            if (item < current.data)
                current = current.lchild;
            else if (item > current.data)
                current = current.rchild;
            if (current == null)
                return null;
        }
        return current;
    }

    public void inOrder() {
        inOrder(root);
    }

    private void inOrder(Node node) {
        if (node == null)
            return;
        inOrder(node.lchild);
        System.out.print(node.data + " ");
        inOrder(node.rchild);
    }

    @Test
    public void test() {
        BST<Integer> bst = new BST<>();
        int[] arr = { 1, 42, 29, 65, 22, 14, 6, 59, 71, 47, 20 };

        for (int i = 0; i < arr.length; i++) {
            bst.insert(arr[i]);
        }
        bst.inOrder();

        if (bst.search(10))
            System.out.println("\nFind 10");
        else
            System.out.println("\n10 not found.");

        // Insert a new element: 18
        bst.insert(18);
        bst.inOrder();
        // Delete an element:
        System.out.println();
        bst.delete(29);
        bst.inOrder();
    }
}
//Running result:
1 6 14 20 22 29 42 47 59 65 71 
10 not found.
1 6 14 18 20 22 29 42 47 59 65 71 
1 6 14 18 20 22 42 47 59 65 71 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值