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

Splay ใน Virtual Tree (การเลื่อนในต้นไม้เสมือน) โครงสร้างข้อมูล

Virtual Tree Overview

ใน Virtual Tree: Edges บางส่วนเป็น Solid (แท้) บางส่วนเป็น Dashed (ขีด)
Splaying ปกติทำใน Solid Trees เท่านั้น

Algorithm: Splay(y) - 3 Passes

Pass 1: Walk Up + Splay in Solid Subtrees Only

  • เริ่มจาก Node y
  • Splay ภายใน Solid Subtrees เท่านั้น
  • จบ Pass 1: Path จาก y ถึง Root กลายเป็น Dashed ทั้งหมด

Pass 2: Walk Up + Splice at Proper Ancestors

  • เดินขึ้นจาก y, Splice ที่ Proper Ancestor ทุกตัว
  • จบ Pass 2: Path จาก y ถึง Root กลายเป็น Solid ทั้งหมด
  • ยกเว้น: y และ Children เดิมของ y (ก่อน Pass 1) กลายเป็น Left Children

Pass 3: Walk Up + Normal Splay

  • เดินขึ้นจาก y ถึง Root, Splying แบบปกติ
  • Result: y กลายเป็น Root ของ Tree

ตัวอย่าง: Huffman Coding บน Virtual Tree

Letter Frequency Table:

Letterzkmcudle
Frequency272432374242120

Huffman Codes:

LetterFreqCodeBits
e12001
d421013
l421103
u371003
c3211104
m24111115
k71111016
z21111006

Splay ใน Virtual Tree (การเลื่อนในต้นไม้เสมือน) โครงสร้างข้อมูล

สรุป

Splay ใน Virtual Tree ใช้ 3 Passes เพื่อย้าย Node ให้กลายเป็น Root พร้อมรักษา Solid/Dashed Structure ใช้ใน Dynamic Trees, Link-Cut Trees, และ Huffman Coding Optimization