Skip to content

JavaScript and TypeScript Data Structures

Summary

Basic Structures

โครงสร้างข้อมูลTime ComplexitySpace Complexityการใช้งานทั่วไปBuilt-in Support
ArrayAccess: O(1)
Insert/Delete: O(n)
O(n)การเก็บข้อมูลแบบลำดับdeveloper.mozilla.org faviconJS, www.typescriptlang.org faviconTS
ObjectAccess: O(1)
Insert/Delete: O(1)
O(n)การเก็บข้อมูลแบบคู่คีย์-ค่าdeveloper.mozilla.org faviconJS, www.typescriptlang.org faviconTS

Linear Structures

โครงสร้างข้อมูลTime ComplexitySpace Complexityการใช้งานทั่วไปBuilt-in Support
StackAccess: O(1)
Insert/Delete: O(1)
O(n)การเรียกฟังก์ชัน, การยกเลิกการทำงานJS/TS (via developer.mozilla.org faviconArray)
QueueAccess: O(1)
Insert/Delete: O(1)
O(n)การจัดตารางงานJS/TS (via developer.mozilla.org faviconArray)

Tree Structures

โครงสร้างข้อมูลTime ComplexitySpace Complexityการใช้งานทั่วไปBuilt-in Support
Binary TreeAccess: O(log n)
Insert/Delete: O(log n)
O(n)ข้อมูลแบบลำดับชั้นNo
Binary Search TreeAccess: O(log n)
Insert/Delete: O(log n)
O(n)การค้นหาข้อมูลแบบเรียงลำดับNo

Key-Value Structures

โครงสร้างข้อมูลTime ComplexitySpace Complexityการใช้งานทั่วไปBuilt-in Support
Hash TableAccess: O(1)
Insert/Delete: O(1)
O(n)แคช, พจนานุกรมdeveloper.mozilla.org faviconJS, www.typescriptlang.org faviconTS

Graph Structures

โครงสร้างข้อมูลTime ComplexitySpace Complexityการใช้งานทั่วไปBuilt-in Support
Directed GraphAccess: Varies
Insert/Delete: Varies
O(n + m)การกำหนดเส้นทางเครือข่ายNo
Undirected GraphAccess: Varies
Insert/Delete: Varies
O(n + m)เครือข่ายสังคมNo

Basic Structures

Array

Array เป็นโครงสร้างข้อมูลพื้นฐานที่เก็บข้อมูลเป็นลำดับ โดยแต่ละ element มี index เป็นตัวระบุตำแหน่ง

www.typescriptlang.org faviconTS Documentation

array-interface.ts
ts
interface Array<T> {
  length: number;
  push(...items: T[]): number;
  pop(): T | undefined;
  indexOf(searchElement: T, fromIndex?: number): number;
}
shopping-cart.ts
ts
// ตะกร้าสินค้าใช้ Array
interface CartItem {
  id: number;
  name: string;
  price: number;
  quantity: number;
}

const cart: CartItem[] = [
  { id: 1, name: 'Laptop', price: 999.99, quantity: 1 },
  { id: 2, name: 'Mouse', price: 19.99, quantity: 2 }
];

// เพิ่มสินค้า
cart.push({ id: 3, name: 'Keyboard', price: 49.99, quantity: 1 });

// ลบสินค้าด้วย ID
const itemToRemove = 2;
const index = cart.findIndex(item => item.id === itemToRemove);
if (index !== -1) cart.splice(index, 1);
ลักษณะความซับซ้อนเวลาการใช้งาน
เรียงลำดับ, เข้าถึงด้วย indexเข้าถึง: O(1)
เพิ่ม/ลบ: O(n)
เก็บข้อมูลแบบลำดับ

Object

Object เก็บข้อมูลแบบ key-value pairs โดย key เป็น unique identifier

www.typescriptlang.org faviconTS Documentation

object-interface.ts
ts
interface Object {
  hasOwnProperty(v: PropertyKey): boolean;
  keys(): string[];
  values(): any[];
}
user-profile.ts
ts
// จัดการโปรไฟล์ผู้ใช้
interface UserPreferences {
  theme: string;
  notifications: boolean;
}

interface UserProfile {
  username: string;
  preferences: UserPreferences;
  updatePreference: (key: string, value: any) => UserPreferences;
}

