#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define DEBUG(X) printf(X)
struct TreeNode;
typedef struct TreeNode *Position_t;
typedef struct TreeNode SearchTree_t;
typedef int ElementType_t;
struct TreeNode{
ElementType_t Element;
SearchTree_t* Left;
SearchTree_t* Right;
};
SearchTree_t* MakeEmpty(SearchTree_t* Tree){
if(Tree != NULL){
MakeEmpty(Tree -> Left);
MakeEmpty(Tree -> Right);
free(Tree);
}
return NULL;
}
SearchTree_t* Insert(ElementType_t X,SearchTree_t* Tree){
if(Tree == NULL){
//create and return a one node tree
Tree = malloc(sizeof(struct TreeNode));
if(Tree == NULL){
DEBUG("Out of space!!!!");
}else{
Tree->Element = X;
Tree->Left = Tree->Right = NULL;
}
}else if(X < Tree->Element){
Tree->Left = Insert(X,Tree->Left);
}else if(X > Tree->Element){
Tree->Right = Insert(X,Tree->Right);
}else{
//else X is in the tree already,we'll do nothing
}
return Tree;
}
Position_t Find(ElementType_t X,SearchTree_t* Tree){
if(Tree == NULL){
return NULL;
}
if( X < Tree->Element){
return Find(X,Tree -> Left);
}else if( X > Tree->Element){
return Find(X,Tree -> Right);
}else{
return Tree;
}
}
Position_t FindMin(SearchTree_t* Tree){
if(Tree == NULL){
return NULL;
}else if(Tree->Left == NULL){
return Tree;
}else{
return FindMin(Tree->Left);
}
}
Position_t FindMax(SearchTree_t* Tree){
if(Tree != NULL){
while(Tree -> Right != NULL){
Tree = Tree -> Right;
}
}
return Tree;
}
void print_Tree(SearchTree_t* Tree,int Depth){
int i;
if(Tree == NULL){
return;
}
for(i = 0;Depth > i;i++){
printf(" ");
}
printf("%d\r\n",Tree->Element);
print_Tree(Tree->Left,Depth+1);
print_Tree(Tree->Right,Depth+1);
}
int main(){
SearchTree_t* myTree;
myTree = Insert(6,NULL);
Insert(2,myTree);
Insert(1,myTree);
Insert(4,myTree);
Insert(3,myTree);
Insert(8,myTree);
print_Tree(myTree,0);
Position_t min = FindMin(myTree);
printf("min = %d\r\n",min->Element);
Position_t max = FindMax(myTree);
printf("max = %d\r\n",max->Element);
return 1;
}
#include<string.h>
#include<stdlib.h>
#define DEBUG(X) printf(X)
struct TreeNode;
typedef struct TreeNode *Position_t;
typedef struct TreeNode SearchTree_t;
typedef int ElementType_t;
struct TreeNode{
ElementType_t Element;
SearchTree_t* Left;
SearchTree_t* Right;
};
SearchTree_t* MakeEmpty(SearchTree_t* Tree){
if(Tree != NULL){
MakeEmpty(Tree -> Left);
MakeEmpty(Tree -> Right);
free(Tree);
}
return NULL;
}
SearchTree_t* Insert(ElementType_t X,SearchTree_t* Tree){
if(Tree == NULL){
//create and return a one node tree
Tree = malloc(sizeof(struct TreeNode));
if(Tree == NULL){
DEBUG("Out of space!!!!");
}else{
Tree->Element = X;
Tree->Left = Tree->Right = NULL;
}
}else if(X < Tree->Element){
Tree->Left = Insert(X,Tree->Left);
}else if(X > Tree->Element){
Tree->Right = Insert(X,Tree->Right);
}else{
//else X is in the tree already,we'll do nothing
}
return Tree;
}
Position_t Find(ElementType_t X,SearchTree_t* Tree){
if(Tree == NULL){
return NULL;
}
if( X < Tree->Element){
return Find(X,Tree -> Left);
}else if( X > Tree->Element){
return Find(X,Tree -> Right);
}else{
return Tree;
}
}
Position_t FindMin(SearchTree_t* Tree){
if(Tree == NULL){
return NULL;
}else if(Tree->Left == NULL){
return Tree;
}else{
return FindMin(Tree->Left);
}
}
Position_t FindMax(SearchTree_t* Tree){
if(Tree != NULL){
while(Tree -> Right != NULL){
Tree = Tree -> Right;
}
}
return Tree;
}
void print_Tree(SearchTree_t* Tree,int Depth){
int i;
if(Tree == NULL){
return;
}
for(i = 0;Depth > i;i++){
printf(" ");
}
printf("%d\r\n",Tree->Element);
print_Tree(Tree->Left,Depth+1);
print_Tree(Tree->Right,Depth+1);
}
int main(){
SearchTree_t* myTree;
myTree = Insert(6,NULL);
Insert(2,myTree);
Insert(1,myTree);
Insert(4,myTree);
Insert(3,myTree);
Insert(8,myTree);
print_Tree(myTree,0);
Position_t min = FindMin(myTree);
printf("min = %d\r\n",min->Element);
Position_t max = FindMax(myTree);
printf("max = %d\r\n",max->Element);
return 1;
}