亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? avl_tree.h

?? AVL Tree的實現(xiàn)
?? H
?? 第 1 頁 / 共 2 頁
字號:
// Include once.
#ifndef ABSTRACT_CONTAINER_AVL_TREE_H_
#define ABSTRACT_CONTAINER_AVL_TREE_H_

// Abstract AVL Tree Template.
//
// This code is in the public domain.  See avl_tree.html for interface
// documentation.
//
// Version: 1.2  Author: Walt Karas
//
// NOTE: Within the implementation, it's generally more convenient to
// define the depth of the root node to be 0 (0-based depth) rather than
// 1 (1-based depth).


#include "bitset"

namespace abstract_container
{

#ifndef ABSTRACT_CONTAINER_SEARCH_TYPE_
#define ABSTRACT_CONTAINER_SEARCH_TYPE_

enum search_type
  {
    EQUAL = 1,
    LESS = 2,
    GREATER = 4,
    LESS_EQUAL = EQUAL | LESS,
    GREATER_EQUAL = EQUAL | GREATER
  };

#endif

// The base_avl_tree template is the same as the avl_tree template,
// except for one additional template parameter: bset.  Here is the
// reference class for bset.
//
// class bset
//   {
//   public:
//
//     class ANY_bitref
//       {
//       public:
//         operator bool (void);
//         void operator = (bool b);
//       };
//
//     // Does not have to initialize bits.
//     bset(void);
//
//     // Must return a valid value for index when 0 <= index < max_depth
//     ANY_bitref operator [] (unsigned index);
//
//     // Set all bits to 1.
//     void set(void);
//
//     // Set all bits to 0.
//     void reset(void);
//   };
//
template <class abstractor, unsigned max_depth, class bset>
class base_avl_tree
  {
  public:

    typedef typename abstractor::key key;
    typedef typename abstractor::handle handle;
    typedef typename abstractor::size size;

    inline handle insert(handle h);

    inline handle search(key k, search_type st = EQUAL);
    inline handle search_least(void);
    inline handle search_greatest(void);

    inline handle remove(key k);

    void purge(void) { root = null(); }

    bool is_empty(void) { return(root == null()); }

    bool read_error(void) { return(abs.read_error()); }

    base_avl_tree(void) : root(null()) { }

    class iter
      {
      public:

	// NOTE:  GCC allows these member functions to be defined as
	// explicitly inline outside the class, but Visual C++ .NET does
	// not.

	// Initialize depth to invalid value, to indicate iterator is
	// invalid.   (Depth is zero-base.)
	iter(void) { depth = ~0; }

	void start_iter(base_avl_tree &tree, key k, search_type st = EQUAL)
	  {
	    // Mask of high bit in an int.
	    const int MASK_HIGH_BIT = (int) ~ ((~ (unsigned) 0) >> 1);

	    // Save the tree that we're going to iterate through in a
	    // member variable.
	    tree_ = &tree;

	    int cmp, target_cmp;
	    handle h = tree_->root;
	    unsigned d = 0;

	    depth = ~0;

	    if (h == null())
	      // Tree is empty.
	      return;

	    if (st & LESS)
	      // Key can be greater than key of starting node.
	      target_cmp = 1;
	    else if (st & GREATER)
	      // Key can be less than key of starting node.
	      target_cmp = -1;
	    else
	      // Key must be same as key of starting node.
	      target_cmp = 0;

	    for ( ; ; )
	      {
		cmp = cmp_k_n(k, h);
		if (cmp == 0)
		  {
		    if (st & EQUAL)
		      {
			// Equal node was sought and found as starting node.
			depth = d;
			break;
		      }
		    cmp = -target_cmp;
		  }
		else if (target_cmp != 0)
		  if (!((cmp ^ target_cmp) & MASK_HIGH_BIT))
		    // cmp and target_cmp are both negative or both positive.
		    depth = d;
		h = cmp < 0 ? get_lt(h) : get_gt(h);
		if (read_error())
		  {
		    depth = ~0;
		    break;
		  }
		if (h == null())
		  break;
		branch[d] = cmp > 0;
		path_h[d++] = h;
	      }
	  }

