Vous êtes sur la page 1sur 3

typedef int ElementType;

02
03 #ifndef _Tree_H
04 #define _Tree_H
05
06 struct TreeNode;
07 typedef struct TreeNode *Position;
08 typedef struct TreeNode *SearchTree;
09
10 SearchTree MakeEmpty( SearchTree T );
11 Position Find( ElementType X, SearchTree T );
12 Position FindMin( SearchTree T );
13 Position FindMax( SearchTree T );
14 SearchTree Insert( ElementType X, SearchTree T );
15 SearchTree Delete( ElementType X, SearchTree T );
16 ElementType Retrieve( Position P );
17
18 #endif /* _Tree_H */
view source
print?
001 #include "tree.h"
002 #include <stdlib.h>
003 #include "fatal.h"
004
005 struct TreeNode
006 {
007 ElementType Element;
008 SearchTree Left;
009 SearchTree Right;
010 };
011
012 SearchTree MakeEmpty( SearchTree T )
013 {
014 if( T != NULL )
015 {
016 MakeEmpty( T->Left );
017 MakeEmpty( T->Right );
018 free( T );
019 }
020 return NULL;
021 }
022
023
024
025 Position Find( ElementType X, SearchTree T )
026 {
027 if( T == NULL )
028 return NULL;
029 if( X < T->Element )
030 return Find( X, T->Left );
031 else
032 if( X > T->Element )
033 return Find( X, T->Right );
034 else
035 return T;
036 }
037 Position FindMin( SearchTree T )
038 {
039 if( T == NULL )
040 return NULL;
041 else
042 if( T->Left == NULL )
043 return T;
044 else
045 return FindMin( T->Left );
046 }
047 Position FindMax( SearchTree T )
048 {
049 if( T != NULL )
050 while( T->Right != NULL )
051 T = T->Right;
052
053 return T;
054 }
055
056
057 SearchTree Insert( ElementType X, SearchTree T )
058 {
059 /* 1*/ if( T == NULL )
060 {
061 /* Create and return a one-node tree */
062 /* 2*/ T = malloc( sizeof( struct TreeNode ) );
063 /* 3*/ if( T == NULL )
064 /* 4*/ FatalError( "Out of space!!!" );
065 else
066 {
067 /* 5*/ T->Element = X;
068 /* 6*/ T->Left = T->Right = NULL;
069 }
070 }
071 else
072 /* 7*/ if( X < T->Element )
073 /* 8*/ T->Left = Insert( X, T->Left );
074 else
075 /* 9*/ if( X > T->Element )
076 /*10*/ T->Right = Insert( X, T->Right );
077 /* Else X is in the tree already; we'll do nothing */
078
079 /*11*/ return T; /* Do not forget this line!! */
080 }
081 SearchTree Delete( ElementType X, SearchTree T )
082 {
083 Position TmpCell;
084
085 if( T == NULL )
086 Error( "Element not found" );
087 else
088 if( X < T->Element ) /* Go left */
089 T->Left = Delete( X, T->Left );
090 else
091 if( X > T->Element ) /* Go right */
092 T->Right = Delete( X, T->Right );
093 else /* Found element to be deleted */
094 if( T->Left && T->Right ) /* Two children */
095 {
096 /* Replace with smallest in right subtree */
097 TmpCell = FindMin( T->Right );
098 T->Element = TmpCell->Element;
099 T->Right = Delete( T->Element, T->Right );
100 }
101 else /* One or zero children */
102 {
103 TmpCell = T;
104 if( T->Left == NULL ) /* Also handles 0 children */
105 T = T->Right;
106 else if( T->Right == NULL )
107 T = T->Left;
108 free( TmpCell );
109 }
110
111 return T;
112 }
113
114 ElementType Retrieve( Position P )
115 {
116 return P->Element;
117 }
view source
print?
01 #include "tree.h"
02 #include <stdio.h>
03
04 main( )
05 {
06 SearchTree T;
07 Position P;
08 int i;
09 int j = 0;
10
11 T = MakeEmpty( NULL );
12 for( i = 0; i < 50; i++, j = ( j + 7 ) % 50 )
13 T = Insert( j, T );
14 for( i = 0; i < 50; i++ )
15 if( ( P = Find( i, T ) ) == NULL || Retrieve( P ) != i )
16 printf( "Error at %d\n", i );
17
18 for( i = 0; i < 50; i += 2 )
19 T = Delete( i, T );
20
21 for( i = 1; i < 50; i += 2 )
22 if( ( P = Find( i, T ) ) == NULL || Retrieve( P ) != i )
23 printf( "Error at %d\n", i );
24 for( i = 0; i < 50; i += 2 )
25 if( ( P = Find( i, T ) ) != NULL )
26 printf( "Error at %d\n", i );
27
28 printf( "Min is %d, Max is %d\n", Retrieve( FindMin( T ) ),
29 Retrieve( FindMax( T ) ) );
30
31 return 0;
32 }

Vous aimerez peut-être aussi