我必须实现Treesort,inOrderTraversal,Max和Height函数。不确定我的代码有什么问题。它构建时没有错误或警告,但控制台是空的。
void TreeSort(ElementType *X, long n, SearchTree T){
X = NULL;
for (T = 0; T < n; T++)
X = Insert(X, T);
}
void inOrderTraversal(SearchTree T){
if (T = NULL)
return;
inOrderTraversal(T->Left);
printf("%d", T->Element);
inOrderTraversal(T->Right);
}
long Height(SearchTree T){
if (T = NULL)
return 0;
else
{
long LHeight = Height(T->Left);
long RHeight = Height(T->Right);
if (LHeight > RHeight)
return LHeight + 1;
else return RHeight + 1;
}
}
long Max(int left, int right){
if (right = NULL);
int Max = left + 1;
return Max;
}
编辑:这是tree.c的其余部分。上述功能所在的位置。
struct TreeNode{
ElementType Element;
SearchTree Left;
SearchTree Right;
};
/* Figure 4.17 Routine to make an empty tree */
SearchTree MakeEmpty( SearchTree T ){
if( T != NULL ){
MakeEmpty( T->Left );
MakeEmpty( T->Right );
free( T );
}
return NULL;
}
/* Figure 4.18 Find operation for binary search trees */
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;
}
/* Figure 4.19 Recursive implementation of FindMin for binary search trees */
Position FindMin( SearchTree T ){
if( T == NULL ) return NULL;
else if( T->Left == NULL ) return T;
else return FindMin( T->Left );
}
/* Figure 4.20 Nonrecursive implementation of FindMax for binary search trees */
Position
FindMax( SearchTree T ){
if( T != NULL )
while( T->Right != NULL )T = T->Right;
return T;
}
/* Figure 4.22 Insertion into a binary search tree */
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 ) FatalError( "Out of space!!!" );
else{
/* 5*/ T->Element = X;
/* 6*/ T->Left = T->Right = NULL;
}
}
else if( X < T->Element ) T->Left = Insert( X, T->Left );
else if( X > T->Element ) T->Right = Insert( X, T->Right );
/* Else X is in the tree already; we'll do nothing */
/*11*/ return T; /* Do not forget this line!! */
}
/* Figure 4.25 Deletion routine for binary search trees */
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 );
/* Found element to be deleted */
else if( T->Left && T->Right ){ /* Two children */
/* Replace with smallest in right subtree */
TmpCell = FindMin( T->Right );
T->Element = TmpCell->Element;
T->Right = Delete( T->Element, T->Right );
}
else{ /* One or zero children */
TmpCell = T;
/* Also handles 0 children */
if( T->Left == NULL ) T = T->Right;
else if( T->Right == NULL ) T = T->Left;
free( TmpCell );
}
return T;
}
ElementType Retrieve( Position P ){
return P->Element;
}
主要功能
SearchTree T=MakeEmpty(NULL);
long *array;
long i;
long j;
long heightSum=0;
float avgHeight;
#ifdef PART_A
array=permutation(N);
TreeSort(array, N, T);
free(array);