mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Add implementation of array binary tree.
Rewrite the tree serialization and deserialization methods. Add applications of array and linked list.
This commit is contained in:
136
codes/java/chapter_tree/array_binary_tree.java
Normal file
136
codes/java/chapter_tree/array_binary_tree.java
Normal file
@ -0,0 +1,136 @@
|
||||
/**
|
||||
* File: array_binary_tree.java
|
||||
* Created Time: 2023-07-19
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_tree;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
/* 数组表示下的二叉树类 */
|
||||
class ArrayBinaryTree {
|
||||
private List<Integer> tree;
|
||||
|
||||
/* 构造方法 */
|
||||
public ArrayBinaryTree(List<Integer> arr) {
|
||||
tree = new ArrayList<>(arr);
|
||||
}
|
||||
|
||||
/* 节点数量 */
|
||||
public int size() {
|
||||
return tree.size();
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的值 */
|
||||
public Integer val(int i) {
|
||||
// 若索引越界,则返回 null ,代表空位
|
||||
if (i < 0 || i >= size())
|
||||
return null;
|
||||
return tree.get(i);
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的左子节点的索引 */
|
||||
public Integer left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的右子节点的索引 */
|
||||
public Integer right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的父节点的索引 */
|
||||
public Integer parent(int i) {
|
||||
return (i - 1) / 2;
|
||||
}
|
||||
|
||||
/* 层序遍历 */
|
||||
public List<Integer> levelOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
// 直接遍历数组
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (val(i) != null)
|
||||
res.add(val(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 深度优先遍历 */
|
||||
private void dfs(Integer i, String order, List<Integer> res) {
|
||||
// 若为空位,则返回
|
||||
if (val(i) == null)
|
||||
return;
|
||||
// 前序遍历
|
||||
if (order == "pre")
|
||||
res.add(val(i));
|
||||
dfs(left(i), order, res);
|
||||
// 中序遍历
|
||||
if (order == "in")
|
||||
res.add(val(i));
|
||||
dfs(right(i), order, res);
|
||||
// 后序遍历
|
||||
if (order == "post")
|
||||
res.add(val(i));
|
||||
}
|
||||
|
||||
/* 前序遍历 */
|
||||
public List<Integer> preOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
dfs(0, "pre", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
public List<Integer> inOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
dfs(0, "in", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
public List<Integer> postOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
dfs(0, "post", res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_binary_tree {
|
||||
public static void main(String[] args) {
|
||||
// 初始化二叉树
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
List<Integer> arr = Arrays.asList(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15);
|
||||
|
||||
TreeNode root = TreeNode.listToTree(arr);
|
||||
System.out.println("\n初始化二叉树\n");
|
||||
System.out.println("二叉树的数组表示:");
|
||||
System.out.println(arr);
|
||||
System.out.println("二叉树的链表表示:");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// 数组表示下的二叉树类
|
||||
ArrayBinaryTree abt = new ArrayBinaryTree(arr);
|
||||
|
||||
// 访问节点
|
||||
int i = 1;
|
||||
Integer l = abt.left(i);
|
||||
Integer r = abt.right(i);
|
||||
Integer p = abt.parent(i);
|
||||
System.out.println("\n当前节点的索引为 " + i + " ,值为 " + abt.val(i));
|
||||
System.out.println("其左子节点的索引为 " + l + " ,值为 " + (l == null ? "null" : abt.val(l)));
|
||||
System.out.println("其右子节点的索引为 " + r + " ,值为 " + (r == null ? "null" : abt.val(r)));
|
||||
System.out.println("其父节点的索引为 " + p + " ,值为 " + (p == null ? "null" : abt.val(p)));
|
||||
|
||||
// 遍历树
|
||||
List<Integer> res = abt.levelOrder();
|
||||
System.out.println("\n层序遍历为:" + res);
|
||||
res = abt.preOrder();
|
||||
System.out.println("前序遍历为:" + res);
|
||||
res = abt.inOrder();
|
||||
System.out.println("中序遍历为:" + res);
|
||||
res = abt.postOrder();
|
||||
System.out.println("后序遍历为:" + res);
|
||||
}
|
||||
}
|
||||
@ -8,61 +8,66 @@ package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Definition for a binary tree node. */
|
||||
/* 二叉树节点类 */
|
||||
public class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode left; // 左子节点引用
|
||||
public TreeNode right; // 右子节点引用
|
||||
|
||||
/* 构造方法 */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
/* Generate a binary tree given an array */
|
||||
public static TreeNode listToTree(List<Integer> list) {
|
||||
int size = list.size();
|
||||
if (size == 0)
|
||||
return null;
|
||||
// 序列化编码规则请参考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// 二叉树的数组表示:
|
||||
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
// 二叉树的链表表示:
|
||||
// /——— 15
|
||||
// /——— 7
|
||||
// /——— 3
|
||||
// | \——— 6
|
||||
// | \——— 12
|
||||
// ——— 1
|
||||
// \——— 2
|
||||
// | /——— 9
|
||||
// \——— 4
|
||||
// \——— 8
|
||||
|
||||
TreeNode root = new TreeNode(list.get(0));
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
int i = 0;
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if (++i >= size)
|
||||
break;
|
||||
if (list.get(i) != null) {
|
||||
node.left = new TreeNode(list.get(i));
|
||||
queue.add(node.left);
|
||||
}
|
||||
if (++i >= size)
|
||||
break;
|
||||
if (list.get(i) != null) {
|
||||
node.right = new TreeNode(list.get(i));
|
||||
queue.add(node.right);
|
||||
}
|
||||
/* 将列表反序列化为二叉树:递归 */
|
||||
private static TreeNode listToTreeDFS(List<Integer> arr, int i) {
|
||||
if (i < 0 || i >= arr.size() || arr.get(i) == null) {
|
||||
return null;
|
||||
}
|
||||
TreeNode root = new TreeNode(arr.get(i));
|
||||
root.left = listToTreeDFS(arr, 2 * i + 1);
|
||||
root.right = listToTreeDFS(arr, 2 * i + 2);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Serialize a binary tree to a list */
|
||||
public static List<Integer> treeToList(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
/* 将列表反序列化为二叉树 */
|
||||
public static TreeNode listToTree(List<Integer> arr) {
|
||||
return listToTreeDFS(arr, 0);
|
||||
}
|
||||
|
||||
/* 将二叉树序列化为列表:递归 */
|
||||
private static void treeToListDFS(TreeNode root, int i, List<Integer> res) {
|
||||
if (root == null)
|
||||
return list;
|
||||
Queue<TreeNode> queue = new LinkedList<TreeNode>() {{ add(root); }};
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if (node != null) {
|
||||
list.add(node.val);
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
} else {
|
||||
list.add(null);
|
||||
}
|
||||
return;
|
||||
while (i >= res.size()) {
|
||||
res.add(null);
|
||||
}
|
||||
return list;
|
||||
res.set(i, root.val);
|
||||
treeToListDFS(root.left, 2 * i + 1, res);
|
||||
treeToListDFS(root.right, 2 * i + 2, res);
|
||||
}
|
||||
|
||||
/* 将二叉树序列化为列表 */
|
||||
public static List<Integer> treeToList(TreeNode root) {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
treeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user