void freeTree(Sprig* thisSprig) { if (thisSprig == NULL) return; //free childSprigs first freeTree(thisSprig->leftSprig); int i = numLeaves(thisSprig); Leaf* curr = thisSprig->firstLeaf; for (; i>0; i--) { freeTree(curr->childSprig); curr = curr->nextLeaf; } //then free leaves in own scope curr = thisSprig->firstLeaf; Leaf* next = curr->nextLeaf; for(i=numLeaves(thisSprig); i>0; i--) { free(curr); curr = next; if (curr != NULL) next = curr->nextLeaf; } //then free self free(thisSprig); return; }
bool BspSceneFile::loadLeaves(std::istream& bspStream, const Q3Bsp::DirEntry& leavesEntry) { if (!bspStream.seekg(leavesEntry.offset, std::ios::beg)) { return false; } std::size_t numLeaves(leavesEntry.length / sizeof(Q3Bsp::Leaf)); m_leaves.reserve(numLeaves); if (!bspStream.read(reinterpret_cast<char*>(&m_leaves[0]), leavesEntry.length)) { return false; } return true; }
void addLeaf(Sprig* thisSprig, int key, int caliper){ Leaf* thisLeaf = thisSprig->firstLeaf; if (thisLeaf == NULL) { thisLeaf = (Leaf*)malloc(sizeof(Leaf)); createLeaf(thisLeaf, key, NULL, NULL, thisSprig); thisSprig->firstLeaf = thisLeaf; return; } int count = caliper; while(thisLeaf->key < key) { if (thisLeaf->nextLeaf != NULL) { thisLeaf = thisLeaf->nextLeaf; } else if (count == 2) { //reached end of sprig, keys still smaller. if (thisLeaf->childSprig == NULL) { //split and promote, because no far right sprig available splitPromote(thisLeaf, thisSprig, key, caliper); } //add this key to far right sprig because it exists addLeaf(thisLeaf->childSprig, key, caliper); } else { //there is room in this sprig for a new leaf Leaf* newLeaf = (Leaf*)malloc(sizeof(Leaf)); createLeaf(newLeaf, key, thisLeaf, NULL, thisSprig); thisLeaf->nextLeaf = newLeaf; return; } count--; } //ran into a key too big //try to fit in same sprig if (numLeaves(thisSprig) < caliper - 1) { int tempKey = thisLeaf->key; thisLeaf->key = key; Leaf* newLeaf = (Leaf*)malloc(sizeof(Leaf)); createLeaf(newLeaf, tempKey, thisLeaf, NULL, thisSprig); if (thisLeaf->nextLeaf != NULL) { thisLeaf->nextLeaf->prevLeaf = newLeaf; newLeaf->nextLeaf = thisLeaf->nextLeaf; } thisLeaf->nextLeaf = newLeaf; } //sprig is too full, else { splitPromote(thisLeaf, thisSprig, key, caliper); } return; }
void explore(Sprig* thisSprig, int cursor, int caliper) { printSprig(thisSprig, cursor); Leaf* selectedLeaf = nthLeaf(thisSprig, cursor); char keypress = 0; printf("Command: "); scanf(" %c", &keypress); switch (keypress) { case 'w': if (thisSprig->parentSprig == NULL) { printf("You are already at the top!\n"); } else thisSprig = thisSprig->parentSprig; break; case 'a': if (cursor > 0) cursor--; break; case 's': if (cursor == 0) { if (thisSprig->leftSprig != NULL) { thisSprig = thisSprig->leftSprig; } else printf("No child at this leaf!\n"); } else { if (selectedLeaf->childSprig != NULL) { thisSprig = selectedLeaf->childSprig; } else printf("No child at this leaf!\n"); } break; case 'd': if (cursor < caliper && cursor < numLeaves(thisSprig)) cursor++; break; case 'q': while (thisSprig->parentSprig != NULL) { thisSprig = thisSprig->parentSprig; } freeTree(thisSprig); return; default: printf("Command not recognized. Try again.\n"); break; } explore(thisSprig, cursor, caliper); }
void StackTraceProfiler::print(Node* n, std::string indent) { fprintf(stderr, "%s%lu ", indent.c_str(), n->hits); if (n->addr) { std::string s = StackTrace::Translate(n->addr)->toString(); fprintf(stderr, "%s\n", s.c_str()); } else { fprintf(stderr, "%s\n", m_name.c_str()); } if (numLeaves(n) <= 1) return; indent += " "; std::vector<Node*> v; for (Node* caller = n->callers; caller; caller = caller->next) { v.push_back(caller); } std::sort(v.begin(), v.end(), compareNodes); for (auto i : v) { if (i->hits > n->hits / 100.0) print(i, indent); } }
int BST<ItemType>::numLeaves(TreeNode<ItemType> *t) { if( t == NULL ) return 0; else if( t->left == NULL && t->right == NULL ) return 1; else return ( numLeaves(t->left) + numLeaves(t->right) ); }
int BST<ItemType>::numLeaves() { return ( numLeaves(root) - 1 ); }
// finding the number of leaves in a tree int numLeaves(TreeNodePtr root){ if(root == NULL) return 0; if(root->leftPtr == NULL && root->rightPtr == NULL) return 1; return numLeaves(root->leftPtr) + numLeaves(root->rightPtr); }
int StackTraceProfiler::numLeaves(Node* n) { if (!n->callers) return 1; int count = 0; for (Node* c = n->callers; c; c = c->next) count += numLeaves(c); return count; }