/*
 * ALGO:
 * Above solution can be optimized by calculating the height in the
 * same recursion rather than calling height() function separately.
 */
int checkHeight(struct node* root){
	if(root == NULL){
		return 0; // Height of 0
	}

	// Check if left is balanced
	int leftHt = checkHeight(root->left);
	if(leftHt == -1){
		return -1; // Not balanced
	}

	// Check if right is balanced
	int rightHt = checkHeight(root->right);
	if(rightHt == -1){
		return -1; // Not balanced
	}

	// Check if current node is balanced
	if(abs(leftHt-rightHt) > 1){
		return -1; // Not balanced
	}else{
		// return height
		return max(leftHt,rightHt) + 1;
	}
}
Exemple #2
0
 int checkHeight(TreeNode* p) {  
 // return the height of subtree if it is banlanced, otherwise -1 for early pruning
     if (p==NULL) return 0;
     int leftHeight = checkHeight(p->left);
     if (leftHeight==-1) return -1;  // pruning
     int rightHeight = checkHeight(p->right);
     if (rightHeight==-1) return -1;  // pruning
     
     int heightDiff = abs(leftHeight-rightHeight);
     if (heightDiff>1) return -1;
     else return max(leftHeight, rightHeight)+1;
 }
bool isBalancedSol2(struct node* root){
	if(checkHeight(root) == -1){
		return false;
	}else{
		return true;
	}
}
void NotificationLayout::removeNotification(uint key, uint reason)
{
    Notification *n = m_notifications.take(key);
    if (!n)
    {
        qDebug() << "Oooook! Expecting instance of notification, got:" << key;
        return;
    }

    int ix = m_layout->indexOf(n);
    if (ix == -1)
    {
        qDebug() << "Qooook! Widget not in layout. Impossible!" << n;
        return;
    }

    delete m_layout->takeAt(ix);
    n->deleteLater();
    emit notificationClosed(key, reason);

    if (m_notifications.count() == 0)
        emit allNotificationsClosed();

    checkHeight();
}
void NotificationLayout::addNotification(uint id, const QString &application,
                                        const QString &summary, const QString &body,
                                        const QString &icon, int timeout,
                                        const QStringList& actions, const QVariantMap& hints)
{
//    qDebug() << "NotificationLayout::addNotification" << id << application << summary << body << icon << timeout;
    if (m_notifications.contains(id))
    {
        // TODO/FIXME: it can be deleted by timer in this block. Locking?
        Notification *n = m_notifications[id];
        n->setValues(application, summary, body, icon, timeout, actions, hints);
    }
    else
    {
        Notification *n = new Notification(application, summary, body, icon, timeout, actions, hints, this);

        // NOTE: it's hard to use == operator for Notification* in QList...
        QHashIterator<uint, Notification*> it(m_notifications);
        while (it.hasNext())
        {
            it.next();
            if (it.value()->application() == application
                    && it.value()->summary() == summary
                    && it.value()->body() == body)
            {
                qDebug() << "Notification app" << application << "summary" << summary << "is already registered but this request is not an update. Broken app?";
                delete n;
                return;
            }
        }

        connect(n, &Notification::timeout, this, &NotificationLayout::removeNotificationTimeout);
        connect(n, &Notification::userCanceled, this, &NotificationLayout::removeNotificationUser);
        connect(n, &Notification::actionTriggered,
                this, &NotificationLayout::notificationActionCalled);
        m_notifications[id] = n;
        m_layout->addWidget(n);
        n->show();
    }

    checkHeight();
    emit notificationAvailable();

    // NOTE by pcman:
    // This dirty hack is used to workaround a weird and annoying repainting bug caused by Qt.
    // See https://github.com/Razor-qt/razor-qt/issues/536
    // razot-qt bug #536 - Notifications do not repaint under certain conditions
    // When we create the first notification and are about to show the widget, force repaint() here.
    // FIXME: there should be better ways to do this, or it should be fixed in Qt instead.
    if(m_notifications.count() == 1)
        repaint();
}
/**
*	runs a simulation without a view
*/
void Physics::runSimulation(){
	solveGroundConflicts();

	if(checkInternCollissions() && relaxCreature() && checkHeight()){
		totaltime=0;
		m_clock.reset();
		while(totaltime<simulationTime){
			simulationLoopStep(1/1000.f);
		}
	}else{
		fitness = -999999;
		dead=true;
	}

	checkForDismemberment();
}
void NotificationLayout::addNotification(uint id, const QString &application,
                                        const QString &summary, const QString &body,
                                        const QString &icon, int timeout,
                                        const QStringList& actions, const QVariantMap& hints)
{
//    qDebug() << "NotificationLayout::addNotification" << id << application << summary << body << icon << timeout;
    if (m_notifications.contains(id))
    {
        // TODO/FIXME: it can be deleted by timer in this block. Locking?
        Notification *n = m_notifications[id];
        n->setValues(application, summary, body, icon, timeout, actions, hints);
    }
    else
    {
        Notification *n = new Notification(application, summary, body, icon, timeout, actions, hints, this);

        // NOTE: it's hard to use == operator for Notification* in QList...
        QHashIterator<uint, Notification*> it(m_notifications);
        while (it.hasNext())
        {
            it.next();
            if (it.value()->application() == application
                    && it.value()->summary() == summary
                    && it.value()->body() == body)
            {
                qDebug() << "Notification app" << application << "summary" << summary << "is already registered but this request is not an update. Broken app?";
                delete n;
                return;
            }
        }

        connect(n, SIGNAL(timeout()), this, SLOT(removeNotificationTimeout()));
        connect(n, SIGNAL(userCanceled()), this, SLOT(removeNotificationUser()));
        connect(n, SIGNAL(actionTriggered(QString)),
                this, SLOT(notificationActionCalled(QString)));
        m_notifications[id] = n;
        m_layout->addWidget(n);
        n->show();
    }

    emit notificationAvailable();
    checkHeight();
}
 bool isBalanced(TreeNode* root) {
     return checkHeight(root) != -1;
 }
