/**
* @author:xingquanxiang createTime:2019/12/14 19:58
* description: 二叉搜索树、二叉排序树
* 特点:
* 1.如果左子树不为空,则左子树上结点的值都小于根节点
* 2.如果右子树不为空,则右子树上结点的值都大于根节点
* 弊端:可能会变化成链表,解决方法:AVL树:平衡二叉树
*/
public class BinarySearchTree {
int data;
BinarySearchTree left;
BinarySearchTree right;
public BinarySearchTree(int data) {
this.data = data;
this.left = null;
this.right = null;
}
public void insert(BinarySearchTree root, int data) {
if (root != null) {
if (root.data < data){
if (root.right != null){
insert(root.right, data);
}else{
root.right = new BinarySearchTree(data);
}
}else{
if (root.left !=null){
insert(root.left, data);
}else{
二叉搜索树、二叉排序树(Java代码实现)
最新推荐文章于 2023-04-03 17:21:52 发布
/** * @author:xingquanxiang createTime:2019/12/14 19:58 * description: 二叉搜索树、二叉排序树 * 特点: * 1.如果左子树不为空,则左子树上结点的值都小于根节点 * 2.如果右子树不为空,则右子树上结点的值都大于根节点 * 弊端:可能会变化成链表,解决方法:AVL树:平衡二叉树 */...
摘要由CSDN通过智能技术生成