当前位置:网站首页>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
边栏推荐
- With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?
- 什么是 Meebits?一个简短的解释
- Introduction to ROS from introduction to mastery (zero) tutorial
- Biological unlocking - Fingerprint entry process
- What is pytorch? Explain the basic concepts of pytorch
- Using com0com/com2tcp to realize TCP to serial port (win10)
- Arduino controls tb6600 driver +42 stepper motor
- Arduino uses esp8266+ lighting technology + Xiaoai audio to realize voice control switch
- What is dummy change?
- Leetcode weekly -- April to May
猜你喜欢
![[error] invalid use of incomplete type uses an undefined type](/img/8a/7cb5d270cfd8831ddc146687fe4499.png)
[error] invalid use of incomplete type uses an undefined type

MySQL lpad() and rpad() concatenate string functions with specified length

磁盘分区方式对比(MBR与GPT)

(01).NET MAUI实战 建项目

Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis

DNS attack surface analysis

Win10 home vs pro vs enterprise vs enterprise LTSC

How many steps are appropriate for each cycle of deep learning?

Arduino control tm1637 common positive four digit nixie tube

Kotlin 协程的四种启动模式
随机推荐
Using fastjson to solve the problem of returning an empty array from a null value of a field string object
[buglist] serial port programming does not read data
Basic operations of FreeMarker
@Disallowcurrentexecution prevents quartz scheduled tasks from executing in parallel
MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
AOF持久化
With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?
三角波与三角波卷积
Triangle wave and triangle wave convolution
[JS] solve the problem that removeeventlistener is invalid after the class listening event from new is bound to this
[Error] invalid use of incomplete type 使用了未定义的类型
从ADK的WinPE自己手动构建自己的PE
[JS component library] drag sorting component
A simple deadlock example
Arduino uses esp8266+ lighting technology + Xiaoai audio to realize voice control switch
Some basic design knowledge
【服务器数据恢复】存储服务器之间迁移数据时数据丢失恢复成功案例
[gxyctf2019] no dolls -- detailed explanation
[virtual machine] notes on virtual machine environment problems
1. Google grpc framework source code analysis Hello World