📄 tree.h
字号:
/* * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * */#ifndef TREE_H#define TREE_H/*Red-black tree class, designed for use in implementing STLassociative containers (set, multiset, map, and multimap). Theinsertion and deletion algorithms are based on those in Cormen,Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),except that(1) the header cell is maintained with links not only to the rootbut also to the leftmost node of the tree, to enable constant timebegin(), and to the rightmost node of the tree, to enable linear timeperformance when used with the generic set algorithms (set_union,etc.);(2) when a node being deleted has two children its successor node isrelinked into its place, rather than copied, so that the onlyiterators invalidated are those referring to the deleted node.*/#include <algobase.h>#include <iterator.h>#include <function.h>#include <bool.h>#include <projectn.h>#ifndef rb_tree #define rb_tree rb_tree#endiftemplate <class Key, class Value, class KeyOfValue, class Compare>class rb_tree {protected: enum color_type {red, black}; typedef Allocator<void>::pointer void_pointer; struct rb_tree_node; friend rb_tree_node; struct rb_tree_node { color_type color_field; void_pointer parent_link; void_pointer left_link; void_pointer right_link; Value value_field; }; static Allocator<rb_tree_node> rb_tree_node_allocator; static Allocator<Value> value_allocator;public: typedef Key key_type; typedef Value value_type; typedef Allocator<Value>::pointer pointer; typedef Allocator<Value>::reference reference; typedef Allocator<Value>::const_reference const_reference; typedef Allocator<rb_tree_node> rb_tree_node_allocator_type; typedef Allocator<rb_tree_node>::pointer link_type; typedef Allocator<rb_tree_node>::size_type size_type; typedef Allocator<rb_tree_node>::difference_type difference_type;protected: size_type buffer_size() { return rb_tree_node_allocator.init_page_size(); } struct rb_tree_node_buffer; friend rb_tree_node_buffer; struct rb_tree_node_buffer { void_pointer next_buffer; link_type buffer; };public: typedef Allocator<rb_tree_node_buffer> buffer_allocator_type; typedef Allocator<rb_tree_node_buffer>::pointer buffer_pointer; protected: static Allocator<rb_tree_node_buffer> buffer_allocator; static buffer_pointer buffer_list; static link_type free_list; static link_type next_avail; static link_type last; void add_new_buffer() { buffer_pointer tmp = buffer_allocator.allocate((size_type)1); tmp->buffer = rb_tree_node_allocator.allocate(buffer_size()); tmp->next_buffer = buffer_list; buffer_list = tmp; next_avail = buffer_list->buffer; last = next_avail + buffer_size(); } static size_type number_of_trees; void deallocate_buffers(); link_type get_node() { link_type tmp = free_list; return free_list ? (free_list = (link_type)(free_list->right_link), tmp) : (next_avail == last ? (add_new_buffer(), next_avail++) : next_avail++); // ugly code for inlining - avoids multiple returns } void put_node(link_type p) { p->right_link = free_list; free_list = p; }protected: link_type header; link_type& root() { return parent(header); } link_type& root() const { return parent(header); } link_type& leftmost() { return left(header); } link_type& leftmost() const { return left(header); } link_type& rightmost() { return right(header); } link_type& rightmost() const { return right(header); } size_type node_count; // keeps track of size of tree bool insert_always; // controls whether an element already in the // tree is inserted again//public: Compare key_compare; static link_type NIL; static link_type& left(link_type x) { return (link_type&)((*x).left_link); } static link_type& right(link_type x) { return (link_type&)((*x).right_link); } static link_type& parent(link_type x) { return (link_type&)((*x).parent_link); } static reference value(link_type x) { return (*x).value_field; } static Allocator<Key>::const_reference key(link_type x) { return KeyOfValue()(value(x)); } static color_type& color(link_type x) { return (color_type&)(*x).color_field; } static link_type minimum(link_type x) { while (left(x) != NIL) x = left(x); return x; } static link_type maximum(link_type x) { while (right(x) != NIL) x = right(x); return x; }public: class iterator; friend iterator; class const_iterator; friend const_iterator; class iterator : public bidirectional_iterator<Value, difference_type> { friend class rb_tree<Key, Value, KeyOfValue, Compare>; friend class const_iterator;/* friend bool operator==(const iterator& x, const iterator& y) { return x.node == y.node; }*/ protected: link_type node; iterator(link_type x) : node(x) {} public: iterator() {} bool operator==(const iterator& y) const { return node == y.node; } reference operator*() const { return value(node); } iterator& operator++() { if (right(node) != NIL) { node = right(node); while (left(node) != NIL) node = left(node); } else { link_type y = parent(node); while (node == right(y)) { node = y; y = parent(y); } if (right(node) != y) // necessary because of rightmost node = y; } return *this; } iterator operator++(int) { iterator tmp = *this; ++*this; return tmp; } iterator& operator--() { if (color(node) == red && parent(parent(node)) == node) // check for header node = right(node); // return rightmost else if (left(node) != NIL) { link_type y = left(node); while (right(y) != NIL) y = right(y); node = y; } else { link_type y = parent(node); while (node == left(y)) { node = y; y = parent(y); } node = y; } return *this; } iterator operator--(int) { iterator tmp = *this; --*this; return tmp; } }; class const_iterator : public bidirectional_iterator<Value,difference_type> { friend class rb_tree<Key, Value, KeyOfValue, Compare>; friend class iterator;/* friend bool operator==(const const_iterator& x, const const_iterator& y) { return x.node == y.node; }*/ protected: link_type node; const_iterator(link_type x) : node(x) {} public: const_iterator() {} const_iterator(const iterator& x) : node(x.node) {} bool operator==(const const_iterator& y) const { return node == y.node; } bool operator!=(const const_iterator& y) const { return node != y.node; } const_reference operator*() const { return value(node); } const_iterator& operator++() { if (right(node) != NIL) { node = right(node); while (left(node) != NIL) node = left(node); } else { link_type y = parent(node); while (node == right(y)) { node = y; y = parent(y); } if (right(node) != y) // necessary because of rightmost node = y; } return *this; } const_iterator operator++(int) { const_iterator tmp = *this; ++*this; return tmp; } const_iterator& operator--() { if (color(node) == red && parent(parent(node)) == node) // check for header node = right(node); // return rightmost else if (left(node) != NIL) { link_type y = left(node); while (right(y) != NIL) y = right(y); node = y; } else { link_type y = parent(node); while (node == left(y)) { node = y; y = parent(y); } node = y; } return *this; } const_iterator operator--(int) { const_iterator tmp = *this; --*this; return tmp; } }; typedef reverse_bidirectional_iterator<iterator, value_type, reference, difference_type> reverse_iterator; typedef reverse_bidirectional_iterator<const_iterator, value_type, const_reference, difference_type> const_reverse_iterator;private: iterator __insert(link_type x, link_type y, const value_type& v); link_type __copy(link_type x, link_type p); void __erase(link_type x); void init() { ++number_of_trees; if (NIL == 0) { NIL = get_node(); color(NIL) = black; parent(NIL) = 0; left(NIL) = 0; right(NIL) = 0; } header = get_node(); color(header) = red; // used to distinguish header from root, // in iterator.operator++ root() = NIL; leftmost() = header; rightmost() = header; }public: // allocation/deallocation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -