Skip to content

7.3   Array representation of binary trees

Under the linked list representation, the storage unit of a binary tree is a node TreeNode, with nodes connected by pointers. The basic operations of binary trees under the linked list representation were introduced in the previous section.

So, can we use an array to represent a binary tree? The answer is yes.

7.3.1   Representing perfect binary trees

Let's analyze a simple case first. Given a perfect binary tree, we store all nodes in an array according to the order of level-order traversal, where each node corresponds to a unique array index.

Based on the characteristics of level-order traversal, we can deduce a "mapping formula" between the index of a parent node and its children: If a node's index is \(i\), then the index of its left child is \(2i + 1\) and the right child is \(2i + 2\). The Figure 7-12 shows the mapping relationship between the indices of various nodes.

Array representation of a perfect binary tree

Figure 7-12   Array representation of a perfect binary tree

The mapping formula plays a role similar to the node references (pointers) in linked lists. Given any node in the array, we can access its left (right) child node using the mapping formula.

7.3.2   Representing any binary tree

Perfect binary trees are a special case; there are often many None values in the middle levels of a binary tree. Since the sequence of level-order traversal does not include these None values, we cannot solely rely on this sequence to deduce the number and distribution of None values. This means that multiple binary tree structures can match the same level-order traversal sequence.

As shown in the Figure 7-13 , given a non-perfect binary tree, the above method of array representation fails.

Level-order traversal sequence corresponds to multiple binary tree possibilities

Figure 7-13   Level-order traversal sequence corresponds to multiple binary tree possibilities

To solve this problem, we can consider explicitly writing out all None values in the level-order traversal sequence. As shown in the following figure, after this treatment, the level-order traversal sequence can uniquely represent a binary tree. Example code is as follows:

# Array representation of a binary tree
# Using None to represent empty slots
tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
/* Array representation of a binary tree */
// Using the maximum integer value INT_MAX to mark empty slots
vector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
/* Array representation of a binary tree */
// Using the Integer wrapper class allows for using null to mark empty slots
Integer[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
/* Array representation of a binary tree */
// Using nullable int (int?) allows for using null to mark empty slots
int?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
/* Array representation of a binary tree */
// Using an any type slice, allowing for nil to mark empty slots
tree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}
/* Array representation of a binary tree */
// Using optional Int (Int?) allows for using nil to mark empty slots
let tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
/* Array representation of a binary tree */
// Using null to represent empty slots
let tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
/* Array representation of a binary tree */
// Using null to represent empty slots
let tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
/* Array representation of a binary tree */
// Using nullable int (int?) allows for using null to mark empty slots
List<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
/* Array representation of a binary tree */
// Using None to mark empty slots
let tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];
/* Array representation of a binary tree */
// Using the maximum int value to mark empty slots, therefore, node values must not be INT_MAX
int tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
/* Array representation of a binary tree */
// Using null to represent empty slots
val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )


Array representation of any type of binary tree

Figure 7-14   Array representation of any type of binary tree

It's worth noting that complete binary trees are very suitable for array representation. Recalling the definition of a complete binary tree, None appears only at the bottom level and towards the right, meaning all None values definitely appear at the end of the level-order traversal sequence.

This means that when using an array to represent a complete binary tree, it's possible to omit storing all None values, which is very convenient. The Figure 7-15 gives an example.

Array representation of a complete binary tree

Figure 7-15   Array representation of a complete binary tree

The following code implements a binary tree based on array representation, including the following operations:

  • Given a node, obtain its value, left (right) child node, and parent node.
  • Obtain the preorder, inorder, postorder, and level-order traversal sequences.
