二叉树--C语言实现 发表于 2017-07-20 | 分类于 c , 算法数据结构 数据结构与算法分析 笔记searchtree.c123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208#include <stdio.h>;#include <stdlib.h>;#define FatalError(str) fprintf(stderr, "%s\n", str),exit(1);#define Error(str) FatalError(str);struct TreeNode;typedef struct TreeNode *Position;typedef struct TreeNode *SearchTree;typedef int ElementType;SearchTree MakeEmpty(SearchTree T);Position Find(ElementType X, SearchTree T);Position FindMin(SearchTree);Position FindMax(SearchTree);SearchTree Insert(ElementType X, SearchTree T);SearchTree Delete(ElementType X, SearchTree T);ElementType Retrieve(Position P);ElementType DeleteMin(SearchTree T);struct TreeNode { ElementType Element; Position Left; Position Right;};SearchTree MakeEmpty(SearchTree T){ if(T != NULL){ T->Element = NULL; MakeEmpty(T->Left); MakeEmpty(T->Right); } return NULL;}Position Find(ElementType X, SearchTree T){ if(T == NULL){ return NULL; }else if(X < T->Element){ return Find(X, T->Left); }else if(X > T->Element){ return 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){ while(T->Right != NULL){ T = T->Right; } } return T;}// Position FindMax(SearchTree T)// {// if(T == NULL){// return NULL;// }else// if(T->Right == NULL){// return T;// }else{// return FindMax(T->Right);// }// }SearchTree Insert(ElementType X, SearchTree T){ if(T == NULL) { T = malloc(sizeof(struct TreeNode)); if( T == NULL) { FatalError("Out of space!!!"); } else { T->Element = X; 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); return T;}ElementType Retrieve(Position P){ return P->Element;}SearchTree Delete(ElementType X, SearchTree T){ Position P; if(T == NULL) { Error("Element not found"); } else if(X < T->Element) { T = T->Left; return Delete(X, T); } else if(X > T->Element) { T = T->Right; return Delete(X, T); } else if(T->Left && T->Right) { T->Element = DeleteMin(T->Right); } else { P = T; if(T->Left == NULL) T = T->Right; else if(T->Right == NULL) T = T->Left; free(P); } return T;}ElementType DeleteMin(SearchTree T){ Position P; ElementType X; P = FindMin(T); X = P->Element; free(P); return X;}void PrintSearchTree(SearchTree T){ if(T == NULL) { Error("Element not found"); } if(T->Left == NULL && T->Right == NULL) { printf("%d ", T->Element); }else{ if(T->Left != NULL) { PrintSearchTree(T->Left); } printf("%d ", T->Element); if(T->Right != NULL) { PrintSearchTree(T->Right); } }}void SearchTreeTest(){ SearchTree T; T = MakeEmpty(T); T = Insert(1,T); T = Insert(3,T); T = Insert(2,T); T = Insert(6,T); T = Insert(4,T); PrintSearchTree(T);}`</pre>> main.c<pre class="line-numbers prism-highlight" data-start="1">`#include "searchtree.c"int main(void){ SearchTreeTest(); return 0;}