// Solution to part two of the lab void labPartTwo(void) { treeLink tree = newTree(); // Any arrangement with the 2 as the last node works to produce // a perfectly balanced outcome. tree = splayInsert (tree,3); tree = splayInsert (tree,1); tree = splayInsert (tree,2); printf("Balanced tree with nodes 1..3:\n"); printTree(tree); tree = newTree(); tree = splayInsert (tree,3); tree = splayInsert (tree,2); tree = splayInsert (tree,1); tree = splayInsert (tree,6); tree = splayInsert (tree,5); tree = splayInsert (tree,7); tree = splayInsert (tree,4); printf("Balanced tree with nodes 1..7:\n"); printTree(tree); }
Tree * insert (Tree * tree, int node) { if (tree -> nil) { tree -> nil = False; tree -> node = node; tree -> left = newTree(); tree -> right = newTree(); return tree; } else { if (node == tree -> node) { return tree; } if (node < tree -> node) { tree -> left = insert(tree -> left, node); } else { tree -> right = insert(tree -> right, node); } // showPreorder(tree); // tree = balance(tree); // printf("- -\n"); // showPreorder(tree); // printf("---\n"); return balance(tree); // return tree; } }
TreeNode *newTree(vector<int> &preorder, vector<int> &inorder, int inst, int inend, int prest){ if(inst > inend) return NULL; TreeNode *node = new TreeNode(preorder[prest]); int pos = -1; for(int i = 0; i <= inorder.size(); i++){ if(inorder[i] == preorder[prest]){ pos = i; break; } } node -> left = newTree(preorder, inorder, inst, pos -1, prest + 1); // the second root is prest + 1 node -> right = newTree(preorder, inorder, pos + 1, inend, prest + 1 + pos - inst); // mid - inst is the size of left side return node; }
const BoostInfoTree& BoostInfoTree::createSubtree(const string& treeName, const string& value) { ptr_lib::shared_ptr<BoostInfoTree> newTree(new BoostInfoTree(value, this)); addSubtree(treeName, newTree); return *newTree; }
int main() { Tree tree = newTree(); // Binary tree char szInputBuffer[MAX_LINE_SIZE + 1]; // input text line // Variables for Quote QuoteSelection quoteSelection = newQuoteSelection(); // Read command lines until EOF while (fgets(szInputBuffer, MAX_LINE_SIZE, stdin) != NULL) { printf("%s", szInputBuffer); // If the line is just a comment or empty, ignore it if (szInputBuffer[0] == '*' || szInputBuffer[0] == '\0') continue; // Command is a comment so skip it //read in file processCommand(tree, quoteSelection, szInputBuffer); } //Free the tree, quote selection and stdin freeTree(tree); free(quoteSelection); fclose(stdin); printf("\n"); return 0; }
void addRecordToTree(BPlusTree *tree, timeStampT time, double value){ Node *leaf; //the tree does not exist yet --> create tree if (tree->root == NULL){ newTree(tree, time, value); return; } //find the leaf to insert the record leaf = findLeaf(tree, value); //if duplicate -> insert to leaf as Doubly linked list value if(isDuplicateKey(leaf, time, value)){ addDuplicateToDoublyLinkedList(leaf, time, value); } //new key else if(leaf->numOfKeys < tree->nodeSize){ insertRecordIntoLeaf(tree, leaf, time, value); } else{ //leaf must be split splitAndInsertIntoLeaves(tree, leaf, time, value); } }
int main(int argc, char* argv[]) { //--create new TTree gSystem->Load("lib/DynamicTTreeDict.so"); TFile* file = TFile::Open("test.root", "RECREATE"); DynamicTTree newTree("test", "DynamicTTree"); newTree.i = 5; newTree.f = 5.5; for(int j=0; j<newTree.i; ++j) newTree.vi[j] = j; *newTree.s = std::string("string"); (*newTree.m)[*newTree.s] = std::make_pair(3.5, 8); newTree.GetTTreePtr()->Fill(); newTree.GetTTreePtr()->Write(); file->Close(); //---read back the same TTree TFile* inFile = TFile::Open("test.root", "READ"); TTree* test = (TTree*)inFile->Get("test"); DynamicTTree reader(test); while(reader.NextEntry()) { std::cout << reader.i << std::endl; std::cout << reader.f << std::endl; std::cout << *reader.s << std::endl; for(int j=0; j<reader.i; ++j) std::cout << reader.vi[j] << std::endl; for(auto& ele : *reader.m) std::cout << ele.first << ": " << ele.second.first << " " << ele.second.second << std::endl; } return 0; }
signed char TreeKeyIdx::create(const char *ipath) { char *path = 0; char *buf = new char [ strlen (ipath) + 20 ]; FileDesc *fd, *fd2; stdstr(&path, ipath); if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\')) path[strlen(path)-1] = 0; sprintf(buf, "%s.dat", path); FileMgr::removeFile(buf); fd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE); fd->getFd(); FileMgr::getSystemFileMgr()->close(fd); sprintf(buf, "%s.idx", path); FileMgr::removeFile(buf); fd2 = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE); fd2->getFd(); FileMgr::getSystemFileMgr()->close(fd2); TreeKeyIdx newTree(path); TreeKeyIdx::TreeNode root; stdstr(&(root.name), ""); newTree.saveTreeNode(&root); delete [] path; return 0; }
tree * getFactor(token ** tokenPtr,char *buffer) { assert((*tokenPtr) != NULL); if ((*tokenPtr)->type==LPAREN) { (*tokenPtr)=nextToken((*tokenPtr),buffer,true); if ((*tokenPtr)==NULL) { printf("Syntax error... incomplete expression, expected an expression after open parens\n"); exit(1); } tree * result=getExpression(tokenPtr,buffer); if ((*tokenPtr)->type != RPAREN) { tokenError("Syntax error... expected closing parenthesis after expression",buffer,(*tokenPtr)); exit(1); } (*tokenPtr)=nextToken((*tokenPtr),buffer,true); return result; } if ((*tokenPtr)->type==INTEGER || (*tokenPtr)->type==VARIABLE ) { tree * result=newTree(); result->action=(*tokenPtr); (*tokenPtr)=nextToken((*tokenPtr),buffer,false); return result; } printf("Syntax error... expected a factor\n"); exit(1); }
void RedBlackTree<T>::RBDeleteFixUp(Node<T>* x, Node<T>* xparent, bool xisleftchild) { RedBlackTree<T> newTree(); InsertItemIntoTree(root, &newTree); RemoveAll(); this = newTree; newTree.RemoveAll(); }
int main(int argc, char* argv[]) { if (argc<2) return 0; // FILE *f = fopen(argv[1],"r"); // if (f==(void*) 0) // return 0; struct Tree* t; newTree(&t); char* x = argv[1]; char* tmp; tmp = strtok(x," "); int res = 0; while (tmp != (void*)0) { if (tmp[0]== '"') continue; res = res + insertNode(&t, atoi(tmp)); tmp = strtok((void*)0, " "); } printf ("%d ",res); printf ("%d ",t->size); printTree(t->root); return 0; }
tree * getAssignment(token **tokenPtr,char *buffer) { assert((*tokenPtr)!=NULL); if ((*tokenPtr)->type!=SET) return NULL; tree * result=newTree(); result->action=(*tokenPtr); (*tokenPtr)=nextToken((*tokenPtr),buffer,false); if ((*tokenPtr)->type!=VARIABLE) { printf("Syntax error... expected variable after set\n"); exit(1); } tree * variable=newTree(); variable->action=(*tokenPtr); result->left=variable; (*tokenPtr)=nextToken((*tokenPtr),buffer,false); result->right=getExpression(tokenPtr,buffer); return result; }
std::shared_ptr< GroupTree > GroupTree::deepCopy() const { std::shared_ptr< GroupTree > newTree(new GroupTree()); std::shared_ptr< GroupTreeNode > currentOriginNode = m_root; std::shared_ptr< GroupTreeNode > currentNewNode = newTree->getNode("FIELD"); deepCopy(currentOriginNode, currentNewNode); return newTree; }
treeADT buildTestTreeReversed(int TEST_SIZE){ treeADT tree; int i; tree=newTree(); for(i=TEST_SIZE-1;i>=0;i--) treeInsert(tree,i); return (tree); }
GroupTreePtr GroupTree::deepCopy() const { GroupTreePtr newTree(new GroupTree()); GroupTreeNodePtr currentOriginNode = m_root; GroupTreeNodePtr currentNewNode = newTree->getNode("FIELD"); deepCopy(currentOriginNode, currentNewNode); return newTree; }
void buildTree(void){ treeADT tree=newTree(); string userOP; int userInt,element; printf("\n**** Tree builder ****\n\n"); while(TRUE){ printf("(i)nsert, (d)elete, (f)ind, (p)rint, (q)uit\n"); printf("Your choice: "); userOP=GetLine(); if(StringEqual(userOP,"i")){ printf("To end insertion enter: -1.\n"); while(TRUE){ printf("TreeInsert: "); userInt=GetInteger(); if(userInt==-1) break; treeInsert(tree,userInt); } } if(StringEqual(userOP,"d")){ printf("To end deletion enter: -1.\n"); while(TRUE){ printf("TreeDelete: "); userInt=GetInteger(); if(userInt==-1) break; treeDelete(tree,userInt); } } if(StringEqual(userOP,"f")){ printf("FindNode: "); userInt=GetInteger(); element=findNode(tree,userInt); if(element!=NOT_FOUND) printf("\nFound %d in your tree! :-)\n",userInt); else printf("\nDid not find %d in your tree. :-(\n",userInt); } if(StringEqual(userOP,"p")){ printf("\nThis is your current tree:\n\n"); displayTreeStructure(tree); printf("Preorder: "); printTree(tree,preOrder); printf("Inorder: "); printTree(tree,inOrder); printf("Postorder: "); printTree(tree,postOrder); printf("\n"); printf("Height: %d\n",treeHeight(tree)); printf("Black-height: %d\n",blackHeight(tree)); } if(StringEqual(userOP,"q")) break; printf("\n"); } freeTree(tree); }
treeADT buildTestTreeOrdered(int TEST_SIZE){ treeADT tree; int i; tree=newTree(); for(i=0;i<TEST_SIZE;i++) treeInsert(tree,i); return (tree); }
tree *readNewickString (char *str, int numLeaves) { tree *T; node *centerNode; int i = 0; int j = 0; int inputLength; int uCount = 0; int parCount = 0; char rootLabel[MAX_LABEL_LENGTH]; nodeCount = edgeCount = 0; T = newTree(); if ('(' != str[0]) { Rprintf("Error reading generated tree - does not start with '('.\n"); exit(0); } inputLength = strlen (str)+1; for(i = 0; i < inputLength; i++) { if ('(' == str[i]) parCount++; else if (')' == str[i]) parCount--; if (parCount > 0) ; else if (0 == parCount) { i++; /* if(';' == str[i]) sprintf(rootLabel,"URoot"); else {*/ while(';' != str[i]) if (!(whiteSpace (str[i++]))) rootLabel[j++] = str[i-1]; /*be careful here */ rootLabel[j] = '\0'; // } i = inputLength; } else if (parCount < 0) { Rprintf("Error reading generated tree. Too many right parentheses.\n"); exit(0); } } centerNode = decodeNewickSubtree (str, T, &uCount); snprintf (centerNode->label, MAX_LABEL_LENGTH, rootLabel); T->root = centerNode; return (T); }
void Graph::loadTree(char *filename) { int x; int y; void *tmpaddr; void *tmpfather; std::map<void *, Entity *> readdr; std::ifstream file(filename, std::ifstream::in); readdr[NULL] = NULL; if (!file.good()) { newTree(); return; } std::cout << "Load tree." << std::endl; file >> x; if (x <= 0) { std::cout << "Fail." << std::endl; newTree(); return; } _entities.resize(x); x = -1; while (++x < _entities.size()) { file >> y; _entities[x].resize(y); y = -1; while (++y < _entities[x].size()) { file >> tmpaddr; file >> tmpfather; _entities[x][y] = readdr[tmpaddr] = new Entity(readdr[tmpfather]); _entities[x][y]->deserialize(file); } } replaceEntity(); }
tree * getWhile(token **tokenPtr,char *buffer) { assert((*tokenPtr)!=NULL); if ((*tokenPtr)->type!=WHILE) return NULL; tree * result=newTree(); result->action=(*tokenPtr); if ((*tokenPtr)!=NULL) (*tokenPtr)=nextToken((*tokenPtr),buffer,false); result->left=getExpression(tokenPtr,buffer); if ((*tokenPtr)==NULL) { printf("Syntax error... expected expression + statement after while\n"); exit(1); } result->right=getStatement(tokenPtr,buffer); return result; }
treeADT buildTestTreeRandom(int TEST_SIZE){ treeADT tree; int i,MIN_INTERVAL,MAX_INTERVAL; if(TEST_SIZE<26){ MIN_INTERVAL=0; MAX_INTERVAL=30; } else { MIN_INTERVAL=-9000000; MAX_INTERVAL=9000000; } tree=newTree(); for(i=0;i<TEST_SIZE;i++) treeInsert(tree,RandomInteger(MIN_INTERVAL,MAX_INTERVAL)); return (tree); }
static Stack negateExpression(Stack s) { Tree t = s->data.tree; Stack ret; if(s->type != 't') return s; if(t->action == SWFACTION_LOGICALNOT) { ret = t->left; /* free(t); */ return ret; } return newTree(s, SWFACTION_LOGICALNOT, NULL); }
std::shared_ptr<FunctionTree> AmpWigner2::SetupTree( ParameterList& sample, std::string suffix) { std::shared_ptr<FunctionTree> newTree(new FunctionTree()); int sampleSize = sample.GetMultiDouble(0)->GetNValues(); //----Strategies needed std::shared_ptr<WignerDStrategy> angdStrat( new WignerDStrategy("AngD"+suffix) ); newTree->createHead("AngD_"+suffix, angdStrat, sampleSize); newTree->createLeaf("spin",_spin.Val(), "AngD_"+suffix); //spin newTree->createLeaf("m", _mu, "AngD_"+suffix); //OutSpin 1 newTree->createLeaf("n", _muPrime, "AngD_"+suffix); //OutSpin 2 newTree->createLeaf("AngD_sample", sample.GetMultiDouble(_varId), "AngD_"+suffix); return newTree; }
tree * getExpression(token ** tokenPtr,char *buffer) { assert((*tokenPtr)!=NULL); tree * term1=getTerm(tokenPtr,buffer); if ((*tokenPtr)==NULL) return term1; if ((*tokenPtr)->type==PLUS || (*tokenPtr)->type==MINUS ) { tree* result=newTree(); result->left=term1; result->action=(*tokenPtr); (*tokenPtr)=nextToken((*tokenPtr),buffer,false); if ((*tokenPtr)==NULL) { printf("Syntax error... incomplete expression, expected another term"); exit(1); } result->right=getExpression(tokenPtr,buffer); return result; } return term1; }
tree * getTerm(token ** tokenPtr,char *buffer) { assert((*tokenPtr) != NULL); tree * fact1=getFactor(tokenPtr,buffer); if ((*tokenPtr)==NULL) return fact1; if ((*tokenPtr)->type==MULTIPLY || (*tokenPtr)->type==DIVIDE) { tree* result=newTree(); result->left=fact1; result->action=(*tokenPtr); (*tokenPtr)=nextToken((*tokenPtr),buffer,false); if ((*tokenPtr)==NULL) { printf("Syntax error... incomplete expression, expected another factor"); exit(1); } result->right=getTerm(tokenPtr,buffer); return result; } return fact1; }
tree * getExpList(token **tokenPtr,char *buffer) { assert((*tokenPtr)!=NULL); tree * exp=getStatement(tokenPtr,buffer); if ((*tokenPtr)==NULL) return exp; // Single expression only if ((*tokenPtr)->type==RCURLY) return exp; // Expression lists closed by rcurly if ((*tokenPtr)->type==EOS) { tree* result=newTree(); result->left=exp; result->action=(*tokenPtr); (*tokenPtr)=nextToken((*tokenPtr),buffer,false); if (((*tokenPtr)==NULL) || (*tokenPtr)->type==LCURLY) { // Semicolon at end of buffer ignored result->left=NULL; freeTree(result); return exp; } result->right=getExpList(tokenPtr,buffer); return result; } printf("Syntax error: unexpected tokens after expression list\n"); exit(1); }
int main(int argc, char* argv[]) { if (argc<2) return 0; FILE *f = fopen(argv[1],"r"); if (f==NULL) return 0; struct Tree* t; newTree(&t); char x[20]; int res = 0; while (fscanf(f,"%s",x)==1) { if (x[0]== '"') continue; res = res + insertNode(&t, atoi(x)); } fclose(f); printf ("%d ",res); printf ("%d ",t->size); printTree(t->root); return 0; }
int main (int argc, char ** argv) { Tree * tree = newTree(); char file[100]; char input[100]; int number; strcpy(file, argv[1]); FILE * fp = fopen(file, "r"); if (fp == NULL) perror("error opening FILE"); while (fgets(input, 100, fp) != NULL) { number = atoi(input); tree = insert(tree, number); } printf("Pre-order:"); showPreorder(tree); printf("\n"); printf("LL:%d\n", ll); printf("LR:%d\n", lr); printf("RL:%d\n", rl); printf("RR:%d\n", rr); fclose(fp); killTree(tree); return 0; }
void buildTree(void){ treeADT tree=newTree(); string userOP; int userInt; printf("\n**** Tree builder ****\n\n"); while(TRUE){ printf("(i)nsert, (p)rint, (q)uit\n"); printf("Your choice: "); userOP=GetLine(); if(StringEqual(userOP,"i")){ printf("To end insertion enter: -1.\n"); while(TRUE){ printf("TreeInsert: "); userInt=GetInteger(); if(userInt==-1) break; treeInsert(tree,userInt); } } if(StringEqual(userOP,"p")){ printf("\nThis is your current tree:\n"); displayTreeStructure(tree); printf("Preorder: "); printTree(tree,preOrder); printf("Inorder: "); printTree(tree,inOrder); printf("Postorder: "); printTree(tree,postOrder); printf("\n"); printf("Height: %d\n\n",treeHeight(tree)); } if(StringEqual(userOP,"q")) break; } freeTree(tree); }
// should be in separate client file for propert ST ADT // implementation // int main () { treeLink tree = newTree(); tree = splayInsert (tree,3); tree = splayInsert (tree,4); tree = splayInsert (tree,5); tree = splayInsert (tree,6); tree = splayInsert (tree,8); tree = splayInsert (tree,9); tree = splayInsert (tree,10); tree = splayInsert (tree,11); printTree(tree); traverseTreePreorder (tree, &printNode); printf ("\n size %d \n", tree->size); tree = splayInsert (tree,1); printTree(tree); traverseTreePreorder (tree, &printNode); printf ("\n size %d \n", tree->size); tree = splayInsert (tree, 12); printTree(tree); traverseTreePreorder (tree, &printNode); printf ("\n size %d \n", tree->size); // Part two of the lab labPartTwo(); return (0); }