树-数据结构

概述

  1. 数据结构+算法+设计模式 == 内功
  2. 为什么要学习树结构?
    • 因为当数据量比较大时,链表、数组这样的线性结构无法快速查找到需要的数据,访问时间太慢,性能太低。
  3. 树的基本概念
    • 定义数的自然方式是递归,因为可以把树的结构抽象为一个个左中右最小单元组成的,一颗树就是有限节点的集合。
    • 这个集合可以是空集,若不是空集,则树由称作根的节点r和0个或多个非空的子树。
    • 树中的元素称作节点,最上面的为根节点,中间的为普通节点(分支节点),没有子节点的节点称作叶子节点。
      在这里插入图片描述
  4. 树的遍历
  • 先序遍历:对节点的遍历顺序是,先处理父节点,再遍历左子树和右子树。
    比如上面的树结构先序遍历的打印是:root->usr->local->lib->soft->bin->system->opt
  • 后序遍历:先遍历左子树,再遍历右子树,最后处理父节点。
    比如上面的树结构的后序遍历打印是: local->soft->lib->usr->system->bin->opt->root

二叉树

概念

  1. 二叉树的每个节点的子节点都不得多于2个。
  2. 如果二叉树的所有叶子节点都在最后一层,并且节点个数为2^n -1,n为层数,则我们称为满二叉树。
  3. 如果二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层在右边连续(从右边开始连续),我称为完全二叉树。
  4. 对于二叉树独有的遍历方式,中序遍历:先遍历左子树,在处理父节点,最后遍历右子树。
  5. 典型的应用场景是表达式树,比如一个数学运算:a+b+cd,可以把树的叶子看做操作数a,b,c,d,分支节点看做操作符+,
    在这里插入图片描述

二叉树三种遍历方式

场景:
水浒传中不同的英雄人物放在树中,进行遍历
遍历规则
1. 前序遍历:先处理当前节点,如果当前节点的左子树不空,递归前序遍历;再遍历当前节点的又子树,如果右子树不空,递归前序遍历。
2. 中序遍历:先处理当前节点的左子树,如果左子树不空,递归中序遍历;之后处理当前节点;最后然后右子树不空,递归中序遍历。
3. 后序遍历:先处理当前节点的左子树,如果左子树不空,递归后序遍历;之后处理右子树,如果不空,递归后序遍历;最后处理当前节点。

  1. 定义节点类
public 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. 二叉树的遍历方法实现
    遍历的核心就是递归调用。
public class BinaryTree {
    
    
    private HeroNode root;

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

    // 前序遍历
    public void preOrder() {
    
    
        if (!Objects.isNull(root)) {
    
    
            preOrder(root);
        }
    }

    // 中序遍历
    public void infixOrder() {
    
    
        if (!Objects.isNull(root)) {
    
    
            infixOrder(root);
        }
    }
	
	// 后序遍历
    public void postOrder() {
    
    
        if (!Objects.isNull(root)) {
    
    
            postOrder(root);
        }
    }

    private void postOrder(HeroNode node) {
    
    
        // 遍历左子树
        if (!Objects.isNull(node.getLeft())) {
    
    
            postOrder(node.getLeft());
        }

        // 遍历右子树
        if (!Objects.isNull(node.getRight())) {
    
    
            postOrder(node.getRight());
        }

        // 遍历父节点
        System.out.println(node);
    }

    private void infixOrder(HeroNode node) {
    
    
        // 遍历左子树
        if (!Objects.isNull(node.getLeft())) {
    
    
            infixOrder(node.getLeft());
        }
        // 遍历父节点
        System.out.println(node);
        // 遍历右子树
        if (!Objects.isNull(node.getRight())) {
    
    
            infixOrder(node.getRight());
        }
    }

    private void preOrder(HeroNode node) {
    
    
        // 先遍历父节点
        System.out.println(node);
        // 遍历左子树
        if (!Objects.isNull(node.getLeft())) {
    
    
            preOrder(node.getLeft());
        }
        // 遍历右子树
        if (!Objects.isNull(node.getRight())) {
    
    
            preOrder(node.getRight());
        }
    }
}
  1. 客户端调用程序
public class Client {
    
    
    public static void main(String[] args) {
    
    
        HeroNode node1 = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "卢俊义");
        HeroNode node3 = new HeroNode(3, "吴用");
        node1.setLeft(node2);
        node2.setRight(node3);

        BinaryTree tree = new BinaryTree();
        tree.setRoot(node1);
        tree.preOrder();
        tree.infixOrder();
        tree.postOrder();
    }
}

二叉树的删除

场景:
规定:

  1. 如果删除的节点是叶子节点,直接删除该叶子节点;
  2. 如果删除的节点时非叶子节点,删除该子树;
    思路:
  3. 首先考虑如果树是空树、如果root节点是需要删除的节点,则将整个二叉树置空。
  4. 因为二叉树是单向的,没有前驱指针,所有我们是判断当前节点的子节点是否是需要删除的节点,而不是判断当前这个节点是不是要删除。
  5. 如果当前节点的左子节点不为空,且左子节点是需要删除的节点,直接左子节点置空且返回。
  6. 如果当前节点的右子节点不为空,且右子节点是需要删除的节点,直接有子节点置空且返回。
  7. 如果左子树不为空,递归删除方法。
  8. 如果右子树不为空,递归删除方法。
    在这里插入图片描述
  1. 删除方法的实现
public void delNode(int no) {
    
    
        Optional<HeroNode> rootNode = Optional.ofNullable(this.root);
        rootNode.ifPresent(item -> {
    
    
            if (item.getNo() == no) {
    
    
                root = null;
                return;
            }
            delNode(no, item);
        });

    }

    private void delNode(int no, HeroNode node) {
    
    
        if (node.getLeft() != null && node.getLeft().getNo() == no) {
    
    
            node.setLeft(null);
            return;
        }
        if (node.getRight() != null && node.getRight().getNo() == no) {
    
    
            node.setRight(null);
            return;
        }
        if (Optional.ofNullable(node.getLeft()).isPresent()) {
    
    
            delNode(no, node.getLeft());
        }
        if (Optional.ofNullable(node.getRight()).isPresent()) {
    
    
            delNode(no, node.getRight());
        }
    }
  1. 客户端调用程序
public class Client {
    
    
    public static void main(String[] args) {
    
    
        HeroNode node1 = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");

        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setLeft(node5);
        node3.setRight(node4);
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.setRoot(node1);
        System.out.println("删除前");
        binaryTree.preOrder();
        binaryTree.delNode(5);
        System.out.println("删除后");
        binaryTree.preOrder();
    }
}

猜你喜欢

转载自blog.csdn.net/tianzhonghaoqing/article/details/112731500