473,756 Members | 4,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Want help for Optimal Binary Search Tree

1 New Member
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 2696
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
2778
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 display, arrange, find, delete the leaf node in binary tree. I hope that anyone with expereince in building binary tree could help me in explaining the binary tree functions, and give a sample code of the whole picture behind the tree. And...
11
2533
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
4254
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 random binary search tree. We've tried everything but are in the land of pointer hell. If someone could help it would be a huge help. our code follows. We've tried 2 diff methods the split() and splitter() functions #include <iostream> #include...
1
2951
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 <typename Tclass Tree{ private: Tree<T*left;
5
5273
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 working. But before I get to the remove, I need to get my find method working. Basically, I am trying to get a "find" method working that will search for a giving int value, and return the node with that value. I have designed my current find with the...
11
1191
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
2
2600
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
4977
by: Vaishali Deshmukh | last post by:
I need a 'C' Source code for the implementation of Optimal Binary search tree
7
3869
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 binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the 'Root' of the tree. The other two subsets are themselves binary trees, called the 'Left'...
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.