/* add leaf with specified name to specified internal branch */ void tr_add_leaf_internal(TreeNode *t, int branch, char *lname, int lgroup) { TreeNode *oldnode, *newanc, *newleaf; oldnode = lst_get_ptr(t->nodes, branch); /* node beneath branch in question */ if (oldnode == t) die("ERROR tr_add_leaf_internal: oldnode == t\n"); newanc = tr_new_node(); newleaf = tr_new_node(); strcpy(newleaf->name, lname); newleaf->dparent = lgroup; newanc->rchild = newleaf; newleaf->parent = newanc; newanc->lchild = oldnode; newanc->parent = oldnode->parent; if (oldnode->parent->lchild == oldnode) oldnode->parent->lchild = newanc; else oldnode->parent->rchild = newanc; oldnode->parent = newanc; if (lgroup > 0 && lgroup == oldnode->dparent) newanc->dparent = lgroup; /* fix up ids and nodes list */ lst_push_ptr(t->nodes, newanc); newanc->id = lst_size(t->nodes) - 1; /* circumvent normal id assignment */ lst_push_ptr(t->nodes, newleaf); newleaf->id = lst_size(t->nodes) - 1; t->nnodes += 2; }
/* add a leaf with specified name to root branch */ void tr_add_leaf_at_root(TreeNode *t, char *lname, int lgroup) { TreeNode *newanc, *newleaf; newanc = tr_new_node(); newleaf = tr_new_node(); strcpy(newleaf->name, lname); newleaf->dparent = lgroup; /* we don't want to change the identity of the root node, so will add the new node below it and rewire as necessary */ newanc->lchild = t->lchild; newanc->rchild = t->rchild; t->lchild->parent = newanc; t->rchild->parent = newanc; t->lchild = newanc; t->rchild = newleaf; newanc->parent = t; newleaf->parent = t; newanc->dparent = t->dparent; if (lgroup == newanc->dparent) t->dparent = lgroup; else t->dparent = 0; /* fix up ids and nodes list */ lst_push_ptr(t->nodes, newanc); newanc->id = lst_size(t->nodes) - 1; /* circumvent normal id assignment */ lst_push_ptr(t->nodes, newleaf); newleaf->id = lst_size(t->nodes) - 1; t->nnodes += 2; }
void tr_add_node(t_node **tree, struct dirent *file, int key, void *sort) { t_node *tmp_node; t_node *tmp_tree; t_node *elem; tmp_tree = *tree; if (! (elem = tr_new_node(file, key, sort))) return ; if (! tmp_tree) *tree = elem; while (tmp_tree) { tmp_node = tmp_tree; if (ft_strcmp(sort, tmp_tree->sort) > 0) { tmp_tree = tmp_tree->right; if (!tmp_tree) tmp_node->right = elem; } else { tmp_tree = tmp_tree->left; if (!tmp_tree) tmp_node->left = elem; } } }
/* create a trivial, two-leaf tree */ TreeNode *tr_new_trivial(char *name1, char *name2) { TreeNode *root; root = tr_new_node(); root->lchild = tr_new_node(); strcpy(root->lchild->name, name1); root->lchild->parent = root; root->rchild = tr_new_node(); strcpy(root->rchild->name, name2); root->rchild->parent = root; /* bypass default handling of ids and nodes list */ root->nnodes = 3; root->id = 0; root->lchild->id = 1; root->rchild->id = 2; root->nodes = lst_new_ptr(root->nnodes); lst_push_ptr(root->nodes, root); lst_push_ptr(root->nodes, root->lchild); lst_push_ptr(root->nodes, root->rchild); return root; }