初始化二叉搜索树+中序遍历(不使用递归)
#include<iostream>
#include<stack>
using namespace std;
struct binaryTree{
int data;
binaryTree* left;
binaryTree* right;
};
binaryTree* head=NULL;
void insert(int data,binaryTree* node){
if(node==NULL){
node->data=data;
node->left=head->right=NULL;
return;
}
if(data<node->data)
insert(data,node->left);
if(data>=node->data)
insert(data,node->right);
}
void Midoutput(){
binaryTree* temp=new binaryTree;
temp=head;
stack<binaryTree*> s;
while(temp||!s.empty()){
while(temp){
s.push(temp);
temp=temp->left;//一路向左下遍历
}
if(!s.empty()){
temp=s.top();
s.pop();
printf("%d ",temp->data);
temp=temp->right;
}
}
}