	void start_iter_least(base_avl_tree &tree)
	  {
	    tree_ = &tree;

	    handle h = tree_->root;

	    depth = ~0;

	    branch.reset();

	    while (h != null())
	      {
		if (depth != ~0)
		  path_h[depth] = h;
		depth++;
		h = get_lt(h);
		if (read_error())
		  {
		    depth = ~0;
		    break;
		  }
	      }
	  }

	void start_iter_greatest(base_avl_tree &tree)
	  {
	    tree_ = &tree;

	    handle h = tree_->root;

	    depth = ~0;

	    branch.set();

	    while (h != null())
	      {
		if (depth != ~0)
		  path_h[depth] = h;
		depth++;
		h = get_gt(h);
		if (read_error())
		  {
		    depth = ~0;
		    break;
		  }
	      }
	  }

	handle operator * (void)
	  {
	    if (depth == ~0)
	      return(null());

	    return(depth == 0 ? tree_->root : path_h[depth - 1]);
	  }

	void operator ++ (void)
	  {
	    if (depth != ~0)
	      {
		handle h = get_gt(**this);
		if (read_error())
		  depth = ~0;
		else if (h == null())
		  do
		    {
		      if (depth == 0)
			{
			  depth = ~0;
			  break;
			}
		      depth--;
		    }
		  while (branch[depth]);
		else
		  {
		    branch[depth] = true;
		    path_h[depth++] = h;
		    for ( ; ; )
		      {
			h = get_lt(h);
			if (read_error())
			  {
			    depth = ~0;
			    break;
			  }
			if (h == null())
			  break;
			branch[depth] = false;
			path_h[depth++] = h;
		      }
		  }
	      }
	  }

	void operator -- (void)
	  {
	    if (depth != ~0)
	      {
		handle h = get_lt(**this);
		if (read_error())
		  depth = ~0;
		else if (h == null())
		  do
		    {
		      if (depth == 0)
			{
			  depth = ~0;
			  break;
			}
		      depth--;
		    }
		  while (!branch[depth]);
		else
		  {
		    branch[depth] = false;
		    path_h[depth++] = h;
		    for ( ; ; )
		      {
			h = get_gt(h);
			if (read_error())
			  {
			    depth = ~0;
			    break;
			  }
			if (h == null())
			  break;
			branch[depth] = true;
			path_h[depth++] = h;
		      }
		  }
	      }
	  }

	void operator ++ (int) { ++(*this); }

	void operator -- (int) { --(*this); }

	bool read_error(void) { return(tree_->read_error()); }

      protected:

	// Tree being iterated over.
	base_avl_tree *tree_;

	// Records a path into the tree.  If branch[n] is true, indicates
	// take greater branch from the nth node in the path, otherwise
	// take the less branch.  branch[0] gives branch from root, and
	// so on.
	bset branch;

	// Zero-based depth of path into tree.
	unsigned depth;

	// Handles of nodes in path from root to current node (returned by *).
	handle path_h[max_depth - 1];

	int cmp_k_n(key k, handle h)
	  { return(tree_->abs.compare_key_node(k, h)); }
	int cmp_n_n(handle h1, handle h2)
	  { return(tree_->abs.compare_node_node(h1, h2)); }
	handle get_lt(handle h)
	  { return(tree_->abs.get_less(h, true)); }
	handle get_gt(handle h)
	  { return(tree_->abs.get_greater(h, true)); }
	handle null(void) { return(tree_->abs.null()); }

      };

