// Prune a tree at the selected node without removing the selected node // static void treePrune(nbCELL context,BTreeSkill *skillHandle,BTree *tree,nbCELL arglist,char *text){ BTreeNode *node=NULL; nbSET argSet; nbCELL argCell; void *ptr; argSet=nbListOpen(context,arglist); if(argSet==NULL){ if(tree->root!=NULL){ removeTree(context,tree,tree->root); tree->root=NULL; } } else{ ptr=&tree->root; if(argSet!=NULL){ while((argCell=nbListGetCellValue(context,&argSet))!=NULL && (node=treeFindArg(context,tree,argCell,&ptr))!=NULL){ nbCellDrop(context,argCell); ptr=&node->root; } if(argCell!=NULL){ nbLogMsg(context,0,'E',"Entry not found."); return; } if(node->root!=NULL){ removeTree(context,tree,node->root); node->root=NULL; } } } }
// // Recursively remove all nodes in a binary tree // static void *removeTree(nbCELL context,BTree *tree,BTreeNode *node){ node->bnode.key=nbCellDrop(context,node->bnode.key); if(node->bnode.left!=NULL) node->bnode.left=removeTree(context,tree,(BTreeNode *)node->bnode.left); if(node->bnode.right!=NULL) node->bnode.right=removeTree(context,tree,(BTreeNode *)node->bnode.right); if(node->root!=NULL) node->root=removeTree(context,tree,node->root); nbFree(node,sizeof(BTreeNode)); return(NULL); }
void PlayScene::chooseAxeEnded(cocos2d::CCObject *pSender) { sound::playSoundFx(SFX_TOUCH_POP); CCMenuItemSprite* pop = (CCMenuItemSprite*)pSender; pop->setVisible(false); mIsAxePopVisible = false; GameManager::decreaseNumOfAxes(GameManager::getCurrentPlayer()); mTileInfoVector.at(mCurTile)->setHasTree(false); removeTree(); if (mTileInfoVector.at(mCurTile)->getNumberEdgeAvailale() == 0) { mTileInfoVector.at(mCurTile)->setBelongToPlayer(GameManager::getCurrentPlayer()); GameManager::increaseScore(GameManager::getCurrentPlayer()); sprintf(mScoreBuffer, "%i", GameManager::getPlayerScore(GameManager::getCurrentPlayer())); if (GameManager::getCurrentPlayer() == PLAYER_ONE) { mTileInfoVector.at(mCurTile)->getTile()->setColor(ccBLUE); mLbnScorePlayer1->setString(mScoreBuffer); } else { mTileInfoVector.at(mCurTile)->getTile()->setColor(ccRED); mLbnScorePlayer2->setString(mScoreBuffer); } } }
//: Removes the directory or file specified by path OsStatus OsFileSystem::remove(const OsPath& path, UtlBoolean bRecursive, UtlBoolean bForce) { OsStatus retval = OS_INVALID; OsFileInfo info; OsPath testpath = path; getFileInfo(testpath,info); if (info.isDir()) { if (bRecursive) { retval = removeTree(path,bForce); } else { if (rmdir((char *)path.data()) != -1) retval = OS_SUCCESS; } } else { if (bForce) setReadOnly(path,FALSE); if (::remove(path.data()) != -1) retval = OS_SUCCESS; } return retval; }
void main(void) { int i; Bst* bst=allocationTree("one"); //첫 루트 트리노드를 생성 char* data; //입력시 사용자 입력을 받을 변수 char removedata[5]; //삭제시 사용자 입력을 받을 변수 inorderprint(bst); printf("\n"); for(i=1; i<=19; i++) { //19번 반복하면서 twet까지 입력한다. printf("새로운 노드 입력 : "); data=(char*)malloc(sizeof(char)*5); scanf("%s", data); insertTree(bst, allocationTree(data)); inorderprint(bst); printf("\n"); } for(i=1; i<=20; i++) { //총 20번 입력의 역순과 입력순서로 삭제하기 위한 반복문이다. printf("삭제 할 노드 입력 : "); scanf("%s", removedata); removeTree(bst, removedata); inorderprint(bst); printf("\n"); leftdepth=0; //삭제할 노드의 양쪽 서브트리를 비교하기 위한 전역변수를 할때마다 초기화한다. rightdepth=0; leftnumnode=0; rightnumnode=0; } }
Bst* removeTree(Bst* bst, char data[]) { //노드 삭제 함수 Bst* temp; if(bst==NULL) return bst; else if(strcmp(bst->value,data)>0) //데이터를 비교하여 왼쪽 오른쪽으로 트리를 이동시킨다. bst->leftchild=removeTree(bst->leftchild, data); else if(strcmp(data,bst->value)>0) bst->rightchild=removeTree(bst->rightchild, data); else if(strcmp(data,bst->value)==0) { //삭제할 노드를 찾은 경우 if(bst->leftchild!=NULL && bst->rightchild!=NULL) { //삭제할 노드의 자식이 두개인 경우 leftdepth=accountDepth(bst->leftchild); //양 서브트리의 깊이와 노드수를 계산 leftnumnode=accountNumnode(bst->leftchild); rightdepth=accountDepth(bst->rightchild); rightnumnode=accountNumnode(bst->rightchild); if(leftdepth>rightdepth) { //노드의 깊이가 깊은 쪽에서 작거나 큰값의 노드를 골라서 제거 temp=selectmax(bst->leftchild); bst->value=temp->value; bst->leftchild=removeTree(bst->leftchild, temp->value); } if(leftdepth<rightdepth) { temp=selectmin(bst->rightchild); bst->value=temp->value; bst->rightchild=removeTree(bst->rightchild, temp->value); } if(leftdepth==rightdepth && leftnumnode>rightnumnode) { //깊이가 같아서 노드수를 비교하여 노드를 고른다. temp=selectmax(bst->leftchild); bst->value=temp->value; bst->leftchild=removeTree(bst->leftchild, temp->value); } if(leftdepth==rightdepth && leftnumnode<rightnumnode) { temp=selectmin(bst->rightchild); bst->value=temp->value; bst->rightchild=removeTree(bst->rightchild, temp->value); } if(leftdepth==rightdepth && leftnumnode==rightnumnode) { //깊이와 노드수 모두 같아 왼쪽서브트리에서 선택한다. temp=selectmax(bst->leftchild); bst->value=temp->value; bst->leftchild=removeTree(bst->leftchild, temp->value); } } else { //자식 노드가 하나거나 없을 경우의 제거 수행 temp=bst; if(bst->leftchild==NULL) bst=bst->rightchild; else if(bst->rightchild==NULL) bst=bst->leftchild; free(temp); } } return bst; }
PathNode* Astar::findPath( INT a_iSx, INT a_iSy, INT a_iEx, INT a_iEy ) { PathNode* path = NULL; // 타겟 위치와 같은 위치면 탐색 중지 if( a_iSx == a_iEx && a_iSy == a_iEy ) { return NULL; } // 타겟 위치가 가지 못하는 곳이거나 // 타겟 위치 기준 8방향이 가지 못하는 곳이면 탐색 중지 if( m_Map[a_iEx][a_iEy].map == 1 || ( m_Map[a_iEx+0][a_iEy-1].map == 1 && m_Map[a_iEx+1][a_iEy-1].map == 1 && m_Map[a_iEx+1][a_iEy+0].map == 1 && m_Map[a_iEx+1][a_iEy+1].map == 1 && m_Map[a_iEx+0][a_iEy+1].map == 1 && m_Map[a_iEx-1][a_iEy-1].map == 1 && m_Map[a_iEx-1][a_iEy+0].map == 1 && m_Map[a_iEx-1][a_iEy-1].map == 1 ) ) { return NULL; } m_Start[0] = a_iSx; m_Start[1] = a_iSy; m_End[0] = a_iEx; m_End[1] = a_iEy; INT x = a_iSx; INT y = a_iSy; m_Map[x][y].visit = TRUE; m_Map[x][y].cameX = -1; m_Map[x][y].cameY = -1; m_Map[x][y].g = 0; m_Map[x][y].h = getDistH( x,y ); m_Map[x][y].f = getDistF( x,y ); insert( m_pOpen, createNode( x,y ) ); INT v[2]; while( getminimum( v ) ) { x = v[0]; y = v[1]; m_Map[x][y].map = 4; if( x == m_End[0] && y == m_End[1] ) { break; } for( INT i=0; i<8; ++i ) { openMap( x, y, x + m_Dir[i][0], y + m_Dir[i][1] ); } } m_Map[a_iSx][a_iSy].map = 10; m_Map[a_iEx][a_iEy].map = 20; removeTree( m_pOpen ); m_pOpen->root = NULL; return getPath( m_Start[0], m_Start[1], m_End[0], m_End[1] ); }
//: Removes the directory annd all sub-dirs OsStatus OsFileSystem::removeTree(const OsPath& path,UtlBoolean bForce) { UtlBoolean bFailed = FALSE; OsPath origDir; OsFileSystem::getWorkingDirectory(origDir); OsStatus retval = OS_INVALID; OsFileInfo info; OsPath testpath = path; getFileInfo(testpath,info); OsFileIterator::OsFileType fileType = OsFileIterator::ANY_FILE; //only do this if it is a directory if (info.isDir()) { if (OsFileSystem::change(path) == OS_SUCCESS) { OsFileIterator *files = new OsFileIterator(); OsPath entry; OsStatus filestat = files->findFirst(entry,".*", fileType); while (!bFailed && filestat == OS_SUCCESS) { if (entry != "." && entry != "..") { getFileInfo(entry,info); if (info.isDir()) { if (removeTree(entry,bForce) != OS_SUCCESS) { bFailed = TRUE; } } else { OsFile tmpfile(entry); if (tmpfile.remove(bForce) != OS_SUCCESS) { osPrintf("ERROR: can't removing file %s\n",entry.data()); retval = OS_FAILED; bFailed = TRUE; } } } filestat = files->findNext(entry); } delete files; if (OsFileSystem::change(origDir) == OS_SUCCESS) { if (!bFailed && OsFileSystem::remove(path,FALSE,FALSE) != OS_SUCCESS) { osPrintf("ERROR: can't remove dir %s\n",path.data()); retval = OS_FAILED; } else { retval = OS_SUCCESS; } } else { retval = OS_FAILED; } } } return retval; }
/* * assert() method * * assert <node>[(args)][=<value>] * * assert table("a",2,"hello")=5; # set value to 5 * assert table("a",2,"hello"); # set value to 1 * assert !table("a",2,"hello"); # set value to 0 * assert ?table("a",2,"hello"); # remove from table * assert table("a",2,"hello")=?? # remove from table * */ static int baselineAssert(nbCELL context,void *skillHandle,BTree *tree,nbCELL arglist,nbCELL value){ NB_TreePath path; BTreeNode *node=NULL,**nodeP=&tree->root; nbCELL argCell; nbSET argSet; int cellType; double real; int qualifiers=0; nbCELL element[32]; double deviation; if(arglist==NULL) return(0); // perhaps we should set the value of the tree itself argSet=nbListOpen(context,arglist); if(value==NB_CELL_UNKNOWN){ if(argSet==NULL && tree->root!=NULL) tree->root=removeTree(context,tree,tree->root); else removeNode(context,tree,&tree->root,&argSet); return(0); } cellType=nbCellGetType(context,value); if(cellType!=NB_TYPE_REAL){ nbLogMsg(context,0,'E',"Value must be a number"); return(0); } real=nbCellGetReal(context,value); if(argSet==NULL) return(0); argCell=nbListGetCellValue(context,&argSet); while(argCell!=NULL){ element[qualifiers]=argCell; qualifiers++; if(tree->options&BTREE_OPTION_ORDER) node=nbTreeLocateValue(&path,argCell,(NB_TreeNode **)nodeP,treeCompare,context); else node=nbTreeLocate(&path,argCell,(NB_TreeNode **)nodeP); if(node==NULL){ node=nbAlloc(sizeof(BTreeNode)); memset(node,0,sizeof(BTreeNode)); nbTreeInsert(&path,(NB_TreeNode *)node); nodeP=&(node->root); while((argCell=nbListGetCellValue(context,&argSet))!=NULL){ node=nbAlloc(sizeof(BTreeNode)); memset(node,0,sizeof(BTreeNode)); node->bnode.key=(void *)argCell; *nodeP=node; nodeP=&(node->root); } node->value=real; // note we don't set deviation, threshold and level until the end of the period. return(0); } nodeP=&(node->root); nbCellDrop(context,argCell); argCell=nbListGetCellValue(context,&argSet); } if(tree->options&BTREE_OPTION_SUM){ // need to check for zero average and deviation here! node->value+=real; if(node->average==0 && node->deviation==0) return(0); deviation=fabs(node->value-node->average); if(node->value>node->average && deviation>node->threshold) baselineAlert(context,skillHandle,tree,node,element,qualifiers,deviation); } else{ node->value=real; // update value and check limits if(node->average==0 && node->deviation==0) return(0); deviation=fabs(node->value-node->average); if(deviation>node->threshold) baselineAlert(context,skillHandle,tree,node,element,qualifiers,deviation); } return(0); }