Dynamic Planning-optimal binary search tree

Source: Internet
Author: User

Problem description

Given a sequence K = {K1, K2,..., composed of n different keywords ,..., kN}, and the keywords are ordered. For each key word Ki, the probability of one search for Ki is pi. Some search values may not be in K, so there are n + 1 virtual keys D0, D1,..., DN represents a value no longer in K. D0 indicates all values smaller than K1, and DN indicates all values greater than kn. for I =,..., n-1, di indicates all values between KI and Ki + 1. For each virtual key Di, the probability of a single search corresponding to Di is Qi. The expected price for a search within T is defined as E = Σ (depth (Ki) + 1) * PI + Σ (depth (DI) + 1) * qi = 1 + Σ depth (Ki) * PI + Σ depth (DI) * qI

An optimal binary search tree is the BST with the minimum cost.

Note:

1) An optimal binary tree is not necessarily a tree with the smallest overall height, nor is it always the keyword with the highest probability as the root node.
2) the subtree of the binary search tree must contain keywords in a continuous range.
3) when a tree becomes a subtree of a node, its expected cost added value is the sum of all probabilities in the tree.


Optimal sub-structure
Set to include ordered keywords (KI ,..., the optimal binary query tree of kJ) takes Kr (I ≤ r ≤ j) as the root node, then its left subtree (KI ,..., kr-1) and right subtree (Kr + 1 ,..., kJ) is also the optimal binary search tree.


State transition equation

E [I, j] indicates the average cost of searching an optimal binary tree containing the keyword ki... kJ.

W [I, j] indicates the sum of probabilities of Subtrees containing the keyword ki... kJ.

Code Implementation


// Optimal binary search tree, implemented using dynamic planning # include <iostream> using namespace STD; void optimal_bst (double * P, double * q, int length, double (* E) [20], INT (* root) [20]) {int I, J, K, R; Double T; double W [20] [20] = {0 }; for (I = 1; I <= Length + 1; I ++) {e [I] [I-1] = Q [I-1]; W [I] [I-1] = Q [I-1];} // I is the length between keywords for (I = 1; I <= length; I ++) {// from the keyword whose subscript is J to the keyword whose subscript is K for (j = 1; j <= length-I + 1; j ++) {k = I + J-1; E [J] [k] = 0x7fffffff; W [J] [k] = W [J] [k-1] + P [k] + Q [k]; // select a keyword between J and K as the root of J to K. If the expected value of the tree is the smallest, then r is the root node from J // to K for (r = J; r <= K; r ++) {T = E [J] [r-1] + E [R + 1] [k] + W [J] [k]; if (E [J] [k]> T) {e [J] [k] = T; // R is the root node from subscript J to K. Root [J] [k] = r ;}}}} void construct_optimal_bst (INT (* root) [20], int I, Int J, bool flag) {If (flag = 0) {cout <"K" <root [I] [J] <"Yes root" <Endl; flag = 1 ;} int r = root [I] [J]; // If the left subtree is a leaf if (R-1 <I) {cout <"D" <R-1 <"is the left child of" <"K" <r <Endl ;} // If the left subtree is not the leaf else {cout <"K" <root [I] [r-1] <"is the left child of" <"K" <r <Endl; construct_optimal_bst (root, I, R-1, 1);} // if the right subtree is a leaf if (r> = J) {cout <"D" <j <"is the right child of" <"K" <r <Endl ;} // if the right subtree is not a leaf else {cout <"K" <root [R + 1] [J] <"is the right child of" <"K "<r <Endl; construct_optimal_bst (root, R + 1, J, 1) ;}} int main () {Double P [8] = {0, 0.04, 0.06, 0.08, 0.02, 0.10, 0.12, 0.14}; double Q [8] = {0.06, 0.06, 0.06, 0.06, 0.05, 0.05, 0.05 }; double E [20] [20] = {0}; int root [20] [20] = {0}; optimal_bst (p, q, 6, E, root ); cout <E [1] [5] <Endl; construct_optimal_bst (root, 1, 5, 0); Return 0 ;}

Running result:



Reference

Optimal Binary Tree-http://blog.csdn.net/liuzhanchen1987/article/details/7853219

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.