Electrofriends

C++ program that uses dynamic programming algorithm to solve the optimal binary search tree problem

by Ranjith | March 11th, 2010.

/* Write a C++ program that uses dynamic programming algorithm to solve the optimal binary search tree problem */

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
#define MAX 10
int find(int i,int j);
void print(int,int);
int p[MAX],q[MAX],w[10][10],c[10][10],r[10][10],i,j,k,n,m;
char idnt[7][10];
 
main()
{
cout << "enter the no, of identifiers";
cin >>n;
cout <<"enter identifiers";
for(i=1;i<=n;i++)
gets(idnt[i]);
cout <<"enter success propability for identifiers";
for(i=1;i<=n;i++)
cin >>p[i];
cout << "enter failure propability for identifiers";
for(i=0;i<=n;i++)
cin >> q[i];
for(i=0;i<=n;i++)
{
w[i][i]=q[i];
c[i][i]=r[i][i]=0;
w[i][i+1]=q[i]+q[i+1]+p[i+1];
r[i][i+1]=i+1;
c[i][i+1]=q[i]+q[i+1]+p[i+1];
}
w[n][n]=q[n];
r[n][n]=c[n][n]=0;
for(m=2;m<=n;m++)
{
for(i=0;i<=n-m;i++)
{
[...]

Read More..

C++ program that uses non-recursive functions to traverse a binary tree in Post-order

by Ranjith | March 11th, 2010.

/* Write C++ program that uses non-recursive functions to traverse a binary tree in Post-order */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
 
class node
{
public:
class node *left;
class node *right;
int data;
};
 
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;
 
}
node *search(node *temp,int ch)
{
if(root== NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right== NULL)
return temp;
 
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
[...]

Read More..

C++ program that uses non-recursive functions to traverse a binary tree in In-order

by Ranjith | March 11th, 2010.

/* Write C++ program that uses non-recursive functions to traverse a binary tree in In-order */

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *left;
class node *right;
int data;
};
 
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;
 
}
 
node *search(node *temp,int ch)
{
if(root== NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right== NULL)
return temp;
 
if(temp->data>ch)
{ if(temp->left==NULL) return [...]

Read More..

C++ program that uses non-recursive functions to traverse a binary tree in Pre-order

by Ranjith | March 11th, 2010.

/* Write C++ program that uses non-recursive functions to traverse a binary tree in Pre-order */

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *left;
class node *right;
int data;
};
 
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;
 
}
 
node *search(node *temp,int ch)
{
if(root== NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right== NULL)
return temp;
 
if(temp->data>ch)
{ if(temp->left==NULL) return [...]

Read More..

C++ program to perform Insert, Delete, Search an element into a binary search tree

by Ranjith | March 10th, 2010.

/* Write a C++ program to perform the following operations:
a) Insert an element into a binary search tree.
b) Delete an element from a binary search tree.
c) Search for a key element in a binary search tree. */

Read More..

Share and enjoy

    • Digg
    • Facebook
    • Technorati
    • StumbleUpon
    • Twitter
    • Reddit
    • del.icio.us
    • Yahoo! Buzz
Copyright©2009 www.electrofriends.com All Rights Reserved. Powered by Dhyeya