// Description: Computes the height of each node in the tree // and stores the height of the node in the node's // attribute "height" (see Node.h). // For example, if current is a leaf, its height is 1. // If current has a leaf as a left and as a right subtree, // its height is 2, etc... If current is NULL, its height is 0. int BST::heightR(Node* current) const { if(current == NULL) //base case { return 0; } int leftHeight = 0; int rightHeight = 0; Node* leftNode = current->getLeft(); Node* rightNode = current->getRight(); if(leftNode != NULL) { leftHeight = heightR(leftNode); } if(rightNode != NULL) { rightHeight = heightR(rightNode); } if(leftHeight > rightHeight) { current->setHeight(leftHeight + 1); return current->getHeight(); } else { current->setHeight(rightHeight + 1); return current->getHeight(); } }
int ContainerAreaLayout::distanceToPreviousItem(ItemList::const_iterator it) const { assert(it != m_items.constEnd()); ContainerAreaLayoutItem* cur = *it; --it; ContainerAreaLayoutItem* prev = (it != m_items.constEnd()) ? *it : 0; return prev ? cur->leftR() - prev->leftR() - prev->widthForHeightR(heightR()) : cur->leftR() - leftR(); }
void ContainerAreaLayout::setGeometry(const QRect& rect) { //RESEARCH: when can we short curcuit this? // maybe a dirty flag to be set when we have containers // that needs laying out? QLayout::setGeometry(rect); float totalFreeSpace = kMax(0, widthR() - widthForHeightR(heightR())); int occupiedSpace = 0; ItemList::const_iterator it = m_items.constBegin(); while (it != m_items.constEnd()) { ContainerAreaLayoutItem* cur = *it; ++it; ContainerAreaLayoutItem* next = (it != m_items.constEnd()) ? *it : 0; double fs = cur->freeSpaceRatio(); double freeSpace = fs * totalFreeSpace; int pos = int(rint(freeSpace)) + occupiedSpace; int w = cur->widthForHeightR(heightR()); occupiedSpace += w; if (m_stretchEnabled && cur->isStretch()) { if (next) { double nfs = next->freeSpaceRatio(); w += int((nfs - fs)*totalFreeSpace); } else { w = widthR() - pos; } } cur->setGeometryR(QRect(pos, 0, w, heightR())); } }
void ContainerAreaLayout::updateFreeSpaceValues() { int freeSpace = kMax(0, widthR() - widthForHeightR(heightR())); double fspace = 0; for (ItemList::const_iterator it = m_items.constBegin(); it != m_items.constEnd(); ++it) { int distance = distanceToPreviousItem(it); if (distance < 0) distance = 0; fspace += distance; double ssf = ( freeSpace == 0 ? 0 : fspace/freeSpace ); if (ssf > 1) ssf = 1; if (ssf < 0) ssf = 0; (*it)->setFreeSpaceRatio(ssf); } }
// Description: This public method is a wrapper method, // which calls the recursive heightR(Node *current) defined below. // It returns the height of the tree. int BST::height() const { int nodeHeight = heightR(root); return nodeHeight; }