《恋上数据结构第1季》二叉树代码实现

简介: 《恋上数据结构第1季》二叉树代码实现
数据结构与算法笔记目录《恋上数据结构》 笔记目录

想加深 Java 基础推荐看这个Java 强化笔记目录

我的《恋上数据结构》源码(第1季 + 第2季): https://github.com/szluyu99/Data_Structure_Note

我们实现一个 通用二叉树(BinaryTree.java),里面包含所有二叉树类通用的代码。后面学到的 二叉搜索树AVL树红黑树 都需要继承这个 通用二叉树

这一篇是代码实现,先了解 二叉树基础知识

BinaryTree 基础

package com.mj.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 二叉树
 */
@SuppressWarnings("unchecked")
public class BinaryTree<E> {
    protected int size; // 元素数量
    protected Node<E> root; // 根节点

    /**
     * 访问器接口 ——> 访问器抽象类
     * 增强遍历接口
     */
    /*public static interface Visitor<E>{
        void visit(E element);
    }*/
    public static abstract class Visitor<E> {
        boolean stop;
        // 如果返回true,就代表停止遍历
        public abstract boolean visit(E element);
    }

    /**
     * 内部类,节点类
     */
    public static class Node<E> {
        E element;      // 元素值
        Node<E> left;   // 左节点
        Node<E> right;  // 右节点
        Node<E> parent; // 父节点

        public Node(E element, Node<E> parent) {
            this.element = element;
            this.parent = parent;
        }

        public boolean isLeaf() { // 是否叶子节点
            return left == null && right == null;
        }

        public boolean hasTwoChildren() { // 是否有两个子节点
            return left != null && right != null;
        }

        public boolean isLeftChild(){ // 判断自己是不是左子树
            return parent!=null && this==parent.left;
        }
        public boolean isRightChild(){ // 判断自己是不是右子树
            return parent!=null && this==parent.right;
        }
        /*
         * 返回兄弟节点
         */
         public Node<E> sibling() { // 红黑树中用到, 返回兄弟节点
            if (isLeftChild()) {
                return parent.right;
            }
            
            if (isRightChild()) {
                return parent.left;
            }
            return null;
        }    
    }
    
    /**
     * 元素的数量
     */
    public int size() {
        return size;
    }

    /**
     * 是否为空
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 清空所有的元素
     */
    public void clear() {
        root = null;
        size = 0;
    }
    
    /**
     * 创建节点的方法,用于给AVL树创建节点
     */
    protected Node<E> createNode(E element, Node<E> parent){
        return new Node<>(element, parent); // 默认返回一个通用节点
    }
    
}

遍历(先序、中序、后序、层次遍历)

先序遍历: preorder()

/**
 * 前序遍历(递归)
 */
public void preorder(Visitor<E> visitor) {
    if (visitor == null) return;
    preorder(root, visitor);
}
public void preorder(Node<E> node, Visitor<E> visitor) {
    if (node == null || visitor.stop) return;
    // 根
    visitor.stop = visitor.visit(node.element);
    // 左
    preorder(node.left, visitor);
    // 右
    preorder(node.right, visitor);
}

中序遍历: inorder()

/**
 * 中序遍历(递归)
 */
public void inorder(Visitor<E> visitor) {
    if (visitor == null) return;
    inorder(root, visitor);
}
public void inorder(Node<E> node, Visitor<E> visitor) {
    if (node == null || visitor.stop) return;
    // 左
    inorder(node.left, visitor);
    // 根
    if (visitor.stop) return;
    visitor.stop = visitor.visit(node.element);
    // 右
    inorder(node.right, visitor);
}

后序遍历: postorder()

/**
 * 后序遍历(递归)
 */
public void postorder(Visitor<E> visitor) {
    if (visitor == null) return;
    postorder(root, visitor);
}
public void postorder(Node<E> node, Visitor<E> visitor) {
    if (node == null || visitor.stop) return;
    // 左
    postorder(node.left, visitor);
    // 右
    postorder(node.right, visitor);
    // 根
    if (visitor.stop) return;
    visitor.stop = visitor.visit(node.element);
}

层次遍历: levelOrder()

