//树的叶子节点; void Leaf(csTree root) { if(root) { if(root->Firstchild == NULL ) printf("%c",root->data); Leaf(root->Firstchild); Leaf(root->Nextsibling); } }
int Leaf(BiTree T) // 求二叉树叶子结点数目 { int n,m; if (T==NULL) return (0); else if ((T->lchild == NULL) && (T->rchild == NULL)) return (1); // 如果二叉树的左孩子和右孩子均为空,则返回 1 else // 如果二叉树的左孩子或右孩子不为空 { n = Leaf (T->lchild); // 求 T 的左子树的叶子结点数目 m = Leaf (T->rchild); // 求 T 的右子树的叶子结点数目 return ( n+m ); // 返回总的叶子结点数目 }//else }
void Map::generate_map() { std::ofstream debug; Leaf _root = Leaf(Size(0, 0), Size(map_size.x, map_size.y)); std::vector<Rooms> tmpRooms; if (_seed != -1) srand(_seed); else srand(time(NULL)); debug.open("map.txt"); _leafs.push_back(_root); split(); for(size_t i = 0; i < _leafs.size();i++) _leafs[i].create_rooms(tmpRooms, room_max_size); for (int i = 0; i < room_nbr;i++) _rooms.push_back(tmpRooms[rand() % tmpRooms.size()]); for(size_t i = 0; i < _leafs.size();i++) { delete(_leafs[i].leftChild); delete(_leafs[i].rightChild); } blank_map(); draw_rooms(); draw_better_walls(); draw_hallways(); draw_in_out(); draw_hallways_walls(); draw_angles(); for (size_t i = 0; i < _map.size() ; i++) debug << _map[i] << std::endl; debug.close(); }
void main() { BiTree T; CreateBiTree(T); printf("依次先序中序后序输出:\n"); PreOrderTraverse(T,visit); printf("\n"); InOrderTraverse(T,visit); printf("\n"); PostOrderTraverse(T,visit); printf("\n"); printf("树的深度为:%d\n",BiTreeDepth(T)); printf("树的叶子节点数为:%d\n",Leaf(T)); }
int main(void) { csTree root,p; stackStor s; CScreat(&root); output(root); printf("\n"); Leaf(root); printf("\n"); peorder(root); printf("\n"); }
float Parser::ParseInput(char *in) { NewInput(in); unsigned len = strlen(in); float solution; if(len>1) { solution = BuildSubTree(ExpressionInterval(0,len-1),0); } else if(len == 1) { solution = Leaf(ExpressionInterval(0,0)); } return solution; }
StatusCode DataOnDemandSvc::i_setAlgHandler(const std::string &name, const Gaudi::Utils::TypeNameString &alg){ Leaf leaf(alg.type(), alg.name()); if (m_init) { StatusCode sc = configureHandler(leaf); if (sc.isFailure()) { if (m_allowInitFailure) { // re-store the content of the leaf object to try again to initialize // the algorithm later (on demand) leaf = Leaf(alg.type(), alg.name()); } else return sc; } } m_algs[name] = leaf; return StatusCode::SUCCESS; }
float Parser::BuildSubTree(ExpressionInterval expression, int delimiter) { int k = expression._start; bool isFound = false; float lhs = 0, rhs = 0, result = 0; if(delimiter < _operators.size() && (expression._end < strlen(_inputString) && expression._start <= expression._end)) { while(k <= expression._end && isFound == false) { if(StrCompare(_inputString+k, _operators[delimiter]->_symbol,1)) { switch(_operators[delimiter]->_arity) { case 2: lhs = BuildSubTree(ExpressionInterval(expression._start,k-1),delimiter+1); break; case 1: default: break; } rhs = BuildSubTree(ExpressionInterval(k+1,expression._end),0); isFound = true; } k++; } if(!isFound) { result = BuildSubTree(expression,delimiter+1); } else { result = _operators[delimiter]->Fire(lhs,rhs); } } else { result = Leaf(expression); } return result; }
Tree::Branch* Tree::branchFractal(GLfloat cur_angle, GLfloat prev_angle, Vertex2d initPoint, GLfloat height, unsigned char depth, Floor* floor, bool behindLog) { Vertex2d finalPoint = Vertex2d( initPoint.x + height * cosf(prev_angle + cur_angle + M_PI_2), initPoint.y + height * sinf(prev_angle + cur_angle + M_PI_2) ); // Crea un ramo vuoto, direzionato Tree::Branch* fractal = new Branch(); fractal->angle = cur_angle; // Scegli un numero di foglie e creale int foglie = (depth > 4) ? 0 : rand() % (depth + 20) + 1; for (int i=0; i<foglie; i++) { GLfloat perc = std::max(rand() % 100, rand() % 100) / 100.0; Vertex2d position( initPoint.x * (1 - perc) + finalPoint.x * perc, initPoint.y * (1 - perc) + finalPoint.y * perc ); Vertex2d fallPoint( position.x, (float) (((behindLog) ? rand() % (int)(floor->height / 4) + floor->height / 4: rand() % (int)floor->height / 2)) ); position.x += rand() % 20 - 10.0f; GLfloat size = 5.0f; GLfloat leaf_angle = (cur_angle + prev_angle) * 180 / M_PI + rand() % 90 - 45.0f; RGBColor color((rand() % 4) / 10.0f, 1.0f - (rand() % 3) / 10.0f, (rand() % 3) / 10.0f, 1); int age = rand() % 256; fractal->leaves.push_back(Leaf(position, fallPoint, size, leaf_angle, color, age, behindLog)); } // Scegli un numero di fiori e frutti e creali int flowers = (depth > 4) ? 0 : rand() % 4; for (int i=0; i<flowers; i++) { GLfloat perc = std::max(rand() % 100, rand() % 100) / 100.0; Vertex2d position( initPoint.x * (1 - perc) + finalPoint.x * perc, initPoint.y * (1 - perc) + finalPoint.y * perc ); Vertex2d fallPointFruit( position.x, (float) (rand() % (int)(floor->height / 2)) ); GLfloat size = rand() % 3 + 2; GLfloat sizeFruit = 8; RGBColor centerColor(0.96, 0.76, 0.1, 1); RGBColor petalColor(0.9, 0.1, 0.6, 1); RGBColor fruitColor(1 + ((rand()%10 - 5.0f) / 100.0f), 0.6f + ((rand()%10 - 5.0f) / 100.0f), 0, 1); int age = rand() % 256; fractal->flowers.push_back(Flower(position, size, centerColor, petalColor)); fractal->fruits.push_back(Fruit(position, fallPointFruit, sizeFruit, fruitColor, age)); } // Se non ci sono rami "figli" ho finito if (depth == 0) { return fractal; } // Scegli le direzioni per i figli e creali int angles[] = {0, 45, 315}; std::random_shuffle(angles, angles+3); int children = (depth > 2) ? 3 : rand() % 2 + 1; for (int i=0; i<children; i++) { GLfloat new_angle = (angles[i] + rand() % 20 - 10) / 180.0 * M_PI; fractal->child[i] = branchFractal(new_angle, cur_angle + prev_angle, finalPoint, height * 0.7, depth - 1, floor, behindLog); } return fractal; }
int main( int argc, char *argv[] ) { event_pollmask mask; if ( argc > 1 && !stricmp(argv[1],"-iconbar") ) iconbar = TRUE; mask.value = 0; mask.data.null = 1; wimpinit(); while (!quitapp) { if (ackpending) mask.data.null = 0; else mask.data.null = 1; Wimp_Poll(mask,&e); switch (e.type) { case event_NULL: if (ackpending) { ackpending = FALSE; _kernel_oscli("delete <Wimp$Scrap>"); Error_Report2( 0, "Data transfer failed - receiver died" ); } break; case event_OPEN: Wimp_OpenWindow( &e.data.openblock ); break; case event_CLOSE: if ( e.data.openblock.window == wh && !iconbar ) quitapp = TRUE; else if ( e.data.openblock.window == palh ) palette_open = FALSE; else if ( e.data.openblock.window == spropth ) spropt_open = FALSE; Wimp_CloseWindow( e.data.openblock.window ); break; case event_KEY: if ( e.data.key.caret.window == wh && e.data.key.caret.icon == igicon_SAVENAME && e.data.key.code == 13 ) SaveFile( savename ); else if ( e.data.key.caret.window == wCFSI && e.data.key.code == 13 ) { GetCFSIData(); Wimp_CloseWindow( wCFSI ); cfsi_open = FALSE; } else Wimp_ProcessKey( e.data.key.code ); break; case event_BUTTON: WimpButton(); break; case event_MENU: MenuSelection( e.data.selection ); break; case event_USERDRAG: if (draggingfile) { mouse_block mb; DragASprite_Stop(); draggingfile = FALSE; Wimp_GetPointerInfo( &mb ); e.type = event_SEND; e.data.message.header.action = message_DATASAVE; e.data.message.header.yourref = 0; e.data.message.header.size = 256; e.data.message.data.datasave.window = mb.window; e.data.message.data.datasave.icon = mb.icon; e.data.message.data.datasave.pos = mb.pos; e.data.message.data.datasave.estsize = 0; e.data.message.data.datasave.filetype = 0x695; strcpy( e.data.message.data.datasave.leafname, Leaf( savename ) ); Wimp_SendMessage( event_SEND, &e.data.message, (message_destinee) mb.window, mb.icon ); } break; case event_SEND: case event_SENDWANTACK: switch (e.data.message.header.action) { case message_DATASAVEACK: SaveFile( e.data.message.data.datasaveack.filename ); break; case message_DATALOAD: if ( e.data.message.data.dataload.filetype == 0xFED && e.data.message.data.dataload.window == palh ) { LoadPalette( e.data.message.data.dataload.filename ); } else if ( e.data.message.data.dataload.filetype == 0xFF9 || e.data.message.data.dataload.filetype == 0xC2A || e.data.message.data.dataload.filetype == 0x695 || e.data.message.data.dataload.filetype == 0xAFF || cfsi ) { if ( e.data.message.data.dataload.window < 0 ) OpenMainWindow(); LoadFile( e.data.message.data.dataload.filename, e.data.message.data.dataload.filetype ); } break; case message_DATALOADACK: ackpending = FALSE; break; case message_QUIT: quitapp=TRUE; break; } break; } } Wimp_CloseDown(me); return 0; }
Tree::Node* Tree::contructTree_R( IndexVec& idxEdges ) { if ( idxEdges.empty() ) return nullptr; int idx = choiceBestSplitEdge( idxEdges ); Node* node = new Node; node->idxEdge = idx; if ( idx < 0 ) // leaf { node->tag = ~uint32( mLeaves.size() ); node->front = nullptr; node->back = nullptr; mLeaves.push_back( Leaf() ); Leaf& data = mLeaves.back(); data.node = node; data.edges.swap( idxEdges ); return node; } else { node->tag = uint32( mNodes.size() ); mNodes.push_back( node ); } //triList.erase( cIter ); IndexVec idxFronts; IndexVec idxBacks; Plane& plane = mEdges[ idx ].plane; for( IndexVec::iterator iter( idxEdges.begin() ) , itEnd( idxEdges.end() ) ; iter != itEnd ; ++iter ) { int idxTest = *iter; Edge& edgeTest = mEdges[ idxTest ]; Vec2f vSplit[2]; switch ( plane.splice( edgeTest.v , vSplit ) ) { case SIDE_FRONT: case SIDE_IN: idxFronts.push_back( idxTest ); break; case SIDE_BACK: idxBacks.push_back( idxTest ); break; case SIDE_SPLIT: { idxFronts.push_back( idxTest ); idxBacks.push_back( (int)mEdges.size() ); mEdges.push_back( Edge() ); Edge& edge = mEdges.back(); edge.v[0] = vSplit[0]; edge.v[1] = vSplit[1]; edge.plane = edgeTest.plane; edge.idx = edgeTest.idx; } break; } } node->front = contructTree_R( idxFronts ); if ( node->front ) node->front->parent = node; node->back = contructTree_R( idxBacks ); if ( node->back ) node->back->parent = node; node->tag = 0; return node; }
WCardFilter * WCFilterFactory::Construct(string src) { size_t x = 0; string whitespaces(" \t\f\v\n\r"); x = src.find_first_not_of(whitespaces); if (x != string::npos) src = src.substr(x); size_t srcLength = src.size(); if (!srcLength) return NEW WCFilterNULL(); //Empty string. for (size_t i = 0; i < srcLength; i++) { unsigned char c = src[i]; if (isspace(c)) continue; if (c == '(') { //Parenthesis size_t endp = findNext(src, i); if (endp != string::npos) { WCFilterGROUP * g = NEW WCFilterGROUP(Construct(src.substr(i + 1, endp - 1))); if ( endp < (srcLength - 1) ) { if (src[endp + 1] == '|') return NEW WCFilterOR(g, Construct(src.substr(endp + 2))); else if (src[endp + 1] == '&') return NEW WCFilterAND(g, Construct(src.substr(endp + 2))); } return g; } else return NEW WCFilterNULL(); } else if (c == '{') { //Negation size_t endp = findNext(src, i, '{', '}'); if (endp != string::npos) { WCFilterNOT * g = NEW WCFilterNOT(Construct(src.substr(i + 1, endp - 1))); if (endp < (srcLength - 1) ) { if (src[endp + 1] == '|') return NEW WCFilterOR(g, Construct(src.substr(endp + 2))); else if (src[endp + 1] == '&') return NEW WCFilterAND(g, Construct(src.substr(endp + 2))); } return g; } else return NEW WCFilterNULL(); } else if (c == '&') { //And return NEW WCFilterAND(Construct(src.substr(0, i)), Construct(src.substr(i + 1))); } else if (c == '|') { //Or return NEW WCFilterOR(Construct(src.substr(0, i)), Construct(src.substr(i + 1))); } } return Leaf(src); }