1. 程式人生 > >數據結構之查找樹

數據結構之查找樹

lose fine findmi truct 前綴 treenode AR fin delete

 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 }
searchTree.c

數據結構之查找樹