- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
给定以下结构:
struct TElem {
int val;
};
typedef int TKey;
struct Node {
TKey key;
struct TElem *elem;
struct Node *left;
struct Node *right;
};
struct bst {
struct Node *root;
};
还有两个函数bst_Search
和bst_Insert
。
struct TElem* bst_Search(struct bst *T, TKey c, struct Node **posins)
{
struct Node *q = T->root;
posins = &(T->root); // (1)
while (q)
if (q->key == c)
return q->elem;
else if ( c < q->key) {
q = q->left;
posins = &(q->left);
} else {
q = q->right;
posins = &(q->right);
}
return NULL;
}
void bst_Insert(struct bst *T, TKey c, struct TElem *x)
{
struct Node **posins;
struct TElem *v;
posins = malloc(sizeof *posins);
v = bst_Search(T, c, posins); // (2)
if (!v) {
struct Node *q = malloc(sizeof *q);
q->key = c;
q->elem = x;
q->left = NULL;
q->right = NULL;
*posins = q; // (3)
} else {
printf("Key exists %d\n", c);
}
free(posins);
}
而main()
是“主要”
struct bst *T = malloc(sizeof *T);
T->root = NULL;
struct TElem *x = elem_New(10);
bst_Insert(T, c, x);
我正在尝试将一个新元素插入(elem_New
工作正常)到 BST 中,使用辅助函数 bst_Search
返回指向要插入 的正确位置的指针。 (1) 在第一次将 posins
指向 T->root
内存地址时被调用。 (据我所知,它工作正常)。
然后 posins
地址返回给调用函数 bst_Insert
,因为 bst_Search
找到了它返回 NULL
的地方(2)。它在 if
语句中正确设置了 q
。然后在 (3),我希望 posins
指向的位置(在本例中是 T->root 指向的地址)现在应该重定向到新树节点 q
。等同于 T->root = q
,但通过引用使用此调用。
最佳答案
因为你的“posins”在你的“bst_Search”中没有改变
void test(int *p){
// g_c is a gobal int
p = &g_c;
printf("%X ",p);}
调用 test(int *p) 时想想p的值会变指向g_c吗?不,因为 p 只是一个参数,它不会在过程中改变它的值
my english is poor so i just hope the e.g. code will help your understand~ :)
关于c - 在二叉搜索树中通过引用重定向指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44856193/
过去两天我遇到了一个奇怪的问题,但我还无法解决它。我正在尝试从 2 个文本文件中获取单词并将这些单词添加到树中。我选择的获取单词的方法引用这里: Splitting a text file into
例如,像这样的树: 5 / \ 3 6 / \ 7 2 print(tree.branchLenSum()) 将是1+1+2+2=6 树类: class BinaryTre
我正在学习 JavaScript。我是文档对象模型 (DOM) 主题的新手。 我对节点在 DOM 树中的布局方式感到困惑? 我使用了这个 HTML 文件。 List Buy groceries
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
var nodeEnter = node.enter().append("g") .attr("class", "node") .attr("transform", function (d) {
我正在使用数据表来表示我的应用程序中的数据。我有一个功能,可以在单击克隆图标时克隆任何行并动态添加它下面的行。 数据表具有搜索功能,可以搜索表中存在的所有行,但是它只考虑初始页面加载时加载到表中的行。
我已经用 Python 编写了一个 Tree 类,但是我在为它创建迭代器时遇到了问题我希望能够做到 phonebook = MyTree() # Build up tree for node in p
我需要为 FreeBSD 检查这个 libunwind 端口, http://people.freebsd.org/~kib/git/libunwind.git/ 我以前和当我尝试使用命令 check
我已经实现了自己的 AVL 树,并将其用作字典。我想知道,计算以某个字符串开头的所有单词的最快方法是什么。 例如: string prefix = "fa"; output: 4 我已经在 O(n)
我目前正在尝试编写一个使用 2-3-4 树的程序,但我遇到了插入函数的问题。这是相关代码.. int main () { tree234 myTree; myTree.insert("
我的同事问了我这个问题,但我无法想出任何最佳解决方案。 给定一棵具有n 个节点、n-1 条边和 q 个查询的无向加权树。 每个查询都有输入 u v k ,输出位于路径 u to v 的奇数且小于 k
如果我有一个 app.html 模板如下: ${message} test 使用 MyComponent.ts : export class MyComponent { my
我继承了一个 git 存储库,其中树中的提交条目为空 sha1,阻止 FishEye 为存储库编制索引。 $ git fsck Checking object directoriies: 100%(2
这是一个例子。我也许可以在点击函数中弄清楚它,但我不确定从点击函数外部从根开始检查所有子项的最佳方法。谢谢 .node circle { fill: #fff; stroke: stee
我正在使用 Python 批量编辑许多当前看起来像这样的 musicXML 文件: ... -5 -9
假设有一个 8 阶 B 树。这意味着它可以有 8 个指针和 7 个元素。假设字母 A 到 G 存储在这棵 B 树中。所以这棵 B 树只是一个包含 7 个元素的节点。 然后你尝试将 J 插入到树中。没有
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在尝试做一个简单的二维“物理”模拟,主要涉及圆形物体的碰撞,为了避免编程我自己的空间索引(四叉树/r-tree/等),我希望使用 Boost 的 R -树。 问题是我在 Boost 文档中找不到任
我正在从 python 字典中读取数据并尝试在下面的树中添加更多书籍元素。下面只是一个示例,我需要复制一个元素及其子元素但替换内容,在这种情况下我需要复制 book 元素但替换标题和作者。
假设我有一个变量,它包含一个 nltk 树类的树。是否有类似 parent() 或返回节点父节点的函数? 最佳答案 您需要一种不同的数据结构:一棵树,其节点包含指向其父节点的指针。 NLTK 现在提供
我是一名优秀的程序员,十分优秀!