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. java static 变量,和方法从属于类

    第36集 java static 变量,和方法从属于类 可以用类来直接调用static属性和方法 static方法不能调用非静态的属性和方法,反之可以 new产生的对象,不包括static 属性和方法

  2. ANDROID_MARS学习笔记_S02_011_ANIMATION_LayoutAnimationController

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

  3. yii 验证器和验证码

    http://www.yiiframework.com/doc/api/1.1/CCaptcha http://www.cnblogs.com/analyzer/articles/1673015.ht ...

  4. c#执行Dos命令

    一个执行Dos命令的窗口程序,与各位分享.   效果图:     具体实现在代码中有详细的注释,请看代码.   实现执行CMD命令的核心代码(Cmd.cs):   [csharp]   using S ...

  5. 【C++】计算所有小于N的勾股数组合,可以写入txt文件保存,每组占一行。

    #ifndef PYTHAGOREAN_H_ #define PYTHAGOREAN_H_ #include <iostream> class Pythagorean { public: ...

  6. UIScrollView,UIView转换UIImage代码(整个view截图, 不只是可视区域)

    -(UIImage*)captureView:(UIView *)theView{     CGRect rect = theView.frame;     if ([theView isKindOf ...

  7. winhex的使用

    1. 当我们用VC编写代码,将数据写入到磁盘的文件后,用winhex查看,winhex可以检测文件最后写入时间的变动,如果有变动,则会提示“此文件的最后写入时间已经改变.解除当前状态并重新载入吗?”, ...

  8. 步步为营 SharePoint 开发学习笔记系列总结

    转:http://www.cnblogs.com/springyangwc/archive/2011/08/03/2126763.html 概要 为时20多天的sharepoint开发学习笔记系列终于 ...

  9. REST Web 服务介绍

    在项目上使用到了Rest技术,应该是Rest的服务概念才对.主要是对外(BPM)暴露API来提供Service.推荐一篇有质量的文章,接下来会系统一点的学习一下Restful概念.http://kb. ...

  10. Nginx、SSL双向认证、PHP、SOAP、Webservice、https

    本文是1:1模式,N:1模式请参见新的一篇博客<SSL双向认证(高清版)> ----------------------------------------------------- 我是 ...