当前位置:网站首页>Expression tree - medium order printout
Expression tree - medium order printout
2022-06-13 00:48:00 【Feiyichai】
Give an expression tree , Output the middle order expression , You need parentheses to prioritize ( It looks like it is. 408 The real question , I feel I haven't learned recursion well , It can only be realized by stack , I don't know , Ask God for correction )

The expression shown in the figure , Output is (a+(b*(-d)-(e/f))
- Algorithm analysis
( One ) If the current node is a leaf node on the left subtree , Output left parenthesis and data
( Two ) If the current node is the root node, output data directly
( 3、 ... and ) If the current node has a right subtree but the left subtree is empty , Then the left bracket and root node data are output
( Four ) If the current node is a leaf node on the right subtree , Output right parentheses and data
- Code implementation
// The idea of non recursive middle order traversal
void print(Bitree T){
if(!T) return ;// Tree is empty exit function
Stack s; // Declare a stack
BiTNode *p=T;
int flag=1; // The flag that determines whether the current subtree is on the left or right
print('(');
while(p||!IsEmpty(s)){
// The current node is not empty or the stack is not empty
if(p){
push(s,p); // Zuozi tree is not empty , Continue to traverse the left subtree
p=p->lchild;
flag=1; // Flag that the current traversal is the left subtree
}
else{
// Left to the bottom
pop(s,p);// Out of the stack ;
if((!p->lchild&&!p->rchild&&flag))||(!p->lchild&&p->rchild)){
// The first and fourth cases
print('(');
print(p->data);
}
else if(p->lchild&&p->rchild)// The second case
print(p->data);
else if(!flag){
// The third case
print(')');
print(p->data);
}
p=p->rchild;// Go right after judgment
flag=0;
}//while
print(')');
}//print
边栏推荐
猜你喜欢

也许尘埃落地,我们才能想清楚互联网的本质

ROS从入门到精通(零) 教程导读

Win10 home vs pro vs enterprise vs enterprise LTSC

(01). Net Maui actual construction project

pytorch和tensorflow有什么区别?

ROS2之OpenCV人脸识别foxy~galactic~humble

MySQL queries the quantity of each month and the year-on-year and month on month data of each month

Map from getting started to performance optimization
![[MRCTF2020]Ez_bypass --BUUCTF](/img/73/85262c048e177968be67456fa4fe02.png)
[MRCTF2020]Ez_bypass --BUUCTF
![[error] invalid use of incomplete type uses an undefined type](/img/8a/7cb5d270cfd8831ddc146687fe4499.png)
[error] invalid use of incomplete type uses an undefined type
随机推荐
How many rounds of deep learning training? How many iterations?
Stack overflow learning summary
Cards are unpredictable
Zhouchuankai, Bank of Tianjin: from 0 to 1, my experience in implementing distributed databases
MySQL query table field information
硬(磁)盘(二)
Programming training 1
三角波与三角波卷积
Androi天氣
浏览器控制台注入JS
Hard (magnetic) disk (I)
Easyexcel read excel simple demo
[buglist] serial port programming does not read data
Card constructions -- two points
Win10 home vs pro vs enterprise vs enterprise LTSC
antdPro - ProTable 实现两个选择框联动效果
.net core 抛异常对性能影响的求证之路
Introduction to ROS from introduction to mastery (zero) tutorial
[North Asia server data recovery] data recovery case of Hyper-V service paralysis caused by virtual machine file loss
蓝桥杯单片机第七届决赛