带指针的树 typedef struct{//节点的主结构 char*value;//类似指针的字符串视图 结构T*T\u l,*T\u r;//左右指针 }*树,昏暗; 树合并树(char*el,树t1,树t2){//合并子树 treet0=(tree)malloc(sizeof(dim));//为合并子btree创建一个结构 t0->T_l=t1; t0->T_r=t2; t0->value=el; 返回(t0); } tree createleaf(char*el){//调用mergetree函数的新叶 返回mergetree(el,NULL,NULL); } int isvoidtree(树t){//检查树是否为空 返回(t==NULL); } char*root(树t){//节点的返回值 返回t->value; } 树leftchild(树t){//节点的返回指针 返回t->t\u l; } 树rightchild(树t){//节点的返回指针 返回t->t\r; } 树插入(char*el,tree t){//插入调用特定函数的新元素 if(isvoidtree(t)) 返回createleaf(el); if(strcmp(根(t),el)>=0)//左侧 返回mergetree(root(t)、insert(el、leftchild(t))、righchild(t)); 如果(strcmp(root(t),el)
将每个名称读入同一个数组;因此,树的每个元素都指向相同的字符串,该字符串保存输入的姓氏。就像为每个新节点分配空间一样(但无法释放不再使用的节点,唉),您需要为每个名称分配空间。不要使用typedef指针,除非您将它们用于不透明的数据类型。这会影响它们的含义,并使读者感到困惑。使用“会说话”的名称。对于树节点带指针的树 typedef struct{//节点的主结构 char*value;//类似指针的字符串视图 结构T*T\u l,*T\u r;//左右指针 }*树,昏暗; 树合并树(char*el,树t1,树t2){//合并子树 treet0=(tree)malloc(sizeof(dim));//为合并子btree创建一个结构 t0->T_l=t1; t0->T_r=t2; t0->value=el; 返回(t0); } tree createleaf(char*el){//调用mergetree函数的新叶 返回mergetree(el,NULL,NULL); } int isvoidtree(树t){//检查树是否为空 返回(t==NULL); } char*root(树t){//节点的返回值 返回t->value; } 树leftchild(树t){//节点的返回指针 返回t->t\u l; } 树rightchild(树t){//节点的返回指针 返回t->t\r; } 树插入(char*el,tree t){//插入调用特定函数的新元素 if(isvoidtree(t)) 返回createleaf(el); if(strcmp(根(t),el)>=0)//左侧 返回mergetree(root(t)、insert(el、leftchild(t))、righchild(t)); 如果(strcmp(root(t),el),c,pointers,b-tree,C,Pointers,B Tree,将每个名称读入同一个数组;因此,树的每个元素都指向相同的字符串,该字符串保存输入的姓氏。就像为每个新节点分配空间一样(但无法释放不再使用的节点,唉),您需要为每个名称分配空间。不要使用typedef指针,除非您将它们用于不透明的数据类型。这会影响它们的含义,并使读者感到困惑。使用“会说话”的名称。对于树节点节点可能是一个不错的选择。嗯,是的。您有什么实际的解决方案来解决此问题吗? typedef struct T{ //Main struct of the nodes char *va
节点
可能是一个不错的选择。嗯,是的。您有什么实际的解决方案来解决此问题吗?
typedef struct T{ //Main struct of the nodes
char *value; //String view like a pointer
struct T *T_l, *T_r; //Pointers left and right
}*tree, dim;
tree mergetree(char *el, tree t1, tree t2){ // Merging sub-btree
tree t0 = (tree)malloc(sizeof(dim));// Create a struct for merge the sub-btree
t0->T_l = t1;
t0->T_r = t2;
t0->value = el;
return(t0);
}
tree createleaf(char *el){ //New leaf calling the mergetree function
return mergetree(el, NULL, NULL);
}
int isvoidtree(tree t){ // Checking if the tree is void or not
return (t == NULL);
}
char *root(tree t){ //Return value of the node
return t->value;
}
tree leftchild(tree t){ // Return pointer of the node
return t->T_l;
}
tree rightchild(tree t){ // Return pointer of the node
return t->T_r;
}
tree insert(char *el, tree t){ //Insert the new element calling specific functions
if(isvoidtree(t))
return createleaf(el);
if (strcmp(root(t), el)>=0) //Left side
return mergetree(root(t), insert(el, leftchild(t)), rightchild(t));
if (strcmp(root(t),el)<0) //Right side
return mergetree(root(t), leftchild(t), insert(el, rightchild(t)));
else return t;
}
void showtree(tree t){ //Show recursively the root of all sub-btree
int i;
if (isvoidtree(t) == false){ // if the tree is not null the start of recursive calls start
showtree(leftchild(t));
printf("%s\n", root(t));
showtree(rightchild(t));
}
}
int main(int argc, char** argv) {
int N,i;
char el[20];
tree btree = NULL; //init btree
printf("Size:\n");
scanf("%d",&N);
for(i=0;i<N;i++){
printf("Insert name:\n");
scanf("%s",el);
btree = insert(el,btree);}
showtree(btree); //Output btree
return (EXIT_SUCCESS);
}