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 优点:每次查询会调整树的结构,使被查询频率 ...
随机推荐
- spring+mybatis的优缺点
mybatis的优缺点: 优点: 1. 易于上手和掌握. 2. sql写在xml里,便于统一管理和优化. 3. 解除sql与程序代码的耦合. 4. 提供映射标签,支持对象与数据库的orm字段关系映射 ...
- XML和JSON 序列化以及DataTable转JSON
using System.IO; using System.Text; using System.Xml.Serialization; using System.Xml; using System.R ...
- Android:WebView深入使用
webView = (WebView) findViewById(R.id.info_detail_webview); WebSettings webSettings = webView.getSet ...
- Spring AOP实现方式四之注入式AspectJ切面【附源码】
现在我们要讲的是第四种AOP实现之注入式AspectJ切面 通过简单的配置就可以实现AOP了. 源码结构: 1.首先我们新建一个接口,love 谈恋爱接口. package com.spring.ao ...
- Android开发UI之Toast的使用
Toast,A toast provides simple feedback about an operation in a small popup. 对于操作提供一个简单反馈信息. 官网链接:htt ...
- CSS属性一览
CSS 属性 CSS 属性组: 动画 背景 边框和轮廓 盒(框) 颜色 内容分页媒体 定位 可伸缩框 字体 生成内容 网格 超链接 行框 列表 外边距 Marquee 多列 内边距 分页媒体 定位 打 ...
- hadoop mapreduce核心功能描述
核心功能描述 应用程序通常会通过提供map和reduce来实现 Mapper和Reducer接口,它们组成作业的核心. Mapper Mapper将输入键值对(key/value pair)映射到一组 ...
- bzoj1801
题目就是每行每列最多放两个炮的意思: 首先不难想到状态压缩dp,但是当n,m<=100的时候显然会跪掉: 考虑每行最多就2个点,状压dp浪费了大量的空间 由于每行最多两个点,我们可以直接用f[i ...
- C#编程实现Excel文档中搜索文本
有了在Word文档中编程实现搜索文本的经验,在Excel中实现这个功能也并非难事. 打开Excel的VBA帮助,查看Excel的对象模型,很容易找到完成这个功能需要的几个集合和对象:Applicati ...
- 从头开始编写一个Orchard网上商店模块(5) - 创建和渲染ProductCatalog的内容类型
原文地址: http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-pa ...