/**
 * 层次遍历(队列)
 */
public void levelOrder(Visitor<E> visitor){
    if(root == null || visitor.stop) return;
    Queue<Node<E>> queue = new LinkedList<>(); // 队列
    queue.offer(root);
    
    while(!queue.isEmpty()){
        Node<E> node = queue.poll();
        if(visitor.visit(node.element)) return;
        
        if(node.left != null) {
            queue.offer(node.left);
        }
        if(node.right != null) {
            queue.offer(node.right);
        }
    }
}

求二叉树的高度: height()

递归实现

/**
 * 求树的高度(递归)
 */
public int height() {
    return height(root);
}
public int height(Node<E> node) {
    if (node == null) return 0;
    return 1 + Math.max(height(node.left), height(node.right));
}

迭代实现

/**
 * 求树的高度高度(迭代)
 */
public int height() {
    if (root == null) return 0;
    // 存储每一层的元素数量, root!=null, 则首层必然有1个元素
    int levelSize = 1;
    int height = 0; // 树的高度
    Queue<Node<E>> queue = new LinkedList<>();
    queue.offer(root);

    while (!queue.isEmpty()) {
        Node<E> node = queue.poll();
        levelSize--;
        if (node.left != null) {
            queue.offer(node.left);
        }
        if (node.right != null) {
            queue.offer(node.right);
        }
        if (levelSize == 0) { // 即将要访问下一层
            levelSize = queue.size(); // 下一层的元素数量
            height++;
        }
    }
    return height;
}

是否为完全二叉树: isComplaete()

/**
 * 是否是完全二叉树
 */
public boolean isComplete() {
    if (root == null) return false;

    Queue<Node<E>> queue = new LinkedList<>();
    queue.offer(root);

    // leaf代表是否要求后面都是叶子节点
    // 比如遍历到一个节点 left == null && right == null
    //  或者是 left != null && right == null
    // 则要求这个节点后面的节点都是叶子节点
    boolean leaf = false;
    while (!queue.isEmpty()) {
        Node<E> node = queue.poll();
        // 要求是叶子结点,但是当前节点不是叶子结点
        if (leaf && !node.isLeaf()) {
            return false;
        }
        if (node.left != null) {
            queue.offer(node.left);
        } else if (node.right != null) {
            // node.left == null && node.right != null
            return false;
        }
        if (node.right != null) {
            queue.offer(node.right);
        } else {
            // node.left == null && node.right == null
            // node.left != null && node.right == null
            leaf = true; // 要求后面都是叶子节点
        }
    }
    return true;
}

求二叉树的节点

前驱节点: predecessor()

在这里插入图片描述

/**
 * 前驱节点: 中序遍历时的前一个节点
 * 求前驱节点
 */
protected Node<E> predecessor(Node<E> node) {
    if (node == null) return null;

    // 前驱节点在左子树中(left.right.right.right....)
    Node<E> p = node.left;
    if (p != null) {
        // 左子树不为空,则找到它的最右节点
        while (p.right != null) {
            p = p.right;
        }
        return p;
    }

    // 能来到这里说明左子树为空, 则从父节点、祖父节点中寻找前驱节点
    // 当父节点不为空, 且某节点为父节点的左子节点
    // 则顺着父节点找, 直到找到【某结点为父节点或祖父节点的右子树中】时
    while (node.parent != null && node.parent.left == node) {
        node = node.parent;
    }

    // 来到这里有以下两种情况:
    // node.parent == null    无前驱, 说明是根结点
    // node.parent...right == node 找到【某结点为父节点或祖父节点的右子树中】
    // 那么父节点就是某节点的前驱节点
    return node.parent;
}

后继节点: successor()

在这里插入图片描述

/**
 * 后继节点: 中序遍历时的后一个节点
 * 求后继节点
 */
