Tag:

从堆栈创建二叉树?

我的任务是创建一个程序,将((X+3)*(X+4))变成二叉树,以及其他一些function。 到目前为止,我已经接受了输入,并将其解析为两个堆栈,一个包含操作数,另一个包含运算符。 我现在简单地定义了堆栈(所以它们只有一个nextnode和char value 。但是,我似乎在将堆栈中的值添加到树中时遇到了问题(因此定义树时可能存在问题)。 我的堆栈定义如下: typedef struct node { char value; struct node * nextnode; } node; 我的树被定义为: typedef struct tree { node * thisNode; struct tree *right, *left; } tree; 我不确定节点*部分,也许它应该是不同的东西。 我一直在考虑初学者2 + 3的简单案例。 在这种情况下,树的根应为+,左边为2,右边为3。 + /\ 2 3 如何在堆栈中添加堆栈中的内容? 我试过用 root->thisNode = operatorTop; 其中operatorTop是运算符堆栈的顶部(定义为node * operatorTop ),但即使是那条简单的行也似乎是段错误。

为什么在这个prog中使用指针指针?

以下程序显示了如何在C程序中构建二叉树。 它使用动态内存分配,指针和递归。 二叉树是一种非常有用的数据结构,因为它允许在排序列表中进行有效的插入,搜索和删除。 因此,树本质上是递归定义的结构,递归编程是处理它的自然而有效的方式。 tree empty node left-branch right-branch left-branch tree right-branch tree 这是代码: #include #include struct tree_el { int val; struct tree_el * right, * left; }; typedef struct tree_el node; void insert(node ** tree, node * item) { if(!(*tree)) { *tree = item; return; } if(item->valval) insert(&(*tree)->left, item); else if(item->val>(*tree)->val) insert(&(*tree)->right, item); } […]

插入二进制搜索树

我做了一个二叉搜索树 struct BTNode { int info; struct BTNode *left,*right; }; 我写了一个代码来在树中插入一个节点 void insert(struct BTNode *root,int data) { struct BTNode *ptr; struct BTNode *n=malloc(sizeof(struct BTNode)); n->info=data; n->left=NULL; n->right=NULL; if(root==NULL) root=n; else{ ptr=root; while(ptr!=NULL){ if(datainfo){ if(ptr->left==NULL) ptr->left=n; else ptr=ptr->left; } else if(data>ptr->info){ if(ptr->right==NULL) ptr->right=n; else ptr=ptr->right; } } } } 和main()函数 int main() { struct BTNode *root=NULL; […]

将多分支树复制到GPU内存

我有一个节点树,我试图将其复制到GPU内存。 Node看起来像这样: struct Node { char *Key; int ChildCount; Node *Children; } 我的复制function如下所示: void CopyTreeToDevice(Node* node_s, Node* node_d) { //allocate node on device and copy host node cudaMalloc( (void**)&node_d, sizeof(Node)); cudaMemcpy(node_d, node_s, sizeof(Node), cudaMemcpyHostToDevice); //test printf(“ChildCount of node_s looks to be : %d\n”, node_s->ChildCount); printf(“Key of node_s looks to be : %s\n”, node_s->Key); Node *temp; […]

何时将指针作为参数传递给结构,何时将指针传递给指向结构的指针?

我的问题是关于以下代码。 #include #include struct node { int v; struct node * left; struct node * right; }; typedef struct node Node; struct bst { Node * root; }; typedef struct bst BST; BST * bst_insert(BST * tree, int newValue); Node * bst_insert_node(Node * node, int newValue); void bst_traverseInOrder(BST * tree); void bst_traverseInOrderNode(Node * node); int […]

使树水平增长,应用对当前节点的修改

给定一个输入,我正在尝试构建一个树,该树应该水平增长,将变换应用于该输入和随后的子节点。 例如,给定输入’aab’和两个转换规则,如: ab -> bba b -> ba 需要构建这样的树: 我已经编写了代码,但是我已经完成了它,我的树垂直工作,我不想那样做。 我需要它水平工作,我不知道我将在何处/如何编写递归。 这就是我现在所拥有的: #include #include #include #include typedef struct t_string_node { struct t_string_node *next; char *value; } string_node; typedef struct t_transformation_rule { struct t_transformation_rule *next; char *needle; char *replacement; } transformation_rule; void findTransformations(char *origin, string_node **transformations, char *needle, char *replacement) { char *str = origin; for […]

为什么这个搜索函数会返回一个指向指针的指针?

#ifndef _BST_H_ /* Returns negative (leftright). */ typedef int comparator(void* left, void* right); struct bst_node { void* data; struct bst_node* left; struct bst_node* right; }; struct bst_node* new_node(void* data); void free_node(struct bst_node* node); struct bst_node** search(struct bst_node** root, comparator compare, void* data); void insert(struct bst_node** root, comparator compare, void* data); void delete(struct bst_node** node); #endif […]

C和字符串处理

完全代码http://pastebin.com/6bdVTyPt我的树代码完全正常工作,直到我发现我需要validation其不是文本的ID所以它必须是字符串插入函数字符串比较90和129返回8试图使用(atoi)和比较整数不起作用任何帮助赞赏 谢谢你inheritance使用atoi而不是strcomp的插入functionhttp://pastebin.com/yeuktyAF仍然没有工作插入function struct node * insert2(struct node *root, char x[],char id[]) { if(!root) { root=(struct node*)malloc(sizeof(struct node)); free( root->data ); free( root->id );// free previously allocated memory, if any root->data = strdup( x ); // malloc and copy root->id=strdup(id); root->left = NULL; root->right = NULL; // printf(“1\n”); return(root); } printf(“string comp %d of %s of […]

C – 使用后序遍历释放二叉树的内存

我想使用后序遍历删除二叉树 。 这意味着应首先删除树的左侧部分, 然后删除右侧的树,然后在后面的第二个函数中删除整个树并释放内存 。 我不允许更改函数的参数,只能使用它的内部: #include #include #include #include “telefonbuch.h” static inline bstree * create_node(unsigned long phone, char * name) { bstree * newNode = (bstree *) malloc(sizeof(bstree)); newNode->key.phone = phone; strcpy(newNode->key.name, name); newNode->left = NULL; newNode->right = NULL; return newNode; } void bst_insert_node(bstree * bst, unsigned long phone, char * name) { if […]

创建递归二叉树?

我有两个堆栈,一个用操作数,另一个用操作符。 我的问题是将这两个堆栈变成二叉树。 例如,表达式(2+3)*(4-3)将被转换为后缀(例如24+43-* ),然后放入两个堆栈3442和*-+将成为堆栈(顶部为分别为3和*)。 现在使用这些堆栈,我需要形成一个像二进制树 * + – 2 3 4 3 有没有办法递归地做到这一点? 现在,我有一个像这样的算法: 创建树的根,将根的值分配给operator-stack中的第一个运算符。 将右指针和左指针设置为null。 创建正确的节点,如果存在,则分配下一个运算符的值,如果不为其分配操作数。 然后对左节点执行相同操作。 我的问题是使这个递归,或让它来处理许多不同的情况。 谢谢你的帮助。