- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _Tree
{
struct _Tree *child;
struct _Tree *next;
char name[50];
}Tree;
Tree* memoryTree(char nombre_fichero[50]);
Tree*add(Tree*raiz, char name_root[50], char name_newnode[50]);
Tree*addSibling(char dato[50], Tree*top);
void PrintSiblings(Tree*root);
int main()
{
char name_temp[50];
char name_root[50];
char name_newnode[50];
Tree*Root; Root = NULL;
Root = memoryTree("A");
add(Root, "A", "1");
add(Root, "A", "2");
add(Root, "A", "3");
add(Root, "1", "1.1");
add(Root, "1.1", "1.1.1");
add(Root, "1.1.1", "1.1.1.1");
add(Root, "2", "2.1");
add(Root, "2", "2.2");
add(Root, "2", "2.3");
add(Root, "2.3", "2.3.1");
/* I'm printing it here for the moment in order to verify my new nodes addittions*/
Tree*Temp = Root;
while(Temp->child != NULL)
{
Tree*piv = Temp;
if(piv->child != NULL)
{
while(piv != NULL)
{
if(piv->child)
{
printf("\nRoot: %s\n", piv->name);
printf("Names:");
PrintSiblings(piv->child);
printf("\n");
}
piv = piv->next;
}
}
printf("\n");
Temp = Temp->child;
}
system("pause");
system("cls");
return 1;
}
void PrintSiblings(Tree*top)
{
if(top==NULL)
{
//printf("\nNO HAY HERMANOS\n");
}else
{
while(top!=NULL)
{
printf("->%s",top->name);
top=top->next;
}
}
}
Tree*memoryTree(char node_name[50])
{
Tree*root;
root = (Tree*)malloc(sizeof(Tree));
if(root != NULL)
{
root->child = NULL;
strcpy(root->name, node_name);
root->next = NULL;
}
return root;
}
Tree*add(Tree*root, char name_root[50], char name_newnode[50])
{
if (root == NULL)
{
return NULL;
}
else
{
if(strcmp(root->name, name_root) == 0)
{
//If a child-node is already added, then we add it as a Sibling
if(root->child == NULL)
{
root->child = memoryTree(name_newnode);
}
else
{
root->child->next = addSibling(name_newnode, root->child->next);
}
return root;
}
else
{
Tree *found;
/*Search in Siblings*/
if((found=add(root->next,name_root,name_newnode)) != NULL)
{
return root;
}
else
{
Tree *found2;
if((found2 = add(root->child,name_root,name_newnode)) != NULL)
{
return root;
}
}
}
}
}
Tree*addSibling(char new_node[50], Tree*top)
{
Tree*newN, *aux;
newN = memoryTree(new_node);
if(top==NULL)
{
top = newN;
}
else
{
aux=top;
while(aux->next != NULL)
{
aux = aux->next;
}
aux->next = newN;
}
return top;
}
这个方法只打印到第三个 child (我想我可能需要实现一个递归函数,但我不能这样做)
间距根本不是必需的。我只是想像这样打印它:
一个。 1,2,3..
1.1,1.2,..
2.1, 2.2,
[...]
2.2 2.2.1, 2.2.2,...
我的输出是:
Root: A
Names:->1->2->3
Root: 1
Names:->1.1
Root: 2
Names:->2.1->2.2->2.3
Root: 1.1
Names:->1.1.1
Root: 1.1.1
Names:->1.1.1.1
输出应打印“2.3”节点及其兄弟节点(“2.3.1”)
对如何正确完成这项工作有任何想法吗?
最佳答案
由于您在 add()
中递归构建树,最简单的方法似乎是递归 打印函数而不是您的迭代环形。我想出的输出:
> ./a.out
Node: A
Children:
Node: 1
Children:
Node: 1.1
Children:
Node: 1.1.1
Children:
Node: 1.1.1.1
Node: 2
Children:
Node: 2.1
Node: 2.2
Node: 2.3
Children:
Node: 2.3.1
Node: 3
>
我重新编写了您的代码以简化它并解决了一些样式问题:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _Tree
{
struct _Tree *child;
struct _Tree *next;
char name[50];
} Tree;
Tree *memoryTree(const char *node_name)
{
Tree *root = malloc(sizeof(Tree));
if (root != NULL)
{
root->child = NULL;
strcpy(root->name, node_name);
root->next = NULL;
}
return root;
}
Tree *addSibling(const char *node_name, Tree *node)
{
Tree *new_node = memoryTree(node_name);
if (node == NULL)
{
node = new_node;
}
else
{
Tree *aux = node;
while (aux->next != NULL)
{
aux = aux->next;
}
aux->next = new_node;
}
return node;
}
Tree *add(Tree *node, const char *name_root, const char *node_name)
{
if (node == NULL)
{
return NULL;
}
if (strcmp(node->name, name_root) == 0)
{
node->child = addSibling(node_name, node->child);
return node;
}
Tree *found;
/* Search in Siblings */
if ((found = add(node->next, name_root, node_name)) != NULL)
{
return found;
}
if ((found = add(node->child, name_root, node_name)) != NULL)
{
return found;
}
return NULL;
}
void printTabs(int count)
{
for (int i = 0; i < count; i++)
{
putchar('\t');
}
}
void printTreeRecursive(Tree *node, int level)
{
while (node != NULL)
{
printTabs(level);
printf("Node: %s\n", node->name);
if (node->child != NULL)
{
printTabs(level);
printf("Children:\n");
printTreeRecursive(node->child, level + 1);
}
node = node->next;
}
}
void printTree(Tree *node)
{
printTreeRecursive(node, 0);
}
int main()
{
Tree *root = memoryTree("A");
add(root, "A", "1");
add(root, "A", "2");
add(root, "A", "3");
add(root, "1", "1.1");
add(root, "1.1", "1.1.1");
add(root, "1.1.1", "1.1.1.1");
add(root, "2", "2.1");
add(root, "2", "2.2");
add(root, "2", "2.3");
add(root, "2.3", "2.3.1");
printTree(root);
return 1;
}
关于c - 在 C 中打印一棵 n 叉树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62250866/
我想在我的 Tree 类中创建一个函数来遍历 n-ary Tree[T] 以取回具有 (level, T) 的元组,以便该 Tree 的用户可以执行类似 tree.traverse.foreach{
给定一个层次格式的数组,它们的直接子级存储在一个连续的数组中,返回一个 n 叉树 给定输入格式: [{'name':'a', 'level': -1}, {'name':'b', 'level
我要求教授给我一份另一个学期的旧作业。它是关于构建一个家谱,然后找到给定的两个节点之间的亲属关系。家谱是关于那美克星人(龙珠z)的,所以每个那美克星人都有一个父亲。 问题是输入是这样的: First
我正在尝试创建一个包含子 vector 的 n 叉树。 这就是我到目前为止所得到的。 在 node.h 文件中我有这个: #include #include using namespa
我正在尝试了解 n 叉树的预序遍历。我一直在阅读,我发现的所有示例都使用左子树和右子树,但是在 n 叉树中,什么是左子树,什么是右子树?有人可以给出一个很好的解释或伪代码吗? 最佳答案 而不是考虑 l
我应该反序列化一个 n 叉树。 这段代码创建了我的树: foodtree.addChildren("Food", { "Plant", "Animal" } ); foodtree.a
我正在尝试创建叉 TreeMap ,但仍然没有成功。这是我的代码: #include #include #include void procStatus(int level) { prin
我有一个二叉树,代表一个解析后的逻辑公式。例如,f = a & b & -c | d 由前缀表示法的列表列表表示,其中第一个元素是运算符(一元或二元),接下来的元素是它们的参数: f = [ |, [
我正在尝试根据给定的输入创建一棵树。那里将有一个根,包括子节点和子子节点。我可以实现树,在其中我可以将子节点添加到特定的主节点(我已经知道根)。但是,我试图弄清楚实现树的推荐方法是什么,我们必须首先从
我在 n 个节点上有一个完整的 19 元树。我标记所有具有以下属性的节点,即它们的所有非根祖先都是最年长或最小的 child (包括根)。我必须为标记节点的数量给出一个渐近界限。 我注意到 第一层有一
如何在不使用递归的情况下遍历 n 叉树? 递归方式: traverse(Node node) { if(node == null) return; for(Node c
我的树/节点类: import java.util.ArrayList; import java.util.List; public class Node { private T data;
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
我在我的 Java 应用程序中有一个非 UI 使用的所谓的“k-ary”树,我想知道 javax.swing.tree 包是否是完成这项工作的正确工具,即使它与 Swing 打包在一起. 我有一类 W
我正在用 Java 实现 N 叉树;每个节点可以有尽可能多的节点。当我尝试 build 一棵树时,问题就来了。我有一个函数可以递归地创建一个特定高度的树,并根据节点列表分配子节点。当我调用该函数时,根
嗨,我有这段代码来搜索 n 叉树,但它不能正常工作,我不知道这有什么问题当搜索 n4 和 n5 时,它返回 n3怎么了? public FamilyNode findNodeByName(Family
哪个是 C 语言中 N 叉树的简洁实现? 特别是,我想实现一个 n 元树,而不是自平衡的,每个节点中的子节点数量不受限制,其中每个节点都包含一个已经定义的结构,例如: struct task {
#include #include #include typedef struct _Tree { struct _Tree *child; struct _Tree *
我正在编写文件系统层次结构的 N 叉树表示形式,其中每个节点都包含有关它所表示的文件/文件夹的一些信息。 public class TreeNode { private FileSystemE
如何在 R 中为给定数量的分支和深度构建 N 叉树,例如深度为 3 的二叉树? 编辑:将源问题与问答分开。 最佳答案 我想提出解决方案,我用它来构建树数据结构 叶安姆 分支因子。要将数据存储在树中,字
我是一名优秀的程序员,十分优秀!