array_binary_tree.py
class ArrayBinaryTree:
    """数组表示下的二叉树类"""

    def __init__(self, arr: list[int | None]):
        """构造方法"""
        self._tree = list(arr)

    def size(self):
        """列表容量"""
        return len(self._tree)

    def val(self, i: int) -> int:
        """获取索引为 i 节点的值"""
        # 若索引越界,则返回 None ,代表空位
        if i < 0 or i >= self.size():
            return None
        return self._tree[i]

    def left(self, i: int) -> int | None:
        """获取索引为 i 节点的左子节点的索引"""
        return 2 * i + 1

    def right(self, i: int) -> int | None:
        """获取索引为 i 节点的右子节点的索引"""
        return 2 * i + 2

    def parent(self, i: int) -> int | None:
        """获取索引为 i 节点的父节点的索引"""
        return (i - 1) // 2

    def level_order(self) -> list[int]:
        """层序遍历"""
        self.res = []
        # 直接遍历数组
        for i in range(self.size()):
            if self.val(i) is not None:
                self.res.append(self.val(i))
        return self.res

    def dfs(self, i: int, order: str):
        """深度优先遍历"""
        if self.val(i) is None:
            return
        # 前序遍历
        if order == "pre":
            self.res.append(self.val(i))
        self.dfs(self.left(i), order)
        # 中序遍历
        if order == "in":
            self.res.append(self.val(i))
        self.dfs(self.right(i), order)
        # 后序遍历
        if order == "post":
            self.res.append(self.val(i))

    def pre_order(self) -> list[int]:
        """前序遍历"""
        self.res = []
        self.dfs(0, order="pre")
        return self.res

    def in_order(self) -> list[int]:
        """中序遍历"""
        self.res = []
        self.dfs(0, order="in")
        return self.res

    def post_order(self) -> list[int]:
        """后序遍历"""
        self.res = []
        self.dfs(0, order="post")
        return self.res
array_binary_tree.cpp
/* 数组表示下的二叉树类 */
class ArrayBinaryTree {
  public:
    /* 构造方法 */
    ArrayBinaryTree(vector<int> arr) {
        tree = arr;
    }

    /* 列表容量 */
    int size() {
        return tree.size();
    }

    /* 获取索引为 i 节点的值 */
    int val(int i) {
        // 若索引越界,则返回 INT_MAX ,代表空位
        if (i < 0 || i >= size())
            return INT_MAX;
        return tree[i];
    }

    /* 获取索引为 i 节点的左子节点的索引 */
    int left(int i) {
        return 2 * i + 1;
    }

    /* 获取索引为 i 节点的右子节点的索引 */
    int right(int i) {
        return 2 * i + 2;
    }

    /* 获取索引为 i 节点的父节点的索引 */
    int parent(int i) {
        return (i - 1) / 2;
    }

    /* 层序遍历 */
    vector<int> levelOrder() {
        vector<int> res;
        // 直接遍历数组
        for (int i = 0; i < size(); i++) {
            if (val(i) != INT_MAX)
                res.push_back(val(i));
        }
        return res;
    }

    /* 前序遍历 */
    vector<int> preOrder() {
        vector<int> res;
        dfs(0, "pre", res);
        return res;
    }

    /* 中序遍历 */
    vector<int> inOrder() {
        vector<int> res;
        dfs(0, "in", res);
        return res;
    }

    /* 后序遍历 */
    vector<int> postOrder() {
        vector<int> res;
        dfs(0, "post", res);
        return res;
    }

  private:
    vector<int> tree;

    /* 深度优先遍历 */
    void dfs(int i, string order, vector<int> &res) {
        // 若为空位,则返回
        if (val(i) == INT_MAX)
            return;
        // 前序遍历
        if (order == "pre")
            res.push_back(val(i));
        dfs(left(i), order, res);
        // 中序遍历
        if (order == "in")
            res.push_back(val(i));
        dfs(right(i), order, res);
        // 后序遍历
        if (order == "post")
            res.push_back(val(i));
    }
};
array_binary_tree.java
/* 数组表示下的二叉树类 */
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 ("pre".equals(order))
            res.add(val(i));
        dfs(left(i), order, res);
        // 中序遍历
        if ("in".equals(order))
            res.add(val(i));
        dfs(right(i), order, res);
        // 后序遍历
        if ("post".equals(order))
            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;
    }
}
array_binary_tree.cs
/* 数组表示下的二叉树类 */
class ArrayBinaryTree(List<int?> arr) {
    List<int?> tree = new(arr);

