|
| 1 | +package io.github.dunwu.ds.tree; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Queue; |
| 7 | + |
| 8 | +/** |
| 9 | + * 二叉树 |
| 10 | + * |
| 11 | + * @author <a href="mailto:forbreak@163.com">Zhang Peng</a> |
| 12 | + * @since 2020-01-28 |
| 13 | + */ |
| 14 | +public class BTree<T extends Comparable<T>> { |
| 15 | + |
| 16 | + /** |
| 17 | + * 二叉树根节点 |
| 18 | + */ |
| 19 | + private TreeNode<T> root; |
| 20 | + |
| 21 | + public BTree() { |
| 22 | + this.root = null; |
| 23 | + } |
| 24 | + |
| 25 | + public BTree(TreeNode<T> root) { |
| 26 | + this.root = root; |
| 27 | + } |
| 28 | + |
| 29 | + public static <T extends Comparable<T>> BTree<T> buildTree(T... array) { |
| 30 | + BTree<T> tree = new BTree<>(); |
| 31 | + List<TreeNode<T>> list = new ArrayList<>(); |
| 32 | + |
| 33 | + for (T value : array) { |
| 34 | + // 创建结点,每一个结点的左结点和右结点为null |
| 35 | + TreeNode<T> node; |
| 36 | + if (value == null) { |
| 37 | + node = null; |
| 38 | + } else { |
| 39 | + node = new TreeNode<>(value, null, null); |
| 40 | + } |
| 41 | + // list中存着每一个结点 |
| 42 | + list.add(node); |
| 43 | + } |
| 44 | + |
| 45 | + // 构建二叉树 |
| 46 | + if (list.size() > 0) { |
| 47 | + // i表示的是根节点的索引,从0开始 |
| 48 | + for (int i = 0; i < array.length / 2 - 1; i++) { |
| 49 | + if (list.get(2 * i + 1) != null) { |
| 50 | + // 左结点 |
| 51 | + list.get(i).left = list.get(2 * i + 1); |
| 52 | + } |
| 53 | + if (list.get(2 * i + 2) != null) { |
| 54 | + // 右结点 |
| 55 | + list.get(i).right = list.get(2 * i + 2); |
| 56 | + } |
| 57 | + } |
| 58 | + // 判断最后一个根结点:因为最后一个根结点可能没有右结点,所以单独拿出来处理 |
| 59 | + int lastIndex = array.length / 2 - 1; |
| 60 | + // 左结点 |
| 61 | + list.get(lastIndex).left = list.get(lastIndex * 2 + 1); |
| 62 | + // 右结点,如果数组的长度为奇数才有右结点 |
| 63 | + if (array.length % 2 == 1) { |
| 64 | + list.get(lastIndex).right = list.get(lastIndex * 2 + 2); |
| 65 | + } |
| 66 | + |
| 67 | + tree.root = list.get(0); |
| 68 | + } else { |
| 69 | + tree.root = null; |
| 70 | + } |
| 71 | + return tree; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 判断两颗二叉树是否完全一致 |
| 76 | + * <p> |
| 77 | + * 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 |
| 78 | + * |
| 79 | + * @param tree1 {@link BTree} |
| 80 | + * @param tree2 {@link BTree} |
| 81 | + * @param <T> 元素类型 |
| 82 | + * @return true / false |
| 83 | + */ |
| 84 | + public static <T extends Comparable<T>> boolean isEquals(final BTree<T> tree1, final BTree<T> tree2) { |
| 85 | + return isEquals(tree1.root, tree2.root); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * 判断两颗二叉树是否完全一致 |
| 90 | + * |
| 91 | + * @param root1 二叉树根节点,类型:{@link BTree#root} |
| 92 | + * @param root2 二叉树根节点,类型:{@link BTree#root} |
| 93 | + * @param <T> 元素类型 |
| 94 | + * @return true / false |
| 95 | + * @see <a href="https://leetcode-cn.com/problems/same-tree/">相同的树</a> |
| 96 | + */ |
| 97 | + private static <T extends Comparable<T>> boolean isEquals(TreeNode<T> root1, TreeNode<T> root2) { |
| 98 | + if (root1 == null && root2 == null) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + if (root1 == null || root2 == null) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + if (!root1.value.equals(root2.value)) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + return isEquals(root1.left, root2.left) && isEquals(root1.right, root2.right); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * 返回二叉树的最大深度 |
| 115 | + * |
| 116 | + * @return 二叉树的最大深度 |
| 117 | + */ |
| 118 | + public int maxDepth() { |
| 119 | + return maxDepth(this.root); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * 采用递归方法获取二叉树的最大深度 |
| 124 | + * |
| 125 | + * @param root 二叉树根节点,类型:{@link BTree#root} |
| 126 | + * @return 二叉树的最大深度 |
| 127 | + * @see <a href="https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/">二叉树的最大深度</a> |
| 128 | + */ |
| 129 | + private int maxDepth(TreeNode<T> root) { |
| 130 | + if (root == null) return 0; |
| 131 | + |
| 132 | + int left = maxDepth(root.left); |
| 133 | + |
| 134 | + int right = maxDepth(root.right); |
| 135 | + |
| 136 | + return Math.max(left, right) + 1; |
| 137 | + } |
| 138 | + |
| 139 | + // ------------------------------------------------------------- 遍历元素 |
| 140 | + |
| 141 | + /** |
| 142 | + * 将二叉树按层次遍历顺序转换为列表,即广度优先搜索(BFS) |
| 143 | + * |
| 144 | + * @return {@link List<List<T>>} |
| 145 | + * @see <a href="https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/">二叉树的层次遍历 II</a> |
| 146 | + */ |
| 147 | + public List<List<T>> levelOrderList() { |
| 148 | + List<List<T>> lists = new ArrayList<>(); |
| 149 | + if (root == null) { return lists; } |
| 150 | + Queue<TreeNode<T>> queue = new LinkedList<>(); |
| 151 | + queue.offer(root); |
| 152 | + while (!queue.isEmpty()) { |
| 153 | + int size = queue.size(); |
| 154 | + List<T> temp = new ArrayList<>(); |
| 155 | + for (int i = 0; i < size; i++) { |
| 156 | + TreeNode<T> node = queue.poll(); |
| 157 | + temp.add(node.value); |
| 158 | + if (node.left != null) { queue.offer(node.left); } |
| 159 | + if (node.right != null) { queue.offer(node.right); } |
| 160 | + } |
| 161 | + lists.add(temp); |
| 162 | + } |
| 163 | + return lists; |
| 164 | + } |
| 165 | + |
| 166 | + static class TreeNode<T extends Comparable<T>> { |
| 167 | + |
| 168 | + T value; |
| 169 | + |
| 170 | + TreeNode<T> left; |
| 171 | + |
| 172 | + TreeNode<T> right; |
| 173 | + |
| 174 | + public TreeNode(T value, TreeNode<T> left, TreeNode<T> right) { |
| 175 | + this.value = value; |
| 176 | + this.left = left; |
| 177 | + this.right = right; |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments