数据结构2-树

本文详细介绍了树数据结构中的二叉树,包括满二叉树、完全二叉树的概念,前序、中序、后序遍历以及查找和删除操作。特别讨论了顺序二叉树和线索化二叉树,重点讲解了赫夫曼树及其编码原理,用于数据压缩和解压。同时,探讨了二叉排序树的查找、添加、遍历和删除操作,并引入了平衡二叉树及其旋转调整。此外,文章还简单介绍了图的基本概念,包括图的构建、广度优先遍历和深度优先遍历。

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

 

目录

二叉树

顺序二叉树

线索化二叉树

赫夫曼树

霍夫曼编码

数据压缩

数据解压

赫夫曼文件解压与压缩

二叉排序树

查找、添加、遍历、删除

平衡二叉树


 

二叉树

满二叉树 完全二叉树

前序 中序  后序

查找 删除

class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    //删除结点
    public void delNode(int no) {
        if(root != null) {
            //如果只有一个root结点, 这里立即判断root是不是就是要删除结点
            if(root.getNo() == no) {
                root = null;
            } else {
                //递归删除
                root.delNode(no);
            }
        }else{
            System.out.println("空树,不能删除~");
        }
    }

    public void preOder(){
        if (this.root!=null)
            this.root.preOder();
        else
            System.out.println("二叉树为空,无法遍历");
    }
    //中序遍历
    public void infixOrder() {
        if(this.root != null) {
            this.root.infixOder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //后序遍历
    public void postOrder() {
        if(this.root != null) {
            this.root.postOder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    //前序查找
    public HeroNode preOderSearch(int no){
        if (root!=null)
            return root.preOderSearch(no);
        else
            return null;
    }
    //中序遍历
    public HeroNode infixOrderSearch(int no) {
        if(root != null) {
            return root.infixOderSearch(no);
        }else {
            return null;
        }
    }
    //后序遍历
    public HeroNode postOrderSearch(int no) {
        if(root != null) {
            return this.root.postOderSearch(no);
        }else {
            return null;
        }
    }
}

class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //递归删除结点
    //1.如果删除的节点是叶子节点,则删除该节点
    //2.如果删除的节点是非叶子节点,则删除该子树
    public void delNode(int no) {
        //思路
		/*
		 * 	1. 因为我们的二叉树是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点.
			2. 如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除)
			3. 如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
			4. 如果第2和第3步没有删除结点,那么我们就需要向左子树进行递归删除
			5.  如果第4步也没有删除结点,则应当向右子树进行递归删除.
		 */
        //2. 如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除)
        if(this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        //3.如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
        if(this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        //4.我们就需要向左子树进行递归删除
        if(this.left != null) {
            this.left.delNode(no);
        }
        //5.则应当向右子树进行递归删除
        if(this.right != null) {
            this.right.delNode(no);
        }
    }

    //前序遍历
    public void preOder(){
        System.out.println(this);
        if (this.left!=null)
            this.left.preOder();
        if (this.right!=null)
            this.right.preOder();

    }

    //中序
    public void infixOder(){
        if (this.left!=null)
            this.left.infixOder();
        System.out.println(this);
        if (this.right!=null)
            this.right.infixOder();
    }
    //后序
    public void postOder(){
        if (this.left!=null)
            this.left.postOder();
        if (this.right!=null)
            this.right.postOder();
        System.out.println(this);
    }

    //前序查找
    public HeroNode preOderSearch(int no){
        System.out.println("前序遍历查找");
        if (this.no==no)
            return this;
        HeroNode resNode=null;
        if (this.left!=null)
            resNode=this.left.preOderSearch(no);
        if (resNode!=null)
            return resNode;
        if (this.right!=null)
            resNode=this.right.preOderSearch(no);
        return resNode;
    }

    //中序查找
    public HeroNode infixOderSearch(int no){
        HeroNode resNode=null;
        if (this.left!=null)
            resNode=this.left.infixOderSearch(no);
        if (resNode!=null)
            return resNode;
        System.out.println("中序遍历查找");
        if (this.no==no)
            return this;
        if (this.right!=null)
            resNode=this.right.infixOderSearch(no);
        return resNode;
    }

    //又序查找
    public HeroNode postOderSearch(int no){
        HeroNode resNode=null;
        if (this.left!=null)
            resNode=this.left.postOderSearch(no);
        if (resNode!=null)
            return resNode;
        if (this.right!=null)
            resNode=this.right.postOderSearch(no);
        if (resNode!=null)
            return resNode;
        System.out.println("进入后序查找");
        //如果左右子树都没有找到,就比较当前结点是不是
        if(this.no == no) {
            return this;
        }
        return resNode;
    }

}

顺序二叉树

class ArrBinaryTree{
    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }
    //重载preOrder
    public void preOder() {
        this.preOder(0);
    }

//前序
    public void preOder(int index){
        if(arr == null || arr.length == 0) {
            System.out.println("数组为空,不能按照二叉树的前序遍历");
        }
        System.out.println(arr[index]);

        //向左递归遍历
        if((index * 2 + 1) < arr.length) {
            preOder(2 * index + 1 );
        }
        //向右递归遍历
        if((index * 2 + 2) < arr.length) {
            preOder(2 * index + 2);
        }

    }
}

线索化二叉树

class threadedBinaryTree{
    private HeroNode root;
    //需要创建指向前驱节点的指针
    private HeroNode pre=null;

    //重载一把threadedNodes方法
    public void threadedNodes() {
        this.threadedNodes(root);
    }

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void threadedList(){
        HeroNode node =root;
        while (node!=null){
            while (node.getLeftType()==0){
                node=node.getLeft();
            }
            System.out.println(node);
            while (node.getRightType()==1){
                node=node.getRight();
                System.out.println(node);
            }
            node=node.getRight();
        }
    }
    public void threadedNodes(HeroNode node){
        if (node==null)
            return;
        threadedNodes(node.getLeft());

        //(二)线索化当前结点[有难度]
        //处理当前结点的前驱结点
        if (node.getLeft()==null){
            //让当前节点左指针指向前驱节点
            node.setLeft(pre);
            node.setLeftType(1);  //指向前驱节点
        }
        //处理后继结点
        if(pre!=null&&pre.getRight()==null){
            pre.setRight(node);
            pre.setRightType(1);
        }
        //!!! 每处理一个结点后,让当前结点是下一个结点的前驱结点
        pre=node;

        threadedNodes(node.getRight());
    }
    //删除结点
    public void delNode(int no) {
        if(root != null) {
            //如果只有一个root结点, 这里立即判断root是不是就是要删除结点
            if(root.getNo() == no) {
                root = null;
            } else {
                //递归删除
                root.delNode(no);
            }
        }else{
            System.out.println("空树,不能删除~");
        }
    }
    public void preOder(){
        if (this.root!=null)
            this.root.preOder();
        else
            System.out.println("二叉树为空,无法遍历");
    }
    //中序遍历
    public void infixOrder() {
        if(this.root != null) {
            this.root.infixOder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //后序遍历
    public void postOrder() {
        if(this.root != null) {
            this.root.postOder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    //前序查找
    public HeroNode preOderSearch(int no){
        if (root!=null)
            return root.preOderSearch(no);
        else
            return null;
    }
    //中序遍历
    public HeroNode infixOrderSearch(int no) {
        if(root != null) {
            return root.infixOderSearch(no);
        }else {
            return null;
        }
    }
    //后序遍历
    public HeroNode postOrderSearch(int no) {
        if(root != null) {
            return this.root.postOderSearch(no);
        }else {
            return null;
        }
    }
}


class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;
    //说明
    //1. 如果leftType == 0 表示指向的是左子树, 如果 1 则表示指向前驱结点
    //2. 如果rightType == 0 表示指向是右子树, 如果 1表示指向后继结点
    private int leftType;
    private int rightType;
    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getLeftType() {
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值