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的更多相关文章

  1. AVL树插入和删除

    一.AVL树简介 AVL树是一种平衡的二叉查找树. 平衡二叉树(AVL 树)是一棵空树,或者是具有下列性质的二叉排序树:    1它的左子树和右子树都是平衡二叉树,    2且左子树和右子树高度之差的 ...

  2. AVL树插入操作实现

    为了提高二插排序树的性能,规定树中的每个节点的左子树和右子树高度差的绝对值不能大于1.为了满足上面的要求需要在插入完成后对树进行调整.下面介绍各个调整方式. 右单旋转 如下图所示,节点A的平衡因子(左 ...

  3. AVL树插入(Python实现)

    建立AVL树 class AVLNode(object): def __init__(self,data): self.data = data self.lchild = None self.rchi ...

  4. 链表的C++实现——创建-插入-删除-输出-清空

    注:学习了数据结构与算法分析后,对链表进行了C++实现,参考博文:http://www.cnblogs.com/tao560532/articles/2199280.html 环境:VS2013 // ...

  5. 第七章 二叉搜索树 (d3)AVL树:删除

  6. AVL树的插入与删除

    AVL 树要在插入和删除结点后保持平衡,旋转操作必不可少.关键是理解什么时候应该左旋.右旋和双旋.在Youtube上看到一位老师的视频对这个概念讲解得非常清楚,再结合算法书和网络的博文,记录如下. 1 ...

  7. AVL树(查找、插入、删除)——C语言

    AVL树 平衡二叉查找树(Self-balancing binary search tree)又被称为AVL树(AVL树是根据它的发明者G. M. Adelson-Velskii和E. M. Land ...

  8. AVL树的插入和删除

    一.AVL 树 在计算机科学中,AVL树是最早被发明的自平衡二叉查找树.在AVL树中,任一节点对应的两棵子树的最大高度差为 1,因此它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下的时间复杂度 ...

  9. 二叉平衡树AVL的插入与删除(java实现)

    二叉平衡树 全图基础解释参考链接:http://btechsmartclass.com/data_structures/avl-trees.html 二叉平衡树:https://www.cnblogs ...

随机推荐

  1. Kioptrix Level 2

    简介 Vulnhub是一个提供各种漏洞环境的靶场平台. 个人学习目的:1,方便学习更多类型漏洞.2,为OSCP做打基础. 下载链接 https://www.vulnhub.com/entry/kiop ...

  2. ETCD数据迁移

    ETCD数据迁移 本文阅读对象为想要将Rainbond平台rbd-etcd切换至外部etcd的相关人员. 在k8s master节点创建secret 本文中将要切换的ETCD为根据Rainbond官方 ...

  3. nodejs的调试debug

    目录 简介 开启nodejs的调试 调试的安全性 使用WebStorm进行nodejs调试 使用Chrome devTools进行调试 使用node-inspect来进行调试 其他的debug客户端 ...

  4. (16)-Python3之--集合(set)操作

    1.定义 集合的关键字:set 集合主要作用: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 集合用大括号{}表示,元素间用逗号分隔. 建立集合类型用{} ...

  5. Linux 下安装 JDK

    JDK 依赖包: yum install glibc.i686 卸载原有的 JDK 查看本机已安装软件:rpm -qa 查看与java相关的软件:rpm -qa | grep java 删除自带软件: ...

  6. centralized collectors 中心化 采集器

    Fluent Bit https://fluentbit.io/ FluentBit is an open source specialized data collector. It provides ...

  7. threading.RLock()

    # Copyright 2001-2017 by Vinay Sajip. All Rights Reserved.## Permission to use, copy, modify, and di ...

  8. Pulsar Pub/Sub Messaging

    The Apache Software Foundation Announces Apache Pulsar as a Top-Level Project : The Apache Software ...

  9. asctime_s asctime

    asctime_s  asctime // rand随机数.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include "pch.h ...

  10. 正则re高级用法

    search 需求:匹配出文章阅读的次数 #coding=utf-8 import re ret = re.search(r"\d+", "阅读次数为 9999" ...