    template<typename fwd_iter>
    bool build(fwd_iter p, size num_nodes)
      {
	// NOTE:  GCC allows me to define this outside the class definition
	// using the following syntax:
	//
	// template <class abstractor, unsigned max_depth, class bset>
	// template<typename fwd_iter>
	// inline void base_avl_tree<abstractor, max_depth, bset>::build(
	//   fwd_iter p, size num_nodes)
	//   {
	//     ...
	//   }
	//
	// but Visual C++ .NET won't accept it.  Is this a GCC extension?

	if (num_nodes == 0)
	  {
	    root = null();
	    return(true);
	  }

	// Gives path to subtree being built.  If branch[N] is false, branch
	// less from the node at depth N, if true branch greater.
	bset branch;

	// If rem[N] is true, then for the current subtree at depth N, it's
	// greater subtree has one more node than it's less subtree.
	bset rem;

        // Depth of root node of current subtree.
	unsigned depth = 0;

        // Number of nodes in current subtree.
	size num_sub = num_nodes;

	// The algorithm relies on a stack of nodes whose less subtree has
	// been built, but whose right subtree has not yet been built.  The
	// stack is implemented as linked list.  The nodes are linked
	// together by having the "greater" handle of a node set to the
	// next node in the list.  "less_parent" is the handle of the first
	// node in the list.
	handle less_parent = null();

	// h is root of current subtree, child is one of its children.
	handle h, child;

	for ( ; ; )
	  {
	    while (num_sub > 2)
	      {
		// Subtract one for root of subtree.
		num_sub--;
		rem[depth] = !!(num_sub & 1);
		branch[depth++] = false;
		num_sub >>= 1;
	      }

	    if (num_sub == 2)
	      {
		// Build a subtree with two nodes, slanting to greater.
		// I arbitrarily chose to always have the extra node in the
		// greater subtree when there is an odd number of nodes to
		// split between the two subtrees.

		h = *p;
		if (read_error())
		  return(false);
		p++;
		child = *p;
		if (read_error())
		  return(false);
		p++;
		set_lt(child, null());
		set_gt(child, null());
		set_bf(child, 0);
		set_gt(h, child);
		set_lt(h, null());
		set_bf(h, 1);
	      }
	    else  // num_sub == 1
	      {
		// Build a subtree with one node.

		h = *p;
		if (read_error())
		  return(false);
		p++;
		set_lt(h, null());
		set_gt(h, null());
		set_bf(h, 0);
	      }

	    while (depth)
	      {
		depth--;
		if (!branch[depth])
		  // We've completed a less subtree.
		  break;

		// We've completed a greater subtree, so attach it to
		// its parent (that is less than it).  We pop the parent
		// off the stack of less parents.
		child = h;
		h = less_parent;
		less_parent = get_gt(h);
		if (read_error())
		  return(false);
		set_gt(h, child);
		// num_sub = 2 * (num_sub - rem[depth]) + rem[depth] + 1
		num_sub <<= 1;
		num_sub += 1 - rem[depth];
		if (num_sub & (num_sub - 1))
		  // num_sub is not a power of 2
		  set_bf(h, 0);
		else
		  // num_sub is a power of 2
		  set_bf(h, 1);
	      }

	    if (num_sub == num_nodes)
	      // We've completed the full tree.
	      break;

	    // The subtree we've completed is the less subtree of the
	    // next node in the sequence.

	    child = h;
	    h = *p;
	    if (read_error())
	      return(false);
	    p++;
	    set_lt(h, child);

	    // Put h into stack of less parents.
	    set_gt(h, less_parent);
	    less_parent = h;

	    // Proceed to creating greater than subtree of h.
	    branch[depth] = true;
	    num_sub += rem[depth++];

	  } // end for ( ; ; )

	root = h;

	return(true);
      }

  protected:

    friend class iter;

    abstractor abs;

    handle root;

    handle get_lt(handle h, bool access = true)
      { return(abs.get_less(h, access)); }
    void set_lt(handle h, handle lh) { abs.set_less(h, lh); }

    handle get_gt(handle h, bool access = true)
      { return(abs.get_greater(h, access)); }
    void set_gt(handle h, handle gh) { abs.set_greater(h, gh); }

    int get_bf(handle h) { return(abs.get_balance_factor(h)); }
    void set_bf(handle h, int bf) { abs.set_balance_factor(h, bf); }

    int cmp_k_n(key k, handle h) { return(abs.compare_key_node(k, h)); }
    int cmp_n_n(handle h1, handle h2)
      { return(abs.compare_node_node(h1, h2)); }

    handle null(void) { return(abs.null()); }

  private:

    // Balances subtree, returns handle of root node of subtree
    // after balancing.
    handle balance(handle bal_h)
      {
	handle deep_h;

	// Either the "greater than" or the "less than" subtree of
	// this node has to be 2 levels deeper (or else it wouldn't
	// need balancing).

	if (get_bf(bal_h) > 0)
	  {
	    // "Greater than" subtree is deeper.

	    deep_h = get_gt(bal_h);
	    if (read_error())
	      return(null());

	    if (get_bf(deep_h) < 0)
	      {
		handle old_h = bal_h;
		bal_h = get_lt(deep_h);
		if (read_error())

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产午夜精品久久久久久免费视 | 成人app网站| 91小视频在线免费看| 欧美v日韩v国产v| 亚洲精品视频一区| 国产99久久久精品| 欧美一二三区在线观看| 玉足女爽爽91| 99精品在线观看视频| 久久综合色播五月| 日本不卡视频在线观看| 欧美自拍丝袜亚洲| 亚洲日本在线视频观看| 国产成+人+日韩+欧美+亚洲| 日韩一区二区三区四区五区六区| 一区二区三区在线观看欧美 | 在线观看视频一区二区欧美日韩| 欧美国产激情一区二区三区蜜月| 激情五月婷婷综合网| 日韩一区二区影院| 日韩和欧美一区二区三区| 91成人免费在线| 一区二区三区蜜桃网| 色哟哟国产精品免费观看| 亚洲免费观看高清完整版在线观看熊 | 亚洲综合另类小说| 91在线免费看| 亚洲欧美综合色| 成人一区二区视频| 久久精品人人做人人综合 | 国产欧美日韩综合| 国产乱码精品一区二区三区忘忧草| 91精品国产综合久久久久久久 | 国产日韩欧美制服另类| 精品一区二区三区不卡| 欧美mv和日韩mv国产网站| 喷白浆一区二区| 欧美成人在线直播| 国产精品综合一区二区三区| 国产香蕉久久精品综合网| 国产成人在线视频播放| 国产精品久久久久久妇女6080| 国产不卡在线一区| 亚洲欧美日韩国产中文在线| 欧美午夜精品免费| 日韩在线一二三区| 国产日韩欧美一区二区三区综合| 成人免费观看男女羞羞视频| 亚洲视频电影在线| 欧美亚洲国产一区在线观看网站| 亚洲风情在线资源站| 欧美成人一区二区三区| 国产sm精品调教视频网站| 亚洲欧洲日韩综合一区二区| 欧洲精品一区二区三区在线观看| 亚洲成人福利片| 久久这里只有精品首页| 99久久精品国产精品久久| 婷婷成人综合网| 久久久精品一品道一区| 91麻豆免费在线观看| 日韩高清电影一区| 国产欧美精品区一区二区三区 | 美女诱惑一区二区| 日本一区二区三区dvd视频在线| 不卡一区二区中文字幕| 亚洲高清久久久| 中文字幕电影一区| 欧美精选午夜久久久乱码6080| 国内精品国产三级国产a久久| 亚洲色图一区二区三区| 日韩免费成人网| 色哟哟国产精品| 国产乱色国产精品免费视频| 亚洲在线视频网站| 日本一区二区三区dvd视频在线| 欧美日本韩国一区二区三区视频| 懂色av一区二区在线播放| 亚洲一二三区在线观看| 国产午夜三级一区二区三| 精品污污网站免费看| 国产91丝袜在线观看| 欧美96一区二区免费视频| 亚洲欧美一区二区在线观看| 精品国产免费人成在线观看| 欧美性大战久久久久久久| 国产精品白丝av| 麻豆91在线播放| 亚洲一区二区三区国产| 国产精品毛片a∨一区二区三区| 91精品国产福利| 欧美午夜不卡在线观看免费| 成人avav影音| 成人爽a毛片一区二区免费| 久久99久国产精品黄毛片色诱| 亚洲精品网站在线观看| 中文字幕国产一区二区| 久久久亚洲精品一区二区三区| 日韩一区二区三区在线视频| 欧美日韩视频不卡| 91激情在线视频| 一本到不卡精品视频在线观看| 国产精品99久| 国产一区中文字幕| 精品一区免费av| 开心九九激情九九欧美日韩精美视频电影| 亚洲卡通欧美制服中文| 亚洲天堂精品在线观看| 国产精品初高中害羞小美女文| 欧美国产视频在线| 国产欧美精品在线观看| 国产精品日产欧美久久久久| 国产亚洲精品7777| 国产亚洲精品精华液| 国产精品嫩草影院av蜜臀| 欧美激情一区二区三区| 亚洲欧洲日韩av| 亚洲丝袜精品丝袜在线| 亚洲男人天堂av| 亚洲综合色视频| 五月天丁香久久| 蜜臂av日日欢夜夜爽一区| 久久精品国产澳门| 狠狠色丁香九九婷婷综合五月| 极品少妇一区二区三区精品视频| 玖玖九九国产精品| 国产精一区二区三区| 成人毛片视频在线观看| 一本色道久久综合亚洲91| 欧美日韩国产美| 精品国产麻豆免费人成网站| 日韩精品专区在线| 精品国产91亚洲一区二区三区婷婷| 26uuu国产一区二区三区| 国产日韩在线不卡| 一区二区三区中文字幕| 视频一区二区三区在线| 久久99久久99精品免视看婷婷| 国产精品99久久久| 欧美性欧美巨大黑白大战| 日韩美女一区二区三区四区| 日本一区二区三区四区在线视频| 中文字幕av不卡| 亚洲国产日韩一级| 久久99这里只有精品| a美女胸又www黄视频久久| 欧美三电影在线| 精品国产免费一区二区三区四区| 亚洲婷婷综合久久一本伊一区| 性欧美大战久久久久久久久| 国产一区二区中文字幕| av在线免费不卡| 日韩欧美自拍偷拍| 亚洲三级电影网站| 韩国一区二区三区| 欧美色区777第一页| 久久精品一区八戒影视| 亚洲国产精品久久久久秋霞影院| 亚洲大片免费看| 成人爽a毛片一区二区免费| 欧美精品一级二级三级| 国产精品久久久一区麻豆最新章节| 午夜欧美视频在线观看| 成人国产免费视频| 欧美人成免费网站| 成人免费在线观看入口| 久久精品国产亚洲5555| 在线观看国产精品网站| 中文乱码免费一区二区 | 中文字幕一区在线观看| 三级久久三级久久| 一本色道久久综合狠狠躁的推荐 | 一区二区三区四区不卡在线 | 亚洲天堂精品视频| 国产精品一区二区黑丝| 欧美男同性恋视频网站| 亚洲视频一区二区免费在线观看| 国产一区二区三区免费看| 日韩一区二区精品在线观看| 一区二区三区免费网站| 成人免费高清在线观看| 国产亚洲精品aa午夜观看| 老司机午夜精品| 在线不卡欧美精品一区二区三区| 一区二区三区不卡在线观看| 成人三级在线视频| 亚洲国产成人自拍| 国产酒店精品激情| 欧美tickling网站挠脚心| 水蜜桃久久夜色精品一区的特点| 97成人超碰视| 亚洲欧美日本在线| 91一区二区在线观看| 日韩一区日韩二区| 99re6这里只有精品视频在线观看| 国产午夜精品久久| 成人动漫一区二区| 国产精品免费久久久久| 北条麻妃国产九九精品视频| 国产精品天美传媒沈樵|