473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Want help for Optimal Binary Search Tree

1
Here i have coded algorithm for Optimal Binary Search Tree but not getting correct result can anybody help me (it is executing but result is not desired)?

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5.  
  6. #include<iomanip>
  7. using std::setw;
  8.  
  9.  
  10. const int n=4;
  11. int R[n+1][n];
  12. const float p[n] = {0.1,0.4,0.2,0.3};
  13.  
  14.  
  15.     float sump(int i, int j );
  16.     void optsearchtree(float &minavg);
  17.     int main (int argc, char *argv[])
  18.          { 
  19.             char quit;  
  20.             quit = '\0';
  21.             while (quit != 'x')
  22.               {
  23.                 float minavg;  
  24.                 optsearchtree(minavg);
  25.                 cout<<endl<<endl;  
  26.                 cout<<" Average time for Optimal Binary tree -> : "<<minavg<<endl;
  27.                 cout << endl << " Press x to quit "<<endl;
  28.                 cin >> quit;
  29.               }
  30.             return 0;
  31.          }
  32.  
  33. void optsearchtree(float &minavg)                 
  34.          {
  35.             int i,j,k,diagonal;
  36.             float temp;
  37.             float A[n+1][n];
  38.             for (i=1; i <= n; i++)   
  39.              {
  40.                A[i][i-1] = 0;
  41.                A[i][i] = p[i];
  42.                R[i][i] = i;
  43.                R[i][i-1] = 0;       
  44.               }                      
  45.              A[n+1][n] = 0;  
  46.              R[n+1][n] = 0;
  47.              for(diagonal =1; diagonal <= n-1; diagonal++)
  48.                for(i=1; i <= n - diagonal; i++)
  49.                  {
  50.                     j = i+ diagonal;
  51.                     for(k = i; k <= j ; k++)
  52.                           {
  53.                             temp = A[i][k-1]+ A[k+1][j];      
  54.                             if (temp < A[i][j])
  55.                               {
  56.                                 A[i][j]=temp + sump(i,j);
  57.                                 R[i][j]=k;      
  58.                               }   
  59.                           }       
  60.                   }      
  61.               minavg = A[1][n];    
  62.            }       
  63.  
  64.            float sump(int i, int j )
  65.             {
  66.                float sum = 0;
  67.                for(int a=i; a <= j ; a++)
  68.                sum = sum + p[a];
  69.                return sum;  
  70.             }     
  71.  
Nov 13 '07 #1
1 2674
weaknessforcats
9,208 Expert Mod 8TB
What is your question exactly?

I don't have time to debug this code and hand you the results.
Nov 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am cs students and are now facing difficult problems in understanding what a binary tree is, how it works, and the algorithm to...
11
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
0
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
5
gekko3558
by: gekko3558 | last post by:
I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method...
11
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
2
by: Defected | last post by:
Hi, How i can implement a main function with this Binary Search Tree. thanks for help. is this code corrected ? #include<iostream>
1
by: Vaishali Deshmukh | last post by:
I need a 'C' Source code for the implementation of Optimal Binary search tree
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.