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的删除操作的更多相关文章

  1. HDU1890 Robotic Sort Splay tree反转,删除

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...

  2. POJ-3468 A Simple Problem with Integers Splay Tree区间练习

    题目链接:http://poj.org/problem?id=3468 以前用线段树做过,现在用Splay Tree A了,向HH.kuangbin.cxlove大牛学习了各种Splay各种操作,,, ...

  3. HDU-3436 Queue-jumpers 树状数组 | Splay tree删除,移动

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3436 树状数组做法<猛戳> Splay tree的经典题目,有删除和移动操作.首先要离散化 ...

  4. splay tree旋转操作 hdu 1890

    很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...

  5. bzoj 3223/tyvj 1729 文艺平衡树 splay tree

    原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...

  6. 伸展树 Splay Tree

    Splay Tree 是二叉查找树的一种,它与平衡二叉树.红黑树不同的是,Splay Tree从不强制地保持自身的平衡,每当查找到某个节点n的时候,在返回节点n的同时,Splay Tree会将节点n旋 ...

  7. [转] Splay Tree(伸展树)

    好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...

  8. 树-伸展树(Splay Tree)

    伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...

  9. Splay tree

    类别:二叉排序树 空间效率:O(n) 时间效率:O(log n)内完成插入.查找.删除操作 创造者:Daniel Sleator和Robert Tarjan 优点:每次查询会调整树的结构,使被查询频率 ...

随机推荐

  1. Mysql的列索引和多列索引(联合索引)

    转自:http://blog.chinaunix.net/uid-29305839-id-4257512.html 创建一个多列索引:CREATE TABLE test (      id       ...

  2. Codeforces Round #232 (Div. 1)

    这次运气比较好,做出两题.本来是冲着第3题可以cdq分治做的,却没想出来,明天再想好了. A. On Number of Decompositions into Multipliers 题意:n个数a ...

  3. asp.net 框架接触(2)

    1. 学习一个框架就要尽量按照它的各种规则(命名规则等)来命名,写代码 比如 下列Entity层内的代码"StudentInfo"编写应与数据库内的表名严格对应 不然就会报错 [T ...

  4. ANDROID_MARS学习笔记_S02_011_ANIMATION_LayoutAnimationController

    一.简介 二.代码1.xml(1)activity_main.xml <ListView android:id="@id/android:list" android:layo ...

  5. 【Linux安全】查看是否存在特权用户以及是否存在空口令用户

    查看是否存在特权用户 通过判断uid是否为0来查找系统是否存在特权用户,使用命令awk即可查出. [root@pentester ~]# awk -F: '$3==0 {print $1}' /etc ...

  6. Java:JXL解析Excel文件

    项目中,有需求要使用JXL解析Excel文件. 解析Excel文件 我们先要将文件转化为数据流inputStream. 当inputStream很大的时候 会造成Java虚拟器内存不够 抛出内存溢出 ...

  7. Android开发之PackageManager类

    PackageManger,可以获取到手机上所有的App,并可以获取到每个App中清单文件的所有内容. 设置应用程序版本号在应用程序的manifest文件中定义应用程序版本信息.2个必须同时定义的属性 ...

  8. BZOJ_1026_[SCOI2009]_windy数_(数位dp)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1026 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为wi ...

  9. SCOI2007排列perm

    1072: [SCOI2007]排列perm Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 805  Solved: 497[Submit][Stat ...

  10. Win32下 Qt与Lua交互使用(三):在Lua脚本中connect Qt 对象

    话接上文.笔者为了方便使用Lua,自己编写了一个Lua的类.主要代码如下: QLua.h #ifndef QLUA_H #define QLUA_H // own #include "inc ...