มาเข้าใจว่าจะสร้างและแสดง Binary Search Tree ใน JavaScript อย่างไร เราต้องสร้างคลาส BinarySearchTree และกำหนด property Node ไว้ในนั้น
ตัวอย่างโค้ด
class BinarySearchTree {
constructor() {
// Initialize root element เป็น null
this.root = null;
}
}
BinarySearchTree.prototype.Node = class {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
};
เราสร้าง class representation ของ BST class เราจะเพิ่มฟังก์ชันเข้าไปในคลาสนี้ตามที่เราจะเรียนรู้ฟังก์ชันต่างๆ ที่จะเพิ่มเข้าไปในโครงสร้างนี้