让我们了解如何在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类的类表示。在继续学习将添加到该结构的功能时,我们将填写该类。