二叉树遍历(递归与非递归)

  1 #include<iostream>
  2 #include<stack>
  3 #include<queue>
  4 using namespace std;
  5 typedef struct node
  6 {
  7     char data;
  8     struct node* lchild;
  9     struct node* rchild;
 10 }*Tree;
 11 
 12 void CreateBiTree(Tree &T)   //创建二叉树
 13 {
 14     char ch;
 15     cout << "input:";
 16     cin >> ch;
 17     if (ch=='0')  //0代表该分支结束
 18     {
 19         T = NULL;
 20         cout << "end"<<endl;
 21         return;
 22     }
 23     else
 24     {
 25         //if (T == NULL) return;
 26         T = new node;
 27         T->data = ch;
 28         CreateBiTree(T->lchild);
 29         CreateBiTree(T->rchild);
 30     }
 31 }
 32 
 33 void PreOrder(Tree T)   //先序递归遍历
 34 {
 35     if (T)
 36     {
 37         cout << T->data << " ";
 38         PreOrder(T->lchild);
 39         PreOrder(T->rchild);
 40     }
 41 }
 42 
 43 void InOrder(Tree T) //中序递归遍历
 44 {
 45     if (T)
 46     {
 47         InOrder(T->lchild);
 48         cout << T->data << " ";
 49         InOrder(T->rchild);
 50     }
 51 }
 52 
 53 void PostOrder(Tree T) //后序递归遍历
 54 {
 55     if (T)
 56     {
 57         PostOrder(T->lchild);
 58         PostOrder(T->rchild);
 59         cout << T->data << " ";
 60     }
 61 }
 62 
 63 void NoRecursivePreOrder(Tree T)  //非递归前序遍历
 64 {
 65     stack<Tree> S;
 66     Tree p = T;
 67     while (!S.empty()||p)
 68     {
 69         if (p)
 70         {
 71             cout << p->data << " ";
 72             S.push(p);
 73             p = p->lchild;
 74         }
 75         else
 76         {
 77             p = S.top();
 78             S.pop();
 79             p = p->rchild;
 80         }
 81     }
 82 }
 83 
 84 void NoRecursiveInOrder(Tree T)  //非递归中序遍历
 85 {
 86     stack<Tree> S;
 87     Tree p = T;
 88     while (!S.empty()||p)
 89     {
 90         if (p)
 91         {
 92             S.push(p);
 93             p = p->lchild;
 94         }
 95         else
 96         {
 97             p = S.top();
 98             cout << p->data << " ";
 99             S.pop();
100             p = p->rchild;
101         }
102     }
103 }
104 
105 void NoRecursivePostOrder(Tree T)  //非递归后续遍历
106 {
107     stack<Tree> S;
108     Tree p = T;
109     Tree pre = NULL;
110     while (!S.empty() || p)
111     {
112         while (p)
113         {
114             S.push(p);
115             p = p->lchild;
116         }
117         p = S.top();
118         if (!p->rchild || p->rchild == pre)
119         {
120             cout << p->data << " ";
121             pre = p;
122             p = NULL;
123             S.pop();
124         }
125         else
126             p = p->rchild;
127     }
128 }
129 
130 void BreadFirstSearch(Tree T)
131 {
132     queue<Tree> Q;
133     Tree p = T;
134     Q.push(p);
135     while (!Q.empty())
136     {
137         p = Q.front();
138         cout << p->data << " ";
139         Q.pop();
140         if (p->lchild)
141             Q.push(p->lchild);
142         if (p->rchild)
143             Q.push(p->rchild);
144     }
145 }
146 void main()
147 {
148     Tree T=NULL;
149     CreateBiTree(T);
150     cout << "先序递归遍历:";
151     PreOrder(T);
152     cout << "\n先序非递归遍历:";
153     NoRecursivePreOrder(T);
154     cout << "\n中序递归遍历:";
155     InOrder(T);
156     cout << "\n中序非递归遍历:";
157     NoRecursiveInOrder(T);
158     cout << "\n后序递归遍历:";
159     PostOrder(T);
160     cout << "\n后序非递归遍历:";
161     NoRecursivePostOrder(T);
162     cout << "\n广度优先遍历:";
163     BreadFirstSearch(T);
164 }

以下面二叉树为例:

输入:0表示当前分支结束

结果

posted on 2016-10-08 14:11  小菜鸡y  阅读(249)  评论(0编辑  收藏  举报