当前位置:网站首页>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
边栏推荐
猜你喜欢
![[MRCTF2020]Ez_bypass --BUUCTF](/img/73/85262c048e177968be67456fa4fe02.png)
[MRCTF2020]Ez_bypass --BUUCTF
![[ciscn2019 North China Day2 web1]hack world --buuctf](/img/1e/ebf8cc06ba620261f03f84ca2c68e6.png)
[ciscn2019 North China Day2 web1]hack world --buuctf

Kali system -- dnsmap for DNS collection and analysis
![[buglist] serial port programming does not read data](/img/bf/8e63f679bf139fbbf222878792ae21.jpg)
[buglist] serial port programming does not read data

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

(01). Net Maui actual construction project

Learning and Development notes of mongdb

牌好不好无法预料
![[JS component library] drag sorting component](/img/f9/4090b52da1a5784b834cb7dbbb948c.jpg)
[JS component library] drag sorting component

Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis
随机推荐
ImportError: cannot import name 'get_ora_doc' from partially initialized module
Kali system -- fierce of DNS collection and analysis
ROS从入门到精通(零) 教程导读
Blinker FAQs
Maybe we can figure out the essence of the Internet after the dust falls
Antdpro - protable realizes the linkage effect of two selection boxes
[JS component] floating text
ImportError: cannot import name ' get_ ora_ doc' from partially initialized module
[JS component] dazzle radio box and multi box
Oceanbase is the leader in the magic quadrant of China's database in 2021
三栏简约typecho主题Lanstar/蓝星typecho主题
Kali system -- dnsmap for DNS collection and analysis
Druid reports an error connection holder is null
Arduino control tm1637 common positive four digit nixie tube
Buuctf babyupload[gxyctf2019]
ROS2之OpenCV人脸识别foxy~galactic~humble
Solution to the problem of closing the watchdog of STM32 in the sleep mode (stop/standby)
MySQL query table field information
[imx6ull] video monitoring project (USB camera +ffmepeg)
为什么磁盘分区的时候,第一个分区前面总有一段空间(63或者2048个扇区)