设为首页 加入收藏

TOP

基于顺序存储的多叉树实现: (5) 兄弟遍历 (一)
2014-11-23 22:57:49 来源: 作者: 【 】 浏览:5
Tags:基于 顺序 存储 实现 兄弟

一、类型定义
在多叉树中,兄弟遍历迭代器有只读、读写、只读反转、读写反转4种,在mtree容器中的定义如下:
1 typedef sibling_iterator_impl sibling_iterator;
2 typedef sibling_iterator_impl reverse_sibling_iterator;
3 typedef sibling_iterator_impl const_sibling_iterator;
4 typedef sibling_iterator_impl const_reverse_sibling_iterator;

二、接口定义
多叉树的兄弟遍历是指访问给定结点的所有兄弟(包括它自己),下面代码是兄弟遍历迭代器的声明:
1 template
2 class sibling_iterator_impl : public iterator_base_impl
3 {
4 friend class mtree;
5 typedef iterator_base_impl base_type;
6 typedef typename base_type::node_pointer_type node_pointer_type;
7 typedef typename base_type::tree_pointer_type tree_pointer_type;
8 using base_type::tree_;
9 using base_type::off_;
10 using base_type::root_;
11 public:
12 sibling_iterator_impl();
13 sibling_iterator_impl(const base_type& iter);
14 sibling_iterator_impl& operator++();
15 sibling_iterator_impl& operator--();
16 sibling_iterator_impl operator++(int);
17 sibling_iterator_impl operator--(int);
18 sibling_iterator_impl operator + (size_t off);
19 sibling_iterator_impl& operator += (size_t off);
20 sibling_iterator_impl operator - (size_t off);
21 sibling_iterator_impl& operator -= (size_t off);
22 sibling_iterator_impl begin() const;
23 sibling_iterator_impl end() const;
24 protected:
25 void first(no_reverse_tag);
26 void first(reverse_tag);
27 void last(no_reverse_tag);
28 void last(reverse_tag);
29 void increment(no_reverse_tag);
30 void increment(reverse_tag);
31 void decrement(no_reverse_tag);
32 void decrement(reverse_tag);
33 private:
34 void forward_first();
35 void forward_last();
36 void forward_next();
37 void forward_prev();
38 };

三、接口实现
下面重点讲述兄弟遍历中4种定位方法的具体实现,随后列出其它所有方法的实现代码。
(1)forward_first:求正向第一个兄弟,就是其父结点的第一个孩子,代码如下:
1 template
2 template
3 inline void mtree::sibling_iterator_impl::forward_first()
4 {
5 node_pointer_type p_node = &(*tree_)[root_];
6 off_ = root_ + p_node->first_child_;
7 }
(2)forward_last:求正向最后一个兄弟,就是其父结点的最后一个孩子,代码如下:
1 template
2 template
3 inline void mtree::sibling_iterator_impl::forward_last()
4 {
5 node_pointer_type p_node = &(*tree_)[root_];
6 off_ = root_ + p_node->last_child_;
7 }
(3)forward_next:求正向下一个兄弟,如果当前结点存在右兄弟,那么就是它的右兄弟,否则返回end,代码如下:
1 template
2 template
3 inline void mtree::sibling_iterator_impl::forward_next()
4 {
5 node_pointer_type p_node = &(*tree_)[off_];
6 p_node->next_sibling_ off_ += p_node->next_sibling_ : off_ = tree_->size();
7 }
(4)forward_prev:求正向前一个结点,如果当前结点存在左兄弟,那么就是它的左兄弟,否则返回end,代码如下:
1 template
2 template
3 inline void mtree::sibling_iterator_impl::forward_prev()
4 {
5 node_pointer_type p_node = &(*tree_)[off_];
6 p_node->prev_sibling_ off_ -= p_node->prev_sibling_ : off_ = tree_->size();
7 }
(5)构造函数的实现,代码如下:
1 template
2

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇读入文件时最后一次重复 下一篇简易位图内存管理器的设计

评论

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