protected Node<E> successor(Node<E> node) {
    if (node == null) return null;
    // 后继节点与前驱节点正好相反

    // 后继节点在右子树中(node.right.left.left...)
    if (node.right != null) {
        Node<E> p = node.right;
        while (p.left != null) {
            p = p.left;
        }
        return p;
    }

    // 来到这里说明没有右节点, 则从父节点、祖父节点中寻找后继节点
    // 当父节点不为空, 且某节点为父节点的右子节点
    // 则顺着父节点找, 直到找到【某结点在父节点或祖父节点的左子树中】时
    while (node.parent != null && node.parent.right == node) {
        node = node.parent;
    }

    // 来到这里有以下两种情况:
    // node.parent == null 无前驱,说明是根结点
    // node.parent.left == node 找到【某结点在父节点或祖父节点的左子树中】
    // 那么父节点就是某节点的后继节点
    return node.parent;
}

BinaryTreeInfo 工具

这是 MJ 老师自己写的一款工具,可以方便的打印二叉树,git 地址如下:https://github.com/CoderMJLee/BinaryTrees

/**
 * BinaryTreeInfo 工具,用来打印二叉树
 */
@Override
public Object root() {
    return root;
}
@Override
public Object left(Object node) {
    return ((Node<E>)node).left;
}
@Override
public Object right(Object node) {
    return ((Node<E>)node).right;
}
@Override
public Object string(Object node) {
    Node<E> myNode = (Node<E>)node;
    String parentStr = "null";
    if(myNode.parent != null){
        parentStr = myNode.parent.element.toString();
    }
    return myNode.element + "_p(" + parentStr + ")";
}

二叉树完整源码

package com.mj.tree;

import java.util.LinkedList;
import java.util.Queue;
import com.mj.printer.BinaryTreeInfo;

/**
 * 二叉树(通用)
 */
@SuppressWarnings("unchecked")
// 实现BinaryTreeInfo接口是为了使用打印二叉树的工具,非必须
public class BinaryTree<E> implements BinaryTreeInfo {
    protected int size; // 元素数量
    protected Node<E> root; // 根节点

    /**
     * 访问器接口 ——> 访问器抽象类
     * 增强遍历接口
     */
    /*public static interface Visitor<E>{
        void visit(E element);
    }*/
    public static abstract class Visitor<E> {
        boolean stop;
        // 如果返回true,就代表停止遍历
        public abstract boolean visit(E element);
    }

    /**
     * 内部类,节点类
     */
    public static class Node<E> {
        E element;      // 元素值
        Node<E> left;   // 左节点
        Node<E> right;  // 右节点
        Node<E> parent; // 父节点

        public Node(E element, Node<E> parent) {
            this.element = element;
            this.parent = parent;
        }

        public boolean isLeaf() { // 是否叶子节点
            return left == null && right == null;
        }

        public boolean hasTwoChildren() { // 是否有两个子节点
            return left != null && right != null;
        }
        public boolean isLeftChild(){ // 判断自己是不是左子树
            return parent!=null && this==parent.left;
        }
        public boolean isRightChild(){ // 判断自己是不是右子树
            return parent!=null && this==parent.right;
        }
        /*
         * 返回兄弟节点
         */
         public Node<E> sibling() { // 红黑树中用到, 返回兄弟节点
            if (isLeftChild()) {
                return parent.right;
            }
            
            if (isRightChild()) {
                return parent.left;
            }
            return null;
        }
    }


    /**
     * 元素的数量
     */
    public int size() {
        return size;
    }

    /**
     * 是否为空
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 清空所有的元素
     */
    public void clear() {
        root = null;
        size = 0;
    }

    /**
     * 前序遍历
     */
    public void preorder(Visitor<E> visitor) {
        if (visitor == null) return;
        preorder(root, visitor);
    }
    public void preorder(Node<E> node, Visitor<E> visitor) {
        if (node == null || visitor.stop) return;
        // 根
        visitor.stop = visitor.visit(node.element);
        // 左
        preorder(node.left, visitor);
        // 右
        preorder(node.right, visitor);
    }

    /**
     * 中序遍历
     */
    public void inorder(Visitor<E> visitor) {
        if (visitor == null) return;
        inorder(root, visitor);
    }
    public void inorder(Node<E> node, Visitor<E> visitor) {
        if (node == null || visitor.stop) return;
        // 左
        inorder(node.left, visitor);
        // 根
        if (visitor.stop) return;
        visitor.stop = visitor.visit(node.element);
        // 右
        inorder(node.right, visitor);
    }