    /* 列表容量 */
    public int Size() {
        return tree.Count;
    }

    /* 获取索引为 i 节点的值 */
    public int? Val(int i) {
        // 若索引越界,则返回 null ,代表空位
        if (i < 0 || i >= Size())
            return null;
        return tree[i];
    }

    /* 获取索引为 i 节点的左子节点的索引 */
    public int Left(int i) {
        return 2 * i + 1;
    }

    /* 获取索引为 i 节点的右子节点的索引 */
    public int Right(int i) {
        return 2 * i + 2;
    }

    /* 获取索引为 i 节点的父节点的索引 */
    public int Parent(int i) {
        return (i - 1) / 2;
    }

    /* 层序遍历 */
    public List<int> LevelOrder() {
        List<int> res = [];
        // 直接遍历数组
        for (int i = 0; i < Size(); i++) {
            if (Val(i).HasValue)
                res.Add(Val(i)!.Value);
        }
        return res;
    }

    /* 深度优先遍历 */
    void DFS(int i, string order, List<int> res) {
        // 若为空位,则返回
        if (!Val(i).HasValue)
            return;
        // 前序遍历
        if (order == "pre")
            res.Add(Val(i)!.Value);
        DFS(Left(i), order, res);
        // 中序遍历
        if (order == "in")
            res.Add(Val(i)!.Value);
        DFS(Right(i), order, res);
        // 后序遍历
        if (order == "post")
            res.Add(Val(i)!.Value);
    }

    /* 前序遍历 */
    public List<int> PreOrder() {
        List<int> res = [];
        DFS(0, "pre", res);
        return res;
    }

    /* 中序遍历 */
    public List<int> InOrder() {
        List<int> res = [];
        DFS(0, "in", res);
        return res;
    }

    /* 后序遍历 */
    public List<int> PostOrder() {
        List<int> res = [];
        DFS(0, "post", res);
        return res;
    }
}
array_binary_tree.go
/* 数组表示下的二叉树类 */
type arrayBinaryTree struct {
    tree []any
}

/* 构造方法 */
func newArrayBinaryTree(arr []any) *arrayBinaryTree {
    return &arrayBinaryTree{
        tree: arr,
    }
}

/* 列表容量 */
func (abt *arrayBinaryTree) size() int {
    return len(abt.tree)
}

/* 获取索引为 i 节点的值 */
func (abt *arrayBinaryTree) val(i int) any {
    // 若索引越界,则返回 null ,代表空位
    if i < 0 || i >= abt.size() {
        return nil
    }
    return abt.tree[i]
}

/* 获取索引为 i 节点的左子节点的索引 */
func (abt *arrayBinaryTree) left(i int) int {
    return 2*i + 1
}

/* 获取索引为 i 节点的右子节点的索引 */
func (abt *arrayBinaryTree) right(i int) int {
    return 2*i + 2
}

/* 获取索引为 i 节点的父节点的索引 */
func (abt *arrayBinaryTree) parent(i int) int {
    return (i - 1) / 2
}

/* 层序遍历 */
func (abt *arrayBinaryTree) levelOrder() []any {
    var res []any
    // 直接遍历数组
    for i := 0; i < abt.size(); i++ {
        if abt.val(i) != nil {
            res = append(res, abt.val(i))
        }
    }
    return res
}

