zoukankan      html  css  js  c++  java
  • [数据结构与算法] : 二叉查找树

    头文件

     1 typedef int ElementType;
     2 #ifndef _TREE_H_
     3 #define _TREE_H_
     4 
     5 struct TreeNode;
     6 typedef struct TreeNode *Position;
     7 typedef struct TreeNode *SearchTree;
     8 
     9 SearchTree MakeEmpty(SearchTree T);
    10 Position Find(ElementType X, SearchTree T);
    11 Position FindMin(SearchTree T);
    12 Position FindMin2(SearchTree T);
    13 Position FindMax2(SearchTree T);
    14 SearchTree Insert(ElementType X, SearchTree T);
    15 SearchTree Delete(ElementType X, SearchTree T);
    16 ElementType Retrieve(Position P);
    17 
    18 #endif

    源文件

      1 #include "tree.h"
      2 #include "fatal.h"
      3 #include <malloc.h>
      4 
      5 struct TreeNode
      6 {
      7     ElementType Element;
      8     SearchTree  Left;
      9     SearchTree  Right;
     10 };
     11 
     12 // 先清空左子树, 再清空又子树, 再释放自身节点
     13 SearchTree MakeEmpty(SearchTree T)
     14 {
     15     if(T != NULL)
     16     {
     17         MakeEmpty(T->Left);
     18         MakeEmpty(T->Right);
     19         free(T);
     20     }
     21     return NULL; // 递归终止条件
     22 }
     23 
     24 // 查找, 类似二分查找
     25 Position Find(ElementType X, SearchTree T)
     26 {
     27     if(T == NULL) // 递归终止条件
     28         return NULL;
     29     if(X < T->Element)
     30         return Find(X, T->Left);
     31     else if(X > T->Element)
     32         return Find(X, T->Right);
     33     else
     34         return T;
     35 }
     36 
     37 Position FindMin(SearchTree T) // 递归实现
     38 {
     39     if(T == NULL) // 防止非法输入
     40         return NULL;
     41     if(T->Left == NULL) // 递归终止
     42         return T;
     43     else
     44         return FindMin(T->Left);
     45 }
     46 
     47 Position FindMin2(SearchTree T) // 非递归实现
     48 {
     49     if(T == NULL)
     50         return NULL;
     51     while(T->Left != NULL)
     52         T = T->Left;
     53     return T;
     54 }
     55 
     56 Position FindMax(SearchTree T)
     57 {
     58     if(T == NULL)
     59         return NULL;
     60     if(T->Right == NULL)
     61         return T;
     62     else
     63         return FindMax(T->Right);
     64 }
     65 
     66 Position FindMax2(SearchTree T)
     67 {
     68     if(T == NULL)
     69         return NULL;
     70     while(T->Right != NULL)
     71         T = T->Right;
     72     return T;
     73 }
     74 
     75 // 插入
     76 SearchTree Insert(ElementType X, SearchTree T)
     77 {
     78     if(T == NULL) // 如果T为NULL, 创建节点, 递归终止条件
     79     {
     80         T = (SearchTree)malloc(sizeof(struct TreeNode));
     81         if(T == NULL)
     82             FatalError("Out of space!");
     83         else
     84         {
     85             T->Element = X;
     86             T->Left = T->Right = NULL;
     87         }
     88     }
     89     else if(X < T->Element)
     90         T->Left = Insert(X, T->Left);
     91     else if(X > T->Element)
     92         T->Right = Insert(X, T->Right);
     93     // 如果X已经在数中, 则什么也不做
     94     return T; // Do not forget this line!!
     95 }
     96 
     97 // 删除
     98 SearchTree Delete(ElementType X, SearchTree T)
     99 {
    100     Position TempCell;
    101     if(T == NULL) // 递归终止
    102         Error("Element not found!");
    103     else if(X < T->Element)
    104         T->Left = Delete(X, T->Left);
    105     else if(X > T->Element)
    106         T->Right = Delete(X, T->Right);
    107     else if(T->Left && T->Right)  // Two children
    108     {
    109         TempCell = FindMin(T->Right);// 找一个替身, 右子树的最小值
    110         T->Element = TempCell->Element;
    111         T->Right = Delete(T->Element, T->Right);// 替身已经放到T->Element, 再从右子树删除该值即可
    112     }
    113     else                          // One or zero childern
    114     {
    115         TempCell = T;
    116         if(T->Left == NULL)
    117             T = T->Right;
    118         else if(T->Right = NULL)
    119             T = T->Left;
    120         free(TempCell);
    121     }
    122     return T; // Do not forget this line!!
    123 }
    124 
    125 ElementType Retrieve(Position P)
    126 {
    127     return P->Element;
    128 }

    测试文件

     1 #include "tree.h"
     2 #include <stdio.h>
     3 
     4 int main()
     5 {
     6     SearchTree T;
     7     Position P;
     8     int i = 0, j = 0;
     9 
    10     T = MakeEmpty(NULL);
    11     for( i = 0; i < 50; i++, j = ( j + 7 ) % 50 )
    12         T = Insert( j, T );
    13     for( i = 0; i < 50; i++ )
    14         if( ( P = Find( i, T ) ) == NULL || Retrieve( P ) != i )
    15             printf( "Error at %d
    ", i );
    16 
    17     for( i = 0; i < 50; i += 2 )
    18         T = Delete( i, T );
    19 
    20     for( i = 1; i < 50; i += 2 )
    21         if( ( P = Find( i, T ) ) == NULL || Retrieve( P ) != i )
    22             printf( "Error at %d
    ", i );
    23     for( i = 0; i < 50; i += 2 )
    24         if( ( P = Find( i, T ) ) != NULL )
    25             printf( "Error at %d
    ", i );
    26 
    27     printf( "Min is %d, Max is %d
    ", Retrieve( FindMin2( T ) ),
    28                Retrieve( FindMax2( T ) ) );
    29 
    30     return 0;
    31 }
  • 相关阅读:
    剑指offer-二维数组中的查找
    TF-IDF(term frequency–inverse document frequency)
    Java实现中文字符串的排序功能
    当前课程
    【R】资源整理
    CentOS相关
    【转】Setting up SDL Extension Libraries on MinGW
    【转】Setting up SDL Extension Libraries on Visual Studio 2010 Ultimate
    【转】Setting up SDL Extension Libraries on Code::Blocks 12.11
    【转】Setting up SDL Extension Libraries on Visual Studio 2019 Community
  • 原文地址:https://www.cnblogs.com/moon1992/p/7500065.html
Copyright © 2011-2022 走看看