#include<iostream> #include<string.h> #include<stack> using namespace std; typedef struct BTree { int val; struct BTree *left,*right; }BTree; class Tree { public: BTree *create_node(int level,string pos); void PreOrder(BTree *t); //先序遍历 void InOrder(BTree *t); //中序遍历 void PostOrder(BTree *t); //后序遍历 void NonRecursivePreOrder(BTree*t); //非递归前序遍历 void NonRecursiveInOrder(BTree*t); //非递归中序遍历 void NonRecursivePostOrder(BTree*t);//非递归后序遍历 BTree *root; }; BTree* Tree::create_node(int level,string pos) { int data; BTree *node = new BTree; int a[]={100,99,98,97,0,0,20,0,0,10,0,0,5,1,0,0,2,0,0}; static int t=0; cout<<"please enter data:level "<<level<<" "<<pos<<"--->值为:"<<a[t]<<endl; data=a[t++]; if(data == 0) { return NULL; } node->val= data; node->left = create_node(level+1,"left"); node->right= create_node(level+1,"right"); return node; } void Tree::PreOrder(BTree *t) { if(t) { cout<<t->val<<" ";; PreOrder(t->left); PreOrder(t->right); } } void Tree::InOrder(BTree *t) { if(t) { InOrder(t->left); cout<<t->val<<" ";; InOrder(t->right); } } void Tree::PostOrder(BTree *t) { if(t) { PostOrder(t->left); PostOrder(t->right); cout<<t->val<<" "; } } void Tree::NonRecursivePreOrder(BTree*t) { if(t==NULL) return; stack<BTree*>s; BTree *p; p=t; while(p||!s.empty()) { if(p) { cout<<p->val<<" "; s.push(p); p=p->left; } else{ p=s.top(); p=p->right; s.pop(); } } } void Tree::NonRecursiveInOrder(BTree*t) { if(t==NULL) return; stack<BTree*>s; BTree*p; p=t; while(p||!s.empty()) { if(p) { s.push(p); p=p->left; } else { p=s.top(); cout<<p->val<<" "; p=p->right; s.pop(); } } } void Tree::NonRecursivePostOrder(BTree*t) { if(t==NULL) return; stack<BTree*>s; BTree*p=t; BTree*r; while(p||!s.empty()) { if(p) { s.push(p); p=p->left; } else { p=s.top(); if(p->right&&p->right!=r) { p=p->right; s.push(p); p=p->left; } else { cout<<p->val<<" "; r=p; s.pop(); p=NULL; } } } } int main() { Tree tree; tree.root = tree.create_node(1,"root"); cout<<"Pre"<<endl; tree.PreOrder(tree.root); cout<<endl; cout<<"非递归前序遍历"<<endl; tree.NonRecursivePreOrder(tree.root); cout<<endl; cout<<"In"<<endl; tree.InOrder(tree.root); cout<<endl; cout<<"非递归中序遍历"<<endl; tree.NonRecursiveInOrder(tree.root); cout<<endl; cout<<"Post"<<endl; tree.PostOrder(tree.root); cout<<endl; cout<<"非递归后序遍历"<<endl; tree.NonRecursivePostOrder(tree.root); return 0; }
结果:
please enter data:level 1 root--->值为:100 please enter data:level 2 left--->值为:99 please enter data:level 3 left--->值为:98 please enter data:level 4 left--->值为:97 please enter data:level 5 left--->值为:0 please enter data:level 5 right--->值为:0 please enter data:level 4 right--->值为:20 please enter data:level 5 left--->值为:0 please enter data:level 5 right--->值为:0 please enter data:level 3 right--->值为:10 please enter data:level 4 left--->值为:0 please enter data:level 4 right--->值为:0 please enter data:level 2 right--->值为:5 please enter data:level 3 left--->值为:1 please enter data:level 4 left--->值为:0 please enter data:level 4 right--->值为:0 please enter data:level 3 right--->值为:2 please enter data:level 4 left--->值为:0 please enter data:level 4 right--->值为:0 Pre 100 99 98 97 20 10 5 1 2 非递归前序遍历 100 99 98 97 20 10 5 1 2 In 97 98 20 99 10 100 1 5 2 非递归中序遍历 97 98 20 99 10 100 1 5 2 Post 97 20 98 10 99 1 2 5 100 非递归后序遍历 97 20 98 10 99 1 2 5 100