左偏树
2015-07-10 00:19
344 查看
</pre><pre name="code" class="cpp">#include<stdio.h> #include<stdlib.h> typedef struct node { int key,dist; struct node *Left,*Right; }LeftistTree; void Swap(LeftistTree** a,LeftistTree** b) { LeftistTree *tmp; tmp = *a; *a = *b; *b = tmp; } LeftistTree* Merge(LeftistTree *A,LeftistTree *B) { if(A == NULL) return B; if(B == NULL) return A; if(A->key < B->key) { Swap(&A,&B); } A->Right = Merge(A->Right,B); if(A->Left == NULL) { A->Left = A->Right; A->Right = NULL; } else if(A->Right) if(A->Right->dist > A->Left->dist) Swap(&A->Left,&A->Right); if(A->Right == NULL) A->dist = 0; else A->dist = A->Right->dist + 1; return A; } LeftistTree* MakeIntoTree(int x) { LeftistTree *T; T = (LeftistTree*)malloc(sizeof(LeftistTree)); T->key = x; T->dist = 0; T->Left = T->Right = NULL; return T; } LeftistTree* Insert(LeftistTree* A,int x) { LeftistTree *B; B = MakeIntoTree(x); A = Merge(A,B); return A; } /*删除最大节点*/ int DeleteMax(LeftistTree** A) { int t = (*A)->key; *A = Merge((*A)->Left,(*A)->Right); return t; } /*中序输出*/ void InOrder(LeftistTree* T) { if(T->Left) InOrder(T->Left); printf("%d %d\n",T->key,T->dist); if(T->Right) InOrder(T->Right); } int main(void) { LeftistTree* T = NULL; int n,a; scanf("%d",&n); while(n --) { scanf("%d",&a); T = Insert(T,a); InOrder(T); } DeleteMax(&T); printf("\n"); InOrder(T); }
相关文章推荐
- Cocos2dx 响应Android返回键和Menu键
- druid连接池配置
- Android init.rc分析
- [转][Linux]10个最酷的Linux单行命令
- ASP.NET | WebForm 处理机
- elf文件详解
- githup 基础学习
- android屏幕适配
- android屏幕适配 2015-07-10 00:16 2人阅读 评论(0) 收藏
- 在 IE 中承载 Winform 应用程序
- 安装并启用ispin
- Word Search II
- 记一次死锁问题的处理
- 单链表 使用哑结点避免特殊情况 利大于弊?
- 记一次死锁问题的处理
- mongodb与spring集成案例
- PHP环境搭建记录
- 【Swift】 - 函数(Functions)总结 - 比较 与 C# 的异同
- 开始学C#语言
- fedora 提供的安装包