int main(int argc, char *argv[]) { extern struct tree_node head; printf("head.num = %d\n", head.num); insert_tree_node(create_tree_node(10)); insert_tree_node(create_tree_node(18)); insert_tree_node(create_tree_node(12)); insert_tree_node(create_tree_node(1)); insert_tree_node(create_tree_node(9)); insert_tree_node(create_tree_node(3)); insert_tree_node(create_tree_node(50)); insert_tree_node(create_tree_node(70)); insert_tree_node(create_tree_node(100)); insert_tree_node(create_tree_node(10000)); print_tree_num(&head); tree_transfer_list(&head); return 0; }
static int parser_insert_node(Parser *parser, char *key, char *value) { if(!key || !value) { PRINTF("invalid parameter!\n"); return -1; } PRINTF("start!\n"); char *widget_name = NULL; TagType type = TAG_CONTENT; int is_ddl = 0; int ddl_id = 0; TagNode *node = NULL; char tmp_key_buf[32] = {0}; memcpy(tmp_key_buf, key, strlen(key)); parse_key(key, &widget_name, &type, &is_ddl, &ddl_id); PRINTF("\nafter parse key\n"); printf("widget_name(%s) type(%d), is_ddl(%d), ddl_id(%d)\n", widget_name, type, is_ddl, ddl_id); node = create_tree_node(type, widget_name, is_ddl, ddl_id, tmp_key_buf); printf("after create_tree_node type(%d), widget_name(%s), is_ddl(%d), ddl_id(%d)\n", node->tag_type, \ node->tag_name, node->is_ddl, node->ddl_id); parser_tree_insert_node(parser, node); return 0; }
struct tree_node *create_node() { struct node *node; struct tree_node *tmp; int num,i; static int count = 0; count++; node = malloc(sizeof(*node)); printf("Enter number of children for %d:\n",count); scanf("%d",&num); node->data = NULL; if (num) { node->data = malloc(sizeof(int)*num); } node->myval = count; tmp = create_tree_node(node, num); for(i=0;i<num;i++) { node->data[i] = count+i+1; add_child(tmp,create_node()); } return tmp; }
parse_tree create_leaf_node ( char *leafdata ) { /* * create a leaf node - which will always be a relation or a command. * - it must be a tree node in itself, which simply references the leaf. */ parse_leaf leaf; parse_tree tree; printf("Request to create a leaf node\n"); tree=create_tree_node(NODE_TYPE_NAME, NODE_POS_LEAF, NULL, 0); leaf=(parse_leaf_struct *) malloc(sizeof(parse_leaf_struct)); /* Allocate space to hold the item */ leaf->item=malloc(sizeof(leafdata)); /* Copy data into the item */ strcpy(leaf->item,leafdata); tree->no_leaf_nodes++; tree->no_sub_nodes++; tree->leaf[0]=leaf; return(tree); }
int main() { struct tree_node *root; struct tree_node *left, *right; int i = 1,j=2,k=3; root = create_tree_node(&i); left = create_tree_node(&j); right = create_tree_node(&k); TREE_ADD(root, left, left); TREE_ADD(root, right, right); traverse_path(root,traverse); destroy_tree(root,NULL); }
/** * ntree_insert - Following the parent path, adds new node * @tree: Points to root of NTree node * @parents: An array of strings, shows path to new node * @data: New string to be stored * Description: The func adds a new node to ntree */ int ntree_insert(NTree **tree, char **parents, char *data) { List *list_node; if (*tree == NULL) { *tree = create_tree_node(data); return 0; } list_node = traverse_nodes(*tree, parents); if ( create_new_list(list_node, data, *tree) == NULL ) { return 1; } return 0; }
/** * create_new_list - created a new list * @head: pointer to head of a list * @str: string to be added * @root: root of Ntree * Description: Creates a new list node */ List* create_new_list(List *head, char *str, NTree *root) { List *list; list = malloc(sizeof(List)); if (list == NULL) { return NULL; } list->next = NULL; list->node = create_tree_node(str); if (head == NULL) { list->next = root->children; root->children = list; return list; } list->next = head->node->children; head->node->children = list; return list; }
/** * @brief Inserts a new node to the binary search tree. @param root: The root pointer of the binary search tree. @param name: The name (aka key) of the tree node. @param data_ptr: A pointer to the data that will be copied to the new node. @param found: A tree node, in case of not NULL the value of it points to the new allocated node. @return: The new binary search tree. */ tree_node * tree_insert_node(tree_node * root,const char * name, void * data_ptr, tree_node ** found){ tree_node * new_node = create_tree_node(name,data_ptr); tree_node * visitor = root; tree_node * previous = NULL; char is_left_child = 0; short int comparison; if(!new_node) return root; while(visitor){ previous = visitor; comparison = strcmp(name,visitor->name); if(comparison==0){ if(found) *found = visitor; return root; } else if(comparison>0){ visitor = visitor->rc; is_left_child = 0; } else{ visitor = visitor->lc; is_left_child = 1; } } if(root==NULL) root = new_node; else { if(is_left_child) previous->lc = new_node; else previous->rc = new_node; } return root; }
//remember to NULL god damn siblings....... int transform(struct tree *here){ struct tree *temp, *temp1, *temp2, *temp3, *temp4, *temp5, *temp6; switch (here->type){ case LET:{ if(here->child->type != EQUAL) break; here->type = GAMMA; strcpy(here->content,"gamma"); here->child->type = LAMBDA; strcpy(here->child->content, "lambda"); temp = here->child->child->sibling; here->child->child->sibling = here->child->sibling; here->child->sibling = temp; if (debug) printf("Finished LET\n"); return 1; } case WHERE:{ if(here->child->sibling->type != EQUAL) break; /*whenever you copy the child, remeber its sibling may get into your way, many bugs and segmentation fault come from here but you can't NULL it's sibling too soon. == damn you better copy the addresss which will be NULLize later first*/ temp = here->child->sibling; // record here temp1 = here->child; temp1->sibling = NULL; // cut here, if the sequence is wrong, sementation fault may come here->child= temp; here->child->sibling = temp1; here->type = GAMMA; strcpy(here->content,"gamma"); here->child->type = LAMBDA; strcpy(here->child->content, "lambda"); temp = here->child->child->sibling; here->child->child->sibling = here->child->sibling; here->child->sibling = temp; if (debug) printf("Finished WHERE\n"); return 1; } case REC:{ if(here->child->type != EQUAL) break; temp = here->child->child->sibling; temp1 = here->child->child; temp1->sibling = NULL; temp2=malloc(sizeof(struct tree)); copy_tree_node(temp1,temp2); here->type = EQUAL; strcpy(here->content,"="); here->child = temp1; here->child->sibling = create_tree_node(GAMMA); here->child->sibling->child = create_tree_node(YSTAR); here->child->sibling->child->sibling = create_tree_node(LAMBDA); here->child->sibling->child->sibling->child = temp2; here->child->sibling->child->sibling->child->sibling =temp; if (debug) printf("Finished REC\n"); return 1; } case ATX:{ temp = here->child->sibling->sibling; temp1= here->child->sibling; temp1->sibling=NULL; temp2 = here->child; temp2->sibling = NULL; here->type = GAMMA; strcpy(here->content,"gamma"); here->child = create_tree_node(GAMMA); here->child->sibling = temp; here->child->child=temp1; here->child->child->sibling = temp2; return 1; } case WITHIN:{ if(here->child->type != EQUAL || here->child->sibling->type !=EQUAL) break; temp = here->child->sibling->child->sibling; //E2 temp1 = here->child->child->sibling; //E1 temp2 = here->child->sibling->child; //X2 temp2->sibling =NULL; temp3 = here->child->child; //X1 temp3->sibling = NULL; here->type = EQUAL; strcpy(here->content,"="); here->child = temp2; here->child->sibling = create_tree_node(GAMMA); here->child->sibling->child = create_tree_node(LAMBDA); here->child->sibling->child->sibling = temp1; here->child->sibling->child->child = temp3; here->child->sibling->child->child->sibling = temp; if (debug) printf("Finished WITHING\n"); return 1; } case AND2:{ // temp = here->child; // while(temp !=NULL){ // if(temp->type != EQUAL) // break; // else // temp = temp->sibling; // } temp1 = create_tree_node(COMMA); temp3=temp1; temp2 = create_tree_node(TAU); temp4= temp2; temp = here->child; if(temp != NULL){ temp2->child = temp->child->sibling; temp1->child = temp->child; temp1->child->sibling = NULL; temp = temp->sibling; temp1 = temp1->child; temp2 = temp2->child; } while(temp != NULL){ temp2->sibling = temp->child->sibling; temp1->sibling = temp->child; temp1->sibling->sibling =NULL; temp1 = temp1->sibling; temp2 = temp2->sibling; temp= temp->sibling; } here->type = EQUAL; strcpy(here->content,"="); here->child = temp3; here->child->sibling = temp4; here->child->sibling->sibling = NULL; //cut off other siblings if (debug) printf("Finished AND\n"); return 1; } case FCN_FORM:{ temp = here->child->sibling; // point to first var while(temp->sibling != NULL){ //after this, temp will be fn's body temp = temp->sibling; } temp1 = here->child->sibling; temp2 = create_tree_node(LAMBDA); temp2->child = temp1; temp1= temp1->sibling; temp3= temp2->child; // while(temp1 != temp){ temp3->sibling= create_tree_node(LAMBDA); temp3->sibling->child = temp1; temp3=temp3->sibling->child; temp1=temp1->sibling; } temp3->sibling=temp; here->type = EQUAL; strcpy(here->content,"="); here->child->sibling = temp2; here->child->sibling->sibling=NULL; if (debug) printf("Finished FCN_FORM\n"); return 1; } }// end of switch return 0; }