创建AVL树,插入,删除,输出Kth Min
https://github.com/TouwaErioH/subjects/tree/master/C%2B%2B/PA2
没有考虑重复键,可以在结构体内加一个int times。
没有考虑删除不存在的键,加个判断即可。
#include <stdio.h>
#include <assert.h>
#include<iostream>
#include <algorithm>
#include <algorithm>
using namespace std;
int cnt=0;
int max(int a, int b)
{
return (a > b ? a : b);
}
struct node
{
int key;
int height;
int size; //tree node 个数
node *left, *right;
/*
node(int x) : key(x), height(1), size(1), left(NULL), right(NULL) {}
node() : key(NULL), height(NULL), size(NULL), left(NULL), right(NULL){}
*/
node(int k)
{
key = k;
height = 1;
size = 1;
left = right = 0;
}
}; int height(node* r)
{
return r ? r->height : 0;
} void update_height(node* root)
{
root->height = max(height(root->left), height(root->right)) + 1;
} int sizeOfTree(node* root){
return root == NULL? 0 : root->size;
} void right_rotate(node*& ref_root)
{
node *y = ref_root->left;
ref_root->left = y->right;
y->right = ref_root; ref_root->size = sizeOfTree(ref_root->left) + sizeOfTree(ref_root->right) + 1;
update_height(ref_root);
y->size = sizeOfTree(y->left) + sizeOfTree(y->right) + 1;
update_height(y);
ref_root=y;
} void left_rotate(node*& ref_root)
{
node *y = ref_root->right;
ref_root->right = y->left;
y->left = ref_root; ref_root->size = sizeOfTree(ref_root->left) + sizeOfTree(ref_root->right) + 1;
update_height(ref_root);
y->size = sizeOfTree(y->left) + sizeOfTree(y->right) + 1;
update_height(y);
ref_root=y;
} //after deletion and insertion, maintain a balanced tree.
void maintain(node*& ref_root)
{
int balance=0;
if (!ref_root) return ;
update_height(ref_root);
balance = height(ref_root->left)-height(ref_root->right);
if (balance > 1)
{
if ((height(ref_root->left->left)-height(ref_root->left->right)) < 0) //LR
left_rotate(ref_root->left);
right_rotate(ref_root);
}
else if (balance < -1)
{
if ((height(ref_root->right->left)-height(ref_root->right->right)) > 0) //RL
right_rotate(ref_root->right);
left_rotate(ref_root);
}
} void insert_key(int key, node*& ref_root)
{
cnt ++;
node*p=new node(key);
if(!ref_root){
node*p=new node(key);
ref_root=p;
}
if(key < ref_root->key){
insert_key(key,ref_root->left);
ref_root->size=ref_root->size+1;
}
else if(key > ref_root->key){
insert_key(key,ref_root->right);
ref_root->size=ref_root->size+1;
}
/* calculate the height after insertion */
update_height(ref_root);
maintain(ref_root);
} void delete_key(int key, node*& ref_root)
{
if(key < ref_root->key){
ref_root->size=ref_root->size-1;
delete_key(key, ref_root->left);
}
else if(key > ref_root->key){
ref_root->size=ref_root->size-1;
delete_key(key,ref_root->right);
}
else {
if(!ref_root->left) {
struct node *temp = ref_root->right;
//free( ref_root);
ref_root = temp;
if(ref_root!=NULL)
ref_root->size=ref_root->size;
} else if(! ref_root->right) {
struct node *temp = ref_root->left;
//free (ref_root);
ref_root = temp;
} else {
struct node *temp = ref_root->right;
while(temp->left != NULL)
temp = temp->left;
ref_root->key = temp->key;
temp->key=key;
ref_root->size=ref_root->size-1;
delete_key(key,ref_root->right);
}
}
if(ref_root!=NULL)
update_height(ref_root);
maintain(ref_root);
} int KthMin(node * root, int k){
if(root->left!=NULL){
int lSize = sizeOfTree(root->left);
if (k <= lSize)
return KthMin(root->left, k);
else if (lSize + 1 < k)
return KthMin(root->right, k - lSize - 1);
return root->key;
}
else
{
if(root->right==NULL)
return root->key;
else if(k==1)
return root->key;
else
return KthMin(root->right, k - 1); }
} void printtree(node* root) {
if (root == NULL) {
return;
}
cout << root->key<<'X'<<endl;
cout<<"L"<<endl;
printtree(root->left);
cout<<"R"<<endl;
printtree(root->right);
if (root->left == NULL || root->right == NULL) {
return;
}
} void preOrder(node *root)
{
if(root != NULL)
{
printf("k %d s %d h %d ", root->key,root->size,root->height);
preOrder(root->left);
preOrder(root->right);
}
} int main()
{
node *root=NULL;
char op[10] = "";
int k;
while(true)
{
//if(cnt>1)
// { preOrder(root);/*printtree(root);*/}
scanf("%s", op);
if(op[0] == 'E') break;
switch(op[0])
{
case 'A': scanf("%d", &k); insert_key(k, root); break;
case 'D': scanf("%d", &k); delete_key(k, root); break;
case 'M': scanf("%d", &k); printf("%d\n", KthMin(root, k));break;
default: assert(0);
}
} return 0;
}
效果

