C++ BinaryTree Help : Creating main function for Trees... Here are the header fi
ID: 3786794 • Letter: C
Question
C++ BinaryTree Help : Creating main function for Trees...
Here are the header files :
PrintExpressionTour.H:
#ifndef _PRINTEXPRESSIONTOUR_H_
#define _PRINTEXPRESSIONTOUR_H_
#include "EulerTour.h"
#include <iostream>
template <typename E, typename R>
class PrintExpressionTour : public EulerTour<E, R> {
protected: // ...same type name shortcuts as in EvaluateExpressionTour
public:
inline void execute( const BinaryTree& T ) { // execute the tour
initialize( T );
std::cout << "Expression: "; eulerTour( T.root() ); std::cout << std::endl;
}
protected: // leaf: print value
virtual void visitExternal( const Position& p, Result& r ) const {
std::cout << *p;
}
// left: open new expression
virtual void visitLeft( const Position& p, Result& r ) const {
std::cout << "(";
}
// below: print operator
virtual void visitBelow( const Position& p, Result& r ) const {
std::cout << *p;
}
// right: close expression
virtual void visitRight( const BinaryTree::Position& p, Result& r ) const {
std::cout << ")";
}
};
#endif // _PRINTEXPRESSIONTOUR_H_
BinaryTree.h:
#ifndef _BINARYTREE_H_
#define _BINARYTREE_H_
#include <list>
#include <string>
typedef std::string Elem; // base element type
class BinaryTree {
protected:
struct Node { // a node of the tree
Elem elt; // element value
Node* par; // parent
Node* left; // left child
Node* right; // right child
Node() : elt(), par( NULL ), left( NULL ), right( NULL ) {} // constructor
};
public:
class Position { // position in the tree
private:
Node* v; // pointer to the node
public:
Position( Node* _v = NULL ) : v( _v ) {} // constructor
Elem& operator*() const // get element
{
return v->elt;
}
Position left() const // get left child
{
return Position( v->left );
}
Position right() const // get right child
{
return Position( v->right );
}
Position parent() const // get parent
{
return Position( v->par );
}
bool isRoot() const // root of the tree?
{
return v->par == NULL;
}
bool isExternal() const // an external node?
{
return v->left == NULL && v->right == NULL;
}
friend class BinaryTree; // give tree access
};
typedef std::list<Position> PositionList; // list of positions
public:
BinaryTree(); // constructor
int size() const; // number of nodes
bool empty() const; // is tree empty?
Position root() const; // get the root
PositionList positions() const; // list of nodes
void addRoot(); // add root to empty tree
void expandExternal( const Position& p ); // expand external node
Position removeAboveExternal( const Position& p ); // remove p and parent
// housekeeping functions omitted...
protected: // local utilities
void preorder( Node* v, PositionList& pl ) const; // preorder utility
private:
Node* _root; // pointer to the root
int n; // number of nodes
};
#endif // _BINARYTREE_H_
EulerTour.h:
#ifndef _EULERTOUR_H_
#define _EULERTOUR_H_
#include "BinaryTree.h"
template <typename E, typename R> // element and result types
class EulerTour { // a template for Euler tour
protected:
struct Result { // stores tour results
R leftResult; // result from left subtree
R rightResult; // result from right subtree
R finalResult; // combined result
};
//typedef BinaryTree<E> BinaryTree; // the tree
typedef typename BinaryTree::Position Position; // a position in the tree
protected: // data member
const BinaryTree* tree; // pointer to the tree
public:
void initialize( const BinaryTree& T ) // initialize
{
tree = &T;
}
protected: // local utilities
R eulerTour( const Position& p ) const {
Result r = initResult();
if ( p.isExternal() ) { // external node
visitExternal( p, r );
} else { // internal node
visitLeft( p, r );
r.leftResult = eulerTour( p.left() ); // recurse on left
visitBelow( p, r );
r.rightResult = eulerTour( p.right() ); // recurse on right
visitRight( p, r );
}
return result( r );
} // perform the Euler tour who will implement this?
// functions given by subclasses
virtual void visitExternal( const Position& p, Result& r ) const {}
virtual void visitLeft( const Position& p, Result& r ) const {}
virtual void visitBelow( const Position& p, Result& r ) const {}
virtual void visitRight( const Position& p, Result& r ) const {}
Result initResult() const {
return Result();
}
R result( const Result& r ) const {
return r.finalResult;
}
};
#endif
BinaryTree.cpp:
#include "BinaryTree.h"
void BinaryTree::expandExternal( const Position& p ) {
Node* v = p.v; // p's node
v->left = new Node; // add a new left child
v->left->par = v; // v is its parent
v->right = new Node; // and a new right child
v->right->par = v; // v is its parent
n += 2; // two more nodes
}
BinaryTree::PositionList BinaryTree::positions() const {
PositionList pl;
preorder( _root, pl ); // preorder traversal
return PositionList( pl ); // return resulting list
}
// preorder traversal
void BinaryTree::preorder( Node* v, PositionList& pl ) const {
pl.push_back( Position( v ) ); // add this node
if ( v->left != NULL ) // traverse left subtree
preorder( v->left, pl );
if ( v->right != NULL ) // traverse right subtree
preorder( v->right, pl );
}
BinaryTree::BinaryTree() // constructor
: _root( NULL ), n( 0 ) {}
int BinaryTree::size() const // number of nodes
{
return n;
}
bool BinaryTree::empty() const // is tree empty?
{
return size() == 0;
}
BinaryTree::Position BinaryTree::root() const // get the root
{
return Position( _root );
}
void BinaryTree::addRoot() // add root to empty tree
{
_root = new Node; n = 1;
}
I need to make another "main.cpp" for this BinaryTree:
I have started with the following:
#include <cstdio>
#include <cstdlib>
#include "BinaryTree.h"
#include "PrintExpressionTour.h"
int main()
{
// What goes inside here by looking at the Tree Picture?
system( "pause" );
return EXIT_SUCCESS;
}
Help Anyone Please? Thank you
// 4 7 3 2 5 9 3 3Explanation / Answer
Sequence Alignment or sequence comparison lies in spite of appearance of the bioinformatics, that describes the approach of arrangement of DNA/RNA or macromolecule sequences, so as to spot the regions of similarity among them. it's wont to infer structural, useful and biological process relationship between the sequences. Alignment finds similarity level between question sequence and completely different info sequences. The algorithmic program works by dynamic programming approach that divides the matter into smaller freelance sub issues. It finds the alignment a lot of quantitatively by assignment scores.
When a replacement sequence is found, the structure and performance will be simply expected by doing sequence alignment. Since it's believed that, a sequence sharing common ascendent would exhibit similar structure or perform. bigger the sequence similarity, bigger is that the likelihood that they share similar structure or perform.
Methods of Sequence Alignment:
There square measure primarily 2 strategies of Sequence Alignment:
Global Alignment : Closely connected sequences that square measure of same length square measure much applicable for international alignment. Here, the alignment is dole out from starting until finish of the sequence to search out out the simplest potential alignment.
The Needleman-Wunsch algorithmic program (A formula or set of steps to unravel a problem) was developed by Saul B. Needleman and Christian D. Wunsch in 1970, that could be a dynamic programming algorithmic program for sequence alignment. The dynamic programming solves the initial drawback by dividing the matter into smaller freelance sub issues. These techniques square measure utilized in many various aspects of engineering science. The algorithmic program explains international sequence alignment for positioning ester or macromolecule sequences.
Local Alignment : Sequences that square measure suspected to possess similarity or perhaps dissimilar sequences will be compared with native alignment technique. It finds native regions with high level of similarity.
These 2 strategies of alignments square measure outlined by completely different algorithms, that use grading matrices to align the 2 completely different series of characters or patterns (sequences). {the 2|the 2} {different|totally completely different|completely different} alignment strategies square measure largely outlined by Dynamic programming approach for positioning two different sequences.
Dynamic Programming:
Dynamic programming is employed for optimum alignment of 2 sequences. It finds the alignment in an exceedingly a lot of quantitative approach by giving some scores for matches and mismatches (Scoring matrices), instead of solely applying dots. By looking out the best scores within the matrix, alignment will be accurately obtained. The Dynamic Programming solves the initial drawback by dividing the matter into smaller freelance sub issues. These techniques square measure utilized in many various aspects of engineering science. Needleman-Wunsch and Smith-Waterman algorithms for sequence alignment square measure outlined by dynamic programming approach.
Scoring matrices:
In optimum alignment procedures, largely Needleman-Wunsch and Smith-Waterman algorithms use classification system. For ester sequence alignment, the grading matrices used square measure comparatively less complicated since the frequency of mutation for all the bases square measure equal. Positive or higher price is appointed for a match and a negative or a lower price is appointed for pair. These assumption based mostly scores will be used for grading the matrices. There square measure different grading matrices that square measure predefined largely, utilized in the case of organic compound substitutions.
Mainly used predefined matrices square measure PAM and BLOSUM.
PAM Matrices: Margaret Dayhoff was the primary one to develop the PAM matrix, PAM stands for purpose Accepted Mutations. PAM matrices square measure calculated by perceptive the variations in closely connected proteins. One PAM unit (PAM1) specifies one accepted gene mutation per a hundred organic compound residues, i.e. one hundred and twenty fifth amendment and ninety nine remains in and of itself.
BLOSUM: BLOcks SUbstitution Matrix, developed by Henikoff and Henikoff in 1992, used preserved regions. These matrices square measure actual proportion identity values. merely to mention, they depend upon similarity. Blosum sixty two suggests that there's sixty two Gestalt law of organization.
Gap score or gap penalty: Dynamic programming algorithms use gap penalties to maximise the biological that means. Gap penalty is subtracted for every gap that has been introduced. There square measure completely different gap penalties like gap open and gap extension. The gap score defines a penalty given to alignment after we have insertion or deletion. throughout the evolution, there is also a case wherever we are able to see continuous gaps right along the sequence, that the linear gap penalty wouldn't be applicable for the alignment. therefore gap open and gap extension has been introduced once there square measure continuous gaps (five or more). The open penalty is often applied at the beginning of the gap, so the opposite gaps following it's given with a niche extension penalty which is able to be less compared to the open penalty. Typical values square measure –12 for gap gap, and –4 for gap extension.
Working of Needleman -Wunsch algorithmic program
To study the algorithmic program, take into account the 2 given sequences.
CGTGAATTCAT (sequence #1) , GACTTAC (sequence #2)
The length (count of the nucleotides or amino acids) of the sequence one and sequence a pair of square measure eleven and seven severally. The initial matrix is made with A+1 column’s and B+1 row’s (where A and B corresponds to length of the sequences). additional row and column is given, therefore on align with gap, at the beginning of the matrix as shown in Figure one.
Figure 1: Initial matrix
After making the initial matrix, grading schema should be introduced which might be user outlined with specific scores. the straightforward basic grading schema will be assumed as, if 2 residues (nucleotide or amino acid) at ith and jth position square measure same, matching score is one (S(i,j)= 1) or if the 2 residues at ith and jth position don't seem to be same, pair score is assumed as -1 (S(i,j)= -1 ). The gap score(w) or gap penalty is assumed as -1 .
*Note: The a lot of match, pair and gap will be user outlined, provided the gap penalty ought to be negative or zero.
Gap score is outlined as penalty given to alignment, after we have insertion or deletion.
The dynamic programming matrix is outlined with 3 completely different steps.
1.Initialization of the matrix with the scores potential.
2.Matrix filling with most scores.
3.Trace back the residues for applicable alignment.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.