const userProfile: UserProfile = {
  username: 'dev_john',
  preferences: {
    theme: 'dark',
    notifications: true
  },
  updatePreference(key: string, value: any) {
    this.preferences[key as keyof UserPreferences] = value;
    return this.preferences;
  }
};
ลักษณะความซับซ้อนเวลาการใช้งาน
ไม่เรียงลำดับ, เข้าถึงด้วย keyเข้าถึง: O(1)
เพิ่ม/ลบ: O(1)
คอนฟิกูเรชัน, ดิกชันนารี

Linear Structures

Stack

Stack เป็นโครงสร้างข้อมูลแบบ Last-In-First-Out โดยมีการเข้าถึงและลบข้อมูลจากด้านบนเท่านั้น

www.typescriptlang.org faviconTS Documentation

browser-history.ts
ts
// ประวัติการใช้งานเบราว์เซอร์
class BrowserHistory {
  private history: string[];
  private currentIndex: number;

  constructor() {
    this.history = [];
    this.currentIndex = -1;
  }

  // เยี่ยมชมหน้าใหม่
  visit(url: string) {
    this.history = this.history.slice(0, this.currentIndex + 1);
    this.history.push(url);
    this.currentIndex = this.history.length - 1;
  }

  // ย้อนกลับ
  back() {
    if (this.currentIndex > 0) this.currentIndex--;
    return this.history[this.currentIndex];
  }

  // ไปข้างหน้า
  forward() {
    if (this.currentIndex < this.history.length - 1) this.currentIndex++;
    return this.history[this.currentIndex];
  }
}

// ตัวอย่างการใช้งาน
const history = new BrowserHistory();
history.visit('https://example.com');
history.visit('https://example.com/products');
history.back(); // กลับไปหน้าแรก
ลักษณะความซับซ้อนเวลาการใช้งาน
LIFO, เข้าถึงด้วย topPush/Pop: O(1)การเรียกฟังก์ชัน, การยกเลิกการทำงาน

Queue

Queue เป็นโครงสร้างข้อมูลแบบ First-In-First-Out โดยมีการเข้าถึงและลบข้อมูลจากด้านหน้าและด้านหลัง

www.typescriptlang.org faviconTS Documentation

task-queue.ts
ts
// คิวสำหรับทำงานแบบแบ็กกราวด์
class TaskQueue {
  private tasks: (() => void)[];

  constructor() {
    this.tasks = [];
  }

  // เพิ่มงานใหม่
  addTask(task: () => void) {
    this.tasks.push(task);
    this.processQueue();
  }

  // ประมวลผลงานทั้งหมด
  private async processQueue() {
    while (this.tasks.length > 0) {
      const task = this.tasks.shift();
      await task();
    }
  }
}

// ตัวอย่างการใช้งาน
const queue = new TaskQueue();
queue.addTask(() => console.log('งานที่ 1 กำลังทำงาน'));
queue.addTask(() => new Promise(resolve => {
  setTimeout(() => {
    console.log('งานที่ 2 กำลังทำงาน');
    resolve();
  }, 1000);
}));
ลักษณะความซับซ้อนเวลาการใช้งาน
FIFO, เข้าถึงด้วย frontEnqueue/Dequeue: O(1)การจัดตารางงาน

Tree Structures

Binary Tree

Binary Tree เป็นโครงสร้างข้อมูลแบบลำดับชั้น โดยแต่ละ node มีลูกไม่เกินสองตัว

www.typescriptlang.org faviconTS Documentation

file-system.ts
ts
// โครงสร้างไฟล์แบบต้นไม้
interface FileNode {
  name: string;
  isDirectory: boolean;
  children: FileNode[];
  parent: FileNode | null;
}

class FileNodeImpl implements FileNode {
  constructor(public name: string, public isDirectory: boolean = false) {
    this.children = [];
    this.parent = null;
  }

  // เพิ่มไฟล์/โฟลเดอร์ย่อย
  addChild(node: FileNode) {
    node.parent = this;
    this.children.push(node);
    return node;
  }

  // ค้นหาไฟล์ด้วย path
  find(path: string) {
    const parts = path.split('/'); // แยก path
    let current: FileNode | null = this;
    for (const part of parts) {
      if (!part) continue;
      const child = current.children.find(c => c.name === part);
      if (!child) return null; // ไม่พบ
      current = child; // ย้ายไปโหนดลูก
    }
    return current; // คืนค่าสุดท้ายที่พบ
  }
}

