tree 相关问题

树是一种广泛使用的数据结构,它模拟具有一组链接节点的分层树状结构。

Vue js 3 - 当多个值相似时,我的 Json 或对象树查看器有重复的键错误

我对这段代码有一个问题:我试图从一个对象创建一个树查看器,它工作得很好,直到一个值与另一个值完全相同时,它克隆了键并替换了相同的键。 .

回答 1 投票 0

为什么堆几乎是完全二叉树?

我在很多书中读到,二叉堆几乎是完整的二叉树。据我所知,几乎完全二叉树的最后第二层始终未填充,并且从左到右。并且,完整的二进制...

回答 1 投票 0

区间树算法实现

我最近在http://www.geeksforgeeks.org/interval-tree/上完成了间隔树的实现,这里的算法建议在每个子树上使用最大值。 以及寻找的算法

回答 1 投票 0

如何在树结构中整齐地关联下一个和上一个“节点”?

假设我有一个简单的树结构: { 标签:'a', 孩子们: [ { 标签: 'a.a', 孩子们: [ { 标签:'a.a.a', 孩子们: [ { 我...

回答 2 投票 0

ECharts树:如何在树的中间绘制水平分割线

如何从树的左边缘到右边缘有一条分割线,如下使用 ECharts 的分割线: 目前,下面的 HTML 和 JavaScript 代码可以生成没有分割线的树,但是......

回答 1 投票 0

树形图到有向图/树

我正在尝试将树形图转换为图/树,以对其节点、叶子和子树执行计算并找到混合子图,但我还没有找到可以帮助我的函数/包...

回答 1 投票 0

理解递归二叉树操作中的代码行为和变量名称

我正在研究 LeetCode 问题 1325,“删除具有给定值的叶子”,并且我在代码中遇到了一个我正在尝试理解的问题。 在我的代码中,我正在实现递归

回答 1 投票 0

Python BST Range Sum 函数不返回预期结果将使用局部变量

我目前正在开发一个Python函数,该函数应该计算给定范围内二叉搜索树(BST)中的值的总和。但是,我的代码似乎没有按预期工作......

回答 1 投票 0

如何在Vega JS中实现树节点切换?

我正在使用 Vega JS 构建树形图。总的来说,我的问题如下: Vega 文档有一个很好的树布局示例。我怎样才能扩展它并具有折叠和电子的能力...

回答 3 投票 0

使用文本文件的输入在java中构建树

我正在尝试使用文件中的数据构建任意大小的树。在文件中,每个节点都是自己的行,分支由某些关键字分隔。目前,我正在阅读该文件...

回答 1 投票 0

程序树生成器中的问题。 C# 统一

我目前正在开发程序地形生成器,在场景中程序生成树木时遇到了问题。 Unity 给了我一个错误,但我真的不知道如何处理...

回答 1 投票 0

如何添加概率来评估行为树节点?

我正在 Unity 中开发一款游戏,并决定使用 BehaviourTrees 来管理敌人 AI。 其中一名敌人有能力躲避玩家的攻击。然而,这种能力应该只发生少数

回答 1 投票 0

通过中序和先序遍历构造二叉树

我有以下序列: 中序:{4,2,5,1,6,3} 预购:{1,2,4,5,3,6} 我想了解如何从这些序列构建二叉树以及为什么可以用这个

回答 1 投票 0

如何根据“parentOf”关系对有根树进行拓扑排序?

我有一个 N 节点有根树,其节点标记为 0 到 N-1。我得到了parentOf关系,也就是说,对于每个节点i,parentOf[i]是单个节点j,使得j是i的父节点。我...

回答 1 投票 0

将公式作为有向树进行导航以查找符号的所有出现

在R中,公式可以看作一棵树,其中父节点位于索引[[1]]上,左右项位于后续索引中。 例如,公式 f 定义为: <- y ~ (a*...

回答 1 投票 0

CTreeCtrl 项目的边界矩形

过去我们使用 Stingray 树控件来描绘地图的图例。 现在我们要应用 MFC CTreeCtrl 来完成这项工作。 Stingray 控件的优点是可以轻松更改

回答 1 投票 0

构建红黑树过程中的问题

我在构建红黑树时遇到了问题。 insertFixUp等过程中出现分段错误。我不知道该怎么办。 #包括 #包括 我在构建红黑树时遇到了问题。 insertFixUp等过程中出现分段错误。我不知道该怎么办。 #include <stdio.h> #include <stdlib.h> #include <time.h> struct tree_node { int key; int color; struct tree_node *parent; struct tree_node *left_child; struct tree_node *right_child; }; struct tree { int cardinality; struct tree_node *root; struct tree_node *Nil; }; struct tree_node *getNewNode (int keys){ struct tree_node* newNode = (struct tree_node*) malloc(sizeof(struct tree_node)); newNode-> key = keys; newNode-> color = 0; newNode-> parent = NULL; newNode-> right_child = NULL; newNode-> left_child = NULL; return newNode; } void TreeLeftRotate(struct tree *T, struct tree_node *x) { struct tree_node *y = x->right_child; x->right_child = y->left_child; if (y->left_child != T->Nil) { y->left_child->parent = x; } if (x->parent == T->Nil) { T->root = y; } else if (x == x->parent->left_child) { x->parent->left_child = y; } else { x->parent->right_child = y; } y->left_child = x; x->parent = y; } struct tree_node* BSTTreeMinimum(struct tree_node *x){ if(x->left_child== NULL){ return x;} else{ return BSTTreeMinimum(x->left_child);} }; struct tree_node *BSTTreeSearch(struct tree_node *x, int k){ if ((x==NULL)||(x->key=k)){ return x; } if(k<=x->key){ return BSTTreeSearch(x->left_child, k); } else { return BSTTreeSearch(x->right_child, k); } } void BSTTreeInsert(struct tree *T, struct tree_node *z) { struct tree_node *x = NULL; struct tree_node *y = NULL; x = T->root; while (x!=NULL){ y=x; if(z->key <= x->key){ x=x->left_child; } else { x=x->right_child; } } z->parent =y; if(y==NULL){ T->root = z; } if((y!=NULL)&&(z->key <= y->key)) { y->left_child = z; } if((y!=NULL)&&(z->key > y->key)) { y->right_child = z; } } void BSTTreeTransplant(struct tree *T, struct tree_node *u, struct tree_node *v){ if (u->parent==NULL){ T->root = v; } if((u->parent != NULL)&&(u=u->parent->left_child)){ T->root = v; } if((u->parent!=NULL)&&(u=u->parent->right_child)){ u->parent->right_child=v; } if(v!=NULL){ v->parent = u->parent; } } void BSTTreeDelete(struct tree *T, struct tree_node *z){ struct tree_node *y = NULL; if (z->left_child == NULL){ BSTTreeTransplant(T,z,z->right_child); } if((z->left_child != NULL) && (z->right_child == NULL)){ BSTTreeTransplant(T,z,z->left_child); } if((z->left_child != NULL)&&(z->right_child != NULL)){ y=BSTTreeMinimum(z->right_child); if (y->parent != z){ BSTTreeTransplant(T,y,y->right_child); y->right_child = z->right_child; y->right_child->parent =y; } BSTTreeTransplant(T,z,y); y->left_child = z->left_child; y->left_child->parent = y; } } void TreeRightRotate(struct tree *T, struct tree_node *x) { struct tree_node *y = x->left_child; x->left_child = y->right_child; if (y->right_child != T->Nil) { y->right_child->parent = x; } if (x->parent == T->Nil) { T->root = y; } else if (x == x->parent->right_child) { x->parent->right_child = y; } else { x->parent->left_child = y; } y->right_child = x; x->parent = y; } void RBTreeInsertFixUpLeft(struct tree*T, struct tree_node *z){ struct tree_node *y = z->parent->parent->right_child; if (y->color == 1) { z->parent->color = 0; y->color = 0; z->parent->parent->color=1; z=z->parent->parent; } else{ if (z==(z->parent->right_child)){ z=z->parent; TreeLeftRotate(T,z); } z->parent->color = 0; z->parent->parent->color=1; TreeRightRotate(T,z->parent->parent); }; }; void RBTreeInsertFixUpRight(struct tree*T, struct tree_node *z){ struct tree_node *y = z->parent->parent->left_child; if (y->color == 1) { z->parent->color = 0; y->color = 0; z->parent->parent->color=1; z=z->parent->parent; } else{ if (z==z->parent->left_child){ z=z->parent; TreeRightRotate(T,z); } z->parent->color = 0; z->parent->parent->color=1; TreeLeftRotate(T,z->parent->parent); }; }; void RBTreeInsertFixup(struct tree *T, struct tree_node *z){ while (z->parent->color = 1){ //segmentation fault if(z->parent == z->parent->parent->left_child){ RBTreeInsertFixUpLeft(T,z); } else { RBTreeInsertFixUpRight(T,z); } } T->root->color=0; }; void RBTreeInsert(struct tree *T, struct tree_node *z) { struct tree_node *y = T->Nil; struct tree_node *x = T->root; while (x!=T->Nil){ y=x; if (z->key < x->key) { x=x->left_child; } else { x=x->right_child; } } z->parent = malloc(sizeof(struct tree_node)); z->parent = y; if (y==T->Nil){ T->root = z; } if((y!=T->Nil)&&(z->key < y->key)){ y->left_child=z; } if ((y!=T->Nil)&&(z->key >= y->key)){ y->right_child = z; } z->left_child = T->Nil; z->right_child = T->Nil; z->color = 1; RBTreeInsertFixup(T,z); }; void RBTransplant(struct tree *T, struct tree_node *u, struct tree_node *v){ if (u->parent == T->Nil) { T->root = v; } else if (u==u->parent->left_child){ u->parent->left_child = v; } else { u->parent->right_child=v; } v->parent = u->parent; }; void RBDeleteFixup(struct tree *T, struct tree_node *x){ if (x == x->parent->left_child){ struct tree_node *w = x->parent->right_child; if (w->color == 1) { w->color = 0; x->parent->color = 1; TreeLeftRotate(T,x->parent); w=x->parent->right_child; } if (w->left_child->color == 0 && w->right_child->color == 0){ w->color = 1; x = x->parent; } else if (w->right_child->color == 0){ w->left_child->color = 0; w->color = 1; TreeRightRotate(T,w); w = x->parent->right_child; } w->color = x->parent->color; x->parent->color = 0; w->right_child->color = 0; TreeLeftRotate(T,x->parent); x=T->root; } else { struct tree_node *w = x->parent->left_child; if (w->color == 1) { w->color = 0; x->parent->color = 1; TreeRightRotate(T,x->parent); w=x->parent->left_child; } if (w->right_child->color == 0 && w->left_child->color == 0){ w->color = 1; x = x->parent; } else if (w->left_child->color == 0){ w->right_child->color = 0; w->color = 1; TreeLeftRotate(T,w); w = x->parent->left_child; } w->color = x->parent->color; x->parent->color = 0; w->left_child->color = 0; TreeRightRotate(T,x->parent); x=T->root; } } void RBDelete(struct tree *T, struct tree_node *z) { struct tree_node *x = NULL; struct tree_node *y = z; int y_original_color = y->color; if (z->left_child == T->Nil){ x = z->right_child; RBTransplant(T,z,z->right_child); } else if(z->right_child == T->Nil){ x = z->left_child; RBTransplant(T,z,z->left_child); } else { y=BSTTreeMinimum(z->right_child); y_original_color = y->color; x = y->right_child; if (y->parent == z){ x->parent = y; } else { RBTransplant(T,y,y->right_child); y->right_child = z->right_child; y->right_child->parent = y; } RBTransplant(T,z,y); y->left_child = z->left_child; y->left_child->parent = y; y->color = z->color; } if (y_original_color == 0) { RBDeleteFixup(T, x); } } double SingleExperimentBST (int max_keys, int max_search, int max_delete, int max_instances) { int t_tot = 0; int t_elapsed = 0; clock_t t_start = 0, t_final = 0 ; int A[max_keys]; int B[max_search]; int C[max_delete]; struct tree_node *z =NULL; z = malloc(sizeof(struct tree_node)); struct nodo *x =NULL; x = malloc(sizeof(struct tree_node)); for (int instance= 1; instance <= max_instances; instance++){ for (int j= 0; j < max_keys; j++){ A[j] = 1 + rand() % 100; } struct tree *T= NULL; T=malloc(sizeof(struct tree)); t_start = clock(); for (int key =1; key<= max_keys; key++){ struct tree_node* z = getNewNode(A[key]); z->parent = malloc(sizeof(struct tree_node)); z->parent = getNewNode(A[key]); BSTTreeInsert(T, z); } for (int key = 1; key<= max_search; key++){ B[key] = 1 + rand() % 100; int k= B[key]; struct tree_node *x = T->root; BSTTreeSearch(x, k) ; } for (int key = 1; key<= max_delete; key++){ C[key] = 1 + rand() % 100; struct tree_node* z1 = getNewNode(C[key]); BSTTreeDelete(T, z1) ; } t_final = clock(); t_elapsed = t_final - t_start; t_tot = t_tot + t_elapsed; T->root =NULL; } t_final= t_tot / max_keys; return t_final; } double SingleExperimentRBT (int max_keys, int max_search, int max_delete, int max_instances){ int t_tot = 0; int t_elapsed =0; clock_t t_start = 0, t_end = 0 ; int A[max_keys]; int B[max_search]; int C[max_delete]; struct tree_node *z =NULL; z = malloc(sizeof(struct tree_node)); struct tree_node *x =NULL; x = malloc(sizeof(struct tree_node)); for (int instance = 1; instance <=max_instances; instance++){ struct tree *T= NULL; T=malloc(sizeof(struct tree)); for (int j= 0; j < max_keys; j++){ A[j] = 1 + rand() % 100; } t_start = clock(); for (int key=1; key<=max_keys; key++) { struct tree_node *z = getNewNode(A[key]); RBTreeInsert(T, z); } for (int key = 1; key<= max_search; key++){ B[key] = 1 + rand() % 100; int k= B[key]; struct tree_node *x = T->root; BSTTreeSearch(x, k) ; } for (int key = 1; key<= max_delete; key++){ C[key] = 1 + rand() % 100; struct tree_node* z1 = getNewNode(C[key]); RBDelete(T, z1) ; } t_end = clock(); t_elapsed = t_end + t_elapsed; t_tot = t_tot + t_elapsed; T->root = NULL; } return t_tot/max_keys; } void Experiment(int min_keys, int max_keys){ time_t t; int step = 10; int max_instances = 5; int percentage_search = 60; for (int keys = min_keys; keys<=max_keys; keys+step){ srand((unsigned) time(&t)); double max_search = keys*percentage_search/100; double max_delete = keys - max_search; double timeBST =SingleExperimentBST(keys, max_search, max_delete, max_instances); srand((unsigned) time(&t)); double timeRBT = SingleExperimentRBT(keys, max_search, max_delete, max_instances); printf("%f\t\t\t%f\n", timeBST, timeRBT); t = t + 1; } } int main() { int min_key = 10; int max_key = 150; Experiment(min_key, max_key); return 0; } 此代码应该计算 RB Tree 上的插入、删除、研究操作的时间,但它不打印任何内容。代码相当复杂,如果还有其他错误,请告诉我。编译期间它不显示任何内容。调试时出现分段错误。 while (z->parent->color = 1){ //segmentation fault 这是作业=,而不是比较==。 此代码有错误 void RBTreeInsertFixUpLeft(struct tree*T, struct tree_node *z){ struct tree_node *y = z->parent->parent->right_child; if (y->color == 1) { 有可能刚插入一个节点时,父节点存在,但叔节点可能不存在。它可能为 NULL。所以 y 可能为 NULL 但是如果为 NULL,则节点的“颜色”为黑色,如果为非 NULL,则节点->颜色 您应该编写一个返回节点颜色的内联函数 bool IsBlack(const tree_node *curr) { return (curr == NULL || curr->color == 0); } 并使用它。 请注意,删除修正也会发生相同的情况。 如果删除一个叶子黑色节点并且它有父节点,那么兄弟节点存在并且必须是黑色的,它是一个真正的节点。但兄弟姐妹的孩子可能不存在,但他们应该被视为黑人。您的删除代码必须考虑到这一点。

回答 2 投票 0

寻找阿伯茨福德树木服务的建议

我在阿伯茨福德,需要聘请阿伯茨福德树木服务公司来解决我院子里的一些与树木相关的问题。有人可以推荐阿伯茨福德地区可靠的树木服务吗? 我很具体...

回答 1 投票 0

给定n个叶子生成所有可能的二叉树

因此,正如标题所示,任何人都拥有/知道一种算法(如果可能的话,用java)来生成所有可能的二叉树,给定叶子数量,如下面第二个链接的示例所示? ` 不...

回答 1 投票 0

在 Ocaml 中检测无向图中的循环

有人知道如何检测 OCaml 中无向图中是否存在循环吗? 这是我用于图表的类型: 输入 '图表 = { 节点:'列表; 边 : ('a * 'a * int) 列表 } ...

回答 3 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.