zoukankan      html  css  js  c++  java
  • 数据结构学习第十四天

    15:28:36 2019-08-29

    学习 因为开学有考试还要收作业 之后一个星期没有太多时间来学了

    PTA 函数题3 补充二叉树 的查找 插入 删除 操作

     1 Position Find(BinTree BST, ElementType X)
     2 {
     3     if (!BST)
     4         return NULL;
     5     if (X < BST->Data)
     6         Find(BST->Left, X);
     7     else if (X > BST->Data)
     8         Find(BST->Right, X);
     9     else
    10         return BST;
    11 }
    12 Position FindMin(BinTree BST)
    13 {
    14     if (!BST)
    15         return NULL;
    16     while (BST->Left)
    17         BST = BST->Left;
    18     return BST;
    19 }
    20 Position FindMax(BinTree BST)
    21 {
    22     if (!BST)
    23         return NULL;
    24     while (BST->Right)
    25         BST = BST->Right;
    26     return BST;
    27 }
    28 
    29 BinTree Delete(BinTree BST, ElementType X)
    30 {
    31     if (!BST)
    32     {
    33         printf("Not Found
    ");
    34         return NULL;
    35     }
    36     else if (X < BST->Data)
    37         BST->Left = Delete(BST->Left, X);
    38     else if (BST->Data < X)
    39         BST->Right = Delete(BST->Right, X);
    40     else
    41         if (BST->Left != NULL && BST->Right != NULL)
    42         {
    43             BinTree T = FindMin(BST->Right);
    44             BST->Data = T->Data;
    45             BST->Right = Delete(BST->Right, BST->Data);
    46         }
    47         else
    48             if (!BST->Left)
    49                 BST = BST->Right;
    50             else if (!BST->Right)
    51                 BST = BST->Left;
    52     return BST;
    53 }
    54 BinTree Insert(BinTree BST, ElementType X)
    55 {
    56     if (!BST)
    57     {
    58         BST = (BinTree)malloc(sizeof(struct TNode));
    59         BST->Data = X;
    60         BST->Left = BST->Right = NULL;
    61     }
    62     else if (X < BST->Data)
    63         BST->Left = Insert(BST->Left, X);
    64     else if (BST->Data < X)
    65         BST->Right = Insert(BST->Right, X);
    66     return BST;
    67 }
    View Code

    今天还看了下 堆(优先队列) 学习了下 最大堆的插入和删除操作

     1 #include<stdio.h>
     2 #include<malloc.h>
     3 
     4 //用完全二叉树 实现最大(小)堆
     5 //完全二叉树的特点 就是可以在数组中存储
     6 typedef struct HeapStruct* MaxHeap;
     7 struct HeapStruct
     8 {
     9     int* Elements;    //存储堆元素的数组
    10     int Size;        //堆当前元素个数
    11     int Capacity;   //堆的最大容量
    12 };
    13 
    14 MaxHeap Create(int MaxSize);  //创建一个空的最大堆
    15 int IsFull(MaxHeap H);  //判断最大堆是否已满
    16 void Insert(MaxHeap H, int Item);  //将元素Item插入最大堆H
    17 int IsEmpty(MaxHeap H);  //判断最大堆是否为空
    18 int DeleteMax(MaxHeap H); //删除中最大元素(高优先级)  并删除一个节点
    19 
    20 int IsFull(MaxHeap H)
    21 {
    22     return (H->Size == H->Capacity) ? 1 : 0;
    23 }
    24 MaxHeap Create(int MaxSize)
    25 {
    26     MaxHeap H= (MaxHeap)malloc(sizeof(struct HeapStruct));
    27     H->Elements = (int*)malloc(sizeof(int) * (MaxSize + 1));   //从下标为1的地方开始存放
    28     H->Size = 0;
    29     H->Capacity = MaxSize;
    30     H->Elements[0] = MaxData;  //创造一个哨兵 为大于所有元素 为了以后更方便的操作
    31     return H;
    32 }
    33 void Insert(MaxHeap H, int Item)
    34 { //Elements[0] 已经被设置为哨兵 也就意味着大于所有元素
    35     if (IsFull(H))
    36         return;
    37     int i = ++H->Size;
    38     for (; Item > H->Elements[i / 2]; i /= 2)
    39         H->Elements[i] = H->Elements[i / 2];
    40     H->Elements[i] = Item;
    41 }
    42 
    43 int DeleteMax(MaxHeap H)
    44 {
    45     if (IsFull(H))
    46         return;
    47     int Parent, Child;        
    48     int MaxItem, Tmp;  //MaxItem用来记录最大值 Tmp用来记录需要移动的节点
    49     MaxItem = H->Elements[1];
    50     Tmp = H->Elements[H->Size--];
    51     for (Parent = 1; Parent * 2 <= H->Size; Parent = Child)     //Parent记录插入的位置  也可以说是空的位置
    52     {
    53         Child = Parent * 2;
    54         if ((Child!=H->Size) &&( H->Elements[Child] < H->Elements[Child + 1]))
    55             Child++;
    56         if (Tmp >=H->Elements[Child]) break;
    57         else 
    58             H->Elements[Parent] = H->Elements[Child];
    59     }
    60     H->Elements[Parent] = Tmp;
    61     return MaxItem;
    62 }
    View Code

    希望在假期结束前 能稍微认识下图

  • 相关阅读:
    poj 2388
    BUAA 1489
    poj 2524
    poj 2109
    poj 2503 Babelfish
    poj All in All
    poj 1611 The Suspects
    poj 2299
    poj 1328
    hdu 1008 Elevator
  • 原文地址:https://www.cnblogs.com/57one/p/11430810.html
Copyright © 2011-2022 走看看