/* 深度优先遍历 */
func (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {
    // 若为空位,则返回
    if abt.val(i) == nil {
        return
    }
    // 前序遍历
    if order == "pre" {
        *res = append(*res, abt.val(i))
    }
    abt.dfs(abt.left(i), order, res)
    // 中序遍历
    if order == "in" {
        *res = append(*res, abt.val(i))
    }
    abt.dfs(abt.right(i), order, res)
    // 后序遍历
    if order == "post" {
        *res = append(*res, abt.val(i))
    }
}

/* 前序遍历 */
func (abt *arrayBinaryTree) preOrder() []any {
    var res []any
    abt.dfs(0, "pre", &res)
    return res
}

/* 中序遍历 */
func (abt *arrayBinaryTree) inOrder() []any {
    var res []any
    abt.dfs(0, "in", &res)
    return res
}

/* 后序遍历 */
func (abt *arrayBinaryTree) postOrder() []any {
    var res []any
    abt.dfs(0, "post", &res)
    return res
}
array_binary_tree.swift
/* 数组表示下的二叉树类 */
class ArrayBinaryTree {
    private var tree: [Int?]

    /* 构造方法 */
    init(arr: [Int?]) {
        tree = arr
    }

    /* 列表容量 */
    func size() -> Int {
        tree.count
    }

    /* 获取索引为 i 节点的值 */
    func val(i: Int) -> Int? {
        // 若索引越界,则返回 null ,代表空位
        if i < 0 || i >= size() {
            return nil
        }
        return tree[i]
    }

    /* 获取索引为 i 节点的左子节点的索引 */
    func left(i: Int) -> Int {
        2 * i + 1
    }

    /* 获取索引为 i 节点的右子节点的索引 */
    func right(i: Int) -> Int {
        2 * i + 2
    }

    /* 获取索引为 i 节点的父节点的索引 */
    func parent(i: Int) -> Int {
        (i - 1) / 2
    }

    /* 层序遍历 */
    func levelOrder() -> [Int] {
        var res: [Int] = []
        // 直接遍历数组
        for i in 0 ..< size() {
            if let val = val(i: i) {
                res.append(val)
            }
        }
        return res
    }

    /* 深度优先遍历 */
    private func dfs(i: Int, order: String, res: inout [Int]) {
        // 若为空位,则返回
        guard let val = val(i: i) else {
            return
        }
        // 前序遍历
        if order == "pre" {
            res.append(val)
        }
        dfs(i: left(i: i), order: order, res: &res)
        // 中序遍历
        if order == "in" {
            res.append(val)
        }
        dfs(i: right(i: i), order: order, res: &res)
        // 后序遍历
        if order == "post" {
            res.append(val)
        }
    }

    /* 前序遍历 */
    func preOrder() -> [Int] {
        var res: [Int] = []
        dfs(i: 0, order: "pre", res: &res)
        return res
    }

    /* 中序遍历 */
    func inOrder() -> [Int] {
        var res: [Int] = []
        dfs(i: 0, order: "in", res: &res)
        return res
    }

    /* 后序遍历 */
    func postOrder() -> [Int] {
        var res: [Int] = []
        dfs(i: 0, order: "post", res: &res)
        return res
    }
}
array_binary_tree.js
/* 数组表示下的二叉树类 */
class ArrayBinaryTree {
    #tree;

    /* 构造方法 */
    constructor(arr) {
        this.#tree = arr;
    }

    /* 列表容量 */
    size() {
        return this.#tree.length;
    }

    /* 获取索引为 i 节点的值 */
    val(i) {
        // 若索引越界,则返回 null ,代表空位
        if (i < 0 || i >= this.size()) return null;
        return this.#tree[i];
    }

    /* 获取索引为 i 节点的左子节点的索引 */
    left(i) {
        return 2 * i + 1;
    }

    /* 获取索引为 i 节点的右子节点的索引 */
    right(i) {
        return 2 * i + 2;
    }

    /* 获取索引为 i 节点的父节点的索引 */
    parent(i) {
        return Math.floor((i - 1) / 2); // 向下整除
    }

    /* 层序遍历 */
    levelOrder() {
        let res = [];
        // 直接遍历数组
        for (let i = 0; i < this.size(); i++) {
            if (this.val(i) !== null) res.push(this.val(i));
        }
        return res;
    }

    /* 深度优先遍历 */
    #dfs(i, order, res) {
        // 若为空位,则返回
        if (this.val(i) === null) return;
        // 前序遍历
        if (order === 'pre') res.push(this.val(i));
        this.#dfs(this.left(i), order, res);
        // 中序遍历
        if (order === 'in') res.push(this.val(i));
        this.#dfs(this.right(i), order, res);
        // 后序遍历
        if (order === 'post') res.push(this.val(i));
    }

    /* 前序遍历 */
    preOrder() {
        const res = [];
        this.#dfs(0, 'pre', res);
        return res;
    }

    /* 中序遍历 */
    inOrder() {
        const res = [];
        this.#dfs(0, 'in', res);
        return res;
    }

    /* 后序遍历 */
    postOrder() {
        const res = [];
        this.#dfs(0, 'post', res);
        return res;
    }
}
array_binary_tree.ts
/* 数组表示下的二叉树类 */
class ArrayBinaryTree {
    #tree: (number | null)[];