    /**
     * 后序遍历
     */
    public void postorder(Visitor<E> visitor) {
        if (visitor == null) return;
        postorder(root, visitor);
    }
    public void postorder(Node<E> node, Visitor<E> visitor) {
        if (node == null || visitor.stop) return;
        // 左
        postorder(node.left, visitor);
        // 右
        postorder(node.right, visitor);
        // 根
        if (visitor.stop) return;
        visitor.stop = visitor.visit(node.element);
    }

    /**
     * 层次遍历
     */
    public void levelOrder(Visitor<E> visitor) {
        if (root == null || visitor.stop) return;
        Queue<Node<E>> queue = new LinkedList<>(); // 队列
        queue.offer(root);

        while (!queue.isEmpty()) {
            Node<E> node = queue.poll();
            if (visitor.visit(node.element)) return;

            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
    }

    /**
     * 求树的高度(递归)
     */
    public int height1() {
        return height1(root);
    }
    public int height1(Node<E> node) {
        if (node == null) return 0;
        return 1 + Math.max(height1(node.left), height1(node.right));
    }

    /**
     * 求树的高度高度(迭代)
     */
    public int height() {
        if (root == null) return 0;
        int levelSize = 1; // 存储每一层的元素数量
        int height = 0; // 树的高度
        Queue<Node<E>> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            Node<E> node = queue.poll();
            levelSize--;
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
            if (levelSize == 0) { // 即将要访问下一层
                levelSize = queue.size();
                height++;
            }
        }
        return height;
    }

    /**
     * 是否是完全二叉树
     */
    public boolean isComplete() {
        if (root == null) return false;

        Queue<Node<E>> queue = new LinkedList<>();
        queue.offer(root);

        // leaf代表是否要求后面都是叶子节点
        // 比如遍历到一个节点 left == null && right == null
        //  或者是 left != null && right == null
        // 则要求这个节点后面的节点都是叶子节点
        boolean leaf = false;
        while (!queue.isEmpty()) {
            Node<E> node = queue.poll();
            // 要求是叶子结点,但是当前节点不是叶子结点
            if (leaf && !node.isLeaf()) {
                return false;
            }
            if (node.left != null) {
                queue.offer(node.left);
            } else if (node.right != null) {
                // node.left == null && node.right != null
                return false;
            }
            if (node.right != null) {
                queue.offer(node.right);
            } else {
                // node.left == null && node.right == null
                // node.left != null && node.right == null
                leaf = true; // 要求后面都是叶子节点
            }
        }
        return true;
    }

    /**
     * 前驱节点: 中序遍历时的前一个节点
     * 求前驱节点
     */
    protected Node<E> predecessor(Node<E> node) {
        if (node == null) return null;

        // 前驱节点在左子树中(left.right.right.right....)
        Node<E> p = node.left;
        if (p != null) {
            // 左子树不为空,则找到它的最右节点
            while (p.right != null) {
                p = p.right;
            }
            return p;
        }

        // 能来到这里说明左子树为空, 则从父节点、祖父节点中寻找前驱节点
        // 当父节点不为空, 且某节点为父节点的左子节点
        // 则顺着父节点找, 直到找到【某结点为父节点或祖父节点的右子树中】时
        while (node.parent != null && node.parent.left == node) {
            node = node.parent;
        }

        // 来到这里有以下两种情况:
        // node.parent == null 无前驱, 说明是根结点
        // node.parent...right == node 找到【某结点为父节点或祖父节点的右子树中】
        // 那么父节点就是某节点的前驱节点
        return node.parent;
    }