// ตัวอย่างระบบไฟล์
const root = new FileNodeImpl('', true); // โฟลเดอร์ราก
const etc = root.addChild(new FileNodeImpl('etc', true)); // โฟลเดอร์ etc
etc.addChild(new FileNodeImpl('nginx.conf')); // ไฟล์ config
const home = root.addChild(new FileNodeImpl('home', true)); // โฟลเดอร์ home
const user = home.addChild(new FileNodeImpl('user', true)); // โฟลเดอร์ user
user.addChild(new FileNodeImpl('profile.txt')); // ไฟล์ผู้ใช้

root.find('home/user/profile.txt'); // ค้นหาไฟล์
ลักษณะความซับซ้อนเวลาการใช้งาน
ลำดับชั้น, เข้าถึงด้วย nodeVariesข้อมูลแบบลำดับชั้น

Binary Search Tree

Binary Search Tree เป็นประเภทเฉพาะของ Binary Tree ที่มีคุณสมบัติ:

  1. ทุกโหนดใน subtree ซ้ายมีค่าน้อยกว่าโหนดปัจจุบัน
  2. ทุกโหนดใน subtree ขวามีค่ามากกว่าโหนดปัจจุบัน
  3. subtree ทั้งซ้ายและขวาต้องเป็น Binary Search Tree ด้วย

www.typescriptlang.org faviconTS Documentation

bst.ts
ts
// ต้นไม้ค้นหาแบบทวิภาค
interface BSTNode {
  value: number;
  left: BSTNode | null;
  right: BSTNode | null;
}

class BSTNodeImpl implements BSTNode {
  constructor(public value: number) {
    this.left = null;
    this.right = null;
  }

  // เพิ่มค่าใหม่
  insert(value: number) {
    if (value < this.value) {
      if (!this.left) this.left = new BSTNodeImpl(value);
      else this.left.insert(value);
    } else {
      if (!this.right) this.right = new BSTNodeImpl(value);
      else this.right.insert(value);
    }
  }
}
ลักษณะความซับซ้อนเวลาการใช้งาน
ลำดับชั้น, เข้าถึงด้วย nodeO(log n) when balancedการค้นหาข้อมูลแบบเรียงลำดับ

Key-Value Structures

Hash Table

Hash Table เป็นโครงสร้างข้อมูลแบบ key-value pairs โดยใช้ hash function ในการเข้าถึงข้อมูล

www.typescriptlang.org faviconTS Documentation

hash-table.ts
ts
// ตารางแฮช
const hashTable: { [key: string]: any } = {};
hashTable['key1'] = 'value1';  // เพิ่มค่า
const value = hashTable['key1']; // ค้นหาค่า
delete hashTable['key1'];      // ลบค่า
ลักษณะความซับซ้อนเวลาการใช้งาน
เข้าถึงด้วย key, ใช้ hash functionAverage O(1)แคช, พจนานุกรม

Graph Structures

Directed Graph

Directed Graph เป็นโครงสร้างข้อมูลแบบเครือข่ายที่มีการเชื่อมต่อระหว่าง node โดยมีทิศทาง

www.typescriptlang.org faviconTS Documentation

directed-graph.ts
ts
// กราฟทิศทาง
interface Graph {
  [key: string]: string[];
}

const graph: Graph = {
  A: ['B', 'C'],
  B: ['D'],
  C: [],
  D: ['C']
};
ลักษณะความซับซ้อนเวลาการใช้งาน
เข้าถึงด้วย node, เชื่อมต่อด้วย edgeVariesการกำหนดเส้นทางเครือข่าย

Undirected Graph

Undirected Graph เป็นโครงสร้างข้อมูลแบบเครือข่ายที่มีการเชื่อมต่อระหว่าง node โดยไม่มีทิศทาง

www.typescriptlang.org faviconTS Documentation

undirected-graph.ts
ts
// กราฟไม่มีทิศทาง
interface Graph {
  [key: string]: string[];
}

const graph: Graph = {
  A: ['B', 'C'],
  B: ['A', 'D'],
  C: ['A'],
  D: ['B']
};
ลักษณะความซับซ้อนเวลาการใช้งาน
เข้าถึงด้วย node, เชื่อมต่อด้วย edgeVariesเครือข่ายสังคม

Last updated: