非递归实现二叉树的前序/中序/后序遍历——题集(十一)
今天分享一下非递归实现二叉树的前序/中序/后序遍历,以及用不含头结点的单链表存储集合A和B,求解集合A与B的差集,并将结果保存在集合A的单链表中。。
实现二叉树的前序/中序/后序非递归遍历的源代码和运行示例。
源代码如下:
#include<iostream>
using namespace std;
#include<stack>
//二叉树的前序/中序/后序非递归遍历
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x)
:val(x)
,left(NULL)
,right(NULL)
{
}
};
//前序非递归遍历
void PreTraNR(TreeNode* root){//借助栈
cout<<"前序非递归遍历: ";
if(root==NULL){
cout<<endl;
return;
}
TreeNode* cur=root;
stack<TreeNode*> tmp;
tmp.push(cur);
while(!tmp.empty()){
cur=tmp.top();
while(cur->left != NULL){
cout<<cur->val<<" ";
cur=cur->left;
tmp.push(cur);
}
cout<<cur->val<<" ";
while(cur->right==NULL){
tmp.pop();
if(tmp.empty()){
cout<<endl;
return;
}
cur=tmp.top();
}
tmp.pop();
cur=cur->right;
tmp.push(cur);
}
cout<<endl;
return;
}
//中序非递归遍历
void OrdTraNR(TreeNode* root){//借助栈
cout<<"中序非递归遍历: ";
if(root==NULL){
cout<<endl;
return;
}
TreeNode* cur=root;
stack<TreeNode*> tmp;
tmp.push(cur);
while(!tmp.empty()){
cur=tmp.top();
while(cur->left != NULL){
cur=cur->left;
tmp.push(cur);
}
cout<<cur->val<<" ";
while(cur->right==NULL){
tmp.pop();
if(tmp.empty()){
cout<<endl;
return;
}
cur=tmp.top();
cout<<cur->val<<" ";
}
tmp.pop();
cur=cur->right;
tmp.push(cur);
}
cout<<endl;
return;
}
//后序非递归遍历
void PostTraNR(TreeNode* root){//借助栈
cout<<"后序非递归遍历: ";
if(root==NULL){
cout<<endl;
return;
}
TreeNode* cur=root;
TreeNode* prev=NULL;
stack<TreeNode*> tmp;
tmp.push(cur);
while(!tmp.empty()){
cur=tmp.top();
while(cur->left != NULL){
//prev=cur;
cur=cur->left;
tmp.push(cur);
}
while(cur->right == prev || cur->right==NULL){
cout<<cur->val<<" ";
prev=cur;
tmp.pop();
if(tmp.empty()){
cout<<endl;
return;
}
cur=tmp.top();
}
cur=cur->right;
tmp.push(cur);
}
cout<<endl;
return;
}
void TestTree()//二叉树的前序/中序/后序非递归遍历
{
cout<<"二叉树的前序/中序/后序非递归遍历"<<endl<<endl;
TreeNode * pRoot1=new TreeNode(1);
TreeNode * pRoot2=new TreeNode(2);
TreeNode * pRoot3=new TreeNode(3);
TreeNode * pRoot4=new TreeNode(4);
TreeNode * pRoot5=new TreeNode(5);
TreeNode * pRoot6=new TreeNode(6);
TreeNode * pRoot7=new TreeNode(7);
TreeNode * pRoot8=new TreeNode(8);
TreeNode * pRoot9=new TreeNode(9);
pRoot1->left = pRoot2;
pRoot1->right = pRoot3;
pRoot2->left = pRoot4;
pRoot2->right = pRoot5;
pRoot3->left = pRoot6;
pRoot3->right = pRoot7;
pRoot4->left = pRoot8;
PreTraNR(pRoot1);//前序非递归遍历
OrdTraNR(pRoot1);//中序非递归遍历
PostTraNR(pRoot1);//后序非递归遍历
}
int main(){
TestTree();//二叉树的前序/中序/后序非递归遍历
system("pause");
return 0;
}
运行结果:
已知集合A和B的元素分别用不含头结点的单链表存储,函数difference()用于求解集合A与B的差集,并将结果保存在集合A的单链表中。例如,若集合A={5,10,20,15,25,30},集合B={5,15,35,25},完成计算后A={10,20,30}。
链表结点的结构类型定义如下:
struct node
{
int elem;
node* next;
};
用不含头结点的单链表存储集合A和B,求解集合A与B的差集,并将结果保存在集合A的单链表中。其函数void difference(node** LA , node* LB)的源代码和运行示例。
源代码如下:
#include<iostream>
using namespace std;
//用不含头结点的单链表存储集合A和B,求解集合A与B的差集,并将结果保存在集合A的单链表中。
struct ListNode{
int val;
ListNode* next;
ListNode(int _val)
:val(_val)
,next(NULL)
{}
};
void DifSet(ListNode** LA, ListNode* LB ){
if(LB == NULL || LA == NULL) return;
ListNode* cur=*LA;
while(LB){
cur=*LA;
ListNode* prev=NULL;
while(cur){
if(cur->val == LB->val){
if(prev)
prev->next=cur->next;
else
*LA=cur->next;
delete cur;
break;
}
prev=cur;
cur=cur->next;
}
LB=LB->next;
}
return;
}
void TestSet(){//求解集合A与B的差集
ListNode * lA1=new ListNode(1);
ListNode * lA2=new ListNode(2);
ListNode * lA3=new ListNode(3);
ListNode * lA4=new ListNode(4);
ListNode * lA5=new ListNode(5);
ListNode * lA6=new ListNode(6);
ListNode * lA7=new ListNode(7);
ListNode * lA8=new ListNode(8);
lA1->next = lA2;
lA2->next = lA3;
lA3->next = lA4;
lA4->next = lA5;
lA5->next = lA6;
lA6->next = lA7;
lA7->next = lA8;
ListNode * lB1 = new ListNode(1);
ListNode * lB2 = new ListNode(4);
ListNode * lB3 = new ListNode(6);
ListNode * lB4 = new ListNode(8);
ListNode * lB5 = new ListNode(11);
lB1->next = lB2;
lB2->next = lB3;
lB3->next = lB4;
lB4->next = lB5;
cout<<"求解不含头结点的单链表存储集合lA1与lB1的差集,"<<endl<<"并将结果保存在集合lA1的单链表中"<<endl<<endl;
cout<<"打印集合lA1: ";
Printf(lA1);
cout<<"打印集合lB1: ";
Printf(lB1);
DifSet( &lA1, lB1);
cout<<"DifSet( &lA1, lB1)->打印集合lA1与lB1的差集结果; ";
Printf(lA1);
}
int main(){
TestSet();//求解集合A与B的差集
system("pause");
return 0;
}
运行结果:
分享如上,如有错误,望斧正!愿大家学得开心,共同进步!