创建一个堆栈二叉树? [英] Create a binary tree from a Stack?
问题描述
我与创建一个程序,它变成像任务((X + 3)*(X + 4))到一个二叉树,以及一些其他
特征。到目前为止,我已在输入,并解析其成两叠,含有该操作数之一,另外的操作符。
I'm tasked with creating a program that turns something like ((X+3)*(X+4))
into a binary tree, along with some other features. So far, I have taken in the input, and parsed it into two stacks, one containing the operands, the other the operators.
我所定义的协议栈只是现在(所以他们只有一个nextnode和 char值
。
但是,我似乎有增加从烟囱值代入我的树的问题(因此可能是在定义树的问题)。
I defined the stacks simply for now (so they only have a nextnode and char value
.
However, I seem to have problems adding values from the stacks into my tree (so probably a problem in defining the tree).
我的栈的定义是这样的:
My stack is defined as such:
typedef struct node
{
char value;
struct node * nextnode;
} node;
我的树被定义为:
My tree is defined:
typedef struct tree
{
node * thisNode;
struct tree *right, *left;
} tree;
我不知道有关节点*的一部分,或许它应该是不同的东西。
I'm not sure about the node* part, perhaps it should be something different.
我一直在考虑的2 + 3对于初学者的简单情况。在这种情况下,树的根应+与左为2和右为3
I've been considering the simple case of 2+3 for starters. In this case, the root of the tree should be +, with left being 2 and right being 3.
+
/\
2 3
如何添加的东西,这是一个堆到我的树?我已经尝试使用
How to add something that's on a stack to my tree? I have tried using
root->thisNode = operatorTop;
在哪里operatorTop是操作堆栈的顶部(定义为节点* operatorTop
),
但即使这样简单的线条似乎出现段错误。
Where operatorTop is the top of the operator stack (defined as node * operatorTop
),
but even that simple line seems to segfault.
推荐答案
也许问题是你有没有叫的malloc()
来预留<$ C $空间C>根。
Maybe the problem is you have not called malloc()
to reserve the space for root
.
(编译器会报空指针赋值,但段错误的零点解引用,因为你是指向一个随机地点)。
(Compilers will report null pointer assignment, but segfault on null point dereference because you are pointing to a random place.)
这篇关于创建一个堆栈二叉树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!