- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是一个 c++ 学习者,我正在尝试为此表达式创建一个 BST 树:2346*+/8+,并执行中序和后序以获得表达式的修复版本和后修复版本。我很难为表达式创建二叉树。这是我的比索代码:
Tree:
Stack:
inorder fn{}
postorder fn{}
main{
input the file;
while(expression){
if(digit){
s.push}
else if(operator){
s.top = right;
s.pop;
s.top = left;
s.pop;
T1 = new Tree(operator, left, right);
}
}
我要创建的树是这样的
+
/ \
(/) 8
/ \
+ 2
/ \
* 3
/ \
4 6
此代码的问题是在创建 (4*6) 树之后,我无法将 (+3) 链接到 (4*6)。请帮助我。
感谢 Drew McGowen,我更新了我的代码,现在我将 4*6 树推回堆栈,这是代码:
while(input_file >> expression){
if(isdigit(expression[0])){
sscanf(expression,"%c",&digit);
printf("reading a number: %c \n",digit);
Tree* s.push(digit);
}
else {
sscanf(expression,"%c",&oper);
printf("reading an operator: %c \n",oper);
T1 = new Tree(s.top(), NULL, NULL);
s.pop();
T2 = new Tree(s.top(), NULL, NULL);
s.pop;
myTree = new Tree(oper, T1, T2);
s.push(myTree);
我不断收到错误消息,有人可以帮我检查一下代码吗?谢谢大家。
大家好,我认为主要部分是在正确的轨道上,但我应该如何修改堆栈函数以接受树?这是我的堆栈函数:
void Stack::Push(char newthing) {
index++;
data[index] = newthing;
}
void Stack::Pop() {
if (index > -1) { index--; }
}
char Stack::Top() {
return data[index];
最佳答案
您的堆栈似乎包含数字。为了将后缀表示法中的表达式解析为堆栈,堆栈应包含树元素。
伪代码:
main{
input the file;
while(expression){
if(digit){
s.push(new Tree(digit)); //create leaf-element
}
else if(operator){
Tree tr = s.pop();
Tree tl = s.pop();
T1 = new Tree(operator,tl,tr);
}
}
这或多或少也是 L(AL)R 解析器的工作方式。
关于您的代码:
T2
和T1
,因为您是从堆栈中弹出;T1
和T2
应该是简单的弹出操作,没有构造函数调用;和你应该构建一个数字大小写的树节点(叶子)。
while(input_file >> expression){
if(isdigit(expression[0])){
sscanf(expression,"%c",&digit);
printf("reading a number: %c \n",digit);
s.push(new Tree(digit,NULL,NULL)); //create new leaf
}
else {
sscanf(expression,"%c",&oper);
printf("reading an operator: %c \n",oper);
T2 = s.top(); //T2 instead of T1 and simple pop
s.pop();
T1 = s.top(); //T1 instead of T2 and simple pop
s.pop;
myTree = new Tree(oper, T1, T2);
s.push(myTree);
}
}
如果输入流是有效的(例如不是 1+
或 12+6
),结果是一个包含一个元素的堆栈:表达式树。
关于c++ - 如何为算术表达式创建 BST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27956645/
我正在尝试从 1 循环到 12,并为应用中特定 View 的更改网格输出一些跨度宽度。 $span-width: 8.21875%; $gap: 0.125%; @for $i from 1 thro
我试图在 Jekyll 的液体模板引擎中做一些基本的算术。我已经分配了一个变量 numColumns我试图在条件语句中使用它。 {% assign numColumns = 3 %} 注意我在下面的表
与 shift_left ieee.numeric_std 的功能, 我想将信号左移并插入 1或 0从右边。 signal qo: signed (3 downto 0) := (others=>'0
您在控制台中输入一些内容,例如(8+8)。然后程序会告诉你括号的插入是否正确。 这是我对错误括号的定义(当然还没有完成): () this means if one array element is
我有两个表(使用 PostgreSQL),它们看起来如下: 表1(p点从1到450递增1) --------+-------+--------+---------+---------+-------+
我正在编写一个任意精度的有理数包,我需要测试它的正确性和效率。当然,我可以自己组合一组临时测试,但由于我远不是第一个这样做的人,所以我认为值得一问:任何人都可以推荐我可以使用的现有测试集吗? 编辑:我
我最近一直在使用和学习 CSS3,并享受它的许多功能。现在我想知道是否可以设置一个有条件地分配 block 元素宽度的 CSS 规则。我所追求的那种东西 - 如果屏幕宽度小于 500 像素,则使用 3
我对这个实验中h的值有点疑惑。在 cpp 中, int h,J=3,n=200,p=3,h_m=(n+p+1)/2; float rt=(float)h_m/n; for(int j=0,j
算术+和按位或有什么区别吗?这有什么不同。 uint a = 10; uint b = 20; uint arithmeticresult = a + b; uint bitwiseOR = a |
我一直在尝试让算术 if 运算符起作用,但我似乎做不到。我是 C++ 的新手,仍在学习基础知识,但我只是想知道我是否正确使用了这个运算符。如果 x using namespace std; int
我在 VC++2010 中做过一些混合不同大小的操作数导致添加操作溢出的测试: int _tmain(int argc, _TCHAR* argv[]) { __int8 a=127;
#include int main(int argc,char *argv[]) { int i=10; void *k; k=&i; k++; printf("%p\n
在过去的 5 个小时里,我一直在寻找答案。尽管我找到了很多答案,但它们并没有以任何方式提供帮助。 我基本上要寻找的是任何 32 位无符号整数的按位异或运算符的数学、算术唯一表示。 尽管这听起来很简单,
结果是 127 double middle = 255 / 2 虽然这产生了 127.5 Double middle = 255 / 2 同时这也会产生 127.5 double middle = (
我在 Java 1.7 中有以下代码: DateFormat df = DateFormat.getInstance(); Date startDate = df.parse("07/28/12 01
此查询有效,没有错误 select add_months(date '2011-01-31', 1) from dual; ,而这个: select date '2011-01-31' + inter
理论上来说,如果我有一个无序项目列表 Link1 Link1 我如何使用 jQuery 执行以下操作? 1) 找到每个单独a元素的宽度 2) 找到每个单独的 li 元素的宽度 3)
想法如下:假设我有一个列表 P = [(1,0),(4,3)] 或类似的列表。我想以以下方式计算此列表定义的多项式:1X^0 + 4X^3。 为此,我编写了以下内容: evaluate(P,X,Y)
我正在从 mysql 数据库中提取数据。我想添加多次运行的长度,并按照跑得最远的人的排名对它们进行排序。 function determineLength($db, $name){
当尝试执行一个简单的 bash 脚本以将前面带有 0 的数字递增 1 时,原始数字被错误地解释。 #!/bin/bash number=0026 echo $number echo $((number
我是一名优秀的程序员,十分优秀!