예제 #1
0
파일: treeint.cpp 프로젝트: Malows/aedcode
// -------------------------------------------------------------------
// Esta es otra variante. La funcion `fun' ahora es llamada sobre 
// todos los pares correspondientes `x' en `A' e `y' en 'B'. En los 
// casos en que alguno de los dos es Lambda, entonces el valor pasado
// correspondiente es `undef'. 
void  tree_union (btree<int> &A, btree<int>::iterator nA,
	          btree<int> &B, btree<int>::iterator nB,
                  btree<int> &C, btree<int>::iterator nC,
	          bool (*fun)(int x,int y,int &z),
	          int undef=0) {
  nC = C.erase(nC);
  int w;
  if      (nA == A.end() && nB == B.end()) return;
  else if (nA != A.end() && nB != B.end() && fun (*nA,*nB,w)) {
    nC = C.insert (nC,w);
    tree_union (A, nA.left(), B, nB.left(), C, nC.left(), fun);
    tree_union (A, nA.right(),B,nB.right(), C, nC.right(),fun); } 
  else if (nA == A.end() && fun (undef,*nB,w)) {
    nC = C.insert(nC,w);
    tree_union (A, A.end(), B, nB.left() , C, nC.left(), fun);
    tree_union (A, A.end(), B, nB.right(), C, nC.right(),fun); } 
  else if (nB == B.end() && fun (*nA,undef,w)) {
    nC = C.insert (nC,w);
    tree_union (A, nA.left(), B, B.end(), C, nC.left(), fun);
    tree_union (A, nA.right(), B, B.end(), C, nC.right(), fun);
  } // end if
} 
예제 #2
0
파일: treeint.cpp 프로젝트: Malows/aedcode
// -------------------------------------------------------------------
// Introducimos una mejora inspirada en la programacion funcional.
// Pasamos un argumento que es una funcion que toma dos argumentos
// de entrada x,y y retorna bool. 
// La funcion retorna un bool que indica si el elemento
// correspondiente debe estar en C, y si es asi retorna
// por referencia en el argumento z el valor del elemento
// a insertar. 
void tree_intersection (btree<int> &A, btree<int>::iterator nA,
                        btree<int> &B, btree<int>::iterator nB,
                        btree<int> &C, btree<int>::iterator nC,
                        bool (*fun) (int x, int y, int &z)) {
  // Si habia algo en C limpiarlo
  nC = C.erase(nC);
  int w;
  // Solo hace algo si los dos nodos no son Lambda y si
  // `fun' retorna verdadero. 
  if (nA != A.end() && nB != B.end() && fun(*nA,*nB,w)) {
    // insertar 
    nC = C.insert(nC,w);
    // Llama recursivamente
    tree_intersection (A,nA.left(),B,nB.left(),
		       C,nC.left(),fun);
    tree_intersection (A,nA.right(),B,nB.right(),
		       C,nC.right(),fun);
  } // end if
}