学习笔记-算法-11-二叉树-3

28 篇文章 0 订阅
18 篇文章 0 订阅

二叉排序树

  • 二叉排序树
    • 左子节点比当前节点小
    • 右子节点比当前节点大
public class Demo{
    public static void main(String[] args){
        int[] arr = {1,4,5,6,8,9,3};
        
        BinarySortTree binarySortTree = new BinarySortTree();
        for(int i=0;i<arr.lengthli++){
            binarySortTree.add(new Node(arr[i]));
        }
        
        binarySortTree.infixOrder();
    }
    
}

class BinarySortTree{
    private Node root;
    public void add(Node node){
        if(root==null){
            root = node;
        }else{
            root.add(node);
        }
    }
    
    // 遍历
    public void infixOrder(){
        if(root!=null){
            root.infixOrder();
        }else{
            // null
        }
    }
}

// Node节点
class Node{
    int value;
    Node left;
    Node right;
    
    public Node(int value){
        this.value = value;
    }
    
    // 添加
    // 满足二叉排序树
    public void add(Node node){
        if(node==null){
            return;
        }
        // 传入的节点的值与当子树的根节点的值的关系
        if(node.value < this.value){
            if(this.left == null){
                this.left = node;
            }else{
                // 递归
                this.left.add(node);
            }
        }else{
            if(this.right==null){
                this.right=node;  
            }else{
                this.right.add(node);
            }
        }
    }
    
    // 遍历
    
    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
             this.right.infixOrder();
        }
    }
}

删除


class BinarySortTree{
    private Node root;
    // 查找要删除的节点
    public Node search(int value){
        if(root==null){
            return null;
        }else{
            return root.search(value);
        }
    }
    
    // 查找父节点
    public Node searchParent(int value){
        if(root==null){
            return null;
        }else{
            return root.searchParent(value);
        }
    }
    
    // 传入node当作二叉排序树的根节点
    // 返回node为根节点二叉排序树最小节点的值
    public int delRightTreeMin(Node node){
        Node target = node;
        // 循环查找左子节点
        while(target.left !=null){
            target = target.left;
        }
        // target指向最小节点
        // 删除
        delNode(target.value)return target.value
    }
    
    // 删除节点
    public void delNode(int value){
        if(root == null){
            return;
        }else{
            Node targetNode =search(value);
            if(tarhetNode==null){
                return;
            }
            // 如果二叉树只有一个节点
            if(root.left==null&&root.right==null){
                root = null;
                return;
            }
            
            // 查找targetNode父节点
            Node parent = searchParent(value);
            // 如果删除的节点是叶子节点
            if(targetNode.left == null && targetNode.right == null){
                // 判断targetNode是父节点的左子节点还是右子节点
                if(parent.left ! = null && parent/left.value==value){
                    parent.left = null;
                }else if(parent.right!=null && parent.right.value==value){
                    parent.right = null;
                }
            }else if(targetNode.left!=null&&targetNode.right!=null){
                // 两颗子树
                int minVal = delRightTree(target.right);
                targetNode.value = minVal;
                
            }else{
                // 一颗子树
                // 要删除node有左子节点
                if(targetNode.left!=null){
                    if(parent!=null){
                        if(parent.left.value == value){
                            parent.left = targetNode.left;
                        }else{
                            // 右子节点
                            parent.right = targetNode.left;
                        }
                    }else{
                        root = targetNode.left;
                    }
                    
                }else{
                    // 有右子节点
                    if(parent!=null){
                        if(parent.left.value==value){
                            parent.left = targetNode.right;
                        }else{
                            parent.right = targetNode.right;
                        }
                    }else{
                        root = targetNode.right;
                    }
                    
                }
            }
            
        }
    }
    
    public void add(Node node){
        if(root==null){
            root = node;
        }else{
            root.add(node);
        }
    }
    
    // 遍历
    public void infixOrder(){
        if(root!=null){
            root.infixOrder();
        }else{
            // null
        }
    }
}

// Node节点
class Node{
    int value;
    Node left;
    Node right;
    
    public Node(int value){
        this.value = value;
    }
    
    // 查找要删除的节点
    public Node search(int value){
        if(value==this.value){
            return this;
        }else if(value<this.value){
            // 向左查
            if(this.left==null){
                return null;
            }
            return this.left.search(value);
        }else if(){
            // 向右查
            if(this.right==null){
                return  null;
            }
            return this.right.search(value);
        }
    }
    
