Implementation of BINARY TREE
Implementation of BINARY TREE
html
#include "tree.h"
#include <stdlib.h>
#include "fatal.h"
struct TreeNode
{
ElementType Element;
SearchTree Left;
SearchTree Right;
};
/* START: fig4_17.txt */
SearchTree
MakeEmpty( SearchTree T )
{
if( T != NULL )
{
MakeEmpty( T->Left );
MakeEmpty( T->Right );
free( T );
}
return NULL;
}
/* END */
/* START: fig4_18.txt */
Position
Find( ElementType X, SearchTree T )
{
if( T == NULL )
return NULL;
if( X < T->Element )
return Find( X, T->Left );
else
if( X > T->Element )
return Find( X, T->Right );
else
return T;
}
/* END */
/* START: fig4_19.txt */
Position
FindMin( SearchTree T )
{
if( T == NULL )
return NULL;
else
if( T->Left == NULL )
return T;
else
return FindMin( T->Left );
}
/* END */
/* START: fig4_20.txt */
Position
return T;
}
/* END */
/* START: fig4_22.txt */
SearchTree
Insert( ElementType X, SearchTree T )
{
/* 1*/ if( T == NULL )
{
/* Create and return a one-node tree */
/* 2*/ T = malloc( sizeof( struct TreeNode ) );
/* 3*/ if( T == NULL )
/* 4*/ FatalError( "Out of space!!!" );
else
{
/* 5*/ T->Element = X;
/* 6*/ T->Left = T->Right = NULL;
}
}
else
/* 7*/ if( X < T->Element )
/* 8*/ T->Left = Insert( X, T->Left );
else
/* 9*/ if( X > T->Element )
/*10*/ T->Right = Insert( X, T->Right );
/* Else X is in the tree already; we'll do nothing */
/* START: fig4_25.txt */
SearchTree
Delete( ElementType X, SearchTree T )
{
Position TmpCell;
if( T == NULL )
Error( "Element not found" );
else
if( X < T->Element ) /* Go left */
T->Left = Delete( X, T->Left );
else
if( X > T->Element ) /* Go right */
T->Right = Delete( X, T->Right );
else /* Found element to be deleted */
if( T->Left && T->Right ) /* Two children */
{
/* Replace with smallest in right subtree */
TmpCell = FindMin( T->Right );
T->Element = TmpCell->Element;
return T;
}
/* END */
ElementType
Retrieve( Position P )
{
return P->Element;
}
#include <stdlib.h>
#include "fatal.h"
void
Swap( ElementType *Lhs, ElementType *Rhs )
{
ElementType Tmp = *Lhs;
*Lhs = *Rhs;
*Rhs = Tmp;
}
/* START: fig7_2.txt */
void
InsertionSort( ElementType A[ ], int N )
{
int j, P;
ElementType Tmp;
/* START: fig7_4.txt */
void
Shellsort( ElementType A[ ], int N )
{
int i, j, Increment;
ElementType Tmp;
/* START: fig7_8.txt */
#define LeftChild( i ) ( 2 * ( i ) + 1 )
void
void
Heapsort( ElementType A[ ], int N )
{
int i;
/* START: fig7_10.txt */
/* Lpos = start of left half, Rpos = start of right half */
void
Merge( ElementType A[ ], ElementType TmpArray[ ],
int Lpos, int Rpos, int RightEnd )
{
int i, LeftEnd, NumElements, TmpPos;
LeftEnd = Rpos - 1;
TmpPos = Lpos;
NumElements = RightEnd - Lpos + 1;
/* main loop */
while( Lpos <= LeftEnd && Rpos <= RightEnd )
if( A[ Lpos ] <= A[ Rpos ] )
TmpArray[ TmpPos++ ] = A[ Lpos++ ];
else
TmpArray[ TmpPos++ ] = A[ Rpos++ ];
/* START: fig7_9.txt */
void
MSort( ElementType A[ ], ElementType TmpArray[ ],
int Left, int Right )
{
int Center;
void
Mergesort( ElementType A[ ], int N )
{
ElementType *TmpArray;
/* START: fig7_13.txt */
/* Return median of Left, Center, and Right */
/* Order these and hide the pivot */
ElementType
Median3( ElementType A[ ], int Left, int Right )
{
int Center = ( Left + Right ) / 2;
/* START: fig7_14.txt */
#define Cutoff ( 3 )
void
Qsort( ElementType A[ ], int Left, int Right )
{
int i, j;
ElementType Pivot;
/* START: fig7_12.txt */
void
Quicksort( ElementType A[ ], int N )
{
Qsort( A, 0, N - 1 );
}
/* END */
void
Permute( ElementType A[ ], int N )
{
int i;
void
Checksort( ElementType A[ ], int N )
{
int i;
for( i = 0; i < N; i++ )
if( A[ i ] != i )
printf( "Sort fails: %d %d\n", i, A[ i ] );
printf( "Check completed\n" );
}
main( )
{
int i;
return 0;
}