- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
A|'-- B | '-- C | | | '-- X1 | '-- D | '-- E | | | '-- F | | | '-- X2 | '-- X3
给定一棵树和到该树内节点的多条路径,我如何设计一种算法来返回树内与其他节点具有最少公共(public)路径的节点。
例如:
我们有以下到 x 的路径:
A -> B -> C -> x1
A -> B -> D -> E -> F -> x2
A -> B -> D -> x3
我们可以看到 x2
和 x3
共享一条公共(public)路径:A -> B -> D
比共享路径长通过 x1
和 x2
(A -> B
) 或者 x1
和 x3< 共享的路径
(A -> B
).
我的目标是通过算法找到 x1
。
最佳答案
观察:从根向下到每个叶子,公共(public)路径将从根到它们最低的公共(public)祖先节点。在这个例子中,最低共同祖先是节点 B 和节点 D。因此,这个问题的结果将始终在从根到最近的最低共同祖先的路径中。
另请注意,从一个最低公共(public)祖先节点向下一层,我们将始终有一条路径不包含任何其他最低公共(public)祖先节点。
所以我会提出一个解决方案,它需要在树中进行两次遍历。
对于树中的每个节点,我们记录了一个额外的信息,整数count
,它将告诉在这个节点,它的叶子中包含多少个X对象(即X1,X2,X3在示例中)。
class Node {
int count = 0;
Node []children;
}
因此,通过一次传递,我们可以更新所有count
信息。
public int getCount(Node node){
if(children is empty){
if(this node contains object in the list)
return 1;
return 0;
}else{
for(Node child in children){
node.count += getCount(child);
}
}
return node.count;
}
在第二遍中,从根向下到每个叶,问题的结果在具有 count == 1
且最接近根的节点中。 最近的根
实际上是树中节点的级别。
所以,在这个例子中,每个节点的计数是:
A:3(0级)
B:3(1 级)
C : 1(级别 2)包含最接近根的结果
D:2(2 级)
E:1(3 级)
F : 1(4 级)
所以,最后,时间复杂度是 O(N),其中 N 是节点数。
注意:正如 NiklasB 所提到的,整个过程可以一次完成。
只需修改 getCount
方法以包括到 root 的距离,以及两个全局变量,min
保持具有计数的节点的最小距离是1、另一个持有对象result。
int min = 100000;
Object result;
public int getCount(Node node, int distance){
if(children is empty){
if(this node contains object in the list)
if(distance < min){
min = distance;
result = //X
}
return 1;
return 0;
}else{
for(Node child in children){
node.count += getCount(child , distance + 1);
}
}
if(node.count == 1 && distance < min){
min = distance,
result = //X
}
return node.count;
}
关于algorithm - 树 : Find node whose common path is shortest in relation to other nodes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24011006/
关于 B 树与 B+ 树,网上有一个比较经典的问题:为什么 MongoDb 使用 B 树,而 MySQL 索引使用 B+ 树? 但实际上 MongoDb 真的用的是 B 树吗?
如何将 R* Tree 实现为持久(基于磁盘)树?保存 R* 树索引或保存叶值的文件的体系结构是什么? 注意:此外,如何在这种持久性 R* 树中执行插入、更新和删除操作? 注意事项二:我已经实现了一个
目前,我正在努力用 Java 表示我用 SML 编写的 AST 树,这样我就可以随时用 Java 遍历它。 我想知道是否应该在 Java 中创建一个 Node 类,其中包含我想要表示的数据,以及一个数
我之前用过这个库http://www.cs.umd.edu/~mount/ANN/ .但是,它们不提供范围查询实现。我猜是否有一个 C++ 范围查询实现(圆形或矩形),用于查询二维数据。 谢谢。 最佳
在进一步分析为什么MySQL数据库索引选择使用B+树之前,我相信很多小伙伴对数据结构中的树还是有些许模糊的,因此我们由浅入深一步步探讨树的演进过程,在一步步引出B树以及为什么MySQL数据库索引选择
书节上回,我们接着聊二叉树,N叉树,以及树的存储。 01、满二叉树 如果一个二叉树,除最后一层节点外,每一层的节点数都达到最大值,即每个节点都有两个子节点,同时所有叶子节点都在最后一层,则这个
树是一种非线性数据结构,是以分支关系定义的层次结构,因此形态上和自然界中的倒挂的树很像,而数据结构中树根向上树叶向下。 什么是树? 01、定义 树是由n(n>=0)个元素节点组成的
操作系统的那棵“树” 今天从一颗 开始,我们看看如何从小树苗长成一颗苍天大树。 运转CPU CPU运转起来很简单,就是不断的从内存取值执行。 CPU没有好好运转 IO是个耗费时间的活,如果CPU在取值
我想为海洋生物学类(class)制作一个简单的系统发育树作为教育示例。我有一个具有分类等级的物种列表: Group <- c("Benthos","Benthos","Benthos","Be
我从这段代码中删除节点时遇到问题,如果我插入数字 12 并尝试删除它,它不会删除它,我尝试调试,似乎当它尝试删除时,它出错了树的。但是,如果我尝试删除它已经插入主节点的节点,它将删除它,或者我插入数字
B+ 树的叶节点链接在一起。将 B+ 树的指针结构视为有向图,它不是循环的。但是忽略指针的方向并将其视为链接在一起的无向叶节点会在图中创建循环。 在 Haskell 中,如何将叶子构造为父内部节点的子
我在 GWT 中使用树控件。我有一个自定义小部件,我将其添加为 TreeItem: Tree testTree = new Tree(); testTree.addItem(myWidget); 我想
它有点像混合树/链表结构。这是我定义结构的方式 struct node { nodeP sibling; nodeP child; nodeP parent; char
我编写了使用队列遍历树的代码,但是下面的出队函数生成错误,head = p->next 是否有问题?我不明白为什么这部分是错误的。 void Levelorder(void) { node *tmp,
例如,我想解析以下数组: var array1 = ["a.b.c.d", "a.e.f.g", "a.h", "a.i.j", "a.b.k"] 进入: var json1 = { "nod
问题 -> 给定一棵二叉树和一个和,确定该树是否具有从根到叶的路径,使得沿路径的所有值相加等于给定的和。 我的解决方案 -> public class Solution { public bo
我有一个创建 java 树的任务,它包含三列:运动名称、运动类别中的运动计数和上次更新。类似的东西显示在下面的图像上: 如您所见,有 4 种运动:水上运动、球类运动、跳伞运动和舞蹈运动。当我展开 sk
我想在 H2 数据库中实现 B+ Tree,但我想知道,B+ Tree 功能在 H2 数据库中可用吗? 最佳答案 H2 已经使用了 B+ 树(PageBtree 类)。 关于mysql - H2数据库
假设我们有 5 个字符串数组: String[] array1 = {"hello", "i", "cat"}; String[] array2 = {"hello", "i", "am"}; Str
我正在处理树。每个节点都有带有 Tree * 值的对象。我读取的数据如下所示: 1 2 2 ... 这意味着,将 1 作为 0 的子节点,将 2 作为 1 的子节点,将 3 作为 o 2 的子节点。在
我是一名优秀的程序员,十分优秀!