    /* 构造方法 */
    constructor(arr: (number | null)[]) {
        this.#tree = arr;
    }

    /* 列表容量 */
    size(): number {
        return this.#tree.length;
    }

    /* 获取索引为 i 节点的值 */
    val(i: number): number | null {
        // 若索引越界,则返回 null ,代表空位
        if (i < 0 || i >= this.size()) return null;
        return this.#tree[i];
    }

    /* 获取索引为 i 节点的左子节点的索引 */
    left(i: number): number {
        return 2 * i + 1;
    }

    /* 获取索引为 i 节点的右子节点的索引 */
    right(i: number): number {
        return 2 * i + 2;
    }

    /* 获取索引为 i 节点的父节点的索引 */
    parent(i: number): number {
        return Math.floor((i - 1) / 2); // 向下整除
    }

    /* 层序遍历 */
    levelOrder(): number[] {
        let res = [];
        // 直接遍历数组
        for (let i = 0; i < this.size(); i++) {
            if (this.val(i) !== null) res.push(this.val(i));
        }
        return res;
    }

    /* 深度优先遍历 */
    #dfs(i: number, order: Order, res: (number | null)[]): void {
        // 若为空位,则返回
        if (this.val(i) === null) return;
        // 前序遍历
        if (order === 'pre') res.push(this.val(i));
        this.#dfs(this.left(i), order, res);
        // 中序遍历
        if (order === 'in') res.push(this.val(i));
        this.#dfs(this.right(i), order, res);
        // 后序遍历
        if (order === 'post') res.push(this.val(i));
    }

    /* 前序遍历 */
    preOrder(): (number | null)[] {
        const res = [];
        this.#dfs(0, 'pre', res);
        return res;
    }

    /* 中序遍历 */
    inOrder(): (number | null)[] {
        const res = [];
        this.#dfs(0, 'in', res);
        return res;
    }

    /* 后序遍历 */
    postOrder(): (number | null)[] {
        const res = [];
        this.#dfs(0, 'post', res);
        return res;
    }
}
array_binary_tree.dart
/* 数组表示下的二叉树类 */
class ArrayBinaryTree {
  late List<int?> _tree;

  /* 构造方法 */
  ArrayBinaryTree(this._tree);

  /* 列表容量 */
  int size() {
    return _tree.length;
  }

  /* 获取索引为 i 节点的值 */
  int? val(int i) {
    // 若索引越界,则返回 null ,代表空位
    if (i < 0 || i >= size()) {
      return null;
    }
    return _tree[i];
  }

  /* 获取索引为 i 节点的左子节点的索引 */
  int? left(int i) {
    return 2 * i + 1;
  }

  /* 获取索引为 i 节点的右子节点的索引 */
  int? right(int i) {
    return 2 * i + 2;
  }

  /* 获取索引为 i 节点的父节点的索引 */
  int? parent(int i) {
    return (i - 1) ~/ 2;
  }

  /* 层序遍历 */
  List<int> levelOrder() {
    List<int> res = [];
    for (int i = 0; i < size(); i++) {
      if (val(i) != null) {
        res.add(val(i)!);
      }
    }
    return res;
  }

  /* 深度优先遍历 */
  void dfs(int i, String order, List<int?> 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));
    }
  }

  /* 前序遍历 */
  List<int?> preOrder() {
    List<int?> res = [];
    dfs(0, 'pre', res);
    return res;
  }

  /* 中序遍历 */
  List<int?> inOrder() {
    List<int?> res = [];
    dfs(0, 'in', res);
    return res;
  }

  /* 后序遍历 */
  List<int?> postOrder() {
    List<int?> res = [];
    dfs(0, 'post', res);
    return res;
  }
}
array_binary_tree.rs
/* 数组表示下的二叉树类 */
struct ArrayBinaryTree {
    tree: Vec<Option<i32>>,
}

