#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
int data;
struct node *lc,*rc;
};
struct Node{
bool first;
node *a;
};
class bintree{
private:
node *root;
int num;
node* create(node *root,int &num);
void preorder(node *root);
void inorder(node *root);
void postorder(node *root);
void road_(node *root);
public:
bintree(){root=NULL; num=0;}
~bintree();
void bincreate();
void pre_order();
void in_order();
void post_order();
void road();
int nums();
};
bintree::~bintree()
{
stack<node *> s;
while(root!=NULL || !s.empty())
{
if(root!=NULL)
{
s.push(root);
root=root->lc;
}
else if(!s.empty())
{
node *tep=s.top();
s.pop();
root=tep->rc;
free(tep);
}
}
}
node *bintree::create(node *root,int &num)
{
int n;
scanf("%d",&n);
if(n==0)
return NULL;
else
{
root=new node;
root->data=n;
num++;
root->lc=create(root->lc,num);
root->rc=create(root->rc,num);
return root;
}
}
void bintree::bincreate()
{
root=create(root,num);
}
void bintree::preorder(node *root)
{
stack<node *> s;
while(root!=NULL || !s.empty())
{
if(root)
{
cout<<root->data<<" ";
s.push(root);
root=root->lc;
}
else
{
node *tep=s.top();
s.pop();
root=tep->rc;
}
}
}
void bintree::pre_order()
{
cout<<"preorder\n";
preorder(root);
cout<<"end\n\n";
}
void bintree::inorder(node *root)
{
stack<node *> s;
while(root!=NULL || !s.empty())
{
if(root)
{
s.push(root);
root=root->lc;
}
else
{
node *tep=s.top();
s.pop();
cout<<tep->data<<" ";
root=tep->rc;
}
}
}
void bintree::in_order()
{
cout<<"inorder\n";
inorder(root);
cout<<"end\n\n";
}
void bintree::postorder(node *root)
{
Node tep;
stack<Node> s;
while(root!=NULL || !s.empty())
{
if(root!=NULL)
{
while(root!=NULL)
{
tep.first=true;
tep.a=root;
s.push(tep);
root=root->lc;
}
}
else if(!s.empty())
{
tep=s.top();
s.pop();
if(tep.first==true)
{
tep.first=false;
s.push(tep);
root=tep.a->rc;
}
else
{
cout<<tep.a->data<<" ";
root=NULL;
}
}
}
}
void bintree::post_order()
{
cout<<"postorder\n";
postorder(root);
cout<<"end\n\n";
}
int bintree::nums()
{
return num;
}
void bintree::road()
{
road_(root);
}
void bintree::road_(node *root)
{
int des,pre;
int flag=0;
int r[nums()+1];
for(int i=1;i<=nums();i++)
r[i]=i;
cout<<"Please input the point you want to search for\n";
scanf("%d",&des);
stack<node *> s;
pre=root->data;
while(root!=NULL || !s.empty())
{
while(root!=NULL)
{
s.push(root);
if(root->data==des)
{
flag=1;
break;
}
if(root->lc)
{
r[root->lc->data]=root->data;
//pre=root->data;
}
root=root->lc;
}
if(!s.empty())
{
root=s.top();
s.pop();
if(root->rc)
{
r[root->rc->data]=root->data;
//pre=root->data;
}
root=root->rc;
}
if(flag)
{
break;
}
}
if(!flag)
{
printf("Failed to find that point\n");
return ;
}
for(int i=1;i<=nums();i++)
printf("%d%s",r[i],i==nums()?"\n":" ");
stack<int> ss;
pre=des;
do{
ss.push(pre);
pre=r[pre];
}while(pre!=1);
ss.push(r[1]);
cout<<ss.top();
ss.pop();
while(!ss.empty())
{
int tep=ss.top();
ss.pop();
printf(" -> %d",tep);
}
cout<<endl;
}
int main()
{
bintree b;
b.bincreate();
b.pre_order();
b.in_order();
b.post_order();
b.road();
return 0;
}
二叉树非递归遍历
最新推荐文章于 2025-03-07 16:30:53 发布