/*
prime : 素数、质数
Get all the primes less than a known number N.
*/
//main
#include <iostream>
#include <vector>
#include <cmath>
#include "BinaryTree.h"
using namespace std;
int main()
{
BinaryTree bt( 12 );
bt.insert( 2 );
bt.insert( 10 );
bt.insert(20);
bt.insert(13);
bt.print();
cout << "maxdiff:" << bt.getMaxDiff() << endl;
cout << bt.contains(103)<<endl;
bt.remove(20);
bt.print();
return 0;
}
//binarythree.h
class BinaryTree
{
struct BinaryNode
{
int data;
BinaryNode * left;
BinaryNode * right;
BinaryNode( const int & d, BinaryNode * lt, BinaryNode * rt ):data( d ), left( lt ), right( rt ){}
BinaryNode(){}
};
BinaryNode * first;
void insert( BinaryNode * & p, const int & d );
void print( BinaryNode * p );
int getMaxDiff( BinaryNode * p ,int & max, int & min );
bool contains( BinaryNode * p, const int & i , bool & flag ) const;
void remove( BinaryNode * & p, const int & i );
BinaryNode * findMin( BinaryNode * p );
public:
BinaryTree(void);
BinaryTree(int);
~BinaryTree(void);
void insert( const int );
void print();
int BinaryTree::getMaxDiff( );
bool contains( const int & i ) const;
void remove( const int & i );
};
//binarythree.cpp
#include "BinaryTree.h"
#include <iostream>
using namespace std;
BinaryTree::BinaryTree(void)
{
first = NULL;
}
BinaryTree::BinaryTree( int d )
{
first = new BinaryNode( d, NULL, NULL );
/*
first = new BinaryNode;
first->data = d;
first->left = NULL;
first->right = NULL;
*/
}
BinaryTree::~BinaryTree(void)
{
delete first;
}
int BinaryTree::getMaxDiff( )
{
if ( first == NULL )
return 0;
else
{
int max = first->data;
int min = first->data;
//int x = getMaxDiff( first, max, min );
//cout << "max: " << max <<" min : "<< min << endl;
//return x;
return getMaxDiff( first, max, min );
}
}
int BinaryTree::getMaxDiff( BinaryNode * p, int & max, int & min )
{
if ( p == NULL )
return 0;
if( p->data < min )
min = p->data;
else if ( p->data > max )
max = p->data;
getMaxDiff( p->left, max, min );
getMaxDiff( p->right, max, min );
return abs( max-min );
}
void BinaryTree::insert( const int d )
{
insert( first, d );
}
void BinaryTree::insert( BinaryNode * & p, const int & d ) //这里用引用,使得p的地址改变也可以引起调用实参所对应的地址的改变..
{
if ( p == NULL )
p = new BinaryNode( d, NULL, NULL );
else if ( d < p->data )
insert( p->left , d );
else if( d > p->data )
insert( p->right , d );
else
;
}
void BinaryTree::print()
{
if( first == NULL )
{
cout << "The tree is empty." << endl;
return ;
}
else
print( first );
}
void BinaryTree::print( BinaryNode * p )
{
if( p == NULL )
return;
else
cout << p->data << " ";
print( p->left );
print( p->right );
}
bool BinaryTree::contains( BinaryNode * p, const int & i ,bool & flag ) const
{
/*二叉查找树操作方法
if ( p == NULL )
return false;
else if ( p->data < i )
return contains( p->right, i );
else if ( p->data > i )
return contains( p->left, i );
else
return true; //一旦 执行这句,就直接调用结束,即使 返回上层调用函数..
*/
//二叉树操作方法
if ( p == NULL )
{
return false;
}
else if ( p->data == i )
{
flag = true;
return true;
}
else
{
contains( p->left, i, flag );
contains( p->right, i, flag );
}
return flag;
}
bool BinaryTree::contains( const int & i ) const
{
bool flag = false;
return contains( first , i, flag );
}
void BinaryTree::remove( const int & i )
{
remove( first, i );
}
void BinaryTree::remove( BinaryNode * & p, const int & i )
{
//二叉树操作
if ( p == NULL )
return; //Item not found; do nothing
if ( p->data != i)
{
remove( p->left, i );
remove( p->right, i );
}
//二叉查找树操作时,上段改成
/*
if( i < p->data )
remove( p->left, i );
else if( i > p->data )
remove( p->right, i );
*/
else if ( p->left != NULL && p->right != NULL ) //Two Children
{
p->data = findMin( p->right )->data;
remove( p->right, p->data );
}
else
{
BinaryNode * oldNode = p;
p = ( p->left != NULL )? p->left : p->right;
delete oldNode;
}
}
BinaryTree::BinaryNode * BinaryTree::findMin( BinaryNode * p ) //开头一定要加BinaryTree::,因为BinaryNode是定义在该类中的
{
if ( p != NULL )
{
while ( p->left != NULL )
{
p = p->left;
}
return p;
}
}
//binaryheap.main.cpp
#include <vector>
#include <iostream>
#include "BinaryHeap.h"
using namespace std;
template < class T >
void print( const vector< T > & myvec )
{
for ( int i = 0; i < (int)myvec.size(); i++ )
{
cout << myvec[i]<<"*";
}
cout << endl;
}
int main()
{
vector< int > a(4,100);
print( a );
vector< int >b( a.size()+10 ,99);
print( b );
cout << b.size()<<endl;
b.push_back(12);
print( b );
cout << b.size()<<endl;
BinaryHeap bh( b );
bh.print();
bh.deleteMin();
bh.print();
BinaryHeap bh2;
bh2.insert(12);
bh2.print();
bh2.deleteMin();
bh2.print();
//bh.insert(12);
//bh.print();
return 0;
}
//binaryheap.h
#pragma once
#include <vector>
using namespace std;
//最小堆
class BinaryHeap
{
int CurrentSize; //array的size必须比currentsize大1.大多少可以自定义。
vector< int > array; //vector是属于std的名字空间,所以要在开头写using namespace std;
void buildHeap();
void percolateDown( int hole );
public:
BinaryHeap(void);
BinaryHeap( const vector< int > & items );
~BinaryHeap(void);
void insert( const int & x );
void print();
void deleteMin();
};
//binaryheap.cpp
#include "BinaryHeap.h"
#include <iostream>
BinaryHeap::BinaryHeap(void)
{
CurrentSize = 0;
array.resize(1); //优先队列从下标1开始存储
//array.reserve(100);
}
BinaryHeap::BinaryHeap( const vector< int > & items ):array( items.size()+10 ), CurrentSize( int(items.size()) )
{
cout<<CurrentSize<<" "<<array.size()<<endl;
for ( int i = 0; i < items.size(); i++ )
array[ i + 1 ] = items[ i ];
buildHeap();
}
BinaryHeap::~BinaryHeap(void)
{
}
void BinaryHeap::insert( const int & x )
{
if ( CurrentSize == array.size()-1 )
array.resize( array.size() * 2 );
// percolate up
int hole = ++CurrentSize;
for ( ; hole > 1 && x < array[ hole/2 ]; hole /= 2 )
array[ hole ] = array[ hole/2 ];
array[ hole ] = x;
}
void BinaryHeap::print()
{
for ( int i = 1; i < CurrentSize+1; i++ )
{
cout << array[ i ] << " ";
}
cout << endl;
}
void BinaryHeap::percolateDown( int hole )
{
int child;
int tmp = array[ hole ];
for ( ; hole * 2 <= CurrentSize; hole = child )
{
child = hole * 2; //left child
if( child != CurrentSize && array[ child + 1] < array[ child ] ) //取左右儿子中较小的那个的下标。最小堆
child++;
if ( array[ child ] < tmp ) //子结点的值比较小,上移到hole中
array[ hole ] = array[ child ];
else
break;
}
array[ hole ] = tmp;
}
void BinaryHeap::deleteMin()
{
if ( CurrentSize )
{
array[ 1 ] = array[ CurrentSize-- ];
percolateDown( 1 );
}
}
void BinaryHeap::buildHeap()
{
for ( int i = CurrentSize/2; i > 0; i-- ) //从堆的倒数第二层开始上滤
percolateDown( i );
}
//数字计算相关
/*
把中缀(infix)表达式改为后缀(suffix)表达式
and compute the result of the expression.
*/
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <map>
using namespace std;
int compute( const int & a, const int & b , const char & op )
{
switch( op )
{
case '*' : return a*b;
case '/' : return a/b;
case '+' : return a+b;
case '-' : return a-b;
default : return 0;
}
}
bool LessPriority( const char & op1, const char & op2 )
{
map< char ,int > Priority;
Priority[ '+' ] = 0 ;
Priority[ '-' ] = 0 ;
Priority[ '*' ] = 1 ;
Priority[ '/' ] = 1 ;
if ( Priority[ op1 ] < Priority[ op2] )
return true;
else
return false;
}
template < class T >
string infixTosuffix( const string & str )
{
}
int main()
{
string infix( "1+2*3-4/5-8" );
cout << infix << endl;
string suffix;
istringstream inbuf( infix );
ostringstream outbuf( suffix );
//stringstream outbuf( suffix, stringstream::in | stringstream::out );
stack<char> chstack;
stack< int > result; //used to compute the result
int i, num1, num2;
char ch, op;
inbuf >> i;
outbuf << i;
//Once push a number into the outbuf, push it into the result stack too.
result.push( i );
while( inbuf >> ch )
{
//If the top element's priority of stack is less than ch's, then push the ch;
//or, pop the top elements until its priority is not less than ch, and push ch.
while( !chstack.empty() && !LessPriority( chstack.top(), ch ) )
{
outbuf << chstack.top();
op = chstack.top();
chstack.pop();
//Once push an operator into the outbuf, compute the result.
num2 = result.top(); result.pop();
num1 = result.top(); result.pop();
num1 = compute( num1 , num2, op );
result.push( num1 );
}
chstack.push( ch );
inbuf >> i;
outbuf << i;
//Once push a number into the outbuf, push it into the result stack too.
result.push( i );
}
while( !chstack.empty() )//if the stack is not empty, pop all the elements.
{
outbuf << chstack.top();
op = chstack.top();
chstack.pop();
//Once push an operator into the outbuf, compute the result.
num2 = result.top(); result.pop();
num1 = result.top(); result.pop();
num1 = compute( num1 , num2, op );
result.push( num1 );
}
suffix = outbuf.str();
cout << suffix <<endl;
//Pop the only one number in the result stack and print it.
cout << "The size of the result stack: " << ( int )result.size() <<endl ;
cout << "The result: " << result.top() <<endl;
return 0;
}
笔记(二叉树堆栈实现)
最新推荐文章于 2023-10-25 21:54:18 发布