赞
踩
节点的度 : 一个节点含有的子树的个数称为该节点的度; 如上图: A 的为 6叶节点或终端节点 : 度为 0 的节点称为叶节点; 如上图: B 、 C 、 H 、 I... 等节点为叶节点非终端节点或分支节点 : 度不为 0 的节点; 如上图: D 、 E 、 F 、 G... 等节点为分支节点双亲节点或父节点 : 若一个节点含有子节点,则这个节点称为其子节点的父节点; 如上图: A 是 B 的父节点孩子节点或子节点 : 一个节点含有的子树的根节点称为该节点的子节点; 如上图: B 是 A 的孩子节点兄弟节点 : 具有相同父节点的节点互称为兄弟节点; 如上图: B 、 C 是兄弟节点树的度 : 一棵树中,最大的节点的度称为树的度; 如上图:树的度为 6节点的层次 : 从根开始定义起,根为第 1 层,根的子节点为第 2 层,以此类推;树的高度或深度 : 树中节点的最大层次; 如上图:树的高度为 4堂兄弟节点 : 双亲在同一层的节点互为堂兄弟;如上图: H 、 I 互为兄弟节点节点的祖先 : 从根到该节点所经分支上的所有节点;如上图: A 是所有节点的祖先子孙: 以某节点为根的子树中任一节点都称为该节点的子孙。如上图:所有节点都是 A 的子孙森林 : 由 m ( m>0 )棵互不相交的树的集合称为森林
堆的性质:(1)堆中某个节点的值总是不大于或不小于其父节点的值;(2)堆总是一棵完全二叉树。
- #include<stdio.h>
- #include<assert.h>
- #include<stdbool.h>
- typedef int HPDataType;
- typedef struct Heap
- {
- HPDataType* a;
- int size;
- int capacity;
- }Heap;
-
- // 堆的初始化
- void HeapInit(Heap* hp);
- // 堆的销毁
- void HeapDestory(Heap* hp);
- // 堆的插入
- void HeapPush(Heap* hp, HPDataType x);
- // 堆的删除
- void HeapPop(Heap* hp);
- // 取堆顶的数据
- HPDataType HeapTop(Heap* hp);
- // 堆的数据个数
- int HeapSize(Heap* hp);
- // 堆的判空
- bool HeapEmpty(Heap* hp);
- // 堆的初始化
- void HeapInit(Heap*hp)
- {
- assert(hp);
- hp-> a=NULL;
- hp->size=0;
- hp->capacity=0;
- }
- // 堆的销毁
- void HeapDestory(Heap* hp)
- {
- assert(hp);
- free(hp->a);
- hp->a = NULL;
- hp->size = 0;
- hp->capacity = 0;
- }
堆的插入:(1)数组的常规插入
(2)将数据向上调整
- void swap(HPDataType* x, HPDataType* y)
- {
- HPDataType tmp = * x;
- *x = * y;
- *y = tmp;
- }
- // 堆的插入
- void HeapPush(Heap* hp, HPDataType x)
- {
- assert(hp);
- if (hp->size == hp->capacity)
- {
- int newcapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
- HPDataType* tmp = (HPDataType*)realloc(hp->a, sizeof(HPDataType) * newcapacity);
- if (tmp == NULL)
- {
- perror("realloc fail:");
- return;
- }
- hp->a = tmp;
- hp->capacity = newcapacity;
- }
-
- hp->a[hp->size] = x;
- hp->size++;
- int child = hp->size - 1;
- int parent = (child - 1) / 2;
- //小堆
- while (child>0)
- {
- if (hp->a[parent] > hp->a[child])
- {
- swap(&hp->a[parent], &hp->a[child]);
- }
- child = parent;
- parent = (child - 1) / 2;
- }
-
- }
堆的删除:(1)将首尾交换,删除原根节点
(2)将数据向下调整
- // 堆的删除(删除堆顶)
- void HeapPop(Heap* hp)
- {
- assert(hp);
- assert(hp->size > 0);
- swap(&hp->a[hp->size - 1], &hp->a[0]);
- hp->size--;
- int parent = 0;
- int child = parent * 2 + 1;//左孩子
- while (child<hp->size)
- {
-
- if (child<hp->size-1&&hp->a[child] > hp->a[child + 1])
- {
- ++child;
- }
- if (hp->a[parent] > hp->a[child])
- {
- swap(&hp->a[child], &hp->a[parent]);
- parent = child;
- child = parent * 2 + 1;
-
- }
- else
- {
- break;
- }
-
- }
- // 取堆顶的数据
- HPDataType HeapTop(Heap* hp)
- {
- assert(hp);
- return hp->a[0];
- }
- // 堆的数据个数
- int HeapSize(Heap* hp)
- {
- assert(hp);
- return hp->size;
- }
- // 堆的判空
- bool HeapEmpty(Heap* hp)
- {
- assert(hp);
- return hp->size == 0;
- }
- for (int i = 1; i < n; i++)
- {
- AdjustUp(a, i);
- }
向上调整建堆的时间复杂度计算:
总的调整次数为T(h)
T(h)= 2^(h-1)*(h-1)+2^(h-2)*(h-2)+......+2^2*2+2^1*1 ①
2*T(h)= 2^(h)*(h-1)+2^(h-1)*(h-2)+......+2^3*2+2^2*1 ②
② -①得:
T(h)=2^(h)*(h-1)-(2^(h-1)+......+2^3+2^2+2^1+2^0 )+1
T(h)=2^(h)*(h-1)-(2^h-1)+1
T(h)=2^(h)*(h-1)-2^h+2
T(h)=2^h*(h-2)+2 ③
满二叉树又有高度h与节点N的关系
N=2^h-1->h=log(N+1) 带入 ③
O(N)=(N+1)(log(N+1)-2)+2
O(N)=NlogN
(2)向下调整建堆(O(N))
- for (int i = (n - 1 - 1) / 2; i >= 0; i--)
- {
- AdjustDown(a, n, i);
- }
向下调整建堆的时间复杂度计算:
我们从不是叶子的第一个节点(上图中红框标注)开始向下调整,一直到根节点
总的调整次数为T(h)
T(h)= 2^(h-2)*1+2^(h-3)*2+2^(h-4)*3+......+2^1*(h-2)+2^0*(h-1) ①
2*T(h)=2^(h-1)*1+2^(h-2)*2+2^(h-3)*3+......+2^2*(h-2)+2^1*(h-1) ②
② -①得:
T(h)=2^(h-1)*1+2^(h-2)+2^(h-3)+......+2^2+2^1-(h-1)
T(h)=(2^(h-1)*1+2^(h-2)+2^(h-3)+......+2^2+2^1+2^0)-h
T(h)=2^h-1-h ③
满二叉树又有高度h与节点N的关系
N=2^h-1->h=log(N+1) 带入 ③
O(N)=N+1-1-log(N-1)
O(N)=N
- int end = n - 1;
- while(end>0)
- {
- swap(&a[0], &a[end]);
- AdjustDown(a, end, 0);
- --end;
- }
堆排序完整代码:
- void swap(int *x, int *y)
- {
- int tmp = *x;
- *x = *y;
- *y = tmp;
- }
- void AdjustUp(int* a, int child)
- {
-
- while (child > 0)
- {
- int parent = (child - 1) / 2;
- if (a[parent] < a[child])
- {
- swap(&a[parent], &a[child]);
- child = parent * 2 + 1;
- }
- else
- {
- break;
- }
- }
- }
- void AdjustDown(int* a, int size, int parent)
- {
- int child = parent * 2 + 1;//左孩子
- while (child<size)
- {
- if (child < size - 1 && a[child] < a[child + 1])
- {
- ++child;
- }
- if (a[parent] < a[child])
- {
- swap(&a[parent], &a[child]);
- parent = child;
- child = parent * 2 + 1;
- }
- else
- {
- break;
- }
- }
- }
- void HeapSort(int* a, int n)
- {
- //for (int i = 1; i < n; i++)
- //{
- // AdjustUp(a, i);
- //}
- for (int i = (n - 1 - 1) / 2; i >= 0; i--)
- {
- AdjustDown(a, n, i);
- }
- int end = n - 1;
- while(end>0)
- {
- swap(&a[0], &a[end]);
- AdjustDown(a, end, 0);
- --end;
- }
- }
找一堆数据中最大的k个数据:
(1)建立k个元素的小堆
(2)若是其他数据比堆顶大,则替换,并且向下调整,使大数据都沉到堆底
- void AdjustUp(int* a, int child)
- {
-
- while (child > 0)
- {
- int parent = (child - 1) / 2;
- if (a[parent] > a[child])
- {
- swap(&a[parent], &a[child]);
- child = parent * 2 + 1;
- }
- else
- {
- break;
- }
- }
- }
- void AdjustDown(int* a, int size, int parent)
- {
- int child = parent * 2 + 1;//左孩子
- while (child<size)
- {
- if (child < size - 1 && a[child] >a[child + 1])
- {
- ++child;
- }
- if (a[parent] > a[child])
- {
- swap(&a[parent], &a[child]);
- parent = child;
- child = parent * 2 + 1;
- }
- else
- {
- break;
- }
- }
- }
- void CreateDate()
- {
- int n = 1000;
- srand((unsigned int)time(NULL));
-
- FILE* f = fopen("date.txt", "w");
- if (f == NULL)
- {
- perror("fopen error:");
- return;
- }
- for (int i = 0; i < 1000; i++)
- {
- int x = rand()%1000;
- fprintf(f, "%d\n", x);
- }
- fclose(f);
- }
- void PrintTopK(char* file, int k)//从文件中找到前最大的k个数据
- {
- FILE* fout = fopen(file, "r");
- if (fout == NULL)
- {
- perror("fopen fail:");
- return;
- }
- //建k个数据的小堆
- int* minheap = (int*)malloc(sizeof(int) * k);
- for (int i = 0; i < k; i++)
- {
- fscanf(fout, "%d", minheap);
- AdjustUp(minheap, i);
- }
- //继续遍历文件数据,大于堆顶,进堆
- int x = 0;
- while (fscanf(fout, "%d", &x)!=EOF)
- {
-
- if (minheap[0] < x)
- {
- /*if (x > 950)
- {
- int sss = 0;
- }*/
- minheap[0]= x;
- AdjustDown(minheap, k, 0);
- }
- ;
- }
- for (int i = 0; i < k; i++)
- {
- printf("%d ", minheap[i]);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。