大话数据结构-查找-二叉排序树

注:本文同步发布于稀土掘金。

5 二叉排序树

5.1 概述

  二叉排序树(Binary Sort Tree)又称为二叉查找树,它或者是一棵空树,或者是具有下列性质的二叉树:

  (1) 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;

  (2) 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;

  (3) 它的左右子树,也分别为二叉排序树;

  我们使用如下方法创建一棵二叉排序树:

/**
 * create tree from array
 * <p>
 * the first element of data is the root node value
 *
 * @param data array
 * @author Korbin
 * @date 2023-04-23 11:38:06
 **/
public void createTreeFromArray(T[] data) {

    // set the first element as the root node
    root = new TreeNode<>();
    root.setData(data[0]);

    TreeNode<T> node = root;
    // iterate data
    for (int i = 1; i < data.length; i++) {
        while (null != node) {
            if (data[i].compareTo(node.getData()) <= 0) {
                // the value of data[i] little than node data or equals to node data
                TreeNode<T> leftNode = node.getLeftChild();
                if (null == leftNode) {
                    // if node has no left child
                    // set data[i] as node's left child
                    TreeNode<T> newNode = new TreeNode<>();
                    newNode.setData(data[i]);
                    node.setLeftChild(newNode);
                    break;
                } else {
                    // if node has left child
                    // set node as left child and continue
                    node = leftNode;
                }
            } else {
                // the value of data[i] greater than node data
                TreeNode<T> rightNode = node.getRightChild();
                if (null == rightNode) {
                    // if node has no right child
                    // set data[i] as node's right child
                    TreeNode<T> newNode = new TreeNode<>();
                    newNode.setData(data[i]);
                    node.setRightChild(newNode);
                    break;
                } else {
                    // if node has right child
                    // set node as right child and continue
                    node = rightNode;
                }
            }
        }
        node = root;
    }

}

  其中TreeNode实体定义如下所示:

import lombok.Data;

/**
 * Tree Node
 *
 * @author Korbin
 * @date 2023-04-23 11:35:08
 **/
@Data
public class TreeNode<T> {

    /**
     * value of node
     **/
    private T data;

    /**
     * left child
     **/
    private TreeNode<T> leftChild;

    /**
     * right child
     **/
    private TreeNode<T> rightChild;

}

5.2 查找

  由于是二叉排序树,满足左孩子结点一定小于根结点,根结点一定小于右孩子结点,因此可以很简单地使用递归的方式进行关键字查询:

/**
 * find node who's value equals key
 *
 * @param key key to find
 * @return searched node or null
 * @author Korbin
 * @date 2023-04-23 14:36:49
 **/
public TreeNode<T> findNode(T key) {
    return findNode(root, key);
}

/**
 * find node who's value equals key in tree
 *
 * @param tree root node of tree
 * @param key  key to find
 * @return searched node or null
 * @author Korbin
 * @date 2023-04-23 14:35:31
 **/
private TreeNode<T> findNode(TreeNode<T> tree, T key) {

    if (null == tree) {
        return null;
    }

    if (key.equals(tree.getData())) {
        // found node
        return tree;
    }

    if (key.compareTo(tree.getData()) < 0) {
        // find from left tree
        return findNode(tree.getLeftChild(), key);
    } else {
        // find from right tree
        return findNode(tree.getRightChild(), key);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值