    // 查找要删除节点父节点
    public Node searchParent(int value){
        // 当前节点就是要删除节点的父节点
        if(this.left!=null&&this.left.value==value || this.right!=null&&this.right.value==value){
            return this;
        }else{
            if(value<this.value&&this.left!=null){
                return this.left.searchParent(value);
            }else if(value>=this.value&&this.right!=null){
                return this.right.searchParent(value);
            }else{
                return null;
            }
        }
    }
    
    // 添加
    // 满足二叉排序树
    public void add(Node node){
        if(node==null){
            return;
        }
        // 传入的节点的值与当子树的根节点的值的关系
        if(node.value < this.value){
            if(this.left == null){
                this.left = node;
            }else{
                // 递归
                this.left.add(node);
            }
        }else{
            if(this.right==null){
                this.right=node;  
            }else{
                this.right.add(node);
            }
        }
    }
    
    // 遍历
    
    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
             this.right.infixOrder();
        }
    }
}

平衡二叉树(AVL树)

  • 左右子树高度差不超过1
  • 平衡二叉树
    • 红黑树
    • AVL
    • 替罪羊树
    • Treap
    • 伸展树
public class Demo{
    public static void main(String[] args){
        int[] arr = {4,3,6,5,7,8};
        AVLTree avlTree = new AVLTree();
        for(int i=0;i<arr.length;i++){
            avlTree.add(new Node(i));
        }
        // 遍历
        avlTree.infixOrder();
        
        // avlTree.getRoot().height()
        // avlTree.getRoot().leftHeight()
        // avlTree.getRoot().rightHeight()
        
        
        
    }
}

class AVLTree{
    // 同前
    public void add(){
        
    }
    public void infixOrder(){
        
    }
    
}

// Node节点
class Node{
    int value;
    Node left;
    Node right;
    
    public Node(int value){
        this.value = value;
    }
    
    // 左子树高度
    public int leftHeight(){
        if(left == null){
            return 0;
        }
        return left.height();
    }
    
    // 右子树高度
    public int rightHeight(){
        if(right==null){
            return 0;
        }
        return right.height();
    }
    
    // 当前节点高度
    public int height(){
        return Math.max(left==null?0:left.height(),right==null?0:right.height())+1
    }
    
    // 左旋
    private void leftRotate(){
        // 创建新节点,以当前根节点的值
        Node newNode = new Node(value);
        // 把新的节点的左子树设置成当前节点左子树
        newNode.left = left;
        // 把新的节点的右子树设置成当前节点右子树的左子树
        newNode.right = right.left;
        // 当前节点的值替换为右子节点的值
        value = right.value;
        // 把当前节点的右子树设置成当前节点右子树的右子树
        right = right.right;
        // 把当前节点的左子树设置成新节点
        left = newNode;
    }
    // 右旋
    private void rightRotate(){
         Node newNode = new Node(value);
         newNode.right = right;
         newNode.left = left.right;
         value = left.value;
         left = left.left;
         right = newNode;
         
    }
    
    
   
    
    // 遍历
    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
             this.right.infixOrder();
        }
    }
     public void add(Node node){
        if(node==null){
            return;
        }
        // 传入的节点的值与当子树的根节点的值的关系
        if(node.value < this.value){
            if(this.left == null){
                this.left = node;
            }else{
                // 递归
                this.left.add(node);
            }
        }else{
            if(this.right==null){
                this.right=node;  
            }else{
                this.right.add(node);
            }
        }
        // 当添加完一个节点后,如果右子树高度-左子树高度>1,左旋转
        if(rightHeight()-leftHeight()>1){
            if(right!=null && right.leftHeightx()>right.rightHeight()){
                // 先进行子节点右旋转
                left.rightRotate();
                // 左旋转
                leftRotate();
            }else{
                // 左旋转
                leftRotate();
            }
            return;
        }
        if(leftHeight()-rightHeight()>1){
            if(left!=null && left.rightHeight()>left.leftHeight()){
                // 先进行子节点左旋转
                left.leftRotate();
                // 右旋转
                rightRotate();
            }else{
                // 右旋转
                rightRotate();
            }
            
        }
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值