这个树的定义的确很有问题。其实AVLTree和SplayTree以及RedBlackTree都是self-adjusting Tree
因为如果是树做索引那么大部分的时间将用来查找。这个树在查找上可以将查找的节点提升到树根,插入时也是一样。这样提高了查找的效率。并且操作系统有一种说法是近来用到的,将来用到的几率也会很大(LRU原理)。删除,既然删除对于此树来说再也没有什么,那就直接删除算了。
根据这几点想法。自己做一个self-adjusting Tree。但是还没有验证是否能达到实际水平的O(nlogn)。虽然理论上这个树并不能达到这样一个时间复杂度。但是对于实际数据来说,还是有可能达到的。不知道有没有这么lucky。
因为对于这个树来说插入和查找操作其实差不多。
所以下面只介绍插入操作。找到要插入的地方,反遍历单旋转(这里和SplayTree不同一点是只使用单旋转)
TreeNode<T>* insertNode(TreeNode<T>* subroot,TreeNode<T>* prev,T& data)
返回值为当前子树根节点
1.subroot是否为空,创建新的节点,并返回subroot
2.如果不为空
A.subroot中的值 < data中的值,对subroot右子树调用insertNode(),subroot与当前右子树根节点采用旋转操作。将两个节点位置对换。
B.subroot中的值> data中的值,对subroot左子树调用insertNode(),subroot与当前左子树根节点采用旋转操作。将两个节点位置对换。
C.subroot中的值== data中的值,do nothing
3.查看该节点是否已成为根节点。如果是的话,调整根节点的值。
下面给出代码。
-
template<classT>
-
classSelfTree:publicBinaryTree<T>
- {
-
private:
-
-
-
-
- TreeNode<T>*sLeftRotate(TreeNode<T>*a,TreeNode<T>*b)
- {
- a->setLeft(b->getRight());
- b->setRight(a);
-
returnb;
- }
-
-
-
-
- TreeNode<T>*sRightRotate(TreeNode<T>*a,TreeNode<T>*b)
- {
- a->setRight(b->getLeft());
- b->setLeft(a);
-
returnb;
- }
- TreeNode<T>*insertNode(TreeNode<T>*subroot,TreeNode<T>*prev,T&data)
- {
-
if(subroot==NULL)
- {
-
subroot=newTreeNode<T>(data);
-
returnsubroot;
- }
-
else
- {
- TreeNode<T>*tempNode=NULL;
- TreeNode<T>*tempRoot=NULL;
-
if(subroot->getData()>data)
- {
- tempNode=insertNode(subroot->getLeft(),subroot,data);
- tempRoot=sLeftRotate(subroot,tempNode);
- }
-
elseif(subroot->getData()<data)
- {
- tempNode=insertNode(subroot->getRight(),subroot,data);
- tempRoot=sRightRotate(subroot,tempNode);
- }
-
else
- {
-
throw"thesamekey";
- }
-
if(prev==NULL)
- {
-
this->head=tempRoot;
- }
-
else
- {
-
if(prev->getData()>tempRoot->getData())
- {
- prev->setLeft(tempRoot);
- }
-
else
- {
- prev->setRight(tempRoot);
- }
- }
-
returntempRoot;
- }
- }
-
public:
- SelfTree(T&data):BinaryTree(data)
- {
- }
- ~SelfTree()
- {
- }
-
voidinsertNode(T&data)
- {
-
insertNode(this->head,NULL,data);
- }
- };
分享到:
相关推荐
The splay tree, a self-adjusting form of binary search tree, is developed and analyzed. The binary search tree is a data structure for representing tables and lists so that accessing, inserting, and...
这是一篇很好的文献,研究共轭梯度法的很新的一篇文献
This paper proposed a robust diffusion estimation algorithm based on a minimum error entropy criterion with a self-adjusting step-size, which are referred to as the diffusion MEE-SAS (DMEE-SAS) ...
matlab代码粒子群算法分类中大型特征选择的自适应粒子群算法 分类的进化功能选择我们非常乐意与您共享相关的Matlab代码。 如果您根据此研究工作做进一步研究,请在您的论文中引用此研究作为参考,如下所示: X. Y. ...
自调整间隔引言尝试构建准确的计时器时,缺少javascript的setInterval和setTimeout ,此外,由于javascript处于闲置状态,甚至可能使您的计时器中断!自救计时器来了! 经过测试- Nodejs的浏览器React本机它是如何...
cvar代码matlab 一种用于自调度问题的新型DRO模型 那这个项目/研究呢?...adjusting the size of the ambiguity set, so it provides a suitable and adjustable self-scheduling strategy for generation companie
We reported a wavelength-flexible all-polarization-maintaining self-sweeping fiber laser based on the intracavity loss tuning brought by the bent optical fiber. The bidirectional cavity structure ...
Physical Elasticity Rolling Couplet Advertising Code, Self-adjusting Elasticity Width
This paper introduces a self-tuning fuzzy temperature control system for the diode-pumped solid-state (DPSS) blue laser at 473 nm. This temperature control system includes circuit of temperature ...
海德汉PWM20ATS软件pdf,海德汉PWM20ATS软件
Self-adjusting power-saving modes for longest battery life Speed motion detection up to 20 ips and 8G Enhanced SmartSpeed self-adjusting frame rate for optimum performance Motion detect pin output ...
then improved accuracy by adjusting different aspect of algorithms. 最终决策树 数据集来源() 数据集创建者: 匈牙利心脏病研究所。 布达佩斯:医学博士Andras Janosi 瑞士苏黎世大学医院:医学博士...
Consider adjusting the PKG_CONFIG_PATH environment variable if you installed software in a non-standard prefix. Alternatively, you may set the environment variables PCRE_CFLAGS and PCRE_LIBS to avoid...
我们研究了一个多时期的网络收益管理(RM)问题,其中卖方在先验需求函数未知的环境下,以有限的容量销售由多种资源制成的多种产品。 卖方的目的是共同了解产品的需求和定价,以最大程度地减少其预期的收入损失。...
由2-嘧啶和4-吡啶硫乙酸根调节Ag(I)配位聚合物螺旋结构,黄永清,章慧,本文合成并晶体结构表征了新颖的手性配位聚合物[Ag(pmta)]n (1, pmta- =2-嘧啶硫乙酸根)。在[Ag(pmta)]n的扭曲四面体结构中,每个银原子与周�
Umix 是一个用于在声卡混音器中调整声卡音量和其他功能的程序。
Pre-emphasis automatic adjusting system, method of adjusting pre-emphasis and pre-emphasis setting signal generating circuit Distributed pre-emphasis equalizer Pre-emphasis automatic adjusting system,...
Multichannel Mixer For Adjusting The Output Of Audio Sources Using The Mixer API Functions.
Example Adjusting Image Settings of ic imaging source
When the erbium fiber coil temperature cycles from 26 to 70 ℃, the gain tilt variation less than 0.3 dB is achieved experimentally by adjusting only the pre-inserted variable opti