Chinaunix首页 | 论坛 | 博客
  • 博客访问: 652073
  • 博文数量: 160
  • 博客积分: 2384
  • 博客等级: 大尉
  • 技术积分: 1366
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 11:35
文章分类
文章存档

2015年(45)

2014年(36)

2012年(28)

2011年(37)

2010年(2)

2009年(10)

2008年(2)

分类: C/C++

2014-11-14 22:42:51

1. 一些简单的实现
    a.  可以用链表,在头以O(1)执行插入操作,并遍历该链表删除最小的元素,这需要O(n)的时间。或者是链表始终保持排序状态,插入的时间花费O(N),而删除最小元素需要的时间为O(1)。删除的操作次数不多于插入操作次数,应此前一种结构更好。
    b. 二叉查找树, 删除和插入操作的时间都为O(logN)。但是会破坏树的平衡性。
2. 二叉堆
   a.  堆是一棵完全二叉树(complete binary tree), 有可能的例外在底层,底层上的元素从左到右填入。
   b. 一棵高为h的完全二叉树有2h到2h+1 - 1节点。这意味着完全二叉树的高是log(N)。
   c. 可以用一个数组把二叉堆映射到数组里面,i任意位置, 其左儿子是2i,右儿子是2i + 1。数组的0空着,放入最小的数据,例如MIN_INT。

点击(此处)折叠或打开

  1. // binheap.h

  2.         typedef int ElementType;

  3. /* START: fig6_4.txt */
  4.         #ifndef _BinHeap_H
  5.         #define _BinHeap_H

  6.         struct HeapStruct;
  7.         typedef struct HeapStruct *PriorityQueue;

  8.         PriorityQueue Initialize( int MaxElements );
  9.         void Destroy( PriorityQueue H );
  10.         void MakeEmpty( PriorityQueue H );
  11.         void Insert( ElementType X, PriorityQueue H );
  12.         ElementType DeleteMin( PriorityQueue H );
  13.         ElementType FindMin( PriorityQueue H );
  14.         int IsEmpty( PriorityQueue H );
  15.         int IsFull( PriorityQueue H );

  16.         #endif

  17. /* END */

点击(此处)折叠或打开

  1. // fatal.h

  2. #include <stdio.h>
  3. #include <stdlib.h>

  4. #define Error( Str ) FatalError( Str )
  5. #define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )

点击(此处)折叠或打开

  1. // binheap.cpp
  2.         
  3.         #include "binheap.h"
  4.         #include "fatal.h"
  5.         #include <stdlib.h>

  6.         #define MinPQSize (10)
  7.         #define MinData (-32767)

  8.         struct HeapStruct
  9.         {
  10.             int Capacity;
  11.             int Size;
  12.             ElementType *Elements;
  13.         };

  14. /* START: fig6_0.txt */
  15.         PriorityQueue
  16.         Initialize( int MaxElements )
  17.         {
  18.             PriorityQueue H;

  19. /* 1*/ if( MaxElements < MinPQSize )
  20. /* 2*/ Error( "Priority queue size is too small" );

  21. /* 3*/ H = malloc( sizeof( struct HeapStruct ) );
  22. /* 4*/ if( H ==NULL )
  23. /* 5*/ FatalError( "Out of space!!!" );

  24.             /* Allocate the array plus one extra for sentinel */
  25. /* 6*/ H->Elements = malloc( ( MaxElements + 1 )
  26.                                     * sizeof( ElementType ) );
  27. /* 7*/ if( H->Elements == NULL )
  28. /* 8*/ FatalError( "Out of space!!!" );

  29. /* 9*/ H->Capacity = MaxElements;
  30. /*10*/ H->Size = 0;
  31. /*11*/ H->Elements[ 0 ] = MinData;

  32. /*12*/ return H;
  33.         }
  34. /* END */

  35.         void
  36.         MakeEmpty( PriorityQueue H )
  37.         {
  38.             H->Size = 0;
  39.         }

  40. /* START: fig6_8.txt */
  41.         /* H->Element[ 0 ] is a sentinel */

  42.         void
  43.         Insert( ElementType X, PriorityQueue H )
  44.         {
  45.             int i;

  46.             if( IsFull( H ) )
  47.             {
  48.                 Error( "Priority queue is full" );
  49.                 return;
  50.             }

  51.             for( i = ++H->Size; H->Elements[ i / 2 ] > X; i /= 2 )
  52.                 H->Elements[ i ] = H->Elements[ i / 2 ];
  53.             H->Elements[ i ] = X;
  54.         }
  55. /* END */

  56. /* START: fig6_12.txt */
  57.         ElementType
  58.         DeleteMin( PriorityQueue H )
  59.         {
  60.             int i, Child;
  61.             ElementType MinElement, LastElement;

  62. /* 1*/ if( IsEmpty( H ) )
  63.             {
  64. /* 2*/ Error( "Priority queue is empty" );
  65. /* 3*/ return H->Elements[ 0 ];
  66.             }
  67. /* 4*/ MinElement = H->Elements[ 1 ];
  68. /* 5*/ LastElement = H->Elements[ H->Size-- ];

  69. /* 6*/ for( i = 1; i * 2 <= H->Size; i = Child )
  70.             {
  71.                 /* Find smaller child */
  72. /* 7*/ Child = i * 2;
  73. /* 8*/ if( Child != H->Size && H->Elements[ Child + 1 ]
  74. /* 9*/ < H->Elements[ Child ] )
  75. /*10*/ Child++;

  76.                 /* Percolate one level */
  77. /*11*/ if( LastElement > H->Elements[ Child ] )
  78. /*12*/ H->Elements[ i ] = H->Elements[ Child ];
  79.                 else
  80. /*13*/ break;
  81.             }
  82. /*14*/ H->Elements[ i ] = LastElement;
  83. /*15*/ return MinElement;
  84.         }
  85. /* END */

  86.         ElementType
  87.         FindMin( PriorityQueue H )
  88.         {
  89.             if( !IsEmpty( H ) )
  90.                 return H->Elements[ 1 ];
  91.             Error( "Priority Queue is Empty" );
  92.             return H->Elements[ 0 ];
  93.         }

  94.         int
  95.         IsEmpty( PriorityQueue H )
  96.         {
  97.             return H->Size == 0;
  98.         }

  99.         int
  100.         IsFull( PriorityQueue H )
  101.         {
  102.             return H->Size == H->Capacity;
  103.         }

  104.         void
  105.         Destroy( PriorityQueue H )
  106.         {
  107.             free( H->Elements );
  108.             free( H );
  109.         }

  110.         #if 0
  111. /* START: fig6_14.txt */
  112.         for( i = N / 2; i > 0; i-- )
  113.             PercolateDown( i );
  114. /* END */
  115.         #endif



阅读(510) | 评论(0) | 转发(0) |
0

上一篇:查找算法

下一篇:教你透彻了解红黑树

给主人留下些什么吧!~~