二叉树 C实现

二叉树的基本操作,参考维斯的数据结构与算法分析

头文件

#ifndef _Bin_tree_H
#define _Bin_tree_H

struct TreeNode;
typedef struct TreeNode *Position;
typedef struct TreeNode *BinTree;
typedef int Item;

BinTree makeEmpty(BinTree T);
Position find(Item x,BinTree T);
Position findMax(BinTree T);
Position findMin(BinTree T);
BinTree insert(Item x,BinTree T);
BinTree deleteItem(Item x,BinTree T);
void printTree(BinTree T);
Position deleteMin(BinTree T);

#endif

struct TreeNode{
	Item item;
	BinTree left,right;
};

源文件
#include"bin_tree.h"

#include
#include

BinTree makeEmpty(BinTree T){
	
	if(T!=NULL){
		makeEmpty(T->left);
		makeEmpty(T->right);
		free(T);
	}
	return NULL;
}

Position find(Item x,BinTree T){
	
	if(T==NULL){
		printf("find fail,no such a key\n");
		return NULL;
	}
			
	if(x>T->item)
	  return find(x,T->right);
	else if(x item)
	  return find(x,T->left);
	else return T;
}

Position findMax(BinTree T){
	
	if(T==NULL){
		printf("null tree\n");
		return NULL;
	}  
	else if(T->right==NULL)
	  return T;
	else return findMax(T->right);
}

Position findMin(BinTree T){
	
	if(T==NULL){
		printf("null tree\n");
		return NULL;
	}	  
	while(T->left)
	  T=T->left;
	return T;
}

BinTree insert(Item x,BinTree T){
	
	if(T==NULL){
		T=malloc(sizeof(struct TreeNode));
		if(T==NULL){
			puts("arrange tree fail\n");
			exit(1);
		}
		T->item=x;
		T->left=T->right=NULL;
	
	}
	else if(T->item > x)
	  T->left=insert(x,T->left);
	else if(T->item < x)
	  T->right=insert(x,T->right);
	  
	return T;
}

Position deleteMin(BinTree T){
	
	Position p=NULL;
	while(T->left){
		p=T;
		T=T->left;
	}
	if(p!=NULL){
		p->left=T->right;
		return T;
	}
	else{p=T;
	     T=T->right;
	     return p;
	}
	
}

BinTree deleteItem(Item x,BinTree T){
	BinTree tmp;
	if(T==NULL){
		puts("delete fail,no such a key\n");
		exit(1);
	}
	else if(T->item > x)
	  T->left=deleteItem(x,T->left);
	else if(T->itemright=deleteItem(x,T->right);
	else if(T->left && T->right){
	/*	tmp=findMin(T->right);
		T->item=tmp->item;
		T->right=deleteItem(tmp->item,T->right);           //这种方法需要访问两次T->right,浪费时间 
		*/
		tmp=deleteMin(T->right);                 //这个方法可以提高效率 
		T->item=tmp->item;
		free(tmp);
	    }
	else{
		tmp=T;
		if(T->left==NULL)
		  T=T->right;
		else if(T->right==NULL)
		  T=T->left;
		free(tmp);
	} 
	 
	return T;	
}

void printTree(BinTree T){
	
	if(T!=NULL){
		printTree(T->left);
		printf("%d\t",T->item);
		printTree(T->right);
	}
	
}
测试历程

#include
#include

#include"bin_tree.h"

int main(){
	BinTree tree=NULL;
	find(4,tree);
	tree=insert(4,tree);
	tree=insert(43,tree);
	tree=insert(24,tree);
	tree=insert(14,tree);
	tree=insert(234,tree);
	
	printTree(tree);
	
	printf("\n最大值:%d\n",findMax(tree)->item);
	printf("最小值:%d\n",findMin(tree)->item);
	
	tree=deleteItem(findMax(tree)->item,tree);
	tree=deleteItem(findMin(tree)->item,tree);
	tree=insert(7,tree);
	printTree(tree);
	tree=deleteItem(43,tree);
	printf("\n删除43结果:\n");
	printTree(tree);
	
/*	find(100,tree);
	BinTree p;
	p=makeEmpty(tree);
	if(p==NULL)
	  printf("null tree");
*/	
	
}




你可能感兴趣的:(数据结构与算法分析,c语言)