创建AVL树,插入,删除,输出Kth Min的更多相关文章
- AVL树插入和删除
一.AVL树简介 AVL树是一种平衡的二叉查找树. 平衡二叉树(AVL 树)是一棵空树,或者是具有下列性质的二叉排序树: 1它的左子树和右子树都是平衡二叉树, 2且左子树和右子树高度之差的 ...
- AVL树插入操作实现
为了提高二插排序树的性能,规定树中的每个节点的左子树和右子树高度差的绝对值不能大于1.为了满足上面的要求需要在插入完成后对树进行调整.下面介绍各个调整方式. 右单旋转 如下图所示,节点A的平衡因子(左 ...
- AVL树插入(Python实现)
建立AVL树 class AVLNode(object): def __init__(self,data): self.data = data self.lchild = None self.rchi ...
- 链表的C++实现——创建-插入-删除-输出-清空
注:学习了数据结构与算法分析后,对链表进行了C++实现,参考博文:http://www.cnblogs.com/tao560532/articles/2199280.html 环境:VS2013 // ...
- 第七章 二叉搜索树 (d3)AVL树:删除
- AVL树的插入与删除
AVL 树要在插入和删除结点后保持平衡,旋转操作必不可少.关键是理解什么时候应该左旋.右旋和双旋.在Youtube上看到一位老师的视频对这个概念讲解得非常清楚,再结合算法书和网络的博文,记录如下. 1 ...
- AVL树(查找、插入、删除)——C语言
AVL树 平衡二叉查找树(Self-balancing binary search tree)又被称为AVL树(AVL树是根据它的发明者G. M. Adelson-Velskii和E. M. Land ...
- AVL树的插入和删除
一.AVL 树 在计算机科学中,AVL树是最早被发明的自平衡二叉查找树.在AVL树中,任一节点对应的两棵子树的最大高度差为 1,因此它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下的时间复杂度 ...
- 二叉平衡树AVL的插入与删除(java实现)
二叉平衡树 全图基础解释参考链接:http://btechsmartclass.com/data_structures/avl-trees.html 二叉平衡树:https://www.cnblogs ...
随机推荐
- mastercam2018安装教程
安装前先关闭杀毒软件和360卫士,注意安装路径不能有中文,安装包路径也不要有中文. [安装环境]:Win7/Win8/Win10 1.选中[Mastercam2018]压缩包,鼠标右击选择[解压到Ma ...
- Nginx报504 gateway timeout错误的解决方法(小丑搞笑版。。。)
一.今天登录我的网站,突然发现报了下面的一个错误: 我的第一反应是:超时了应该是Nginx代理没有设置超时时间,默认的超时时间估计太小了,然后就按照正常的方式用Xshell连接服务器,应该是网络或者是 ...
- 并发编程之fork/join(分而治之)
1.什么是分而治之 分而治之就是将一个大任务层层拆分成一个个的小任务,直到不可拆分,拆分依据定义的阈值划分任务规模. fork/join通过fork将大任务拆分成小任务,在将小任务的结果join汇总 ...
- python(re正则)
import re #导入模块 info = 'qwewwer12332423kdsjfkl2342kdjfl213nkafal123123' 例1: res1 = re.compile('er( ...
- Linux top命令里面%CPU和cpu(s)的差别
有的同学会把%CPU和us%搞晕,也就是下图所示在top的时候查看cpu的信息. 这时有的同学会问:这两个CPU到底哪个是对的. 其实都是对的,只是表达的意思不一样. 官方解释如下 Cpu(s):34 ...
- 通俗易懂的解释:什么是API
API 全称 Application Programming Interface,即应用程序编程接口. 看到这里,急性子的小白同学马上就憋不住了:这不管是英文还是中文我每个字都懂啊,只是凑一块就不知道 ...
- 【转】使用ssh-keygen和ssh-copy-id三步实现SSH无密码登录
[原]http://blog.chinaunix.net/uid-26284395-id-2949145.html ssh-keygen 产生公钥与私钥对. ssh-copy-id 将本机的公钥复制 ...
- (转载)微软数据挖掘算法:Microsoft 目录篇
本系列文章主要是涉及内容为微软商业智能(BI)中一系列数据挖掘算法的总结,其中涵盖各个算法的特点.应用场景.准确性验证以及结果预测操作等,所采用的案例数据库为微软的官方数据仓库案例(Adventure ...
- .NET5 它来了!微软大一统时代来临!
今天双11,Microsoft released.NET 5(在他们的开发博客上同时发布).新版本的重点是改进.NET Core 3.1: 更小的单文件应用程序.对 Windows ARM64的支持以 ...
- LOJ2436
题目描述 幼儿园里有 N 个小朋友, lxhgww 老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于 ...