c语言数据结构---二叉树非递归输出

#include
#include
#include
using namespace std;
typedef struct node{
	struct node*lchild;
	struct node*rchild;
	char data;
}Node,*Bit;


Bit creat(){//先序创建 
char b;
int t;
scanf("%c",&b);
t=getchar();//吸收空格 
	if(b=='#')return NULL;
	else{
		Bit tree=(Bit)malloc(sizeof(Node));
		tree->data=b;
	printf("please input %c lefttree:",b);
        tree->lchild=creat();
		printf("please input %c righttree:",b); 
		tree->rchild=creat();
		return tree;
	}
}
void fei(Node*tree){//先序 
	stacks;
	Node*cur=tree;
	if(cur==NULL)return;
	while(!s.empty()||cur!=NULL){
	
	if(cur!=NULL){
		printf("%c ",cur->data);
		s.push(cur);
		cur=cur->lchild;
	}
	else{
		cur=s.top();
		s.pop();
		cur=cur->rchild;
	}
}
}

int main(){
Bit tree;

printf("please input root data:\n");
tree=creat();
printf("success_init_tree\n");
fei(tree);
}

你可能感兴趣的:(c语言,数据结构,c++)