数据结构之二叉搜索树基本操作

本文详细介绍了二叉搜索树的基本概念,包括定义、查找、删除、获取高度、获取最大元素及遍历方法。通过具体算法实现,帮助读者深入理解二叉搜索树的操作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,定义

二叉搜索树(二叉排序树)或者是一棵空树,或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
(4)没有键值相等的节点。
在这里插入图片描述

2,查找

若根结点的关键字值等于查找的关键字,成功。
否则,若小于根结点的关键字值,递归查左子树。
若大于根结点的关键字值,递归查右子树。
若子树为空,查找不成功。

    public static boolean find(treeNode node,int value){
        if (node == null){
            return false;
        }
        if (node.data == value) {
            return true;
        } else if (node.data < value) {
            return find(node.rightNode, value);
        } else
           return find(node.leftNode, value);
    }

3,删除

(1)若要删除的节点时叶子节点,则将其父节点的指针指向null。
(2)要删除的节点只有一个孩子结点时,则将其父亲节点的指针指向要删除节点的孩子节点。
(3)要删除的节点有左右两颗子树,则用另一节点替换被删除节点:左子树的最大元素或右子树的最小元素。

4,获取高度

public static int get_depth(treeNode node){
        if(node == null){
            return 0;
        }else {
            int left_depth = get_depth(node.leftNode);
            int right_Depth = get_depth(node.rightNode);
            return left_depth>right_Depth?left_depth+1:right_Depth+1;
        }
    }

5,获取最大元素

public static int get_max(treeNode node){
        if (node == null){
            return -1;
        }else{
            int left_max = get_max(node.leftNode);
            int right_max = get_max(node.rightNode);
            int mid_max = node.data;
            int max = mid_max;
            if (left_max>max){max = left_max;}
            if (right_max>max){max = right_max;}
            return max;
        }
    }

6,前中后序遍历

//前序遍历
    public  static void preOrder(treeNode node){
        if (node != null) {
            System.out.print(node.data+" ");
            preOrder(node.leftNode);
            preOrder(node.rightNode);
        }
    }
    //中序遍历
    public static void midorder(treeNode node){
        if (node != null){
            midorder(node.leftNode);
            System.out.print(node.data+" ");
            midorder(node.rightNode);
        }
    }
    //后序遍历
    public static void postorder(treeNode node){
        if (node != null){
            postorder(node.leftNode);
            postorder(node.rightNode);
            System.out.print(node.data+" ");
        }
    }
### Java中二搜索树数据结构 #### 1. 定义与特性 二搜索树(Binary Search Tree, BST),亦称为二排序树,在Java编程环境中具有重要地位。这种特定类型的二树拥有如下属性:对于任意节点而言,其左子树上的所有键值均小于该节点的键值;同理,右子树上的所有键值皆大于此节点的键值;并且左右子树同样遵循上述规则[^3]。 #### 2. 基本操作实现 为了更好地理解如何在Java中构建并运用二搜索树,下面给出了一段用于创建BST类及其核心功能——查找方法的具体代码实例: ```java class Node { int key; Node left, right; public Node(int item) { key = item; left = right = null; } } public class BinarySearchTree { private Node root; // 插入新节点的方法 void insert(int key) { root = insertRec(root, key); } /* A recursive function to insert a new key in BST */ Node insertRec(Node root, int key) { /* If the tree is empty, return a new node */ if (root == null) { root = new Node(key); return root; } /* Otherwise, recur down the tree */ if (key < root.key) root.left = insertRec(root.left, key); else if (key > root.key) root.right = insertRec(root.right, key); /* Return the unchanged root pointer */ return root; } boolean search(int key) { return searchRec(root, key); } // Recursive method for searching keys within the binary search tree. boolean searchRec(Node root, int key) { // Base cases: root is null or key is present at root if (root==null || root.key==key) return root != null; // Key is greater than root's key if (root.key < key) return searchRec(root.right,key); // Key is smaller than root's key return searchRec(root.left,key); } } ``` 这段代码展示了怎样定义一个简单的`Node`类来表示单个节点,并通过递归方式实现了向二搜索树中插入新元素的功能以及基于比较逻辑执行查找任务的过程[^1]。 #### 3. 应用领域 除了作为基础数据结构外,二搜索树还在多个高级应用场景里扮演着不可或缺的角色。例如,在Java集合框架内,像`TreeMap`和`TreeSet`这样的容器实际上是借助红黑树这一种经过优化后的二搜索树形式得以高效运作的。这类改进型二搜索树不仅继承了传统BST的优点,还额外引入了一些机制以确保整体高度保持相对较低水平,从而提高了查询效率[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值