Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

Data Structures พื้นฐานสำหรับ Ruby Developers: คู่มือปฏิบัติจริง

Data Structure คืออะไร? — วิธีเฉพาะจัดระเบียบและเข้าถึง Data

Examples: Arrays, Binary Trees, Hashes

Data Structures ต่างๆ เก่งงานต่างกัน — Hashes เก่งเก็บ Dictionary (Word & Definition) หรือ Phone Book (Name & Number)

รู้ Data Structures ที่มี และ Characteristics ของแต่ละตัว = Ruby Developer ดีกว่า — นั่นคือบทความนี้!

เข้าใจ Arrays

Array = Data Structure แรกที่เรียน Programming

Arrays ใช้ Contiguous Memory Chunk Objects เก็บติดกัน No Gaps — ต่าง Low-level (C) Ruby จัดการ Memory, Expand Size, Compact ตอน Delete ให้หมด

Uses:

  • Base สำหรับ Advanced Data Structures
  • Gather Results จาก Loop
  • Collect Items

Arrays ทุกที่ — split, chars แยก String -> Array of Characters

ตัวอย่าง:

 out = []
10.times { |i| out << i }
out # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

ตารางแสดง Array Operations Behavior ตอน Size เพิ่ม — ไม่คุ้น Time Complexity อ่านบทความนี้

Time Complexity for Arrays:

OperationComplexity
pushO(1)
popO(1)
accessO(1)
findO(n)
deleteO(n)

ทำไมช่วย? บอก Performance Characteristics — find บน Huge Array = Slow... แต่รู้ Indexes 用 Array = Fast เพราะ O(1)

เลือก Data Structure ด้วย Criteria:

  1. Performance Characteristics => ทำอะไรกับ Data? Dataset ใหญ่แค่ไหน?
  2. Shape & Form of Data => Data ชนิดไหน? จัดใหม่ให้ Fit Better Data Structure ได้ไหม?

The Hash Data Structure

มี Mapping Country Codes <-> Country Names? หรือแค่ Count Stuff? — Hashes ช่วยได้!

Hash = Data Structure ทุก Value มี Key และ Key เป็นอะไรก็ได้ (String, Integer, Symbol ฯลฯ)

ทำงานยังไง? Hash Convert Key -> Number (用 Ruby hash Method) 用 Number เป็น Index — ไม่ต้องเข้าใจลึกถึงใช้ได้

Uses:

  • Count Characters ใน String
  • Map Words -> Definitions, Names -> Phone Numbers
  • Find Duplicates ใน Array

ตัวอย่าง:

 "aaabcd"
  .each_char
  .with_object(Hash.new(0)) { |ch, hash| hash[ch] += 1 }
# {"a"=>3, "b"=>1, "c"=>1, "d"=>1} 

Time Complexity:

OperationComplexity
storeO(1)
accessO(1)
deleteO(1)
find (value)O(n)

Hashes = Most Helpful Data Structure Performance-wise เพราะ Constant O(1) Store/Delete/Access — Find ใน Hash = หา Specific Value

Stacks

Stack = จานซ้อน จานบนสุดถอดได้ — Useful กว่าที่คิด!

Uses:

  • แทน Recursive Methods ด้วย Loop ธรรมดา
  • Track Work Left To Do Recent บนสุด
  • Reverse Array

ตัวอย่าง:

 stack = [1,2,3,4,5]
(1..stack.size).map { stack.pop } # [5, 4, 3, 2, 1] 

用 reverse แทนได้ — แค่ Example แสดง Characteristic

Time Complexity:

OperationComplexity
pushO(1)
popO(1)
find---
access---

Stacks/Queues มี 2 Operations: Insert/Delete (Push/Pop) — Search ใน Stack 可能 แต Rare

วิธีใช้ Binary Trees

Ruby Devs เยอะรู้ Binary Trees แตไม่เคย用 — ทำไม?

  1. No Built-in Binary Tree Implementation
  2. ไม่คุ้มสำหรับ Everyday Challenges เหมือน Arrays/Hashes ที่用ตลอด

แต Binary Trees = Interesting Data Structure

Data Structures พื้นฐานสำหรับ Ruby Developers: คู่มือปฏิบัติจริง

มี Variations เยอะ: Trie (Next Section), Multiway Trees (B-Tree 用ใน Databases), Heap

Uses: Data Compression, Routing Tables, Abstract Syntax Trees (AST)

ตัวอย่าง: (用 bstree Gem)

 # https://github.com/jamesconant/bstree
require 'bstree'
root = Bstree::Node.new(5)
root.insert(2)
root.insert(7)
root.search(3) # nil

Time Complexity:

OperationComplexity
insertO(log n)
deleteO(log n)
findO(log n)
access---

Balanced Binary Tree = ทุก Nodes มี 2 Children Leaves Same Level — Unbalanced = Degrade to O(n)

Self-Balanced (Red-Black Tree, AVL) ทุก Operation Time ∝ Height — No Access Time เพราะ Access ต้อง Find ก่อน (O(log n)) — เก็บ Reference Node ไว้ = O(1) Access

The Trie Data Structure

Trie = Specialized Tree-like Data Structure — เก่ง Words, Quick Search Prefix/Full Word

Uses: Word Games, Spelling Checker, Autocomplete Suggestions

ตัวอย่าง: (用 rambling-trie Gem)

 # https://github.com/gonzedge/rambling-trie
require 'rambling-trie'
trie = Rambling::Trie.create('words.txt')
trie.include?('chocolate') # true
trie.include?('salmon') # true

Time Complexity: (k = Input String Size, n = Data Structure Size)

OperationComplexity
addO(k)
include?O(k)
wordsO(k)

Word apple -> k = 5

Summary (สรุป)

คุณเรียนรู้ Common Data Structures, Uses, Characteristics, และ用ใน Ruby

Data Structures พื้นฐานสำหรับ Ruby Developers: คู่มือปฏิบัติจริง

Apply ใหม่แก้ปัญหาเร็วขึ้น! — Share ถ้าHelpful 🙂

Thank you 🙂