Loading

二叉查找树

 

 

 1 /*接口头文件*/
 2 typedef int ElementType;
 3 
 4 #ifndef _SEARCHTREE_H
 5 #define _SEARCHTREE_H
 6 
 7 struct TreeNode;
 8 typedef struct TreeNode * PtrToNode;
 9 typedef PtrToNode Position;
10 typedef PtrToNode SearchTree;
11 
12 /*操作集*/
13 SearchTree MakeEmpty( SearchTree T );
14 Position Find( ElementType X, SearchTree T );
15 Position FindMin( SearchTree T );
16 Position FindMax( SearchTree T );
17 SearchTree Insert( ElementType X, SearchTree T );
18 SearchTree Delete( ElementType X, SearchTree T );
19 ElementType Retrieve( Position P );
20 void PrintfTree( SearchTree T );
21 
22 #endif 
  1 /*接口实现*/
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include "searchtree.h"
  5 
  6 /*特定结构体定义*/
  7 struct TreeNode
  8 {
  9     ElementType Element;
 10     SearchTree  Left;
 11     SearchTree  Right;
 12 };
 13 
 14 SearchTree MakeEmpty( SearchTree T )
 15 {
 16     if ( T != NULL )
 17     {
 18         MakeEmpty( T->Left );
 19         MakeEmpty( T->Right );
 20         free( T );
 21     }
 22     
 23     return NULL;
 24 }
 25 
 26 Position Find( ElementType X, SearchTree T )
 27 {
 28     if ( T == NULL )
 29         return NULL;
 30     
 31     if ( X < T->Left )
 32         return Find( X, T->Left );     //尾递归 
 33     else if ( X > T->Element )
 34         return Find( X, T->Right );    //尾递归 
 35     else
 36         return T;
 37 }
 38 
 39 /*递归例程*/ 
 40 Position FindMin( SearchTree T )
 41 {
 42     if ( T == NULL )
 43         return NULL;               //No Found
 44     else if ( T->Left == NULL )    //左子树不存在 
 45         return T;
 46     else
 47         return FindMin( T->Left );
 48 }
 49 
 50 /*
 51   非递归例程 
 52 Position FindMin( SearchTree T )
 53 {
 54     if ( T != NULL )
 55         while ( T->Left != NULL )
 56             T = T->Left;
 57     
 58     return T;
 59 }
 60 */
 61 
 62 /*递归例程*/
 63 Position FindMax( SearchTree T )
 64 {
 65     if ( T == NULL )
 66         return NULL;
 67     else if ( T->Right == NULL )
 68         return T;
 69     else
 70         return FindMax( T->Right );
 71 } 
 72 
 73 /*
 74   非递归例程 
 75 Position FindMax( SearchTree T )
 76 {
 77     if ( T != NULL )
 78         while ( T->Right != NULL )
 79             T = T->Right;
 80     
 81     return T;
 82 }
 83 */
 84 
 85 SearchTree Insert( ElementType X, SearchTree T )
 86 {
 87     if ( T == NULL )
 88     {
 89         T = ( SearchTree )malloc( sizeof( struct TreeNode ) );
 90         if ( T == NULL )
 91         {
 92             printf( "No Space!!!\n" );
 93             exit( 1 );
 94         }
 95         T->Element = X;
 96         T->Left = NULL;
 97         T->Right = NULL;
 98     }
 99     else if ( X < T->Element )
100         T->Left = Insert( X, T->Left );
101     else if ( X > T->Element )
102         T->Right = Insert( X, T->Right );
103     
104     return T;
105 }
106 
107 SearchTree Delete( ElementType X, SearchTree T )
108 {
109     Position Temp;
110     
111     if ( T == NULL )
112     {
113         printf( "No Found!!!\n" );
114         exit( 1 );
115     }
116     else if ( X < T->Element )
117         T->Left = Delete( X, T->Left );
118     else if ( X < T->Element )
119         T->Right = Delete( X, T->Right );
120     else if ( T->Left != NULL && T->Right != NULL )
121     {
122         Temp = FindMin( T->Right );
123         T->Element = Temp->Element;
124         T->Right = Delete( T->Element, T->Right );   
125     }
126     else
127     {
128         Temp = T;
129         if ( T->Left == NULL )
130             T = T->Right;
131         else if ( T->Right == NULL )
132             T = T->Left;
133             
134         free( Temp );
135     }
136     
137     return T;
138 }
139 
140 void PrintfTree( SearchTree T )
141 {
142     if ( T != NULL )
143     {
144         PrintfTree( T->Left );
145         printf( "%3d", T->Element );
146         PrintfTree( T->Right );
147     }
148 
149 }

 

posted @ 2017-08-08 15:51  未夏  阅读(193)  评论(0编辑  收藏  举报