/* bst.h */
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/***********************************************************/ /* Copyright (C) SA14226214, USTC, 2014-2015 */ /* */ /* FILE NAME : bst.h */ /* PRINCIPAL AUTHOR : GaoZhipeng */ /* SUBSYSTEM NAME : BST_Tree */ /* MODULE NAME : BST_Tree */ /* LANGUAGE : C */ /* TARGET ENVIRONMENT : ANY */ /* DATE OF FIRST RELEASE : 2015/04/15 */ /* DESCRIPTION : This is a bst_tree program */ /***********************************************************/ /* *Revision log: * *Ceated by GaoZhipeng, 2015/04/15 * */ #ifndef _TREE_H #define _TREE_H #define ElementType int /* struct TreeNode { ElementType Element ; SearchTree Left; SearchTree Right; }; */ struct TreeNode; typedef struct TreeNode *Position; typedef struct TreeNode *SearchTree; SearchTree MakeEmpty( SearchTree T ); Position Find(ElementType X, SearchTree T); Position FindMin(SearchTree T); Position FindMax(SearchTree T); SearchTree Insert( ElementType X, SearchTree T ); SearchTree Delete( ElementType X, SearchTree T ); SearchTree Create( ElementType Node[] ); void PreOrderTraverse(SearchTree T); void InOrderTraverse(SearchTree T); void PostOrderTraverse(SearchTree T); int Height( SearchTree T ); int Max(int T1, int T2); #endif
/* bst.c */
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/***********************************************************/ /* Copyright (C) SA14226214, USTC, 2014-2015 */ /* */ /* FILE NAME : bst.c */ /* PRINCIPAL AUTHOR : GaoZhipeng */ /* SUBSYSTEM NAME : BST_Tree */ /* MODULE NAME : BST_Tree */ /* LANGUAGE : C */ /* TARGET ENVIRONMENT : ANY */ /* DATE OF FIRST RELEASE : 2015/04/15 */ /* DESCRIPTION : This is a bst_tree program */ /***********************************************************/ /* *Revision log: * *Ceated by GaoZhipeng, 2015/04/15 * */ #include<malloc.h> #include<stdio.h> #include"bst.h" struct TreeNode { ElementType Element ; SearchTree Left; SearchTree Right; }; SearchTree MakeEmpty( SearchTree T ) { if(T != NULL) { MakeEmpty(T->Left); MakeEmpty(T->Right); free(T); } return NULL; } Position Find(ElementType X, SearchTree T) { if(T == NULL) { return NULL; } if(T->Element < X) { Find(X, T->Left); } else if(T->Element > X) { Find(X, T->Right); } else return T; } Position FindMin( SearchTree T ) { if(T == NULL) return NULL; else { if(T->Left == NULL) { return T; } else { return FindMin(T->Left); } } } Position FindMax( SearchTree T ) { if(T == NULL) return NULL; else { while(T->Right != NULL) { T = T->Right; } } return T; } SearchTree Insert( ElementType X, SearchTree T ) { if(T == NULL) { /* Create and return a one-node tree */ T = malloc(sizeof(struct TreeNode)); if(T == NULL) { printf("Out of Space!"); } T->Element = X; T->Left = NULL; 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); } return T; } SearchTree Delete(ElementType X, SearchTree T) { Position TmpCell; if(T == NULL) { printf("Element not found"); } else if(X < T->Element) { T->Left = Delete(X, T->Left); } else if(X > T->Element) { T->Right = Delete(X, T->Right); } /*found the element to be deleted*/ /*当程序能执行到这一步,说明已经找到了要删除的节点了, 下面分情况讨论*/ /*第一种情况是要删除的节点有两个孩子*/ else if(T->Left && T->Right) { TmpCell = FindMin( T->Right ); T->Element = TmpCell->Element; T->Right = Delete(T->Element, T->Right); } /*另一种情况是要删除的节点只有一个孩子或者没有孩子*/ else { TmpCell = T; if(T->Left == NULL) { T = T->Right; } else if(T->Right == NULL) { T = T->Left; } free(TmpCell); } return T; } SearchTree Create( ElementType Node[] ) { SearchTree T = NULL; int i; // ElementType Node; // MakeEmpty(T); // printf("请输入二叉排序树的节点: "); for(i=0; i<10; i++) { // scanf("%d",&Node); T = Insert(Node[i], T); } return T; } void PreOrderTraverse(SearchTree T) { /*如果为空树就直接返回*/ if (T == NULL) return; printf("%d ", T->Element); PreOrderTraverse(T->Left); PreOrderTraverse(T->Right); } void InOrderTraverse(SearchTree T) { if( T == NULL ) return; InOrderTraverse(T->Left); printf("%d ", T->Element); InOrderTraverse(T->Right); } void PostOrderTraverse(SearchTree T) { if(T == NULL) return; PostOrderTraverse(T->Left); PostOrderTraverse(T->Right); printf("%d ", T->Element); } int Height( SearchTree T ) { /*树的高度从0开始*/ if(T == NULL) return -1; else { return 1+Max( Height(T->Left), Height(T->Right)); } } int Max(int T1, int T2) { if(T1 > T2) return T1; else return T2; }
/* test_bst.c */
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/***********************************************************/ /* Copyright (C) SA14226214, USTC, 2014-2015 */ /* */ /* FILE NAME : test_bst.c */ /* PRINCIPAL AUTHOR : GaoZhipeng */ /* SUBSYSTEM NAME : BST_Tree */ /* MODULE NAME : BST_Tree */ /* LANGUAGE : C */ /* TARGET ENVIRONMENT : ANY */ /* DATE OF FIRST RELEASE : 2015/04/15 */ /* DESCRIPTION : This is a bst_tree program */ /***********************************************************/ /* *Revision log: * *Ceated by GaoZhipeng, 2015/04/15 * */ #include<stdio.h> #include"bst.h" int main() { SearchTree T; ElementType Node[] = {62, 58, 88, 47, 73, 99, 35, 51, 93, 37}; T = Create(Node); printf("the height of the tree :%d ", Height(T)); printf("PreOrder :"); PreOrderTraverse(T); printf(" "); printf("InOrder :"); InOrderTraverse(T); printf(" "); printf("PostOrder :"); PostOrderTraverse(T); printf(" "); printf("Delete the root node 62 "); T = Delete(62, T); printf("PreOrder :"); PreOrderTraverse(T); printf(" "); printf("InOrder :"); InOrderTraverse(T); printf(" "); printf("PostOrder :"); PostOrderTraverse(T); printf(" "); }
/* makefile */
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#this is a makefile all : test test : test.o bst.o gcc $^ -o $@ test.o : test_bst.c bst.c bst.h gcc -c test_bst.c -o test.o linklist.o : bst.c bst.h gcc -c bst.c -o bst.o clean : -rm test *.o .PHONY: clean