设为首页 加入收藏

TOP

基于顺序存储的多叉树实现: (4) 后序遍历 (四)
2014-11-23 22:57:33 来源: 作者: 【 】 浏览:6
Tags:基于 顺序 存储 实现 后序遍
return *this;
55 }
56 template
57 template
58 inline typename mtree::template post_iterator_impl
59 mtree::post_iterator_impl::operator - (size_t off)
60 {
61 post_iterator_impl iter(*this);
62 iter -= off;
63 return iter;
64 }
65 template
66 template
67 inline typename mtree::template post_iterator_impl&
68 mtree::post_iterator_impl::operator -= (size_t off)
69 {
70 while (off)
71 {
72 if (base_type::is_null()) break;
73 --(*this); --off;
74 }
75 return *this;
76 }
77 template
78 template
79 inline typename mtree::template post_iterator_impl
80 mtree::post_iterator_impl::begin() const
81 {
82 post_iterator_impl iter(*this);
83 iter.first(typename reverse_trait::type());
84 return iter;
85 }
86 template
87 template
88 inline typename mtree::template post_iterator_impl
89 mtree::post_iterator_impl::end() const
90 {
91 post_iterator_impl iter(*this);
92 if (tree_)
93 {
94 iter.off_ = tree_->size();
95 }
96 return iter;
97 }
(7)间隔层定位方法的实现,代码如下:
1 template
2 template
3 inline void mtree::post_iterator_impl::first(no_reverse_tag)
4 {
5 assert(tree_&&root_size());
6 forward_first();
7 }
8 template
9 template
10 inline void mtree::post_iterator_impl::first(reverse_tag)
11 {
12 assert(tree_&&root_size());
13 forward_last();
14 }
15 template
16 template
17 inline void mtree::post_iterator_impl:: last(no_reverse_tag)
18 {
19 assert(tree_&&root_size());
20 forward_last();
21 }
22 template
23 template
24 inline void mtree::post_iterator_impl:: last(reverse_tag)
25 {
26 assert(tree_&&root_size());
27 forward_first();
28 }
29 template
30 template
31 inline void mtree::post_iterator_impl::increment(no_reverse_tag)
32 {
33 assert(tree_&&off_<=tree_->size());
34 off_!=tree_->size() forward_next() : first(no_reverse_tag());
35 }
36 template
37 template
38 inline void mtree::post_iterator_impl::increment(reverse_tag)
39 {
40 assert(tree_&&off_<=tree_->size());
41 off_!=tree_->size() forward_prev() : first(reverse_tag());
42 }
43 template
44 template
45 inline void mtree::post_iterator_impl::decrement(no_reverse_tag)
46 {
47 assert(tree_&&off_<=tree_->size());
48 off_ !=tree_->size() forward_prev() : last(no_reverse_tag());
49 }
50 template
51 template
52 inline void mtree::post_iterator_impl::decrement(reverse_tag)
53 {
54 assert(tree_&&off_<=tree_->size());
55 off_ !=tree_->size() forward_next() : last(reverse_tag());
56 }

四、使用示例
(1)正向遍历整颗树,代码如下:
1 mtree mt;
2 mtree::iterator_base root = mt.get_root();
3 mtree::post_iterator it = root;
4 mtree::post_iterator last = --it.end();
5 for (it = it.begin();it!=it.end();++it)
6 {
7 cout << *it;
8 if (it!=last)
9 cout <<" ";
10 }
(2)反向遍历整颗树 ,代码如下:
1 mtree mt;
2 mtree::iterator_base root = mt.get_root();
3 mtree::reverse_post_iterator r_it = root;
4 mtree::reverse_post_iterator r_last = --r_it.end();
5 for (r_it = r_it.begin();r_it!=r_it.end();++r

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ZOJ-1067基本运算题 下一篇让protobuf生成器支持时间戳检查

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: