Splay Tree的删除操作
Splay Tree的插入操作,搜索操作,和删除操作都实现了,那么就能够使用来解题了。
指针的删除操作的处理还是那么难的,非常多坎须要避开.
同一个坎还是坑了我好多次,就是指针传递的问题,什么时候须要改动指针本身的值,就必须返回指针或者传递指针的指针,或者传递指针的的实參。
这里的删除操作就是须要改变传递到函数的指针本身的,所以我这里使用了返回指针操作。
还有删除树的问题,之前的代码没做删除操作,所以没问题,如今须要逐个节点删除,所以要小心不能把整个树都删除了。
至此, splay 树的功能差点儿相同完好了。
代码中凝视标明了几个坑都被我碰到了。
#pragma once
#include <stdio.h> class SplayTreeComplete
{
struct Node
{
int key;
Node *left, *right;
explicit Node(int k):key(k),left(NULL),right(NULL){}
/*~Node()
{教训:这种话整颗树都删除了,不能这么删除,要逐个节点删除
if (left) delete left, left = NULL;
if (right) delete right, right = NULL;
}*/
}; Node *leftRotate(Node *x)
{
Node *y = x->right;
x->right = y->left;
y->left = x;
return y;
} Node *rightRotate(Node *x)
{
Node *y = x->left;
x->left = y->right;
y->right = x;
return y;
} Node *splay(Node *root, const int key)
{
if (!root || key == root->key) return root; if (key < root->key)
{
if (!root->left) return root; if (key < root->left->key)
{
root->left->left = splay(root->left->left, key);
root = rightRotate(root);
}
else if (root->left->key < key)
{
root->left->right = splay(root->left->right, key);
if (root->left->right) root->left = leftRotate(root->left);
}
return root->left? rightRotate(root) : root;
} if (!root->right) return root;
if (root->right->key < key)
{
root->right->right = splay(root->right->right, key);
root = leftRotate(root);
}
else if (key < root->right->key)
{
root->right->left = splay(root->right->left, key);
if (root->right->left) root->right = rightRotate(root->right);
}
return root->right? leftRotate(root) : root;
} Node *insert(Node *root, const int key)
{
if (!root) return new Node((int)key);//别忘了创建新的节点 root = splay(root, key);//别忘了 root = if (key == root->key) return root; Node *newNode = new Node((int)key);
if (key < root->key)
{
newNode->right = root;
newNode->left = root->left;
root->left = NULL;//别漏了这句,否则破坏了树结构
}
else
{
newNode->left = root;
newNode->right = root->right;
root->right = NULL;
}
return newNode;
} Node *deleteNode(Node *root, const int key)
{
if (!root) return root; root = splay(root, key); if (key == root->key)
{
if (!root->left)
{
Node *x = root;
root = root->right;
delete x, x = NULL;
}
else
{
Node *x = root->right;
Node *y = root->left;
delete root;
root = splay(y, key);
root->right = x;
}
}
return root;
} void preOrder(Node *root)
{
if (root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
public:
SplayTreeComplete()
{
Node *root = NULL;
int keys[] = {100, 50, 200, 40, 30, 20, 25};
int n = sizeof(keys) / sizeof(keys[0]);
for (int i = 0; i < n; i++)
{
root = insert(root, keys[i]);
} printf("\nInser create Preorder traversal Splay tree is \n");
preOrder(root);
putchar('\n'); root = splay(root, 50);
bool found = root->key == 50; printf("\n50 is %s the tree\n", found? "in" : "not in"); root = deleteNode(root, 50);//root 发生改变了,所以必须返回新的指针值 root = splay(root, 50);
found = root->key == 50; printf("\n50 is %s the tree\n", found? "in" : "not in"); printf("\nInser create Preorder traversal Splay tree is \n");
preOrder(root);
putchar('\n'); deleteTree(root);
} void deleteTree(Node *root)
{
if (root)
{
deleteTree(root->left);
deleteTree(root->right);
delete root, root = NULL;
}
}
};
Splay Tree的删除操作的更多相关文章
- HDU1890 Robotic Sort Splay tree反转,删除
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...
- POJ-3468 A Simple Problem with Integers Splay Tree区间练习
题目链接:http://poj.org/problem?id=3468 以前用线段树做过,现在用Splay Tree A了,向HH.kuangbin.cxlove大牛学习了各种Splay各种操作,,, ...
- HDU-3436 Queue-jumpers 树状数组 | Splay tree删除,移动
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3436 树状数组做法<猛戳> Splay tree的经典题目,有删除和移动操作.首先要离散化 ...
- splay tree旋转操作 hdu 1890
很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...
- bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...
- 伸展树 Splay Tree
Splay Tree 是二叉查找树的一种,它与平衡二叉树.红黑树不同的是,Splay Tree从不强制地保持自身的平衡,每当查找到某个节点n的时候,在返回节点n的同时,Splay Tree会将节点n旋 ...
- [转] Splay Tree(伸展树)
好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...
- 树-伸展树(Splay Tree)
伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...
- Splay tree
类别:二叉排序树 空间效率:O(n) 时间效率:O(log n)内完成插入.查找.删除操作 创造者:Daniel Sleator和Robert Tarjan 优点:每次查询会调整树的结构,使被查询频率 ...
随机推荐
- 练习C之SELECT形式的非阻塞IO
呵呵,理解得不深,但毕竟手打全版,且无错.但select.h不知何处找头文件, 粘下来作个记录. POLL,EPOLL感觉代码类似,只是函数和系统实现不一样,,EPOLL目前最合理的.定位精确,算法复 ...
- 在eclipse中将项目发布到tomcat的root目录
(1)设置项目上下文,右击项目-properties >Web Page Edit
- USB CDC类
现代嵌入式系统中,异步串行通信接口往往作为标准外设出现在单片机和嵌入式系统中.但是随着个人计算机通用外围设备越来越少地使用串口,串口正在逐渐从个人计算机特别是便携式电脑上消失.于是嵌入式开发人员常常发 ...
- div+css实现导航示意箭头
1.Div的宽高为100 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <h ...
- 【DataStructure In Python】Python模拟二叉树
使用Python模拟二叉树的基本操作,感觉写起来很别扭.最近做编译的优化,觉得拓扑排序这种东西比较强多.近期刷ACM,发现STL不会用实在太伤了.决定花点儿时间学习一下STL.Boost其实也很强大. ...
- Eclipse全屏及插件下载
Eclipse全屏插件下载 解压下载的压缩包,将 plugins 文件夹中的 cn.pande.eclipsex.fullscreen_1.0.7.jar 文件拷贝到Eclipse安装目录下的 ...
- hadoop mapreduce核心功能描述
核心功能描述 应用程序通常会通过提供map和reduce来实现 Mapper和Reducer接口,它们组成作业的核心. Mapper Mapper将输入键值对(key/value pair)映射到一组 ...
- msvc2010生成的指令序列有问题,可能跟pgo有关
正常序列 有问题序列 这段代码程序启动是执行,会导致崩溃 工程使用ltcg pgo,也就是说,第一次编译连接完成后,会跑一次profile,再执行连接器代码生成优化. 构建记录显示,ltcg已跑完,说 ...
- iOS7 iOS8 毛玻璃效果的分别实现
iOS8用系统的, iOS7用第三方的(效果还是挺快的.) https://github.com/KiranPatel-iOS/KPBlurEffect [_headBGIV sd_setImageW ...
- weblogic配置数据源出错
Connection test failed. Listener refused the connection with the following error: ORA-12505, TNS:lis ...