နားလည်ထားရမယ့် အချက်
Data structure ရွေးတဲ့အခါ structure ကို အရင်နာမည်မခေါ်ဘဲ၊ access pattern ကို အရင် ခွဲခြားသတ်မှတ်ရမှာပါ။ အစီအစဉ်အလိုက် မေးရမယ့် မေးခွန်းတွေက — insertion order ကို ထိန်းသိမ်းပြီး iterate ပြန်လုပ်ဖို့ လိုသလား? ဆိုရင် list (နှစ်ဖက်လုံးမှာ operation လိုရင် deque)။ Ordering လိုအပ်ချက် လုံးဝမရှိဘဲ key ဖြင့် O(1) lookup လိုသလား? ဆိုရင် hashing အခြေခံတဲ့ dict သို့မဟုတ် set။ Sorted order ကို အမြဲထိန်းထားပြီး fast search လည်း လိုသလား? ဆိုရင် balanced tree (insert က read ထက် ရှားရင် binary search နဲ့ sorted list ရိုးရိုးက ရိုးရှင်းပြီး cache-friendly ဖြစ်တယ်)။ Item အလာအသွားများနေပေမယ့် smallest/largest item ကိုသာ ထပ်ခါထပ်ခါ ရယူဖို့ လိုသလား? ဆိုရင် heap က အဲဒီ operation တစ်ခုတည်းကို full sort မလုပ်ဘဲ O(log n) အနေနဲ့ ထိန်းပေးနိုင်တယ်။ Front နဲ့ back နှစ်ဖက်စလုံးမှာ efficient ဖြစ်တဲ့ add/remove လိုသလား? ဆိုရင် deque — ဘာလို့လဲဆိုတော့ ordinary list ရဲ့ front operation တွေက O(n) ဖြစ်နေလို့ပါ။ Autocomplete လို prefix-based lookup လိုသလား? ဆိုရင် Trie က candidate string တစ်ခုချင်းစီကို scan မလုပ်ဘဲ common prefix တွေကို share လုပ်ပေးနိုင်တယ်။ အဲဒီ discipline ဆိုတာ ကိုယ်အကျွမ်းဝင်ဆုံး structure (list ဒါမှမဟုတ် dict) ကို ချက်ချင်း မသုံးမိအောင် ထိန်းချုပ်ပြီး requirement ကို အရင်ထုတ်ယူတာပါ — order? uniqueness? extremity? prefix? — ပြီးမှ ဒီ requirement အတွက် တိတိကျကျ ဒီဇိုင်းလုပ်ထားတဲ့ structure နဲ့ ကိုက်ညီအောင် လုပ်ရမှာပါ။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
Tutorial Platform ရဲ့ "Trending Now" widget ကို audit လုပ်တယ်လို့ ယူဆပါ — engineer တစ်ယောက်က view count အများဆုံး lesson ၅ ခုကို ပြသဖို့ list တစ်ခုလုံးကို sort() တစ်ခါ view ကိုင်လိုက်တိုင်း ပြန်လုပ်ထားတယ်။ Access pattern က "top-k ကို ထပ်ခါထပ်ခါ ရယူ" ဖြစ်နေတာမို့ heap က ပိုသင့်တော်တယ်။ Data structure ရွေးချယ်မှုကို review လုပ်ခြင်းက access pattern ကို လွဲမှားစွာ ဖတ်မိလို့ inefficient design ဖြစ်နေတာကို ဖော်ထုတ်ဖို့ အသုံးဝင်ပါတယ်။
အတူတူ စမ်းရေးကြည့်မယ်
# Scenario 1: always track the 3 most-viewed lessons, updated on every view
# — need repeated access to the current smallest/largest among changing items
# Scenario 2: check in O(1) whether a username has already been taken,
# with no need to ever list usernames in any particular order
# Scenario 3: power an autocomplete box that suggests tutorial titles
# as the user types each additional letter of a prefix
# Scenario 4: maintain a "recently viewed lessons" list where new views
# are added to the front and old views drop off the back once it's fullမှန်ကန်တဲ့ အဖြေက Scenario 1 → heap၊ Scenario 2 → set (သို့) dict၊ Scenario 3 → Trie၊ Scenario 4 → deque ဖြစ်ကြောင်း အတည်ပြုနိုင်ပါမယ်။၅ မိနစ် စမ်းကြည့်
Scenario လေးခုစလုံးအတွက် သင့်တော်ဆုံး data structure ကို ရွေးချယ်ပြီး၊ scenario တစ်ခုစီရဲ့ access pattern ကို ကိုးကားကာ ဘာကြောင့် အဲဒီ structure က အခြား structure များထက် ကိုက်ညီသလဲဆိုတာကို တစ်ကြောင်းစီ ရှင်းပြပါ။
သတိလေးတစ်ချက်
Scenario 1 အတွက် sorted list ကို default အနေနဲ့ ရွေးတတ်တယ် — sorted list က O(n) insert လိုအပ်တာကြောင့် view count မကြာခဏ update ဖြစ်နေတဲ့အခါ heap ရဲ့ O(log n) update ထက် နှေးပါတယ်။
Scenario 4 အတွက် ordinary list ကို ရွေးတတ်တယ် — list ရဲ့ front ကနေ insert/remove လုပ်ခြင်းက item အားလုံးကို shift ရလို့ O(n) ဖြစ်နေပြီး deque ရဲ့ O(1) front operation ကို လွဲချော်သွားစေတယ်။
Wikipedia — Abstract data type — Data Structures & Algorithms