1 // 前缀: STree 2 3 /* 二叉搜索树 */ 4 // 1. 简化:使用int作为数据,各个数据互异。 5 6 #ifndef _TREE_H 7 #define _TREE_H 8 9 typedef int STree_T; 10 struct STreeNode; 11 typedef struct STreeNode *STreePosition; 12 typedef struct STreeNode *SearchTree; 13 14 // 创建树。返回NULL 15 SearchTree STree_Create(); 16 // 清空树并释放其空间,返回NULL 17 SearchTree STree_Disponse( SearchTree Tree ); 18 // 将X插入到Tree中,返回新树 19 SearchTree STree_Insert( SearchTree Tree, STree_T X ); 20 void STree_Insert2(SearchTree *pTree, STree_T X); // 与STree_Insert等价 21 // 将X从Tree中删除,返回新树 22 SearchTree STree_Delete( SearchTree Tree, STree_T X ); 23 // 查找X在Tree中的位置 24 STreePosition STree_Find( SearchTree Tree, STree_T X); 25 STreePosition STree_FindMin( SearchTree Tree ); // 最小元素 26 STreePosition STree_FindMax( SearchTree Tree ); // 最大元素 27 // 取Pos位置处的值 28 STree_T STree_Retrieve( STreePosition Pos ); 29 // 中序遍历 30 void STree_InOrderTraverse(SearchTree Tree); 31 32 #endif /* _TREE_H */
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include "searchTree.h" 4 5 /* 将结构体定义放在.c文件中,可以更好的封装模块 */ 6 typedef struct STreeNode { 7 STree_T Elem; 8 SearchTree Left; // 左子树 9 SearchTree Right; // 右子树 10 }STreeNode; 11 // 树的递归定义 12 13 // 创建树。返回NULL 14 SearchTree STree_Create() 15 { 16 return NULL; 17 } 18 19 // 清空树并释放其空间,返回NULL 20 SearchTree STree_Disponse(SearchTree Tree) 21 { 22 if(Tree != NULL) { 23 STree_Disponse(Tree->Left); 24 STree_Disponse(Tree->Right); 25 free(Tree); 26 } 27 return NULL; 28 } 29 30 // 将X插入到Tree中,返回新树 31 SearchTree STree_Insert(SearchTree Tree, STree_T X) 32 { 33 if(Tree == NULL) { // 创建节点 34 Tree = (SearchTree)malloc(sizeof(STreeNode)); 35 if(Tree == NULL) { 36 return NULL; 37 }else { 38 Tree->Elem = X; 39 Tree->Left = Tree->Right = NULL; 40 } 41 } else if(X < Tree->Elem) { // 在左子树中插入 42 Tree->Left = STree_Insert(Tree->Left, X); 43 } else if(X > Tree->Elem) { // 在右子树中插入 44 Tree->Right = STree_Insert(Tree->Right, X); 45 } 46 // else X == Tree->Elem,不做任何事 47 return Tree; 48 } 49 // 与Insert等价 50 void STree_Insert2(SearchTree *pTree, STree_T X) 51 { 52 if(pTree == NULL) return ; // 不是树,直接返回 53 54 if(*pTree == NULL) { // 在此处插入 55 *pTree = (SearchTree)malloc(sizeof(STreeNode)); 56 if(*pTree == NULL) { // 申请内存失败,返回 57 return ; 58 }else { 59 (*pTree)->Elem = X; 60 (*pTree)->Left = (*pTree)->Right = NULL; 61 } 62 }else if(X < (*pTree)->Elem) { // 在左子树插入 63 STree_Insert2(&(*pTree)->Left, X); 64 }else if(X > (*pTree)->Elem) { // 在右子树插入 65 STree_Insert2(&(*pTree)->Right, X); 66 } 67 // else X == (*pTree)->Elem, do nothing. 68 } 69 70 // 将X从Tree中删除,返回新树 71 SearchTree STree_Delete(SearchTree Tree, STree_T X) 72 { 73 if(Tree == NULL) { // 该树中没有元素X 74 return NULL; 75 }else if (X < Tree->Elem) { // 在左子树中删除 76 Tree->Left = STree_Delete(Tree->Left, X); 77 }else if (X > Tree->Elem) { // 在右子树中删除 78 Tree->Right = STree_Delete(Tree->Right, X); 79 }else { // X == Tree->Elem 删除该节点 80 if(Tree->Left && Tree->Right) { // 2个非空子树 81 // 用右子树最小值替换Tree的值,并删除右子树最小值的节点 82 STree_T rightMin = STree_FindMin(Tree->Right)->Elem; // 右子树不为空,肯定有最小值 83 Tree->Elem = rightMin; 84 Tree->Right = STree_Delete(Tree->Right, rightMin); 85 } else { // 0或1个非空子树 86 STreeNode *temp = Tree; 87 Tree = Tree->Left == NULL ? Tree->Right : Tree->Left; 88 free(temp); 89 } 90 } 91 92 return Tree; 93 } 94 95 // 查找X在Tree中的位置 96 STreePosition STree_Find(SearchTree Tree, STree_T X) 97 { 98 if(Tree == NULL) { // 未找到 99 return NULL; 100 }else if(Tree->Elem == X) { // 找到 101 return Tree; 102 }else if(X < Tree->Elem) { // 在左子树中找 103 return STree_Find(Tree->Left, X); 104 }else { // 在右子树中找 105 return STree_Find(Tree->Right, X); 106 } 107 } 108 109 STreePosition STree_FindMin(SearchTree Tree) 110 { 111 if(Tree != NULL) { 112 while(Tree->Left != NULL) { // 一直向左子树找,直到该节点无左孩子 113 Tree = Tree->Left; 114 } 115 } 116 return Tree; 117 } 118 119 STreePosition STree_FindMax(SearchTree Tree) 120 { 121 if(Tree != NULL) { 122 while(Tree->Right != NULL) { 123 Tree = Tree->Right; 124 } 125 } 126 return Tree; 127 } 128 // 取Pos位置处的值 129 STree_T STree_Retrieve(STreePosition Pos) 130 { 131 if(Pos != NULL) { 132 return Pos->Elem; 133 } 134 } 135 // 中序遍历 136 void STree_InOrderTraverse(SearchTree Tree) 137 { 138 if(Tree != NULL) { 139 STree_InOrderTraverse(Tree->Left); 140 printf("%d ", Tree->Elem); 141 STree_InOrderTraverse(Tree->Right); 142 } 143 }