impl ArrayBinaryTree {
    /* 构造方法 */
    fn new(arr: Vec<Option<i32>>) -> Self {
        Self { tree: arr }
    }

    /* 列表容量 */
    fn size(&self) -> i32 {
        self.tree.len() as i32
    }

    /* 获取索引为 i 节点的值 */
    fn val(&self, i: i32) -> Option<i32> {
        // 若索引越界,则返回 None ,代表空位
        if i < 0 || i >= self.size() {
            None
        } else {
            self.tree[i as usize]
        }
    }

    /* 获取索引为 i 节点的左子节点的索引 */
    fn left(&self, i: i32) -> i32 {
        2 * i + 1
    }

    /* 获取索引为 i 节点的右子节点的索引 */
    fn right(&self, i: i32) -> i32 {
        2 * i + 2
    }

    /* 获取索引为 i 节点的父节点的索引 */
    fn parent(&self, i: i32) -> i32 {
        (i - 1) / 2
    }

    /* 层序遍历 */
    fn level_order(&self) -> Vec<i32> {
        let mut res = vec![];
        // 直接遍历数组
        for i in 0..self.size() {
            if let Some(val) = self.val(i) {
                res.push(val)
            }
        }
        res
    }

    /* 深度优先遍历 */
    fn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {
        if self.val(i).is_none() {
            return;
        }
        let val = self.val(i).unwrap();
        // 前序遍历
        if order == "pre" {
            res.push(val);
        }
        self.dfs(self.left(i), order, res);
        // 中序遍历
        if order == "in" {
            res.push(val);
        }
        self.dfs(self.right(i), order, res);
        // 后序遍历
        if order == "post" {
            res.push(val);
        }
    }

    /* 前序遍历 */
    fn pre_order(&self) -> Vec<i32> {
        let mut res = vec![];
        self.dfs(0, "pre", &mut res);
        res
    }

    /* 中序遍历 */
    fn in_order(&self) -> Vec<i32> {
        let mut res = vec![];
        self.dfs(0, "in", &mut res);
        res
    }

    /* 后序遍历 */
    fn post_order(&self) -> Vec<i32> {
        let mut res = vec![];
        self.dfs(0, "post", &mut res);
        res
    }
}
array_binary_tree.c
/* 数组表示下的二叉树结构体 */
typedef struct {
    int *tree;
    int size;
} ArrayBinaryTree;

/* 构造函数 */
ArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {
    ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));
    abt->tree = malloc(sizeof(int) * arrSize);
    memcpy(abt->tree, arr, sizeof(int) * arrSize);
    abt->size = arrSize;
    return abt;
}

/* 析构函数 */
void delArrayBinaryTree(ArrayBinaryTree *abt) {
    free(abt->tree);
    free(abt);
}

/* 列表容量 */
int size(ArrayBinaryTree *abt) {
    return abt->size;
}

/* 获取索引为 i 节点的值 */
int val(ArrayBinaryTree *abt, int i) {
    // 若索引越界,则返回 INT_MAX ,代表空位
    if (i < 0 || i >= size(abt))
        return INT_MAX;
    return abt->tree[i];
}

/* 层序遍历 */
int *levelOrder(ArrayBinaryTree *abt, int *returnSize) {
    int *res = (int *)malloc(sizeof(int) * size(abt));
    int index = 0;
    // 直接遍历数组
    for (int i = 0; i < size(abt); i++) {
        if (val(abt, i) != INT_MAX)
            res[index++] = val(abt, i);
    }
    *returnSize = index;
    return res;
}

