int maxHeight(treeNode *p) { if (!p) return 0; int left_height = maxHeight(p->left); int right_height = maxHeight(p->right); return (left_height > right_height) ? left_height + 1 : right_height + 1; }
int maxHeight(TreeNode *root) { if(!root) return 0; if(!root->left && !root->right) return 1; return 1 + max(maxHeight(root->left), maxHeight(root->right)); }
// Vrátí výšku celého stromu int maxHeight(struct node **root) { if(*root==0) return 0; int mLeft = maxHeight(&(*root)->left); int mRight = maxHeight(&(*root)->right); return (mLeft > mRight? mLeft : mRight) + 1; }
bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return true; int diff = abs(maxHeight(root->left) - maxHeight(root->right)); if(diff > 1) return false; return isBalanced(root->left) && isBalanced(root->right); }
/* Calculate the height of the tree. */ int maxHeight(node *root){ if (root == NULL){ return 0; } else{ int lh = maxHeight(root->left); int rh = maxHeight(root->right); return 1 + (lh > rh ? lh : rh); } }
//--------------------------------------------------------------------------- // maxHeight // Calculates the height of any Node as defined by the furthest Node from it // in its subtree using recursion. int BinTree::maxHeight(Node* item) const { if (!item) // item not found return 0; int left_height = maxHeight(item->left); // search left int right_height = maxHeight(item->right); // search right // if left height is greater than right, add 1 to left_height, and // vice versa return (left_height > right_height) ? left_height + 1 : right_height + 1; }
int maxHeight(Tree * tree){ if (tree == NULL) return 0; int left = maxHeight(getLeftSubtree(tree)); int right = maxHeight(getRightSubtree(tree)); if (left < right) return right + 1; else return left + 1; }
//--------------------------------------------------------------------------- // getHeight // Returns the height of a Node in given BST, with the definition that a leaf // is of level 1, the next Node up is level 2, etc. This definition says // that for a Node that is not a leaf, you count up from 1 from the lowest // Node in its subtree. int BinTree::getHeight(const NodeData& item) const { bool found = false; // true if data is found Node *temp = root; int heightOfTree; if (isEmpty()) // depth is 0 if tree is empty return 0; while((temp!= NULL)) { if (item == *temp->data) // calculate height of subtree { found = true; heightOfTree = maxHeight(temp); break; } else if(item < *temp->data) // search left temp = temp->left; else temp = temp->right; // search right } if (!found) //No data match, return 0 return 0; if (temp->left == NULL && temp->right == NULL) return 1; return heightOfTree; }
int maxHeight(struct node* node) { if (!node) return 0; int left_height = maxHeight(node->left); int right_height = maxHeight(node->right); if (node->left ==NULL && node->right==NULL) { return 0; } return (left_height > right_height) ? left_height + 1 : right_height + 1; }
// @TODO dopsat funkci pro vytvoření stromu, aby byl veškerý kód ve funkcích int main(int argc, char **argv) { struct node *tree = 0; add(1, &tree); add(2, &tree); add(3, &tree); add(4, &tree); add(5, &tree); add(6, &tree); add(7, &tree); printf("Height: %i\n", maxHeight(&tree)); printf("inOrderWalk: "); inOrderWalk(&tree); printf("\n"); printf("preOrderWalk: "); preOrderWalk(&tree); printf("\n"); printf("postOrderWalk: "); postOrderWalk(&tree); printf("\n"); return 0; }
/** * Return some information about the CMP. * * @return A std::string that represents this object. **/ std::string toString() const { std::string s("CMP\n"); s += "- graph size: " + mtools::toString(graphSize()) + "\n"; s += "- number of clusters: " + mtools::toString(nbClusters()) + "\n"; s += " - number of trivial site: " + mtools::toString(nbTrivialClusters()) + "\n"; s += " - number of single clusters: " + mtools::toString(nbIsolatedClusters()) + "\n"; s += " - number of compounded clusters: " + mtools::toString(nbNonAtomicClusters()) + "\n"; s += "- maximum height: " + mtools::toString(maxHeight()) + "\n"; auto it = clusterActiveSet.rbegin(); s += "- Largest cluster:\n"; if (isMasterCluster()) { s += " - *** MASTER CLUSTER : contains ever other cluster in its action radius ***\n"; } s += " - size: " + mtools::toString((*it)->size) + "\n"; s += " - weight: " + mtools::toString((*it)->weight) + "\n"; s += " - height: " + mtools::toString((*it)->height) + "\n"; if (clusterActiveSet.size() > 1) { it++; s += "- 2nd largest cluster:\n"; s += " - size: " + mtools::toString((*it)->size) + "\n"; s += " - weight: " + mtools::toString((*it)->weight) + "\n"; s += " - height: " + mtools::toString((*it)->height) + "\n"; } if (clusterActiveSet.size() > 2) { it++; s += "- 3th largest cluster:\n"; s += " - size: " + mtools::toString((*it)->size) + "\n"; s += " - weight: " + mtools::toString((*it)->weight) + "\n"; s += " - height: " + mtools::toString((*it)->height) + "\n"; } return s; }
int main(int argc, char const* argv[]) { /** the size of our vectors */ const size_t N = 2; /** the size of gravity */ const double G = 9.81; double initVel[N]; double maxH[N]; double d; //here makeInitVel(1,M_PI/4,initVel); d = maxDistance(initVel,G,N); maxHeight(initVel,G,maxH,N); printf("Max Distance: %f\n",d); printf("Max Height:\n"); printf(" [ %f %f]\n",maxH[0],maxH[1]); return 0; }
// Pretty formatting of a binary tree to the output stream // @ param // level Control how wide you want the tree to sparse (eg, level 1 has the minimum space between nodes, while level 2 has a larger space between nodes) // indentSpace Change this to add some indent space to the left (eg, indentSpace of 0 means the lowest level of the left node will stick to the left margin) void printPretty(BinaryTree *root, int level, int indentSpace, ostream& out) { int h = maxHeight(root); int nodesInThisLevel = 1; int branchLen = 2 * ((int)pow(2.0, h) - 1) - (3 - level)*(int)pow(2.0, h - 1); // eq of the length of branch for each node of each level int nodeSpaceLen = 2 + (level + 1)*(int)pow(2.0, h); // distance between left neighbor node's right arm and right neighbor node's left arm int startLen = branchLen + (3 - level) + indentSpace; // starting space to the first node to print of each level (for the left most node of each level only) deque<BinaryTree*> nodesQueue; nodesQueue.push_back(root); for (int r = 1; r < h; r++) { printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out); branchLen = branchLen / 2 - 1; nodeSpaceLen = nodeSpaceLen / 2 + 1; startLen = branchLen + (3 - level) + indentSpace; printNodes(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out); for (int i = 0; i < nodesInThisLevel; i++) { BinaryTree *currNode = nodesQueue.front(); nodesQueue.pop_front(); if (currNode) { nodesQueue.push_back(currNode->left); nodesQueue.push_back(currNode->right); } else { nodesQueue.push_back(NULL); nodesQueue.push_back(NULL); } } nodesInThisLevel *= 2; } printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out); printLeaves(indentSpace, level, nodesInThisLevel, nodesQueue, out); }
int main(){ int64_t n; scanf("%lld", &n); std::vector<std::pair<int64_t, std::pair<int64_t, int64_t> > > v(n); for(int64_t p = 0; p < n; p++){ int64_t a, b, h; scanf("%lld %lld %lld", &a, &b, &h); v[p].first = b; v[p].second.first = a; v[p].second.second = h; } sort(v.rbegin(), v.rend()); std::stack<int64_t> s; int64_t height(0), maxHeight(0); for(int64_t p = 0; p < n; p++){ while(!s.empty() && v[p].first <= v[s.top()].second.first){height -= v[s.top()].second.second; s.pop();} s.push(p); height += v[p].second.second; maxHeight = (maxHeight > height) ? maxHeight : height; } printf("%lld\n", maxHeight); return 0; }
/* Debugging/Testing */ int main(int argc, char *argv[]) { Node *tree = createNode(3); Node *tmp = createNode(2, tree, true); createNode(1, tmp, true); createNode(5, tree, false); // Inorder inorderR(tree); std::cout << std::endl; inorder(tree); std::cout << std::endl; // Preorder preorderR(tree); std::cout << std::endl; preorder(tree); std::cout << std::endl; // Postorder postorderR(tree); std::cout << std::endl; postorder(tree); std::cout << std::endl; // Max Height std::cout << "Max Height R: " << maxHeightR(tree) << std::endl; std::cout << "Max Height I: " << maxHeight(tree) << std::endl; return 0; }
vector<vector<int> > levelOrderBottom(TreeNode *root) { vector < vector<int> > result; if(!root) return result; int height = maxHeight(root); result.resize(height); queue < pair <TreeNode*, int> > Q; int prev_level = 0, curr_level; Q.push(make_pair(root, 1)); vector <int> container; while(!Q.empty()) { pair <TreeNode*, int> node = Q.front(); Q.pop(); TreeNode *curr = node.first; curr_level = node.second; if(curr_level > prev_level) { if(container.size() > 0) { result[--height] = container; } container.clear(); } container.push_back(curr->val); if(curr->left) Q.push(make_pair(curr->left, curr_level + 1)); if(curr->right) Q.push(make_pair(curr->right, curr_level + 1)); prev_level = curr_level; } if(container.size() > 0) result[--height] = container; return result; }
/** * Removes full rows * */ short removeRows(Game* game) { short i, j, k, l; short counter = 0, rowCounter = 0; short gameHeight = maxHeight(game); for(i=T_HEIGHT-1; i>=gameHeight; i--) { j=0; counter = 0; while(getBitboardValue(game->array[i], j) !=0 && j < T_WIDTH) { j++; counter++; } if(counter == T_WIDTH) { rowCounter++; for(k=i; k>=gameHeight-1; k--) { for(l=0; l<T_WIDTH; l++) game->array[k] = setBitboardValue(game->array[k], l, getBitboardValue(game->array[k-1], l)); } i++; // incrementing i because all went one case down } } return rowCounter; }
int maxHeight(TreeNode* node) { if (node == NULL) { return 0; } int leftChildHeight = maxHeight(node->left); if (leftChildHeight == -1) { return -1; } int rightChildHeight = maxHeight(node->right); if (rightChildHeight == -1) { return -1; } if (abs(leftChildHeight - rightChildHeight) > 1) { return -1; } return max(leftChildHeight, rightChildHeight) + 1; }
int main(){ int num, i, n, c; /* Prompt the user to input the size. */ printf("Please enter the number of nodes in the tree: "); scanf("%d", &num); while (num < 1) { printf("Invalid, please make sure the number is greater than 0.\n"); printf("Please enter the number of nodes in the tree: "); scanf("%d", &num); } node *root = NULL; for (i = 0; i < num; i++){ /* Prompt the user to input the value. */ printf("Please enter an integer: "); scanf("%d", &n); insertNode(&root, createNode(n)); } /* Constantly ask the user to choose an option. */ while (1){ printf("0 -- Exit the program.\n"); printf("1 -- Print the inorder traversal of the Tree.\n"); printf("2 -- Print the preorder traversal of the Tree.\n"); printf("3 -- Print the size(no. of nodes) in the Tree.\n"); printf("4 -- Print the maximum height of the Tree.\n"); printf("5 -- Print the minimum value in the Tree.\n"); printf("Please choose an option among 0-5: "); scanf("%d", &c); switch(c){ case 0: return 0; case 1: inOrder(root); printf("\n"); break; case 2: preOrder(root); printf("\n"); break; case 3: printf("There are %d nodes in tree.\n", sizeOfTree(root)); break; case 4: printf("Maximum height of the tree is %d.\n", maxHeight(root)); break; case 5: printf("Minimum value in the tree is %d.\n", minValue(root)); break; default: printf("Invalid choice. try again!!\n"); break; } printf("\n"); } return 0; }
int heightRight( struct node* node) // Find the # of leaves in the right subtree { int right_height = maxHeight(node->right); if (node == NULL) return 0; else return right_height; }
/** * Update the AWT and Swing size information due to change in internal * state, e.g. if one or more of the icons that might be displayed * is changed */ /*public*/ void PositionableJPanel::updateSize() { // invalidate(); setSize(maxWidth(), maxHeight()); if (debug) { // javax.swing.JTextField text = (javax.swing.JTextField)_popupUtil->_textComponent; // log->debug("updateSize: "+_popupUtil->toString()+ // ", text: w="+getFontMetrics(_popupUtil->getFont()).stringWidth(_popupUtil->getText())+ // "h="+getFontMetrics(_popupUtil->getFont()).getHeight()); } //validate(); repaint(); }
int heightLeft( struct node* node) // Find the # of leaves in the left subtree { int left_height = maxHeight(node->left); if (node==NULL) return(0); else return left_height; }
// driver program to test indenticalTrees function int main() { struct node * root1 = newNode(1); root1->left = newNode(2); root1->right = newNode(3); struct node * root2 = newNode(1); int s = maxHeight(root1); //int s2 = size(root2); printf("%d\n",s); //printf("%d\n",s2); }
void printT(treeNode *tree) { char s[consoleY+1][consoleX+1]; int i, j, height = maxHeight(tree) * 2; if (!height) { puts("Empty!"); return; } memset(s, 32, sizeof(s)); _printT(tree, 0, 0, 0, s); for (i = 0; i < height; ++i) { putchar(10); for (j = 0; j < consoleX; ++j) putchar(s[i][j]); } puts(""); }
SurfaceItem::SurfaceItem(QWaylandSurface *surface) : m_depthOffset(0) , m_opacity(0.55) , m_textureId(0) , m_height(maxHeight() * 0.99) , m_focus(false) , m_mipmap(true) { setSurface(surface); m_time.start(); connect(surface, &QWaylandSurface::damaged, this, &SurfaceItem::surfaceDamaged); connect(surface, &QWaylandSurface::sizeChanged, this, &SurfaceItem::sizeChanged); //m_dirty = QRect(QPoint(), surface->size()); qDebug() << "surface size:" << surface->size(); m_opacityAnimation = new QPropertyAnimation(this, "opacity"); m_opacityAnimation->setDuration(400); sizeChanged(); }
void displayOBSTInfo(OBSTComputation *obstComp) { BinaryTree *obst = obstComp->getOBST(); double timeTaken = obstComp->getTimeTaken(); double averageCase = obstComp->getAverageTime(); int worstCase = maxHeight(obst) - 1; double stdDev = obstComp->getStandardDeviation(); double eOfXSquared = obstComp->getEOfXSquared(); queue<int> levelOrderTraversal = levelOrderTraversalWrapper(obst, 7); cout << "Level Order Traversal: "; displayQueue(levelOrderTraversal); cout << "Program completed in " << timeTaken << " seconds" << endl << "Total Frequencies: " << obstComp->getTotalFrequencies() << endl << "Total Nodes: " << obstComp->getTotalNodes() << endl << "Best Case: 1" << endl << "Average Case: " << setprecision(3) << averageCase << endl << "Worst Case: " << worstCase << endl << "E[X^2] = " << eOfXSquared << endl << "Standard Deviation: " << stdDev << endl << "Sum of Frequencies: " << obstComp->getSumOfFrequencies() << endl; }
void SurfaceItem::setHeight(qreal height) { m_height = qBound(qreal(0.4), height, maxHeight()); }
// Find the maximum height of the binary tree int maxHeight(BinaryTree *p) { if (!p) return 0; int leftHeight = maxHeight(p->left); int rightHeight = maxHeight(p->right); return (leftHeight > rightHeight) ? leftHeight + 1 : rightHeight + 1; }
bool ExynosMPP::isProcessingSupported(hwc_layer_1_t &layer, int format, bool local_path, int loc_out_downscale) { if (local_path && loc_out_downscale == 0) return false; if (isUsingMSC() && local_path) return false; private_handle_t *handle = private_handle_t::dynamicCast(layer.handle); int max_w = maxWidth(layer); int max_h = maxHeight(layer); int min_w = minWidth(layer); int min_h = minHeight(layer); int crop_max_w = 0; int crop_max_h = 0; if (isUsingMSC()) { crop_max_w = 8192; crop_max_h = 8192; } else { crop_max_w = isRotated(layer) ? 2016 : 4800; crop_max_h = isRotated(layer) ? 2016 : 3344; } int crop_min_w = isRotated(layer) ? 32: 64; int crop_min_h = isRotated(layer) ? 64: 32; int srcAlign = sourceAlign(handle->format); int dstAlign; if (local_path) dstAlign = destinationAlign(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M); else dstAlign = destinationAlign(HAL_PIXEL_FORMAT_BGRA_8888); int maxDstWidth; int maxDstHeight; bool rot90or270 = !!(layer.transform & HAL_TRANSFORM_ROT_90); // n.b.: HAL_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_90 | // HAL_TRANSFORM_ROT_180 int src_w = WIDTH(layer.sourceCropf), src_h = HEIGHT(layer.sourceCropf); int dest_w, dest_h; if (rot90or270) { dest_w = HEIGHT(layer.displayFrame); dest_h = WIDTH(layer.displayFrame); } else { dest_w = WIDTH(layer.displayFrame); dest_h = HEIGHT(layer.displayFrame); } if (getDrmMode(handle->flags) != NO_DRM) alignCropAndCenter(dest_w, dest_h, NULL, GSC_DST_CROP_W_ALIGNMENT_RGB888); int max_downscale = local_path ? loc_out_downscale : 16; maxDstWidth = 2560; maxDstHeight = 1600; int max_upscale = 8; /* check whether GSC can handle with local path */ if (local_path) { /* GSC OTF can't handle rot90 or rot270 */ if (!rotationSupported(rot90or270)) return 0; /* * if display co-ordinates are out of the lcd resolution, * skip that scenario to OpenGL. * GSC OTF can't handle such scenarios. */ if (layer.displayFrame.left < 0 || layer.displayFrame.top < 0 || layer.displayFrame.right > mDisplay->mXres || layer.displayFrame.bottom > mDisplay->mYres) return 0; /* GSC OTF can't handle GRALLOC_USAGE_PROTECTED layer */ if (getDrmMode(handle->flags) != NO_DRM) return 0; return isFormatSupportedByGsc(format) && isFormatSupportedByGscOtf(format) && mDisplay->mHwc->mS3DMode == S3D_MODE_DISABLED && paritySupported(dest_w, dest_h) && handle->stride <= max_w && src_w <= dest_w * max_downscale && dest_w <= maxDstWidth && dest_w <= src_w * max_upscale && handle->vstride <= max_h && src_h <= dest_h * max_downscale && dest_h <= maxDstHeight && dest_h <= src_h * max_upscale && src_w <= crop_max_w && src_h <= crop_max_h && src_w >= crop_min_w && src_h >= crop_min_h; } bool need_gsc_op_twice = false; if (getDrmMode(handle->flags) != NO_DRM) { need_gsc_op_twice = ((dest_w > src_w * max_upscale) || (dest_h > src_h * max_upscale)) ? true : false; if (need_gsc_op_twice) max_upscale = 8 * 8; } else { if (!mDisplay->mHasDrmSurface) { need_gsc_op_twice = false; max_upscale = 8; } } if (getDrmMode(handle->flags) != NO_DRM) { /* make even for gscaler */ layer.sourceCropf.top = (unsigned int)layer.sourceCropf.top & ~1; layer.sourceCropf.left = (unsigned int)layer.sourceCropf.left & ~1; layer.sourceCropf.bottom = (unsigned int)layer.sourceCropf.bottom & ~1; layer.sourceCropf.right = (unsigned int)layer.sourceCropf.right & ~1; } /* check whether GSC can handle with M2M */ return isFormatSupportedByGsc(format) && src_w >= min_w && src_h >= min_h && isDstCropWidthAligned(dest_w) && handle->stride <= max_w && handle->stride % srcAlign == 0 && src_w < dest_w * max_downscale && dest_w <= src_w * max_upscale && handle->vstride <= max_h && handle->vstride % srcAlign == 0 && src_h < dest_h * max_downscale && dest_h <= src_h * max_upscale && // per 46.2 (!rot90or270 || (unsigned int)layer.sourceCropf.top % 2 == 0) && (!rot90or270 || (unsigned int)layer.sourceCropf.left % 2 == 0) && src_w <= crop_max_w && src_h <= crop_max_h && src_w >= crop_min_w && src_h >= crop_min_h; // per 46.3.1.6 }
bool isBalanced(TreeNode* root) { return maxHeight(root) != -1; }