程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 基於順序存儲的多叉樹實現: (6) 葉子遍歷

基於順序存儲的多叉樹實現: (6) 葉子遍歷

編輯:關於C語言

一、類型定義
   在多叉樹中,葉子遍歷迭代器有只讀、讀寫、只讀反轉、讀寫反轉4種,在mtree容器中的定義如下:
1        typedef leaf_iterator_impl<false,false> leaf_iterator;
2        typedef leaf_iterator_impl<false,true> reverse_leaf_iterator;
3        typedef leaf_iterator_impl<true,false> const_leaf_iterator;
4        typedef leaf_iterator_impl<true,true> const_reverse_leaf_iterator;

二、接口定義
   多叉樹的葉子遍歷是指訪問某子樹的所有葉子結點,下面代碼是葉子遍歷迭代器的聲明: 
 1        template<bool is_const,bool is_reverse>
 2        class leaf_iterator_impl : public iterator_base_impl<is_const>
 3        {
 4            friend class mtree<T,false>;
 5            typedef iterator_base_impl<is_const> 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            leaf_iterator_impl();           
13            leaf_iterator_impl(const base_type& iter);           
14            leaf_iterator_impl&  operator++();           
15            leaf_iterator_impl&  operator--();           
16            leaf_iterator_impl operator++(int);           
17            leaf_iterator_impl operator--(int);           
18            leaf_iterator_impl operator + (size_t off);           
19            leaf_iterator_impl& operator += (size_t off);           
20            leaf_iterator_impl operator - (size_t off);           
21            leaf_iterator_impl& operator -= (size_t off);           
22            leaf_iterator_impl begin() const;           
23            leaf_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<typename T>
 2    template<bool is_const,bool is_reverse>
 3    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::forward_first()
 4    {
 5        assert(tree_&&root_<tree_->size());
 6        off_ = root_; node_pointer_type p_node = &(*tree_)[off_];
 7        while (p_node->first_child_)
 8        {
 9            off_ += p_node->first_child_;
10            p_node = &(*tree_)[off_];
11        }
12    }
   (2)forward_last:求正向最後一個葉子,就是位於子樹最右側最深且沒有孩子的結點,代碼如下:    
 1    template<typename T>
 2    template<bool is_const,bool is_reverse>
 3    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::forward_last()
 4    {
 5        assert(tree_&&root_<tree_->size());
 6        off_ = root_; node_pointer_type p_node = &(*tree_)[off_];
 7        while (p_node->last_child_)
 8        {
 9            off_ += p_node->last_child_;
10            p_node = &(*tree_)[off_];
11        }
12    }
(3)forward_next:求正向下一個葉子,步驟如下:1) 如果當前結點不是子樹根結點且存在父親但沒有右兄弟,那麼就一直向上回溯直到結點為子樹根結點或不存在父親或存在右兄弟為止,反之轉到2)。2) 如果當前結點是子樹根結點或沒有父親,那麼返回end,否則轉到3)。3) 這時存在右兄弟,那麼沿該右兄弟的第一個孩子一直向下搜索直到沒有孩子結點為止。代碼如下:
 1    template<typename T>
 2    template<bool is_const,bool is_reverse>
 3    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::forward_next()
 4    {
 5        node_pointer_type p_node = &(*tree_)[off_];
 6        while (off_!=root_&&p_node->parent_&&!p_node->next_sibling_)
 7        {
 8            off_ -= p_node->parent_;
 9            p_node = &(*tree_)[off_];
10        }
11        if (off_==root_||!p_node->parent_)
12        {
13            off_ = tree_->size();
14            return;
15        }
16        off_ += p_node->next_sibling_; p_node = &(*tree_)[off_];
17        while (p_node->first_child_)
18        {
19            off_ += p_node->first_child_;
20            p_node = &(*tree_)[off_];
21        }
22    }
   (4)forward_prev:求正向前一個葉子,步驟如下:1) 如果當前結點不是子樹根結點且存在父親但沒有左兄弟,那麼就一直向上回溯直到結點為子樹根結點或不存在父親或存在左兄弟為止,反之轉到2)。2) 如果當前結點是子樹根結點或沒有父親,那麼返回end,否則轉到3)。3) 這時存在左兄弟,那麼沿該左兄弟的最後一個孩子一直向下搜索直到沒有孩子結點為止,代碼如下:
 1    template<typename T>
 2    template<bool is_const,bool is_reverse>
 3    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::forward_prev()
 4    {
 5        node_pointer_type p_node = &(*tree_)[off_];
 6        while (off_!=root_&&p_node->parent_&&!p_node->prev_sibling_)
 7        {
 8            off_ -= p_node->parent_;
 9            p_node = &(*tree_)[off_];
10        }
11        if (off_==root_||!p_node->parent_)
12        {
13            off_ = tree_->size();
14            return;
15        }
16        off_ -= p_node->prev_sibling_; p_node = &(*tree_)[off_];
17        while (p_node->last_child_)
18        {
19            off_ += p_node->last_child_;
20            p_node = &(*tree_)[off_];
21        }
22    }
   (5)構造函數的實現,代碼如下:
 1    template<typename T>
 2    template<bool is_const,bool is_reverse>
 3    inline mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::leaf_iterator_impl()
 4        :base_type()
 5    {
 6        root_ = 0;
 7    }
 8    template<typename T>
 9    template<bool is_const,bool is_reverse>
