//已知对应二叉树的先序遍历及中序求后序列 int initTree(BTree root,char *front,char *middle,int num) { int i=0; root->data=front[0]; if(num==0) return 0; //找到根节点在middle的位置,用于分开分治法求解 for (i = 0; i < num; ++i) { if(middle[i]==num) break; } //如果存在左孩子 if(i>0) { root->lchild=new struct BTreeNode(); //此时左子树的根一定为紧接的第一个元素,为i个左子树 initTree(root->lchild,front+1,middle,i); } //如果存在右孩子 if(i<num-1) { root->rchild=new struct BTreeNode(); //此时右子树的中序一定为middle+i,右子树个数为num-1-i //因为对应左子树肯定为i个,所以右子树的根为front + i + 1(根)个 initTree(root->rchild,front+i+1,middle+i,num-1-i); } return 1; }
void main() { Graph graph; Graph tree; createdGraph(&graph); initTree(&tree); printf("普里姆算法树中顶点加入的顺序:\n"); prim(&graph,&tree); printf("\n"); initTree(&tree); printf("克鲁斯卡尔算法树中边加入的顺序:\n"); kruskal(&graph,&tree); printf("\n"); }
/** * Initializes the Binary Tree from a Database File. * @param fp the file being read. * @return the current node being worked on. **/ node *initTree(FILE *fp) { char *tmpAnimal = (char *) malloc(sizeof(char) * LONGEST_ANIMAL_NAME); char *tmpQuestion = (char *) malloc(sizeof(char) * BUF_SIZE); char *strNdx, tmpChar; node *tmpNode = (node *) malloc(sizeof(node)); switch (fgetc(fp)) { case 'A': strNdx = tmpAnimal; while ((tmpChar = fgetc(fp)) != '\n' && tmpChar != EOF) { *strNdx++ = tmpChar; } *strNdx = '\0'; tmpNode->animal = (char *) strdup(tmpAnimal); tmpNode->question = NULL; break; case 'Q': strNdx = tmpQuestion; while ((tmpChar = fgetc(fp)) != '\n' && tmpChar != EOF) { *strNdx++ = tmpChar; } *strNdx = '\0'; tmpNode->question = (char *) strdup(tmpQuestion); tmpNode->animal = NULL; break; case '\n': case EOF: return NULL; break; default: fprintf(stderr, "reached default case\n"); break; } tmpNode->left = initTree(fp); tmpNode->right = initTree(fp); free(tmpAnimal); free(tmpQuestion); return tmpNode; }
/** * Call init() after the constructor, either in the end of your own constructor or from outside. */ void CModuleChooserDialog::init() { //Set the flag to destroy when closed - otherwise eats memory setAttribute(Qt::WA_DeleteOnClose); setWindowTitle(m_title); initView(); initTree(); }
void let_257(){ Solution sol; initTree(); vector<string> result=sol.binaryTreePaths(tree); FOR(i,result.size()) cout<<result[i]<<","; cout<<endl; }
int main() { struct searchTree * t = initTree(); struct searchTree *root = t = treeInsert(t , 3); treeInsert(t , 8); struct searchTree * x = treeInsert(t , 4); struct searchTree * y = treeInsert(t , 14); treeInsert(t , 9); treeInsert(t , 2); treeInsert(t , 0); treeWalk(t); treeDelete(x); treeWalk(root); treeDelete(y); treeWalk(root); printf("\nmax = %d min = %d \n", treeMax(t) -> k, treeMin(t) -> k); if(treeSearch(t, 3) != NULL) printf("found\n"); printf("%d\n",t -> k); t = treeSuccessor(t); printf("%d\n",t -> k); t = treePredecessor(t); printf("%d\n",t -> k); }
bool packRectanglesSize(rectangle2DList_struct* l, short* w, short* h) { tree_struct t; bool tr=true; bool rr=true; *w=32; *h=32; while(rr && *w<=512 && *h<=256) { if(l->surface<=(*w)*(*h)) { NOGBA("doing : %d %d",(*w),(*h)); initTree(&t,*w,*h); bool r=insertRectangles(&t,l); freeTree(&t); if(r){rr=false;break;} unrotateRectangles(l); } if(tr)(*w)*=2; else (*h)*=2; tr^=1; } NOGBA("surface : %d <= %d",l->surface,(*w)*(*h)); return !rr; }
void packRectangles(rectangle2DList_struct* l, short w, short h) { tree_struct t; initTree(&t,w,h); insertRectangles(&t,l); freeTree(&t); }
void pcl_ros::BoundaryEstimation::computePublish (const PointCloudInConstPtr &cloud, const PointCloudNConstPtr &normals, const PointCloudInConstPtr &surface, const IndicesPtr &indices) { // Set the parameters impl_.setKSearch (k_); impl_.setRadiusSearch (search_radius_); // Initialize the spatial locator initTree (spatial_locator_type_, tree_, k_); impl_.setSearchMethod (tree_); // Set the inputs impl_.setInputCloud (cloud); impl_.setIndices (indices); impl_.setSearchSurface (surface); impl_.setInputNormals (normals); // Estimate the feature PointCloudOut output; impl_.compute (output); // Enforce that the TF frame and the timestamp are copied output.header = cloud->header; pub_output_.publish (output.makeShared ()); }
/** * Initializes the binary tree if Database file is not present. * Also calls initTree if a Database file is present. * @param choice an Enum representing the presence of a database file. * @param fp the file being read. * @return the current node being worked on. **/ node *getInput(type_t choice, FILE *fp) { node *tmpNode = (node *) malloc(sizeof(node)); char *tmpAnimal = (char *) malloc(sizeof(char) * LONGEST_ANIMAL_NAME); char *strNdx, tmpChar; switch (choice) { case NO_FILE: fprintf(stdout, "\nWhat is it (with article)? "); strNdx = tmpAnimal; while ((tmpChar = getchar()) != '\n') { *strNdx++ = tmpChar; } *strNdx = '\0'; tmpNode->animal = (char *) strdup(tmpAnimal); tmpNode->question = NULL; break; case DB_FILE: tmpNode = initTree (fp); break; } free(tmpAnimal); return tmpNode; }
Tree* HuffmanTree(char *collection, int *frequency, int num) { int *used = (int *)malloc(sizeof(int)* num * 2); Node *arr[100]; int i; int nodeNumber = num; Tree *tree = initTree(); for (i = 0; i < num * 2; i++) { used[i] = 0; arr[i] = createNode(frequency[i]); arr[i]->name = collection[i]; } Node *node; for (i = 0; i < num - 1; i++) { node = (Node *)malloc(sizeof(Node)); Node *left = min(arr, used, nodeNumber); Node *right = min(arr, used, nodeNumber); node->left = left; node->right = right; node->value = left->value + right->value; arr[nodeNumber] = node; nodeNumber++; } tree->root = node; free(used); return tree; }
static BinaryTree *myInitTree() { BinaryTree *root; ElementType arr[TREE_NODES] = {7,5,11,4,6,2,12,13}; int len = 8; root = initTree(arr, len); return root; }
SegTreeNode* initTree(int* nums, int start, int end) { if (start > end) { return NULL; } SegTreeNode* node = malloc(sizeof(SegTreeNode)); node->start = start; node->end = end; if (start == end) { node->sum = nums[start]; } else { int mid = start + (end - start) / 2; node->left = initTree(nums, start, mid); node->right = initTree(nums, mid + 1, end); node->sum = node->left->sum + node->right->sum; } return node; }
int main(int argc, char** argv) { GtkWidget* w; GtkWidget* box; GtkWidget* p; GtkWidget* f; GtkWidget* menu; GtkWidget* item; GtkWidget* sub; gtk_init(&argc, &argv); // creates the window w = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(w), "ev.view"); gtk_window_resize(GTK_WINDOW(w), 640, 480); box = gtk_vbox_new(FALSE, 0); // set menues menu = gtk_menu_bar_new(); gtk_box_pack_start(GTK_BOX(box), menu, FALSE, TRUE, 0); // File item = gtk_menu_item_new_with_label("File"); sub = gtk_menu_new(); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub); gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); newMenuItem(sub, "Open data...", G_CALLBACK(menuOpenData), w); itemRecons = newMenuItem(sub, "Open reconstruction...", G_CALLBACK(menuOpenRec), w); gtk_widget_set_sensitive(GTK_WIDGET(itemRecons), FALSE); newMenuItem(sub, "Quit", G_CALLBACK(menuQuit), NULL); // Map item = gtk_menu_item_new_with_label("Map"); sub = gtk_menu_new(); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub); gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); newMenuItem(sub, "Open raster...", G_CALLBACK(menuOpenRaster), NULL); // Layout p = gtk_hpaned_new(); gtk_widget_set_size_request(p, 100, -1); f = gtk_frame_new(NULL); gtk_paned_pack1(GTK_PANED(p), f, TRUE, FALSE); gtk_widget_set_size_request(f, 50, -1); initTree(f); f = gtk_frame_new(NULL); gtk_paned_pack2(GTK_PANED(p), f, TRUE, FALSE); gtk_widget_set_size_request(f, 50, -1); initMap(f); gtk_box_pack_start(GTK_BOX(box), p, TRUE, TRUE, 0); gtk_container_add(GTK_CONTAINER(w), box); gtk_widget_show_all(w); gtk_widget_add_events(w, GDK_KEY_PRESS_MASK); g_signal_connect(G_OBJECT(w), "key_press_event", G_CALLBACK(keyPress), w); gtk_main(); return 0; }
Tree* initRBTree() { Tree *tree = initTree(); tree->nil = (Node *)malloc(sizeof(Node)); tree->nil->color = BLACK; tree->nil->left = NULL; tree->nil->right = NULL; return tree; }
void Planner3D::testControls() { initTree(m_rrttree, m_npose); Matrix<3,1> u; bool flip = false; Matrix<3> pt; pt[0] = 5; pt[1] = 0; pt[2] = -5; double d = dist(m_npose, pt); std::cout << "dist: " << d << std::endl; localPlanner(m_rrttree, 0, pt, u, flip, false); std::cout << "u: " << u[0] << " " << u[1] << " " << u[2] << std::endl; std::list<Control> clist; u[0] = 3*M_PI*0.5; u[1] = -M_PI*0.1; u[2] = 5; Matrix<4,4> nT = stepTDiscrete(m_npose, u, flip, 1.0); std::cout << "Projected transform matrix:\n" << nT << std::endl; Control c; c.m_v = 0.0; c.m_w = -M_PI*0.1; c.m_dt = 1.0; clist.push_back(c); c.m_v = 3.0*M_PI*0.5; c.m_w = 0.0; c.m_dt = 1.0; clist.push_back(c); m_npose = executeControls(m_npose, clist); /* getControls(u, flip, clist); //m_npose = executeControlsWithNoise(m_npose, clist); m_npose = executeControls(m_npose, clist); for(std::list<Control>::iterator cit = clist.begin(); cit != clist.end(); ++cit) { std::cout << "v: " << cit->m_v << " w: " << cit->m_w << " dt: " << cit->m_dt << std::endl; } */ std::cout << "New transform matrix:\n" << m_npose << std::endl; updateNeedleTipPose(m_npose); }
RBTree *import_database() { ProgressPtr process_file; FilePathList *list; char path[80]; RBTree *tree; tree = malloc(sizeof(RBTree)); list = malloc(sizeof(FilePathList)); // For each file indicated by the file specified by argument, it will parse // its content, searching for words, and it will store them in the global // structure. process_file = ^(FilePathItem *item, int total_files){ HashList hl; int result; printf("Reading file [%s] with id [%d/%d]\n", item->path, item->id + 1, total_files); hl_initialize(&hl, HASH_LIST_SIZE); result = create_local_structure(&hl, item->path); if ( result == 0 ) { update_global_structure(tree, &hl, item->id); hl_free(&hl); } }; cfg_init(list); printf("Mode multifil\n"); printf("Nom del fitxer de configuració: "); scanf("%s", path); flush(); cfg_import_config(path, list); //cfg_print(list); if ( list->size == 0 ) { cfg_free(list); free(tree); return NULL; } initTree(tree, list->size); cfg_mt_iterate(list, process_file, 32); cfg_free(list); return tree; }
int main (int argc, const char * argv[]) { int height; printf("\n\tEnter height of the tree: "); scanf(" %d", &height); TREE_p_t tree = initTree(height); printf("\n\tEnter the tree (characters) '#' for null\n: "); inputTree(tree); char choice = '\0'; do { printf("\n\n\t1. Input tree (overwrite)\n\t2. Inorder transversal\n\t3. Preorder transversal\n\t4. Postorder transversal\n\tChoice: "); scanf(" %c", &choice); if (choice == '1') { printf("\n\tEnter height of the tree: "); scanf(" %d", &height); tree = initTree(height); inputTree(tree); } else if (choice == '2') { printf("\n\n\t Inorder: "); inorderTransversal(tree, 1); } else if (choice == '3') { printf("\n\n\t Preorder: "); preorderTransversal(tree, 1); } else if (choice == '4') { printf("\n\n\tPostorder: "); postorderTransversal(tree, 1); } } while (choice >= '1' && choice <= '4'); printf("\n\n"); }
CBookmarkIndex::CBookmarkIndex(QWidget *parent) : QTreeWidget(parent), m_magTimer(this), m_previousEventItem(0) { setMouseTracking(true); m_magTimer.setSingleShot(true); m_magTimer.setInterval(CBTConfig::get(CBTConfig::magDelay)); setContextMenuPolicy(Qt::CustomContextMenu); initView(); initConnections(); initTree(); }
void initDialogue(Tree *pTree) { initTree(pTree); insertElement(2.0, pTree); insertElement(1.0, pTree); insertElement(3.0, pTree); insertElement(0.5, pTree); insertElement(1.5, pTree); insertElement(2.5, pTree); insertElement(3.5, pTree); }
CBookshelfIndex::CBookshelfIndex(QWidget *parent) : QTreeWidget(parent), m_searchDialog(0), m_autoOpenFolder(0), m_autoOpenTimer(this) { m_grouping = (BTModuleTreeItem::Grouping)CBTConfig::get(CBTConfig::bookshelfGrouping); m_showHidden = CBTConfig::get(CBTConfig::bookshelfShowHidden); setContextMenuPolicy(Qt::CustomContextMenu); initView(); initConnections(); initTree(); }
/* parses curJSON and draws the tree. marks the error location in case of a parsing error */ void JSONDialog::drawTreeSaxParse() { HTREEITEM tree_root; tree_root = initTree(this->getHSelf()); if (strlen(curJSON) == 0) { insertToTree(this->getHSelf(), tree_root, "Error:Please select a JSON String."); TreeView_Expand(GetDlgItem(this->getHSelf(), IDC_TREE), tree_root, TVE_EXPAND); return; } populateTreeUsingSax(this->getHSelf(), tree_root, curJSON); TreeView_Expand(GetDlgItem(this->getHSelf(), IDC_TREE), tree_root, TVE_EXPAND); }
int main() { BTree* head = NULL; initTree(&head); Node data; BTree* item = addLeftChild(head, &data); item = addRightChild(head, &data); item = addRightChild(item, &data); item = addRightChild(head, &data); printTreeMid(head); deleteTree(head); return 0; }
int main(){ cin.tie(0); ios_base::sync_with_stdio(0); lli N,Q,o,l,r,k; cin >> N >> Q; tree *root; root = NULL; initTree(&root,0,N); for(lli i = 0 ; i < Q ; i++) { cin >> o; if( o == 1 ){ cin >> l >> r >> k; operar(&root,l-1,r,k,0); } else if( o == 2 ){
int main(int argc, char *argv[]) { struct tree at; int op,lr,parent,child; initTree(&at); while(scanf("%d",&op)==1 && op!=0){ switch(op){ case 1://insert node scanf("%d%d%d",&parent,&lr,&child); printf("insert node(p=%d lr=%d c=%d):",parent,lr,child); if(insertTreeNode(&at,parent,lr,child)){ printf("success\n"); }else{ printf("fail\n"); } break; case 2://breadfirst traversal printf("breadthfirst:"); breadthFirstTraversal(&at); printf("\n"); break; case 3://pre order traversal printf("depth preorder:"); depthFirstTraversal(&at,PREORDER); printf("\n"); break; case 4://in order traversal printf("depth inorder:"); depthFirstTraversal(&at,INORDER); printf("\n"); break; case 5://post order traversal printf("depth postorder:"); depthFirstTraversal(&at,POSTORDER); printf("\n"); break; default: printf("error operator\n"); break; } } releaseTree(&at); return 0; }
HybridLB::HybridLB(const CkLBOptions &opt): CBase_HybridLB(opt) { #if CMK_LBDB_ON lbname = (char *)"HybridLB"; // defines topology in base class // tree = new ThreeLevelTree; // decide which load balancer to call // IMPORTANT: currently, the greedy LB must allow objects that // are not from existing processors. refine = (CentralLB *)AllocateRefineLB(); // greedy = (CentralLB *)AllocateMetisLB(); greedy = (CentralLB *)AllocateGreedyLB(); initTree(); #endif }
void CSVWholeview::refresh() { string strUserID = GetWebUserID(); HitLog LogItem; LogItem.sUserName = strUserID; LogItem.sHitPro = "wholetree"; LogItem.sHitFunc = "refresh"; LogItem.sDesc = strRefresh; DWORD dcalBegin=GetTickCount(); InsertHitRecord(LogItem.sUserName, LogItem.sHitPro, LogItem.sHitFunc, LogItem.sDesc, 0, 0); svutil::TTime ttime = svutil::TTime::GetCurrentTimeEx(); string curTime = ttime.Format(); if(m_pTime) m_pTime->setText(m_szRefreshTime + curTime); char szQuery[4096]={0}; int nSize = 4095; m_nShowType = -1; GetEnvironmentVariable( "QUERY_STRING", szQuery,nSize); char *pPos = strchr(szQuery, '='); if(pPos) { pPos ++; m_nShowType = atoi(pPos); } string szUserID = GetWebUserID(); if(szUserID != m_szUserID) { m_szUserID = szUserID; if(m_pSVUser) m_pSVUser->setUserID(m_szUserID); else m_pSVUser = new CUser(m_szUserID); } clearTree(); initTree(); DWORD dcalEnd1=GetTickCount(); InsertHitRecord(LogItem.sUserName, LogItem.sHitPro, LogItem.sHitFunc, LogItem.sDesc, 1, dcalEnd1 - dcalBegin); }
RBTree * loadTree(char *filename){ RBTree *tree = malloc(sizeof(RBTree)); initTree(tree); FILE *fp; fp = fopen(filename, "r"); if(!fp){ deleteTree(tree); free(tree); return NULL; } int sizeDb, numNodes = NULL; fread( &(sizeDb), sizeof(int), 1, fp ); fread( &(numNodes), sizeof(int), 1, fp ); if(numNodes == 0){ fclose(fp); deleteTree(tree); free(tree); return NULL; } int i; for(i=0; i< numNodes; i++){ RBData *data = malloc(sizeof(RBData)); int length = NULL; fread(&(length), sizeof(int), 1, fp); data->primary_key = malloc(sizeof(char) * (length+1) ); fread(data->primary_key, sizeof(char), length, fp); data->primary_key[length] = '\0'; fread(&(data->numFiles), sizeof(int), 1, fp); data->numTimes = malloc(sizeof(int) * sizeDb); fread(data->numTimes, sizeof(int), sizeDb, fp); insertNode(tree, data); } fclose(fp); return tree; }
FunctionManager::FunctionManager(QWidget* parent, Qt::WindowFlags flags) : QWidget(parent, flags) { new QVBoxLayout(this); initActions(); initMenu(); initToolbar(); initTree(); updateActionStatus(); /* Listen to document changes */ connect(_app, SIGNAL(documentChanged(Doc*)), this, SLOT(slotDocumentChanged(Doc*))); /* Use the initial document */ slotDocumentChanged(_app->doc()); m_tree->sortItems(KColumnName, Qt::AscendingOrder); }
/** * @brief Default constructor. */ NamdHybridLB::NamdHybridLB(): HybridBaseLB(CkLBOptions(-1)) { // setting the name lbname = (char *)"NamdHybridLB"; delete tree; // delete the tree built from the base class if (CkNumPes() <= 128) { tree = new TwoLevelTree; // similar to centralized load balancing } else { #if CHARM_VERSION > 60304 const SimParameters* simParams = Node::Object()->simParameters; tree = new ThreeLevelTree(simParams->hybridGroupSize); initTree(); #else tree = new ThreeLevelTree(); #endif // can only do shrink strategy on levels > 1 statsStrategy = SHRINK_NULL; } // initializing thisProxy thisProxy = CProxy_NamdHybridLB(thisgroup); // initializing the central LB centralLB = AllocateNamdCentLB(); // initializing the dummy LB dummyLB = AllocateNamdDummyLB(); // assigning initial values to variables from_procs = NULL; computeArray = NULL; patchArray = NULL; processorArray = NULL; updateCount = 0; updateFlag = false; collectFlag = false; }