设计更高效的树

Designing a more efficient tree

假设我们有一个基本的二叉树,可以递归搜索:

class BinaryTree{

    private int Element;
    private BinaryTree LeftNode;
    private BinaryTree RightNode;

    //Class Constructors

    //Class Methods

}

我最初学习这种二叉树和一般树的设计主要是因为它简单易用。然而,有人讨论过这种设计,每次我扩展树时,a.k.a 将 BinaryTree 的实例添加到 LeftNode 或 RightNode,实例化另一个 BinaryTree 也需要预留内存 space 来存储所有class BinaryTree 的非静态方法。随着树呈指数增长,所需的space数量也会大幅增加。

是否有更有效的方法来实现递归树设计,同时保持面向对象的范例?

你的假设:

the instantiating of another BinaryTree would also require reserving memory space to store all non-static methods of the class BinaryTree.

不正确。

Class 实例方法不会占用 space 每个 class 的新实例。实例占用的唯一内存 space 是它的数据。