使用 Javascript 创建二叉树
web developmentfront end technologyjavascript
让我们了解如何在 Javascript 中创建和表示二叉搜索树。我们首先需要创建 BinarySearchTree 类并在其上定义一个属性 Node。
示例
class BinarySearchTree { constructor() { // 将根元素初始化为 null。 this.root = null; } } BinarySearchTree.prototype.Node = class { constructor(data, left = null, right = null) { this.data = data; this.left = left; this.right = right; } };
我们只是创建了 BST 类的类表示。我们将在继续学习将添加到此结构的函数时填充此类。