int checkBalance(struct TreeNode* root) { if(root==NULL) return 0; int leftheight = checkBalance(root->left); if(leftheight == -1) return -1; int rightheight = checkBalance(root->right); if(rightheight == -1) return -1; if(abs(leftheight - rightheight) > 1) return -1; return leftheight > rightheight ? leftheight + 1 : rightheight + 1; }
int addChild(node **tree, int value){ node* subtree = *tree; node* n = NULL; int result; if(!subtree){// If the tree is empty n = createNode(value); *tree = n; return 1; } if(value < subtree->value){ if(subtree->left == NULL){// We can add a child n = createNode(value); subtree->left = n; subtree->lh += 1; subtree->balanceFactor = subtree->lh - subtree->rh; return 1; } else{// Entering a recursion if an element has left child. Checking balance factors. If the tree needs balancing it performs the task. subtree->lh = addChild(&(subtree->left), value) + 1; subtree->balanceFactor = subtree->lh - subtree->rh; result = checkBalance(subtree); if(result){// If the tree needs to be balanced printf("Rotacija: %d\n", result); printf("value: %d, balance factor: %d\n", subtree->value, subtree->balanceFactor); *tree = performRotation(subtree, result); } return (*tree)->lh >= (*tree)->rh ? (*tree)->lh : (*tree)->rh; } } else if(value > subtree->value){ if(subtree->right == NULL){// We can add a child n = createNode(value); subtree->right = n; subtree->rh += 1; subtree->balanceFactor = subtree->lh - subtree->rh; return 1; } else{// Entering a recursion if an element has right child. Checking balance factors. If the tree needs balancing it performs the task. subtree->rh = addChild(&(subtree->right), value) + 1; subtree->balanceFactor = subtree->lh - subtree->rh; result = checkBalance(subtree); if(result){// If the tree needs to be balanced printf("Rotacija: %d\n", result); printf("value: %d, balance factor: %d\n", subtree->value, subtree->balanceFactor); *tree = performRotation(subtree, result); } return (*tree)->lh >= (*tree)->rh ? (*tree)->lh : (*tree)->rh; } } //Never, never, neverland return 0; }
bool StorageTreeModel::checkBalance(StorageTreeItem *parent) { static int ok; QSqlQuery sql; sql.exec(QString("SELECT COUNT(*) FROM storage_transaction " "JOIN article ON tr_article = ar_id " "JOIN equipment ON ar_equipment = eq_id " "WHERE tr_storage = %1 AND tr_temp <> 1 " "AND eq_id = %2") .arg(parent->storageId()) .arg(parent->equipment())); if (!sql.next()) { qDebug() << Q_FUNC_INFO << sql.lastError(); ok = false; } ok = sql.value(0).toInt() > 0; for (int i = 0; i < parent->childCount(); i++) { ok = checkBalance(parent->child(i)); } return ok; }
void StorageTreeModel::loadEquipments(StorageTreeItem *parent) { QSqlQuery sql; sql.exec(QString("SELECT eq_id, eq_name " "FROM equipment " "WHERE eq_parent = %1 " "ORDER BY eq_name") .arg(parent->equipment() < 0 ? 0 : parent->equipment())); if (sql.lastError().isValid()) { QMessageBox::critical(QApplication::activeWindow(),"StorageTreeModel::loadGroups",sql.lastError().text()); return; } while (sql.next()) { StorageTreeItem *item = new StorageTreeItem(sql.record().field(1).value().toString(), parent); item->setStorage(storage()); item->setEquipment(sql.record().field(0).value().toInt()); item->setIcon(icon_group); loadEquipments(item); if (checkBalance(item)) parent->appendChild(item); } }
void Player::step_idle() { judo.energy = 3.0; // Check walking delta for no movement and set deceleration flag if (xDelta == 0 && (vel.x > 0 || vel.x < 0)) { if (vel.x < 0) { vel.x += ACCEL; initialAccel -= 0.02; if (vel.x > 0) vel.x = 0; } else { vel.x -= ACCEL; initialAccel -= 0.02; if (vel.x < 0) vel.x = 0; } if (!jump.jumping && !jump.falling) { if (vel.y > 0) { vel.y -= ACCEL; if (vel.y < 0) vel.y = 0; } } decelerating = true; } step_normal(); checkBalance(); }
void checkAccount (sqlite3 *db, int acc_id) { double sum; char * acc; acc = calloc(100, sizeof(char)); sprintf(acc, "%i", acc_id); checkBalance(db, acc, &sum); free(acc); printf("balance (of id = %i) = %lf\n", acc_id, sum); }
void Player::step_recover() { recover.elapsed += TIME_STEP; if (recover.elapsed >= 1) { recover.state = false; recover.elapsed = 0; } step_normal(); checkBalance(); }
void Player::step_run() { facingDirection = currentInputDir; if (standingStill) initialAccel = 0; if (initialAccel < 1.2) initialAccel += 0.09; else initialAccel = 1.2; xDelta += initialAccel * currentInputDir; step_normal(); checkBalance(); }
/// validate the reactions bool CKReader::validateReactions(std::ostream& log) { bool ok = true; // int ns = species.size(); int nrxns = static_cast<int>(reactions.size()); vector<int> unbal; log << "checking that all reactions balance..."; if (checkBalance(log, speciesData, reactions, unbal)) { log << " OK" << endl; } else { int nu = static_cast<int>(unbal.size()); for (int iu = 0; iu < nu; iu++) { log << " error... reaction " << unbal[iu] << " does not balance" << endl; } ok = false; } log << "checking for duplicate reactions..."; for (int nn = 0; nn < nrxns; nn++) { Reaction& r1 = reactions[nn]; for (int mm = nn + 1; mm < nrxns; mm++) { Reaction& r2 = reactions[mm]; if (r1 == r2) { r1.duplicate = mm + 1; r2.duplicate = nn + 1; if (!r1.isDuplicate || !r2.isDuplicate) { log << endl << " error... undeclared duplicate reactions: " << nn + 1 << " and " << mm + 1; ok = false; } else { log << endl << " declared duplicate reactions: " << nn + 1 << " and " << mm + 1; } } } } if (ok) log << "...OK" << endl; return ok; }
int TEST_infix2postfix() { element_type data; char *string = "{fdffldfsdfds()}fdfssd[ff...]"; if (checkBalance(string) == SUCCESS) { printf("success, balance!\n"); } char *infix_string = "12 + 2 * 3 + (4*5+6)*1"; char out_string[100] = ""; infix2postfix(infix_string, out_string); printf("infix string: %s\n", infix_string); printf("postfix string: %s\n", out_string); data = calcPostfix(out_string); printf("postfix result: %d\n", data); return 0; }
int main () { Tree *t = NULL; t = insert(t, 1); t = insert(t, -1); t = insert(t, 7); t = insert(t, 40000); t = insert(t, 42000); t = insert(t, 45000); t = insert(t, 30); t = insert(t, 32); t = insert(t, 19); preorder(t); printf("\n"); t = checkBalance(t); inorder(t); printf("\n"); preorder(t); printf("\n"); return 0; }
void Player::step_dash() { currentInputDir = dash.direction; // input direction is always taken over (Possibly unnecessary) facingDirection = currentInputDir; dash.elapsed += TIME_STEP; xDelta += dash.vel * dash.direction; double v = dash.elapsed; dash.vel = dash.vel * (1 - (1 - v) * (1 - v) * (1 - v)); if (dash.elapsed >= dash.elapsedDashed) { // dash is over, but still in dash position dash.vel = 0; } if (dash.elapsed >= dash.elapsedRecovered) { // Cool down time, player is standing back up, can walk after this dash.elapsed = 0; dash.isDashing = false; dash.vel = dash.initialVel; initialAccel = 0; } step_normal(); checkBalance(); }
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_CPP11_NULLPTR #include "../lib/catch.hpp" #include "../CtCI5/4_1_check_balance.cpp" TEST_CASE( "Check if this is a balanced tree" ) { SECTION( "Empty tree" ) { REQUIRE( checkBalance((Node *)nullptr) == true ); Node node(1); REQUIRE( checkBalance(&node) == true ); } SECTION( "Tree with one node" ) { Node node(1); node.left = new Node(2); REQUIRE( checkBalance(&node) == true ); } SECTION( "Complicated tree" ) { Node node(0); node.left = new Node(1); node.right = new Node(2); REQUIRE( checkBalance(&node) == true ); node.left->left = new Node(3); node.left->right = new Node(4);
int removeChild(node **subtree, int value){ node *tmp = *subtree; int result = 0; if(tmp == NULL) return 0; if(tmp->value == value){ if(tmp->left == NULL && tmp->right == NULL){ free(tmp); *subtree = NULL; return -1; } else if(tmp->left == NULL){ *subtree = (*subtree)->right; free(tmp); return (*subtree)->lh > (*subtree)->rh ? (*subtree)->lh : (*subtree)->rh; } else if(tmp->right == NULL){ *subtree = (*subtree)->left; free(tmp); return (*subtree)->lh > (*subtree)->rh ? (*subtree)->lh : (*subtree)->rh; } else{ node *min = findMin(tmp->right); tmp->value = min->value; tmp->rh = removeChild(&((*subtree)->right), (*subtree)->value) + 1; result = checkBalance(*subtree); if(result) { printf("Rotacija: %d\n", result); printf("value: %d, balance factor: %d\n", tmp->value, tmp->balanceFactor); *subtree = performRotation(*subtree ,result); } return (*subtree)->lh > (*subtree)->rh ? (*subtree)->lh : (*subtree)->rh; } } if(tmp->value > value) { tmp->lh = removeChild(&(tmp->left), value) + 1; tmp->balanceFactor = tmp->lh - tmp->rh; result = checkBalance(*subtree); if(result) { printf("Rotacija: %d\n", result); printf("value: %d, balance factor: %d\n", tmp->value, tmp->balanceFactor); *subtree = performRotation(*subtree, result); } return (*subtree)->lh > (*subtree)->rh ? (*subtree)->lh : (*subtree)->rh; } else { tmp->rh = removeChild(&(tmp->right), value) + 1; tmp->balanceFactor = tmp->lh - tmp->rh; result = checkBalance(*subtree); if(result) { printf("Rotacija: %d\n", result); printf("value: %d, balance factor: %d\n", tmp->value, tmp->balanceFactor); *subtree = performRotation(*subtree, result); } return (*subtree)->lh > (*subtree)->rh ? (*subtree)->lh : (*subtree)->rh; } return 0; }
bool isBalanced(struct TreeNode* root) { return checkBalance(root) != -1; }
void *hablar(int fd) { int numbytes; char operacion[BUFF_DEFAULT_SIZE]; char datos1[BUFF_DEFAULT_SIZE]; char datos2[BUFF_DEFAULT_SIZE]; char bigBuff[BIG_BUFF_DEFAULT_SIZE]; if ((numbytes = recv(fd, operacion, BUFF_DEFAULT_SIZE, 0)) == -1) { printf("Error en recv() \n"); exitWithError(); } operacion[numbytes] = '\0'; send(fd, "ok", 3, 0); if ((numbytes = recv(fd, datos1, BUFF_DEFAULT_SIZE, 0)) == -1) { printf("Error en recv() \n"); exitWithError(); } datos1[numbytes] = '\0'; send(fd, "ok", 3, 0); if ((numbytes = recv(fd, datos2, BUFF_DEFAULT_SIZE, 0)) == -1) { printf("Error en recv() \n"); exit(-1); } datos2[numbytes] = '\0'; if (strcmp(operacion, "authtarj") == 0) { CreditCard card; strcpy(card.number, datos1); strcpy(card.password, datos2); if (isValidCreditCard(&card)) { send(fd, "1", 2, 0); } else { send(fd, "0", 2, 0); } }; if (strcmp(operacion, "extraer") == 0) { float ammount = 0; CreditCard card; strcpy(card.number, datos1); ammount = atof(datos2); if (changeAmmount(&card, -ammount) == 0) { send(fd, "1", 2, 0); } else { send(fd, "0", 2, 0); } }; if (strcmp(operacion, "depositar") == 0) { float ammount = 0; CreditCard card; strcpy(card.number, datos1); ammount = atof(datos2); if (changeAmmount(&card, ammount) != 0) { send(fd, "1", 2, 0); } else { send(fd, "0", 2, 0); } }; if (strcmp(operacion, "consulta") == 0) { CreditCard card; strcpy(card.number, datos1); strcpy(card.password, datos2); float balance = checkBalance(&card); char buffer[BUFF_DEFAULT_SIZE]; sprintf(buffer, "%.2f", balance); send(fd, buffer, 10, 0); }; if (strcmp(operacion, "listado") == 0) { CreditCard card; int sent = 0; strcpy(card.number, datos1); fillBufferWithMovements(&card, bigBuff); send(fd, bigBuff, strlen(bigBuff), 0); while (sent < strlen(bigBuff)) { sent += send(fd, bigBuff+sent, strlen(bigBuff) - sent, 0); } }; close(fd); return 0; }