Treap堆的插入,删除操作
来源:互联网 发布:centos 指定ip访问ssh 编辑:程序博客网 时间:2024/05/02 04:16
#include <stdio.h>#include <stdlib.h>#include <iomanip>#include <time.h>#include <iostream>using namespace std;typedef struct node{ node* left; node* right; int priority; int key;}*Node;typedef struct treap{ Node root;}*Treap;void rotate_left(Node node){ Node x = node->right; node->right = x->left; x->left = node; node = x;}void rotate_right(Node node){ Node x = node->left; node->left = x->right; x->right = node; node = x;}void insert(Node root, int key, int priority){ if (root == NULL) { root = (Node)malloc(sizeof(Node)); root->left = NULL; root->right = NULL; root->priority = priority; root->key = key; } else if (key < root->key) { insert(&(root->left), key, priority); if (root->left->priority < root->priority) rotate_right(root); } else { insert(&(root->right), key, priority); if (root->right->priority < root->priority) rotate_left(root); }}void deletenode(Node root, int key){ if (root != NULL) { if (key < root->key) deletenode(&(root->left), key); else if (key > root->key) deletenode(&(root->right), key); else { if (root->left == NULL) root = root->right; else if (root->right == NULL) root = root->left; else { if (root->left->priority < root->right->priority) { rotate_right(root); deletenode(&(root->right), key); } else { rotate_left(root); deletenode(&(root->left), key); } } } }}void output(Node tree, int key, int x){ if(tree != NULL) { if(x==0) cout << setw(2) << tree->key <<" is root" << " 优先级为:"<<tree->priority<<endl; else cout << setw(2) << tree->key <<" is " << setw(2) << key << "'s " << setw(12) << (x==1?"right child" : "left child") << " 优先级为:"<<tree->priority<<endl; output(tree->left, tree->key, -1); output(tree->right,tree->key, 1); }}void output(Node mRoot){ if (mRoot != NULL)output(mRoot, mRoot->key, 0);}int main(){ Treap treap = (Treap)malloc(sizeof(Treap)); treap->root = NULL; int i = 0; cout<<"请输入要插入的节点的数目:"<<endl; int n,a[20]; cin>>n; srand(time(0)); cout<<"请输入要插入节点的key值:"<<endl; for (i = 0; i < n; i++) {cin>>a[i]; insert(&(treap->root), a[i], rand()); } output(treap->root); cout<<"请输入要删除节点的key值:"<<endl;cin>>n; deletenode(&(treap->root), n);output(treap->root); system("pause"); return 0;}
0 0
- Treap堆的插入,删除操作
- 堆的建立、删除、插入操作以及堆排序
- 堆的插入、删除和建立操作,堆排序
- 堆的插入、删除和建立操作,堆排序
- java实现堆的操作(建堆,插入,删除)
- 堆 的取最值删除操作和插入操作
- 最大堆的初始化、删除、插入等基本操作
- 二叉堆的插入删除等操作C++实现
- 堆的动态操作(插入、删除、调整)
- 堆的基本操作:定义、创建、插入、删除、排序
- 最大堆的插入/删除/调整/排序操作
- 堆的插入 删除 排序
- 堆的创建、插入、删除
- 堆的创建,插入,删除
- 堆的创建,插入,删除
- 最大堆 插入和删除操作
- C++实现最小堆及插入,调整顺序,删除堆顶元素的操作
- C++实现堆、最大堆、最小堆 -- 堆排序插入删除操作
- Ext.js5带过滤功能的表格(defaultListenerScope 作用域)(17)
- Nginx学习之六-nginx核心进程模型
- DSOJ Sliding Window
- 1927: [Sdoi2010]星际竞速
- iOS中崩溃调试的使用和技巧总结 韩俊强的博客
- Treap堆的插入,删除操作
- 第2章 活动(Activity)
- redis中key的操作和数据库的选择
- From C++ to Objective-C: A quick guide for practical programmers
- 彻底解决_OBJC_CLASS_$_某文件名", referenced from:问题
- DSOJ Addition of Polynomial(多项式求和)
- 各开发者平台需要的android应用签名
- innoDB缓冲池参数
- iOS Programming 101: Record and Play Audio using AVFoundation Framework