NODE * addToTree(NODE * root, int val){ NODE * temp = root; NODE * newNode = NULL; if(root == NULL){ root = createNode(val); return root; } if(temp->val >= val){ if (temp->left == NULL){ newNode = createNode(val); temp->left = newNode; return root; } addToTree(temp->left, val); }else{ if (temp->right == NULL){ newNode = createNode(val); temp->right = newNode; return root; } addToTree(temp->right, val); } return root; }
void addToTree(tleaf*ptree,void*pdata,int(*fcmp)(void*,void*)) { if (ptree->pdata==NULL) ptree->pdata=pdata; else { if(!fcmp(ptree->pdata,pdata)) { if (ptree->pr==NULL) { ptree->pr=malloc(sizeof(tleaf)); *(ptree->pr)=leafempty; } addToTree(ptree->pr,pdata,fcmp); } else { if (ptree->pl==NULL) { ptree->pl=malloc(sizeof(tleaf)); *(ptree->pl)=leafempty; } addToTree(ptree->pl,pdata,fcmp); } } }
void parsePath(AllData& alldata, string path, int rule, uint64_t n, double timeB) { trimString(path); if (rule != 1) { timeB = -1; } cur_pos = NULL; uint32_t cid; string child; while (path != "") { uint32_t pid = atoi(path.substr(0, path.find_first_of(" ")).c_str()); string parent = alldata.functionIdNameMap[pid]; path = path.substr(path.find_first_of(" ") + 1); if (path == "") { if (cur_pos != NULL) break; cid = 5; child = parent; child = ""; } else { cid = atoi(path.substr(0, path.find_first_of(" ")).c_str()); child = alldata.functionIdNameMap[cid]; } if (path.find_first_of(" ") == path.npos) { addToTree(parent, pid, child, cid, rule, n, timeB); break; } else addToTree(parent, pid, child, cid, -1, 0, -1); } }
void wxSpinTreeCtrl::addToTree(spin::ReferencedNode *n) { wxTreeItemId nodeInTree = GetTreeItem(n); if (nodeInTree) { // If node is already in the tree, we check to see if the parent has // changed. If it has, we remove it first wxTreeItemId parentTreeItem = GetTreeItem(n->getParent()); if (parentTreeItem == GetItemParent(nodeInTree)) { // the parent in the tree is already correct, so we don't need to do // anything std::cout << "Warning (wxSpinTreeCtrl::addToTree). Node " << n->getID() << " already exists in tree." << std::endl; } else { // The node in the tree has the wrong parent, so we need to first // remove the node from the tree, before we can add it to the proper // parent. Freeze(); Delete(nodeInTree); Thaw(); } } wxTreeItemId parentTreeItem = GetTreeItem(n->getParent()); if (parentTreeItem) addToTree(n,parentTreeItem); else addToTree(n,GetRootItem()); }
void parsePath(AllData& alldata, string path, int rule) { cur_pos = NULL; uint32_t cid; string child; while (path != "") { uint32_t pid = 5; string parent = path.substr(0, path.find_first_of(";")); path = path.substr(path.find_first_of(";") + 1); if (path == "") { if (cur_pos != NULL) break; cid = 5; child = parent; child = ""; } else { cid = 5; child = path.substr(0, path.find_first_of(";")); } if (path.find_first_of(";") == path.npos) { addToTree(parent, pid, child, cid, rule, 0, -1); break; } else addToTree(parent, pid, child, cid, -1, 0, -1); } }
void exploitMemLeaks() { // All data allocated and tracked, all will be freed int * low = calloc(1, sizeof(int)); * low = 1; int * mid = calloc(1, sizeof(int)); * mid = 2; int * hi = calloc(1, sizeof(int)); * hi = 3; printf("Created the following data to add to a tree:\n"); printf("Low: %d, Mid: %d, Hi: %d\n", * low, * mid, * hi); // Test inside a tree Tree * test = createBinTree(&compNum, &destructor); addToTree(test, mid); addToTree(test, low); addToTree(test, hi); printInOrder(test, &print); // Up to here, as long as destroyBinTree is called, no leaks are possible // Now lets look at some of the issues with the library // Left will contain Low (1), we know this. This function allocates memory // so it's return value must be freed Tree * left = getLeftSubtree(test); free(left); // All is well, no memory leaks here // Now let's get the right subtree, Hi (3), but before we free it, let's get // get one of it's subtrees too. We know it won't contain anything so // presumably we'll get some sort of error return value Tree * right = getRightSubtree(test); Tree * empty = getRightSubtree(right); if (empty == NULL) printf("The call to an empty subtree returned NULL.\n"); // Oh good, we did get some error handling, great now there's no problem // OOPS, no, there's a huge issue. The getSubtree functions are presumably // doing the following: allocating memory to a temporary new tree pointer, // checking what's in the subtree and returning NULL if nothing is in it. // So the temporary new tree pointer to allocated memory is completely lost // in all instances. // We can't even detect the error without a memory checker because the // following call of free(NULL) has no effect free(empty); free(right); destroyBinTree(test); }
TreeNode *addToTree(vector<int> &num, int b, int e) { if (b > e) return NULL; int mid = (b + e + 1) / 2; TreeNode *ret = new TreeNode(num[mid]); ret->left = addToTree(num, b, mid - 1); ret->right = addToTree(num, mid + 1, e); return ret; }
void addToTree(ttree* pTree, void* pdata, int (*fcmp)(void*,void*)) { if (pTree->pdata==NULL)pTree->pdata=pdata; else if (fcmp(pTree->pdata,pdata)>0) { if(pTree->pl==NULL){pTree->pl=malloc(sizeof(ttree));*(pTree->pl)=treeInit;} addToTree(pTree->pl,pdata,fcmp); } else { if(pTree->pr==NULL){pTree->pr=malloc(sizeof(ttree));*(pTree->pr)=treeInit;} addToTree(pTree->pr,pdata,fcmp); } }
void Configuration::addItem(ConfigItem* item) { _allItems.push_back(item); if (_tree) { addToTree(item); } }
void hashTree::add(const data & aData) { data * ptrItem = new data; *ptrItem = aData; addToTable(ptrItem); addToTree(ptrItem); }
void addToTree(tleaf* tree, void*pdata, int (*fc)(void*,void*)) { if(tree->pData==NULL) tree->pData=pdata; else { int lr=fc(pdata,tree->pData); if(tree->p[lr]==NULL) {tree->p[lr]=malloc(sizeof(tleaf));*tree->p[lr]=EmptyLeaf;} addToTree(tree->p[lr],pdata,fc); } }
int main() { int i; ptree=malloc(sizeof(tleaf)); *ptree=leafempty; for(i=0; i< sizeof(vdata)/sizeof(char*);i++) addToTree(ptree,vdata[i],mycmp); disptree(ptree,printstr); return 0; }
//------------------------------------------------------------- // Model::buildTree() build a tree //------------------------------------------------------------- TreeNode *Model::buildTree(NodeIF *node) { TreeNode *root=new TreeNode(node); LinkedList<NodeIF*>children; if(node->getChildren(children)){ NodeIF *child; children.ss(); while((child=children++)>0) addToTree(root,child); } return root; }
void Configuration::tree(QTreeWidget* tree) { assert(!_tree); _tree = tree; connect(_tree, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(itemChanged(QTreeWidgetItem*, int))); // Add items that were created before we got a tree for (ConfigItem* item : _allItems) { addToTree(item); } }
int main() { int i; ttree* pt=malloc(sizeof(ttree)); *pt=treeInit; for (i=0; vTest[i];i++)addToTree(pt,vTest[i],mycompare); dispTree(pt,mydisp); return 0; }
void addToTree(tleaf* tree, void*pdata, int (*fc)(void*,void*)) { if(tree->pData==NULL) tree->pData=pdata; else { if(fc(pdata,tree->pData)) //rechts { if(tree->pr==NULL) {tree->pr=malloc(sizeof(tleaf));*tree->pr=EmptyLeaf;} addToTree(tree->pr,pdata,fc); } else //link { if(tree->pl==NULL) {tree->pl=malloc(sizeof(tleaf));*tree->pl=EmptyLeaf;} addToTree(tree->pl,pdata,fc); } } }
void RuleTreeAdapter::fetcherAllRulesReceived() { mRules = mFetcher->getRules(); std::string rootRule = mFetcher->getRootRule(); delete mFetcher; mFetcher = nullptr; const auto& root = getRule(rootRule); if (root.isValid()) { addToTree(root, nullptr, true); } EventAllRulesReceived.emit(); }
int main() { int i; ptree=malloc(sizeof(tleaf)); *ptree=EmptyLeaf; for (i=0;i<sizeof(vdata)/sizeof(char*);i++) { addToTree(ptree,vdata[i],cmpstr); } dispTree(ptree,printStr); while(1) { fgets(buf,128,stdin); buf[strlen(buf)-1]=0; p=malloc(strlen(buf)+1); strcpy(p,buf); addToTree(ptree,p,cmpstr); dispTree(ptree,printStr); puts("-------------------"); } return 0; }
void StatsView::addStatsDirectory(std::string name, std::string text, QTreeWidgetItem *parent) { QTreeWidget *statsTree = this->findChild<QTreeWidget *>("statsTree"); StatsContainer *boostBase = GenStatsReader::getContainer(allItemStats, name); std::vector<StatsContainer *> boosts = GenStatsReader::getSubclassContainers(allItemStats, boostBase); DataContainerTreeItem *boostFolder; if (parent != 0) { boostFolder = new DataContainerTreeItem(parent); } else { boostFolder = new DataContainerTreeItem(statsTree); } boostFolder->setText(0, text.c_str()); addToTree(boosts, boostFolder); }
void StatsView::addBoostDirectory(std::string modifierType, std::string text) { QTreeWidget *statsTree = this->findChild<QTreeWidget *>("statsTree"); std::vector<StatsContainer *> boosts = GenStatsReader::getContainersByContainerType(allItemStats, "deltamod"); for (int i=0; i<boosts.size(); ++i) { StatsContainer *boost = boosts[i]; if (boost->getData("ModifierType") != modifierType) { boosts.erase(boosts.begin() + i); --i; continue; } } DataContainerTreeItem *boostFolder = new DataContainerTreeItem(statsTree); boostFolder->setText(0, text.c_str()); addToTree(boosts, boostFolder); }
//------------------------------------------------------------- // Model::addToTree() add a tree node //------------------------------------------------------------- TreeNode *Model::addToTree(TreeNode *parent, TreeNode *child, NodeIF *node) { TreeNode *root=new TreeNode(node); if(parent) parent->addChild(root); LinkedList<NodeIF*>children; if(node->getChildren(children)){ NodeIF *child; children.ss(); while((child=children++)>0) addToTree(root,child); } return root; }
void ProfilerUIImpl::refreshAllocations() { m_allocation_root->clear(m_allocator); LUMIX_DELETE(m_allocator, m_allocation_root); m_allocation_root = LUMIX_NEW(m_allocator, AllocationStackNode)(m_allocator); m_allocation_root->m_stack_node = nullptr; m_main_allocator.lock(); auto* current_info = m_main_allocator.getFirstAllocationInfo(); while (current_info) { addToTree(current_info); current_info = current_info->m_next; } m_main_allocator.unlock(); }
void StatsView::addStatsDirectoryByPrefix(std::string prefix, std::string text, QTreeWidgetItem *parent) { QTreeWidget *statsTree = this->findChild<QTreeWidget *>("statsTree"); std::vector<StatsContainer *> toAdd; for (int i=0; i<allItemStats.size(); ++i) { StatsContainer *container = allItemStats[i]; if (boost::starts_with(container->getArg(0), prefix) && container->getUsing() == 0) { toAdd.push_back(container); } } DataContainerTreeItem *boostFolder; if (parent != 0) { boostFolder = new DataContainerTreeItem(parent); } else { boostFolder = new DataContainerTreeItem(statsTree); } boostFolder->setText(0, text.c_str()); addToTree(toAdd, boostFolder); }
int main(int argc, const char * argv[]) { NODE * root = createNode(5); root = addToTree(root, 3); root = addToTree(root, 6); root = addToTree(root, 1); root = addToTree(root, 8); root = addToTree(root, 2); root = addToTree(root, 7); root = addToTree(root, 4); inorderTraversal(root); printf("\n"); preorderTraversal(root); printf("\n"); postorderTraversal(root); return 0; }
void RuleTreeAdapter::addToTree(const Root& rule, CEGUI::TreeItem* parent, bool addRecursive) { CEGUI::TreeItem* item = ColouredTreeItem::create(rule->getId()); item->toggleIsOpen(); if (!parent) { mTreeWidget.addItem(item); } else { parent->addItem(item); } if (addRecursive) { std::list<std::string> children; extractChildren(rule, children); for (auto& child : children) { const auto& childData = getRule(child); if (childData.isValid()) { addToTree(childData, item, addRecursive); } } } }
TreeNode *sortedArrayToBST(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function return addToTree(num, 0, num.size() - 1); }
//------------------------------------------------------------- // Model::addToTree() add a tree node //------------------------------------------------------------- TreeNode *Model::addToTree(TreeNode *parent, NodeIF *node) { return addToTree(parent,0,node); }
//------------------------------------------------------------- // Model::insertInTree() add a tree node //------------------------------------------------------------- TreeNode *Model::insertInTree(TreeNode *parent, NodeIF *node) { return addToTree(parent, node); }
int main() { char programName[] = "trees"; int data; int chosenOperation; printf ( "Welcome to %s\n", programName ); BinaryTree *theTree = NULL; initializeTree ( theTree ); do { printf ( "%s", mainMenuString ); chosenOperation = fetchINT ( stdin ); switch ( chosenOperation ) { case AddToTree: if ( isTreeFull ( *theTree ) ) { printf ( "sorry the queue is full\n" ); } else { printf ( "Enter a number: " ); data = fetchINT ( stdin ); addToTree ( theTree, data ); } break; case 2: if ( isTreeEmpty ( *theTree ) ) { printf ( "sorry the queue is empty\n" ); } else { printf ( "The number is %d\n", takeFromTree ( theTree, data ) ); } break; case 3: if ( isTreeEmpty ( *theTree ) ) { printf ( "sorry the queue is empty\n" ); } else { printTree ( theTree ); } break; case 4: printf ( "bye\n" ); break; default: printf ( "Oops Wrong input\n" ); chosenOperation = AddToTree; } } while ( ! ( ( chosenOperation < AddToTree ) || ( chosenOperation >= Quit ) ) ); emptyTree ( theTree ); free ( theTree ); return EXIT_SUCCESS; }
void wxSpinTreeCtrl::addNode(const char *id) { spin::ReferencedNode *n = spin::spinApp::Instance().sceneManager->getNode(id); if (n) addToTree(n); }