笔记-二叉查找树

本文介绍了一种二叉查找树的实现方式,该树的性质是对于树中的每个节点X,其左子树中的所有项的值都小于X中的项,而右子树相反。文章提供了插入、删除、查找最小值和最大值等基本操作的具体实现。

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

      二叉查找树的性质是,对于树中的每个节点X,它的左子树中的所有项的值都小于X中的项,而右子树相反。这样只需递归的比较两项的大小便可很容易的从树内查找出所需元素。通过实现Comparable接口的compareTo方法来对两项进行比较。

一个节点代表其与其的后代节点构成的树,根节点代表整棵树。


页面卡死两次!!白打了太多!!学校10点断网!!注释不重新加了!!!


public class BinarySearchTree>{
    private static class BinaryNode{
        
        AnyType element;
        BinaryNode left;
        BinaryNode right;
        
        BinaryNode(AnyType theElement){
            this(theElement,null,null);
        }
        
        BinaryNode(AnyType theElement,BinaryNode lt,BinaryNode rt){
            element = theElement;
            left = lt;
            right = rt;
        }
    
    }    
    
    private BinaryNode root;
    
    public BinarySearchTree(){
        root = null;
    }
    
    public void makeEmpty(){
        root = null;
    }
    
    public boolean isEmpty(){
        return root == null;
    }
    
    public boolean contains(AnyType x){
        return contains(x,root);
    }
    
    public AnyType findMin(){
        if(isEmpty()) throw new UnderflowException();
        return findMin(root).element;
    }
    
    public AnyType findMax(){
        if(isEmpty()) throw new UnderflowException();
        return findMax(root).element;
    }
    
    public void insert(AnyType){
        root = insert(x,root);
    }  
    
    publci void remove(AnyType x){
        root  = remove(x,root);
    }
    
    public void printTree(){
        if(isEmpty())
            sysout("Empty Tree");
        else
            printTree(root);
    }
    
    private void printTree(BinaryNode t){
        if(t!=null){
            printTree(t.left);
            sysout(t.element);
            printTree(t.right);
        }
    }
    
    
    public boolean contains(AnyType x,BinaryNode t){
        if(t==null)
            return false;
        
        int compareResult = x.compareTo(t.element);
        
        if(compareResult<0)
            return contains(x,t.left);
        else if(compareResult>0)
            return contains(x,t,right);
        else
            return true;
    }
    
    private BianryNode findMin(BianryNode t){
        if(t==null)
            return null;
        else if(t.left==null)
             return t;
        return findMin(t.left);
    }
    
    private BianryNode findMax(BianryNode t){
        if(t!=null)
            while(t.right!=ull)
                t = t.right;
        return t;
    }
    
    public BianryNode insert(AnyType x,BinaryNode t){
        if(t==null)
            return new BinaryNode(x,null,null);
            
        int compareResult = x.compareTo(t.element);
        
        if(compareResult<0)
            t.left = insert(x,t.left);
        else if(compareResult>0)
             t.right = insert(x,t.right);
        else ;
        
        return t;
    }
    
    public BianryNode remove(AnyType x,BinaryNode t){
        if(t==null)
            return t;
        int compareResult = x.compareTo(t.element);
        
        if(compareResult<0)
            t.left = remove(x,t.left);
        else if(compareResult>0)
             t.right = remove(x,t.right);
        else if(t.left!=null && t.right !=null){
            t.element = findMin(t.right).element;
            t.right = remove(t.element,t.right);
        }
        else
            t = (t.left !=null) ? t.left:t.right;
        return t;
    }
    
}    
    
    

### 关于二叉查找树的数据结构 #### 定义与特性 二叉查找树(Binary Search Tree, BST),是一种特殊的二叉树数据结构,其特点是对于任意结点而言,左子树上的所有键值都小于该结点的键值;而右子树上的所有键值则大于该结点的键值[^2]。 #### 动态集合操作的支持 这种特定的排列方式使得二叉查找树能够高效地支持多种动态集合的操作。具体来说,它不仅允许快速执行基本查询功能如`SEARCH`(查找指定结点)、`MINIMUM`(找到具有最小关键字的结点)、`MAXIMUM`(定位拥有最大关键字的结点),还能处理更复杂的任务像寻找某个元素之前的直接前驱者(`PREDECESSOR`)或是紧接在其后的后继者(`SUCCESSOR`)。除此之外,通过适当调整指针链接关系即可完成新增加成员(`INSERT`)以及移除现有条目(`DELETE`)的动作。 #### 删除操作的具体情形 值得注意的是,在涉及删除节点的过程中为了保持原有属性不变,通常依据目标对象所处位置的不同采取差异化的策略: - **叶子节点**:如果待删项位于末端即无任何后代,则只需简单切断连接并释放空间; - **单分支内部节点**:当面临仅有一个直系继承者的状况时,可直接用后者替代前者的位置; - **双重分支内部节点**:最棘手的情形莫过于存在两个子女的情况,这时一般会选择右侧序列中最左侧的小孩作为替补人选,并依照前述原则重新安置其余部分[^3]。 ```python class TreeNode: def __init__(self, key=None): self.key = key self.left = None self.right = None self.parent = None def tree_delete(node): if node is None or not isinstance(node, TreeNode): return # Node with no children (a leaf) if node.left is None and node.right is None: transplant(node, None) elif node.left is None: transplant(node, node.right) elif node.right is None: transplant(node, node.left) else: successor = minimum(node.right) if successor.parent != node: transplant(successor, successor.right) successor.right = node.right successor.right.parent = successor transplant(node, successor) successor.left = node.left successor.left.parent = successor def transplant(u, v): if u.parent is None: root = v elif u == u.parent.left: u.parent.left = v else: u.parent.right = v if v is not None: v.parent = u.parent def minimum(x): while x.left is not None: x = x.left return x ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值