/* 深度优先遍历 */
void dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {
    // 若为空位,则返回
    if (val(abt, i) == INT_MAX)
        return;
    // 前序遍历
    if (strcmp(order, "pre") == 0)
        res[(*index)++] = val(abt, i);
    dfs(abt, left(i), order, res, index);
    // 中序遍历
    if (strcmp(order, "in") == 0)
        res[(*index)++] = val(abt, i);
    dfs(abt, right(i), order, res, index);
    // 后序遍历
    if (strcmp(order, "post") == 0)
        res[(*index)++] = val(abt, i);
}

/* 前序遍历 */
int *preOrder(ArrayBinaryTree *abt, int *returnSize) {
    int *res = (int *)malloc(sizeof(int) * size(abt));
    int index = 0;
    dfs(abt, 0, "pre", res, &index);
    *returnSize = index;
    return res;
}

/* 中序遍历 */
int *inOrder(ArrayBinaryTree *abt, int *returnSize) {
    int *res = (int *)malloc(sizeof(int) * size(abt));
    int index = 0;
    dfs(abt, 0, "in", res, &index);
    *returnSize = index;
    return res;
}

/* 后序遍历 */
int *postOrder(ArrayBinaryTree *abt, int *returnSize) {
    int *res = (int *)malloc(sizeof(int) * size(abt));
    int index = 0;
    dfs(abt, 0, "post", res, &index);
    *returnSize = index;
    return res;
}
array_binary_tree.kt
/* 数组表示下的二叉树类 */
class ArrayBinaryTree(val tree: List<Int?>) {
    /* 列表容量 */
    fun size(): Int {
        return tree.size
    }

    /* 获取索引为 i 节点的值 */
    fun value(i: Int): Int? {
        // 若索引越界,则返回 null ,代表空位
        if (i < 0 || i >= size()) return null
        return tree[i]
    }

    /* 获取索引为 i 节点的左子节点的索引 */
    fun left(i: Int): Int {
        return 2 * i + 1
    }

    /* 获取索引为 i 节点的右子节点的索引 */
    fun right(i: Int): Int {
        return 2 * i + 2
    }

    /* 获取索引为 i 节点的父节点的索引 */
    fun parent(i: Int): Int {
        return (i - 1) / 2
    }

    /* 层序遍历 */
    fun levelOrder(): List<Int?> {
        val res = ArrayList<Int?>()
        // 直接遍历数组
        for (i in 0..<size()) {
            if (value(i) != null) res.add(value(i))
        }
        return res
    }

    /* 深度优先遍历 */
    fun dfs(i: Int, order: String, res: MutableList<Int?>) {
        // 若为空位,则返回
        if (value(i) == null) return
        // 前序遍历
        if ("pre" == order) res.add(value(i))
        dfs(left(i), order, res)
        // 中序遍历
        if ("in" == order) res.add(value(i))
        dfs(right(i), order, res)
        // 后序遍历
        if ("post" == order) res.add(value(i))
    }

    /* 前序遍历 */
    fun preOrder(): List<Int?> {
        val res = ArrayList<Int?>()
        dfs(0, "pre", res)
        return res
    }

    /* 中序遍历 */
    fun inOrder(): List<Int?> {
        val res = ArrayList<Int?>()
        dfs(0, "in", res)
        return res
    }

    /* 后序遍历 */
    fun postOrder(): List<Int?> {
        val res = ArrayList<Int?>()
        dfs(0, "post", res)
        return res
    }
}
array_binary_tree.rb
[class]{ArrayBinaryTree}-[func]{}
array_binary_tree.zig
[class]{ArrayBinaryTree}-[func]{}
Code Visualization

7.3.3   Advantages and limitations

The array representation of binary trees has the following advantages:

  • Arrays are stored in contiguous memory spaces, which is cache-friendly and allows for faster access and traversal.
  • It does not require storing pointers, which saves space.
  • It allows random access to nodes.

However, the array representation also has some limitations:

  • Array storage requires contiguous memory space, so it is not suitable for storing trees with a large amount of data.
  • Adding or deleting nodes requires array insertion and deletion operations, which are less efficient.
  • When there are many None values in the binary tree, the proportion of node data contained in the array is low, leading to lower space utilization.
Feel free to drop your insights, questions or suggestions