Exemple #9
0
void PrefsTable::checkSize(int rowIndex, int colIndex, const QString& defValue)
{
	checkHeight(rowIndex);
	checkWidth(rowIndex, colIndex, defValue);
}
Exemple #10
0
 bool isBalanced(TreeNode* root) {
     if (checkHeight(root)==-1) return false;
     else return true;
 }
Exemple #11
0
void Box::update(Events gameEvents)
{
    // ------------------------------- ACTIVE -------------------------------
    if(body->IsActive())
    {
        // Update UserData
        body->SetUserData(this);

        // ------------------------------- Dynamic only -------------------------------
        if(body->GetType() == b2_dynamicBody)
        {
            sprite.setPosition(body->GetPosition().x * SCALE, body->GetPosition().y * SCALE);
            sprite.setRotation(180/b2_pi * body->GetAngle());
            overlaySprite.setPosition(body->GetPosition().x * SCALE, body->GetPosition().y * SCALE);
            overlaySprite.setRotation(180/b2_pi * body->GetAngle());

            lastMouse.x = gameEvents.lastMouse.x;
            lastMouse.y = gameEvents.lastMouse.y;

            // Movement
            moveToTargetArea();

            // AI
            ai();

            // Atack
            attack();
        }
        else // ------------------------------- Static only -------------------------------
        {
            ai();
            attack();
            checkNotifications();
            checkForConversion();
        }
        // ------------------------------- Both -------------------------------
        processImpulse();

        // Deletes boxes that have fallen out of the world
        checkHeight();

        // MouseOver
        if(isSelected(gameEvents.lastMouse.x, gameEvents.lastMouse.y))
        {
            isInfo = true;
            sprite.setColor(sf::Color(120, 120, 120, 255));
        }
        else
        {
            isInfo = false;
            sprite.setColor(sf::Color(255, 255, 255, 255));
        }
    }
    else // ------------------------------- INACTIVE -------------------------------
    {
        if(body->GetType() == b2_staticBody)
        {
            checkNotifications();

            if(isSelected(gameEvents.lastMouse.x, gameEvents.lastMouse.y))
            {
                sprite.setColor(sf::Color(120, 120, 120, 255));
            }
            else
            {
                sprite.setColor(sf::Color(200, 200, 200, 255));
            }
        }
    }

    // ------------------------------- Independant of active status -------------------------------
    // Check for minimap
//    float percX = gameEvents.lastMouseWindow.x / window_width;
//    float percY = gameEvents.lastMouseWindow.y / window_height;
//
//    if(percX >= 0.0f && percX <= minimapSize && percY >= 1.0f - minimapSize && percY <= 1.0f)
//    {
//        sprite.setColor(sf::Color(255, 255, 255, 255));
//    }

    // Flicker as long as the timer is active
    if(!hitTimer.timeReached())
    {
        sprite.setColor(sf::Color::Red);
    }

    // Update timer
    hitTimer.update();
    hitNumberTimer.update();
}