使用 JavaScript 建立二叉樹


讓我們瞭解如何在 JavaScript 中建立和表示二叉查詢樹。我們首先需要建立類 BinarySearchTree,並在其上定義一個屬性 Node。 

示例

class BinarySearchTree {
   constructor() {
      // Initialize a root element to null.
      this.root = null;
   }
}

BinarySearchTree.prototype.Node = class {
   constructor(data, left = null, right = null) {
      this.data = data;
      this.left = left;
      this.right = right;
   }
};

我們僅建立了自己的 BST 類的類表示形式。我們將在此類中填入內容,因為我們將繼續學習將新增到此結構中的函式。

更新日期: 15-6-2020

205 瀏覽量

開始您的職業

完成本課程以獲得認證

入門
廣告
© . All rights reserved.