10    inline mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::leaf_iterator_impl(const base_type& iter)
11        :base_type(iter)
12    {
13        root_ = off_;
14    }
   (6)公有方法的實現,代碼如下:
 1    template<typename T>
 2    template<bool is_const,bool is_reverse>
 3    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>&
 4        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::operator++()
 5    {
 6        increment(typename reverse_trait<is_reverse>::type());
 7        return *this;
 8    }
 9    template<typename T>
10    template<bool is_const,bool is_reverse>
11    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>&
12        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::operator--()
13    {
14        decrement(typename reverse_trait<is_reverse>::type());
15        return *this;
16    }
17    template<typename T>
18    template<bool is_const,bool is_reverse>
19    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>
20        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::operator++(int)
21    {
22        leaf_iterator_impl<is_const,is_reverse> iter(*this);
23        ++(*this);
24        return iter;
25    }
26    template<typename T>
27    template<bool is_const,bool is_reverse>
28    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>
29        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::operator--(int)
30    {
31        leaf_iterator_impl<is_const,is_reverse> iter(*this);
32        --(*this);
33        return iter;
34    }
35    template<typename T>
36    template<bool is_const,bool is_reverse>
37    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>
38        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::operator + (size_t off)
39    {
40        leaf_iterator_impl<is_const,is_reverse> iter(*this);
41        iter += off;
42        return iter;
43    }
44    template<typename T>
45    template<bool is_const,bool is_reverse>
46    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>&
47        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::operator += (size_t off)
48    {
49        while (off)
50        {
51            if (base_type::is_null()) break;
52            ++(*this); --off;
53        }
54        return *this;
55    }
56    template<typename T>
57    template<bool is_const,bool is_reverse>
58    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>
59        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::operator - (size_t off)
60    {
61        leaf_iterator_impl<is_const,is_reverse> iter(*this);
62        iter -= off;
63        return iter;
64    }
65    template<typename T>
66    template<bool is_const,bool is_reverse>
67    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>&
68        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::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<typename T>
78    template<bool is_const,bool is_reverse>
79    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>
80        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::begin() const
81    {
82        leaf_iterator_impl<is_const,is_reverse> iter(*this);
83        iter.first(typename reverse_trait<is_reverse>::type());
84        return iter;
85    }
86    template<typename T>
87    template<bool is_const,bool is_reverse>
88    inline typename mtree<T,false>::template leaf_iterator_impl<is_const,is_reverse>
89        mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::end() const
90    {
91        leaf_iterator_impl<is_const,is_reverse> iter(*this);
92        if(tree_)
93        {
94            iter.off_ = tree_->size();
95        }
96        return iter;
97    }
   (7)間隔層定位方法的實現,代碼如下:

 1    template<typename T>
 2    template<bool is_const,bool is_reverse>
 3    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::first(no_reverse_tag)
 4    {
 5        assert(tree_&&root_<tree_->size());
 6        forward_first();
 7    }
 8    template<typename T>
 9    template<bool is_const,bool is_reverse>
10    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::first(reverse_tag)
11    {
12        assert(tree_&&root_<tree_->size());
13        forward_last();
14    }
15    template<typename T>
16    template<bool is_const,bool is_reverse>
17    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::last(no_reverse_tag)
18    {
19        assert(tree_&&root_<tree_->size());
20        forward_last();
21    }
22    template<typename T>
23    template<bool is_const,bool is_reverse>
24    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::last(reverse_tag)
25    {
26        assert(tree_&&root_<tree_->size());
27        forward_first();
28    }
29    template<typename T>
30    template<bool is_const,bool is_reverse>
31    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::increment(no_reverse_tag)
32    {
33        assert(tree_);
34        off_!=tree_->size() ? forward_next() : first(no_reverse_tag());
35    }
36    template<typename T>
37    template<bool is_const,bool is_reverse>
38    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::increment(reverse_tag)
39    {
40        assert(tree_);
41        off_!=tree_->size() ? forward_prev() : first(reverse_tag());
42    }
43    template<typename T>
44    template<bool is_const,bool is_reverse>
45    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::decrement(no_reverse_tag)
46    {
47        assert(tree_);
48        off_!=tree_->size() ? forward_prev() : last(no_reverse_tag());
49    }
50    template<typename T>
51    template<bool is_const,bool is_reverse>
52    inline void mtree<T,false>::leaf_iterator_impl<is_const,is_reverse>::decrement(reverse_tag)
53    {
54        assert(tree_);
55        off_!=tree_->size() ? forward_next() : last(reverse_tag());
56    }

四、使用示例
   (1)正向遍歷某子樹的所有葉子,代碼如下:

1    mtree<int,false>::iterator_base node;
2    mtree<int,false>::leaf_iterator it = node;
3    mtree<int,false>::leaf_iterator last = --it.end();
4    for (it = it.begin();it!=it.end();++it)
5    {
6        cout << *it;
7        if (it!=last)
8            cout <<" ";
9    }
   (2)反向遍歷某子樹的所有葉子,代碼如下:
1    mtree<int,false>::iterator_base node;
2    mtree<int,false>::reverse_leaf_iterator r_it = node;
3    mtree<int,false>::reverse_leaf_iterator r_last = --r_it.end();
4    for (r_it = r_it.begin();r_it!=r_it.end();++r_it)
5    {
6        cout << *r_it;
7        if (r_it!=r_last)
8            cout <<" ";
9    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved