00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 # ifndef TPL_BALANCEXT_H
00045 # define TPL_BALANCEXT_H
00046
00047 # include <tpl_binNodeXt.H>
00048
00049 using namespace Aleph;
00050
00051 namespace Aleph {
00052
00071 template <class Node> inline
00072 Node * select_gotoup_root(Node * root, const size_t & i)
00073 {
00074
00075 I(root not_eq Node::NullPtr);
00076 I(COUNT(Node::NullPtr) == 0);
00077
00078 if (i >= COUNT(root))
00079 throw std::out_of_range ("");
00080
00081 if (i == COUNT(LLINK(root)))
00082 return root;
00083
00084 if (i < COUNT(LLINK(root)))
00085 {
00086 LLINK(root) = select_gotoup_root(LLINK(root), i);
00087 root = rotate_to_right_xt(root);
00088 }
00089 else
00090 {
00091 RLINK(root) =
00092 select_gotoup_root(RLINK(root), i - COUNT(LLINK(root)) - 1);
00093 root = rotate_to_left_xt(root);
00094 }
00095
00096 return root;
00097 }
00111 template <class Node> inline
00112 Node * balance_tree(Node * root)
00113 {
00114 if (COUNT(root) <= 1)
00115 return root;
00116
00117 root = select_gotoup_root(root, COUNT(root) / 2);
00118
00119 LLINK(root) = balance_tree(LLINK(root));
00120 RLINK(root) = balance_tree(RLINK(root));
00121
00122 return root;
00123 }
00124
00125 }
00126
00127 # endif // TPL_BALANCEXT_H
00128