Cây nhị phân tìm kiếm là 1 cấu trúc dữ liệu quen thuộc với chúng ta. Có rất nhiều nghiên cứu và các thuật toán xoay quanh cấu trúc dữ liệu này. Trong talk này, xin giới thiệu một kỹ thuật giúp tối ưu cây nhị phân tìm kiếm dựa trên tần suất tìm kiếm, qua đó giúp giảm chi phí tìm kiếm xuống mức thấp nhất.
- Speaker: Phong Vu - Software Engineer
2. About me
▶ Le Phong Vu
▶ Member of Grokking
▶ Data structure and caching system
▶ Email: lephongvu90@gmail.com
2
3. Agenda
▶ Problem: Build a dictionary online with fast lookup
▶ Approach to apply OBST in dictionary application
▶ Use Dynamic programming to find OBST
▶ Optimize solution to reduce time create OBST
▶ Application of OBST
3
4. Problem: Build a dictionary
▶ Given an English world, you want to search its meaning quickly
4
5. Build a dictionary with BST
apple
hatch
mango
pizzajump
Balanced binary search tree
Worst-case O(log n) time per query
Total cost: 2000100
Word Total
search
Depth of
Node
Cost
apple 1.000.000 2 1000000 * 2
hatch 100 1 100 * 1
jump,
mango,
pizza
0 * 0
5
6. Re-structure BST
apple
hatch
…
Re-structure BST, move “apple” node
to root
Total cost: 1000200
Word Total search Depth of
Node
Cost
apple 1.000.000 1 1.000.000 * 1
hatch 100 2 100 * 2
jump,
mango,
pizza
0 * 0
OBTIMAL BINARY
SEARCH TREE
6
12. Optimal Binary Search Tree
▶ Given the set probabilities p and q, can we construct a
binary search tree whose expected search time is minimized?
▶ This tree is called an Optimal Binary Search Tree
12
13. Dynamic Programming
▶ Solves problems by combining the solutions to sub-problems.
13
Figure 1: Dynamic Programming
https://itnext.io/dynamic-programming-vs-divid
e-and-conquer-2fea680becbe
14. Step 1: The structure of OBST
▶ If an OBST has a sub-tree T, then this sub-tree T must be optimal BST.
14
Optimal
BST
Optimal
BST
Optimal
BST
21. Reference
▶ Le Phong Vu, “Dùng kỹ thuật quy hoạch động giải bài toán tối ưu
cây nhị phân tìm kiếm”
https://engineering.grokking.org/optimal-binary-search-tree/
▶ D. Knuth, “Optimum binary search trees”, Acta Informatica
www.inrg.csie.ntu.edu.tw/algorithm2014/presentation/Knuth71.pdf
▶ Thomas H. Cormen, “Introduction to algorithm”, MIT Press, 3rd
Edition
▶ D. Knuth, “The art of computer programming”, volume 3
▶ Kurt Mehlhorn, “Nearly Optimal Binary Search Tree”
https://people.mpi-inf.mpg.de/~mehlhorn/ftp/mehlhorn3.pdf
21