All pastes #1997308 Raw Edit

Binary_Tree.c implementation

public c v1 · immutable
#1997308 ·published 2010-11-20 19:04 UTC
rendered paste body
#include <stdio.h>#include "BinaryTree.h"#include <conio.h>#include <stdlib.h>Leaf* createLeaf(Value value){ // creating a new leaf which will contain the value	Leaf *leaf = NULL;	if( (leaf = (Leaf*)malloc(sizeof(Leaf)))==NULL ) // allocation memory		_ErrMsg("Insufficient Memory when allocation a new Leaf");	leaf->value = value;	leaf->leftTree = NULL;	leaf->rightTree = NULL;		return leaf;}/* initializing all the functions pointers to the specific pointer */void init(Compare_Func *_cmpF, Free_Func *_freeF, Create_New *_crtNewF, Print_Function *_printF, Compare_Func cmpF, Free_Func freeF, Create_New crtNewF, Print_Function printF){	*_cmpF = cmpF;	*_freeF = freeF;	*_crtNewF = crtNewF;	*_printF = printF;}Boolean insert(Binary_Tree **tree,Value value,Compare_Func cmpF){	CmpResult result;	if( (*tree) == NULL ){		(*tree) = createLeaf(value);		return True;	}	 result = cmpF(value,(*tree)->value);	if( result == Equals )		return False; // already exists.	if( result == Smaller ) 		return insert(&(*tree)->leftTree,value,cmpF);	else if( result == Bigger )		return insert(&(*tree)->rightTree,value,cmpF);	return False; // maybe we will change the compare values in the future...}Boolean isExist(Binary_Tree *tree,Value value,Compare_Func cmpF){	CmpResult result;	if( tree == NULL )		return False;	result = cmpF(value,tree->value);	if( result == Equals )		return True;	if( result == Bigger )		return isExist(tree->rightTree,value,cmpF);	if( result == Smaller )		return isExist(tree->leftTree,value,cmpF);		return False;}void destroyTree(Binary_Tree **tree, Free_Func freeF){		if( (*tree) == NULL || ( (*tree)->leftTree == NULL && (*tree)->rightTree == NULL ) )		return;	destroyTree(&(*tree)->leftTree,freeF);	if( (*tree)->leftTree != NULL ) {		freeF((*tree)->leftTree->value);		free((*tree)->leftTree);		(*tree)->leftTree = NULL;	}	destroyTree(&(*tree)->rightTree,freeF);	if( (*tree)->rightTree != NULL ){		freeF((*tree)->rightTree->value);		free((*tree)->rightTree);		(*tree)->rightTree = NULL;	}}void removeValue(Binary_Tree **tree,Value value,Compare_Func cmpF, Free_Func freeF){		CmpResult result;	Binary_Tree *temp,**anotherTemp;	if( (*tree) == NULL )		return;		// head	result = cmpF(value,(*tree)->value);	if( result == Equals ){				// if the tree is a Leaf		if( (*tree)->leftTree == NULL && (*tree)->rightTree == NULL ){			freeF((*tree)->value);			free((*tree));			(*tree)=NULL;		}		else{	// there is more than one side on the CURRENT(sub) tree			temp = (*tree);			if( (*tree)->leftTree == NULL )				(*tree) = (*tree)->rightTree;			else if( (*tree)->rightTree == NULL)				(*tree) = (*tree)->leftTree;			else{				(*tree) = (*tree)->rightTree;				for(anotherTemp = tree; (*anotherTemp)->leftTree != NULL; anotherTemp = &(*anotherTemp)->leftTree);   // go to the end of the subTree				(*anotherTemp)->leftTree = temp->leftTree;			}						freeF(temp->value);			free(temp);		}	}	else if( result == Smaller )		removeValue(&(*tree)->leftTree,value,cmpF,freeF);	else if( result == Bigger )		removeValue(&(*tree)->rightTree,value,cmpF,freeF);	}void inOrder(Binary_Tree *tree,Action_Func actionF){	if( tree == NULL )		return ;	inOrder(tree->leftTree,actionF);	actionF(*tree);	inOrder(tree->rightTree,actionF);}void postOrder(Binary_Tree *tree, Action_Func actionF){	if( tree == NULL )		return;	postOrder(tree->leftTree,actionF);	postOrder(tree->rightTree,actionF);	actionF(*tree);}void preOrder(Binary_Tree *tree, Action_Func actionF){	if ( tree == NULL )		return;	actionF(*tree);	preOrder(tree->leftTree,actionF);	preOrder(tree->rightTree,actionF);}void printMax(Binary_Tree *tree, Print_Function printF){	if( tree == NULL )		return;	printMax(tree->rightTree,printF);	printF(*tree);	printMax(tree->leftTree,printF);}void printMin(Binary_Tree *tree, Print_Function printF){	inOrder(tree,printF);}