struct treenode* handle_UND_treenode (int attr,int line,int column,int cnt, ... ) { struct treenode *now=new_treenode(); now->attr=attr; now->line=line; now->column=column; if (cnt) { va_list ap; va_start(ap,cnt); struct treenode* tmp=now; int j; for (j=0;j<cnt;j++) { struct treenode** t2=va_arg(ap,struct treenode **); assert((*t2)->attr); if ((*t2)->right_child!=NULL) { Print_Name((*t2)->right_child->attr); printf("\n"); } assert((*t2)->right_child==NULL); if (j) assert(tmp->right_child==NULL); else assert(tmp->left_child==NULL); if (j) tmp->right_child=*t2; else tmp->left_child=*t2; tmp=*t2; } va_end(ap); } return now; }
treenode* insert_key(char* s,int val,treenode* node) { int i; if(strlen(s)==1) { // reached last character if(node->size>0){ for(i=0;i<node->size;i++){ if(s[0]==node->next[i]->letter) { node->next[i]->val=val; return(node);} } } //either node size==0 or new key letter node = new_branch(node); node->next[node->size-1]=new_lasttreenode(s[0],val); return(node); } else { // not the last character if(node->size>0){ for(i=0;i<node->size;i++){ if(s[0]==node->next[i]->letter) { node->next[i] = insert_key(s+1,val,node->next[i]); return(node); } } } //either node size==0 or new key letter node = new_branch(node); node->next[node->size-1]=new_treenode(s[0]); node->next[node->size-1]=insert_key(s+1,val,node->next[node->size-1]); return(node); } }
struct treenode* make_D_treenode(int attr,char* val) { struct treenode *now=new_treenode(); now->attr=attr; if (val!=NULL) { now->value=(char*)malloc(strlen(val)*sizeof(char)); strcpy(now->value,val); } assert(now->right_child==NULL && now->left_child==NULL); return now; }
/* lookup the given path, starting at the given node, and return the node * described by the path or NULL if there is none (unless create is non-zero, * in which case all required nodes are created instead) * path is modified but will be restored to its original state */ TreeNode *lookup_treenode(TreeNode *t, char *path, int create) { if(*path == '/') path++; while(*path && strcmp(path, "/") != 0) { if(!t->dir)/* the child node can't be found if not a directory */ return NULL; /* mark the end of the next path component for strcmp's sake */ char *endpath = NULL; if((endpath = strchr(path, '/'))) *endpath = '\0'; /* store nchilds because t gets updated to point at a different node */ int nchilds = t->dir->nchilds; int i; for(i = 0; i < nchilds; i++) { if(strcmp(t->dir->child[i]->name, path) == 0) { /* move on to the child */ t = t->dir->child[i]; break; } } if(i == nchilds) {/* no such child was found */ /* return NULL if we're not creating new nodes */ if(!create) return NULL; /* create the node */ TreeNode *child = new_treenode(path); add_child(t, child); if(endpath) child->dir = new_dirinfo(child); t = child; } /* if there are more path components, restore *endpath and * move on, otherwise jump to the end of the path */ if(endpath) { *endpath = '/'; path = endpath + 1; } else { path += strlen(path); } } return t; }
int main(int argc, const char *argv[]) { // Build a tree: // 4 // / \ // 2 5 // / \ // 1 3 // \ // 7 struct tnode* _2 = new_treenode(2); _2->left = new_treenode(1); struct tnode* _3 = new_treenode(3); _3->right = new_treenode(7); _2->right = _3; struct tnode* _4 = new_treenode(4); _4->left = _2; _4->right = new_treenode(5); struct list* path = leftmostPath(_4); info_list(path); return 0; }
void case1() { /** * 3 * / \ * 2 1 * / \ \ * 5 4 0 * * 325 + 324 + 310 = 959 */ struct TreeNode* n1 = new_treenode(5, NULL, NULL); struct TreeNode* n2 = new_treenode(4, NULL, NULL); n1 = new_treenode(2, n1, n2); n2 = new_treenode(0, NULL, NULL); n2 = new_treenode(1, NULL, n2); n1 = new_treenode(3, n1, n2); printf("%s: %d\n", __func__, sumNumbers(n1)); }