    /**
     * 后继节点: 中序遍历时的后一个节点
     * 求后继节点
     */
    protected Node<E> successor(Node<E> node) {
        if (node == null) return null;
        // 后继节点与前驱节点正好相反

        // 后继节点在右子树中(node.right.left.left...)
        if (node.right != null) {
            Node<E> p = node.right;
            while (p.left != null) {
                p = p.left;
            }
            return p;
        }

        // 来到这里说明没有右节点, 则从父节点、祖父节点中寻找后继节点
        // 当父节点不为空, 且某节点为父节点的右子节点
        // 则顺着父节点找, 直到找到【某结点在父节点或祖父节点的左子树中】时
        while (node.parent != null && node.parent.right == node) {
            node = node.parent;
        }

        // 来到这里有以下两种情况:
        // node.parent == null 无前驱,说明是根结点
        // node.parent.left == node 找到【某结点在父节点或祖父节点的左子树中】
        // 那么父节点就是某节点的后继节点
        return node.parent;
    }
    
    /**
     * 创建节点的方法,用于给AVL树创建节点
     */
    protected Node<E> createNode(E element, Node<E> parent){
        return new Node<>(element, parent); // 默认返回一个通用节点
    }

    /**
     * BinaryTreeInfo 工具,用来打印二叉树
     */
    @Override
    public Object root() {
        return root;
    }

    @Override
    public Object left(Object node) {
        return ((Node<E>) node).left;
    }

    @Override
    public Object right(Object node) {
        return ((Node<E>) node).right;
    }

    @Override
    public Object string(Object node) {
        Node<E> myNode = (Node<E>) node;
        String parentStr = "null";
        if (myNode.parent != null) {
            parentStr = myNode.parent.element.toString();
        }
        return myNode.element + "_p(" + parentStr + ")";
    }
}
相关文章
|
11天前
|
存储 算法 关系型数据库
数据结构与算法学习二一:多路查找树、二叉树与B树、2-3树、B+树、B*树。(本章为了解基本知识即可,不做代码学习)
这篇文章主要介绍了多路查找树的基本概念,包括二叉树的局限性、多叉树的优化、B树及其变体(如2-3树、B+树、B*树)的特点和应用,旨在帮助读者理解这些数据结构在文件系统和数据库系统中的重要性和效率。
13 0
数据结构与算法学习二一:多路查找树、二叉树与B树、2-3树、B+树、B*树。(本章为了解基本知识即可,不做代码学习)
|
11天前
|
存储 算法 搜索推荐
数据结构与算法学习十七:顺序储存二叉树、线索化二叉树
这篇文章主要介绍了顺序存储二叉树和线索化二叉树的概念、特点、实现方式以及应用场景。
14 0
数据结构与算法学习十七:顺序储存二叉树、线索化二叉树
|
13天前
|
Java
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(二)
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(二)
23 1
|
13天前
|
算法 Java C语言
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(一)
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(一)
20 1
|
8天前
|
存储 算法 索引
HashMap底层数据结构及其增put删remove查get方法的代码实现原理
HashMap 是基于数组 + 链表 + 红黑树实现的高效键值对存储结构。默认初始容量为16,负载因子为0.75。当存储元素超过容量 * 负载因子时,会进行扩容。HashMap 使用哈希算法计算键的索引位置,通过链表或红黑树解决哈希冲突,确保高效存取。插入、获取和删除操作的时间复杂度接近 O(1)。
18 0
|
11天前
|
存储 算法
探索数据结构:分支的世界之二叉树与堆
探索数据结构:分支的世界之二叉树与堆
|
11天前
|
存储 算法
数据结构与算法学习十六:树的知识、二叉树、二叉树的遍历(前序、中序、后序、层次)、二叉树的查找(前序、中序、后序、层次)、二叉树的删除
这篇文章主要介绍了树和二叉树的基础知识,包括树的存储方式、二叉树的定义、遍历方法(前序、中序、后序、层次遍历),以及二叉树的查找和删除操作。
15 0
|
11天前
|
算法 程序员 索引
数据结构与算法学习七:栈、数组模拟栈、单链表模拟栈、栈应用实例 实现 综合计算器
栈的基本概念、应用场景以及如何使用数组和单链表模拟栈,并展示了如何利用栈和中缀表达式实现一个综合计算器。
16 1
数据结构与算法学习七:栈、数组模拟栈、单链表模拟栈、栈应用实例 实现 综合计算器
|
11天前
初步认识栈和队列
初步认识栈和队列
35 10
|
5天前
数据结构(栈与列队)
数据结构(栈与列队)
11 1