二叉树遍历(中序,先序,后序,层次等)

层次遍历
 1 //层次遍历,队列实现,依次取对头元素,将左,右结点入队
2 template <class T> void tree<T>::level_order()
3 {
4 if (root)
5 {
6 tree_node<T> *ptr=root;
7 Queue_list<tree_node<T>*> queue;
8 queue.push(root);
9 while (1)
10 {
11 if (queue.isEmpty())
12 {
13 break;
14 }
15 ptr=queue.Front();
16 cout<<ptr->value;
17 if (ptr->lchild)
18 queue.push(ptr->lchild);
19 if (ptr->rchild)
20 queue.push(ptr->rchild);
21 queue.pop();
22
23 }
24
25 }
26 else
27 {
28 cout<<"empty tree";
29 }
30
31 cout<<endl;
32
33 }

遍历:

中序:左子树->根节点->右子树

先序:根节点->左子树->右子树

后序:左子树->右子树->根节点

1.中序遍历

中序遍历递归实现
 1 //中序遍历
2 template<class T> void tree<T>::in_order()
3 {
4 if (root)
5 {
6 order_in(root);
7
8 }else
9 {
10 cout<<"empty tree ";
11 }
12
13 cout<<endl;
14
15 }
16
17 template<class T> void tree<T>::order_in(tree_node<T> *p)
18 {
19 if (p)
20 {
21 order_in(p->lchild);
22 cout<<p->value;
23 order_in(p->rchild);
24 }
25 }
中序遍历迭代实现
 1 //先将栈顶元素左节点入栈,如果没有最左,则出栈,压入出栈元素的右结点,依次知道栈为空
2 template<class T> void tree<T>::iter_in_order()
3 {
4 if (root)
5 {
6 tree_node<T> *ptr=root;
7 stack<tree_node<T>* > stack;
8 while (1)
9 {
10 //左子树左节压栈
11 while (ptr)
12 {
13 stack.push(ptr);
14 ptr=ptr->lchild;
15 }
16
17 if (stack.isEmpty())
18 break;
19
20 ptr=stack.Top();
21 cout<<ptr->value;
22 stack.pop();
23 ptr=ptr->rchild;
24 }
25
26 }else
27 {
28 cout<<"empty tree ";
29 }
30
31 cout<<endl;
32
33 }

2.先序遍历

先序递归实现
//先序遍历
template<class T>void tree<T>::pre_order()
{
if (root)
{
order_pre(root);

}
else
{
cout
<<"empty tree ";
}
cout
<<endl;
}

template
<class T> void tree<T>::order_pre(tree_node<T> *p)
{
if(p)
{
cout
<<p->value;
order_pre(p
->lchild);
order_pre(p
->rchild);
}
}
先序迭代实现
 1 //栈顶出栈,压入栈顶元素右结点,左结点,依次循环直到栈空
2 template<class T>void tree<T>::iter_pre_order()
3 {
4 if (!root)
5 {
6 cout<<"empty tree ";
7 }else
8 {
9 tree_node<T> *ptr=root;
10 stack_list<tree_node<T> *>stack;
11 stack.push(root);
12 while (1)
13 {
14 if (stack.isEmpty())
15 {
16 break;
17 }
18 ptr=stack.Top();
19 stack.pop();
20 cout<<ptr->value;
21 if (ptr->rchild)
22 {
23 stack.push(ptr->rchild);
24 }
25 if (ptr->lchild)
26 {
27 stack.push(ptr->lchild);
28 }
29 }
30
31 }
32 cout<<endl;
33 }

3.后序遍历

后序递归实现
 1 //后序遍历
2 template<class T>void tree<T>::post_order()
3 {
4 if (root)
5 {
6 order_post(root);
7
8 }else
9 {
10 cout<<"empty tree ";
11 }
12
13 cout<<endl;
14 }
15
16 template<class T>void tree<T>::order_post(tree_node<T> *p)
17 {
18 if (p)
19 {
20 order_post(p->lchild);
21 order_post(p->rchild);
22 cout<<p->value;
23 }
24 }
后序迭代实现
 1 template<class T> void tree<T>::iter_post_order()
2 {
3 if(root)
4 {
5 tree_node<T> *ptr=root,*pre=NULL;
6 stack_list<tree_node<T> *>stack;
7 stack.push(root);
8 while (1)
9 {
10 if (stack.isEmpty())
11 {
12 break;
13 }
14 ptr=stack.Top();
15 if (pre!=ptr->rchild&&pre!=ptr->lchild)
16 {
17 if (ptr->rchild)
18 stack.push(ptr->rchild);
19 if (ptr->lchild)
20 stack.push(ptr->lchild);
21
22 }
23 if(ptr->lchild==NULL&&ptr->rchild==NULL||pre==ptr->lchild||pre==ptr->rchild)
24 {
25 cout<<ptr->value;
26 stack.pop();
27
28 }
29 pre=ptr;
30 }
31
32 }else
33 {
34 cout<<"empty tree ";
35 }
36
37 cout<<endl;
38
39 }

4.层次遍历


 

posted on 2011-09-05 22:26  youngkang  阅读(6936)  评论(0)    收藏  举报