- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
事情是这样的。我想遍历我的 N 叉树(在这种情况下是家谱)中的任意两个节点。下面是我从根遍历到任意节点的情况的简化代码:
#include <iostream>
#include <string>
using namespace std;
const int max=100;
//structure definition
//every children has only one parent
struct Tree {
string name;
int numChildren;
Tree *children[max];
~Tree(){delete [] children;}
};
//Label the nodes that are supposed to be in the path
//Suppose the names of those nodes do not contain whitespaces,just trivial names
int *travel_point(Tree *tree,string s){
Tree *node = new Tree;
Tree *temp = new Tree;
node = tree;
int a[100],i=0,j;
for(j=0;j<100;j++) a[j]=-1;
while(tree){
if(tree->name == s){
a[i]=0;
break;
}
else{
for(j=0;j<node->numChildren;j++){
if(travel_point(node->children[j],s)!=NULL){
break;
}
}
a[i]=j+1;
i++;
temp=node->children[j];
node=temp;
}
}
if(a[i-1]==-1) return NULL;
else a;
}
这大致就是我一直在做的事情。因为每个 child 只有一个 parent ,那么从根到一个任意节点的路径也是唯一的。所以我想将所有其他路径设置为 NULL,以防我可以采取递归期间的优势。
我知道递归不是一个好主意,但我只是想试一试。
最佳答案
好吧,我不想解决你的作业,但我想给你一些建议。首先,树的递归函数通常很好,通常树的高度不会太深,所以递归函数很好。它们更易于编写和理解。
1) 你不能在你的堆栈内存中返回一个指向数组的指针并假装它会工作:)
2) 你需要一个堆栈类,使用std::stack<int>
,将一个非常量引用作为参数传递给它,这样您就可以在所有函数中修改它,并返回一个 bool 值,指示您是否找到了该节点。然后您可以将堆栈转换为 vector 或数组,但正如我想象的算法,您需要一个堆栈数据结构。
3) 不需要通过复制传递字符串,传递一个常量引用给它。 (常量字符串& s)
4)算法错误,重写。多想想。
5) 析构函数错误,它不会杀死子节点,但会进行无效的释放,你需要一个循环。
6) 我会使用 std::vector<Tree*>
而不是 [max] 个子树的数组
我把这个函数想象成...
bool travel_point(const Tree *tree, const string& s, stack<int>& result)
并使用它...
stack<int> path;
if (travel_point(mytree, "ciao", path))
{
// print path
}
关于c++ - 在处理 N 元树遍历时我完全被卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7942237/
1。 Set 的 parallelStream 没有使用足够的线程。 Java8 parallelStream 不能完全并行工作。在我的计算机中,当任务数小于处理器数时,java8 集的 parall
我想将位置发送到 Google Geocoding API,因此我想用 + 替换文本中的任何空格或逗号(因为可以接收)。 例如,所有这些样本应返回 Glentworth+Ireland: Glentw
所以我需要为将要上传的图像文件生成较小的预览,并且我必须在每个文件名的末尾附加“_preview”。 目前我正在这样做: uploadFile.map((file) => { if (fi
我们可以用参数定义类型同义词,这在与实际类型一起使用时效果很好: type MyType t = t String String data Test a b = Test a b f :: MyTyp
给定一个包含一些 TGraphic 后代的 Delphi TPicture,我需要计算像素颜色和不透明度。我认为我必须为每个类提供不同的实现,并且我认为我已经涵盖了 TPngImage。 32 位位图
我正在调试 Powershell 项目。我正在使用 Import-Module 从我的 C# dll 加载 PS 模块,一切正常。尽管调用 Remove-Module 并不会完全卸载模块,因为 DLL
有没有办法在ElasticSearch中要求完整(尽管不一定精确)匹配? 例如,如果一个字段具有术语"I am a little teapot short and stout",我想匹配" i am
我正在尝试根据日期范围连接两个表。 表A格式为: ID CAT DATE_START DATE_END 1 10 2018-01-01 2020-12-31 2
我最近加入了一家公司,在分析他们的环境时,我注意到 SharePoint web.config 的信任级别设置为“完全”。我知道这绝对是一个糟糕的做法,并且希望 stackoverflow 社区能够帮
我构建了一个完全依赖 AJAX 的 php/js 应用程序,因此没有任何内容是静态的。 我正在尝试找到一种方法来转换基于内容的广告,该广告使用 AJAX 交付的内容作为关键字。 Google 的 Ad
我正在尝试根据日期范围连接两个表。 表A格式为: ID CAT DATE_START DATE_END 1 10 2018-01-01 2020-12-31 2
我熟悉 FileSystemWatcher 类,并使用它进行了测试,或者我使用快速循环进行了测试,并在目录中列出了类型文件的目录列表。在这种特殊情况下,它们是 zip 压缩的 SDF 文件,我需要解压
按照 Disqus 上的教程进行操作时,评论框不会呈现。从 disqus 上找到的管理员看来,它的设置似乎是正确的。 var disqus_config = function () { this
是否可以使用 Cython 将 Python 3 应用程序完全编译/链接为可执行格式(当然假设所有使用的模块都是 cythonable)。 我在 Linux 下工作,我希望获得一个依赖性尽可能小的 E
我有一个 C# 控制台应用程序,而不是运行预构建步骤(以获取 NuGet 包)。 当我调试这个时,我想传入一个参数并显示控制台。当我不调试它时,我不想看到它。我什至不希望它在那里闪烁一秒钟。 我找到了
我在 n 个节点上有一个完整的 19 元树。我标记所有具有以下属性的节点,即它们的所有非根祖先都是最年长或最小的 child (包括根)。我必须为标记节点的数量给出一个渐近界限。 我注意到 第一层有一
我正在阅读一篇关于 Java Volatile 关键字的文章,遇到了一些问题。 click here public class MyClass { private int years;
一本书中写道——“如果问题 A 是 NP-Complete,则存在解决 A 的非确定性多项式时间算法”。但据我所知,"is"——NP 完全问题的答案可以在多项式时间内“验证”。我真的很困惑。能否使用非
考虑以下问题: 有N个硬币,编号为1到N。 你看不到它们,但是给出了关于它们的 M 个事实,形式如下: struct Fact { set positions int num_head
我想制作一个包装数字类型的类型(并提供额外的功能)。 此外,我需要数字和包装器可以隐式转换彼此。 到目前为止我有: template struct Wrapper { T value;
我是一名优秀的程序员,十分优秀!