static void binary_tree_insert(qrb_tree* tree, qrb_node* new_node_p) { qrb_node* curr_node_p; if( RB_TREE_ROOT(tree) == NULL ){ // first time //fprintf(stderr,"making new node %s root\n",(char *)(new_node_p->key)); set_root_node(tree,new_node_p); return; } curr_node_p = RB_TREE_ROOT(tree); // descend the tree to find where the new key goes while( curr_node_p != NULL ){ if( /*tree->comp_func*/ strcmp( RB_NODE_KEY(new_node_p),RB_NODE_KEY(curr_node_p)) < 0 ){ if( curr_node_p->left == NULL ){ new_node_p->parent = curr_node_p; curr_node_p->left = new_node_p; return; } curr_node_p = curr_node_p->left; } else { if( curr_node_p->right == NULL ){ new_node_p->parent = curr_node_p; curr_node_p->right = new_node_p; return; } curr_node_p = curr_node_p->right; } } // should never get here! assert(1==0); }
/** * This write the information from the Parser to the XML file * It calls the root node and waypoint functions in order to complete this operation * @param *model is the current Parser value */ void write_to_file(Parser *model) { FILE *file; //creates and appends to the c.gps file file = fopen("c.gps", "a+"); //sets the values to the file fprintf(file, "%s", set_root_node()); fprintf(file, "%s", wpt_node(model)); fprintf(file, "%s", closing_wpt()); fprintf(file, "%s", close_root_node()); fclose(file); }
/* * This function takes the leaf nodes which have been written in * outdir, and populates the root node and any necessary interior nodes. */ static errcode_t calculate_tree(ext2_filsys fs, struct out_dir *outdir, ext2_ino_t ino, ext2_ino_t parent) { struct ext2_dx_root_info *root_info; struct ext2_dx_entry *root, *dx_ent = 0; struct ext2_dx_countlimit *root_limit, *limit; errcode_t retval; char * block_start; int i, c1, c2, nblks; int limit_offset, root_offset; root_info = set_root_node(fs, outdir->buf, ino, parent); root_offset = limit_offset = ((char *) root_info - outdir->buf) + root_info->info_length; root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset); c1 = root_limit->limit; nblks = outdir->num; /* Write out the pointer blocks */ if (nblks-1 <= c1) { /* Just write out the root block, and we're done */ root = (struct ext2_dx_entry *) (outdir->buf + root_offset); for (i=1; i < nblks; i++) { root->block = ext2fs_cpu_to_le32(i); if (i != 1) root->hash = ext2fs_cpu_to_le32(outdir->hashes[i]); root++; c1--; } } else { c2 = 0; limit = 0; root_info->indirect_levels = 1; for (i=1; i < nblks; i++) { if (c1 == 0) return ENOSPC; if (c2 == 0) { if (limit) limit->limit = limit->count = ext2fs_cpu_to_le16(limit->limit); root = (struct ext2_dx_entry *) (outdir->buf + root_offset); root->block = ext2fs_cpu_to_le32(outdir->num); if (i != 1) root->hash = ext2fs_cpu_to_le32(outdir->hashes[i]); if ((retval = get_next_block(fs, outdir, &block_start))) return retval; dx_ent = set_int_node(fs, block_start); limit = (struct ext2_dx_countlimit *) dx_ent; c2 = limit->limit; root_offset += sizeof(struct ext2_dx_entry); c1--; } dx_ent->block = ext2fs_cpu_to_le32(i); if (c2 != limit->limit) dx_ent->hash = ext2fs_cpu_to_le32(outdir->hashes[i]); dx_ent++; c2--; } limit->count = ext2fs_cpu_to_le16(limit->limit - c2); limit->limit = ext2fs_cpu_to_le16(limit->limit); } root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset); root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1); root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit); return 0; }
static void rb_delete(qrb_tree *tree_p, qrb_node *n_p ) { qrb_node *c_p; // the single non-leaf child qrb_node *parent; tree_p->node_count --; if( tree_p->node_count == 0 ){ assert( IS_ROOT_NODE(n_p) ); givbuf(n_p); tree_p->root = NULL; return; } if( n_p->left != NULL && n_p->right != NULL ){ //fprintf(stderr,"before calling binary_tree_delete:\n"); //dump_rb_node(n_p); n_p = binary_tree_delete(n_p); //fprintf(stderr,"after calling binary_tree_delete:\n"); //dump_rb_node(n_p); } else if( IS_ROOT_NODE(n_p) ){ // we are deleting a root node with only one child if( n_p->left != NULL ){ set_root_node(tree_p,n_p->left); } else { set_root_node(tree_p,n_p->right); } givbuf(n_p); return; } assert( n_p->left == NULL || n_p->right == NULL ); if( n_p->left != NULL ) c_p = n_p->left; else c_p = n_p->right; // may be NULL // if c_p is null, then we need to keep a reference to the parent... parent=n_p->parent; replace_node(tree_p,n_p,c_p); if( IS_RED(n_p) ){ givbuf(n_p); return; } // Now we can free n_p givbuf(n_p); if( IS_RED(c_p) ){ MAKE_BLACK(c_p); return; } // Now we know the node in question is black and has no red child //assert( n_p->left != NULL && n_p->right != NULL ); // Now we know that both n_p and c_p are black rebalance(tree_p,c_p,parent); }