二叉搜索树基本操作:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct TreeNode *BinTree; 5 struct TreeNode{ 6 BinTree Left; 7 BinTree Right; 8 ElementType Data; 9 }; 10 BinTree BST; 11 12 Position Find(ElementType X, BinTree BST) 13 { 14 if(!BST) 15 return NULL; 16 if(X > BST->Data) 17 return Find(X, BST->Right); 18 else if(X < BST->Data) 19 return Find(X, BST->Left); 20 else 21 return BST; 22 } 23 24 //将尾递归函数改为迭代函数 25 Position IterFind(ElementType X, BinTree BST) 26 { 27 while(BST){ 28 if(X > BST->Data); 29 BST = BST->Right; 30 else if(X < BST->Data); 31 BST = BST->Left; 32 else 33 return BST; 34 } 35 return NULL; 36 } 37 38 Position FindMin(BinTree BST) 39 { 40 if(!BST) 41 return NULL; 42 else if(!BST->Left) 43 return BST; 44 else 45 return FindMin(BST->Left); 46 } 47 48 Position FindMax(BinTree BST) 49 { 50 if(BST){ 51 while(BST->Right) 52 BST = BST->Right; 53 } 54 return BST; 55 } 56 57 BinTree Insert(ElementType X, BinTree BST) 58 { 59 if(!BST){ 60 BST = (BinTree)malloc(sizeof(struct TreeNode)); 61 BST->Data = X; 62 BST->Left = NULL; 63 BST->Right = NULL; 64 }else{ 65 if(X > BST->Data) 66 BST->Right = Insert(X, BST->Right); 67 if(X < BST->Data) 68 BST->Left = Insert(X, BST->Left); 69 } 70 return BST; 71 } 72 73 BinTree Delete(ElementType X, BinTree BST) 74 { 75 Position Tmp; 76 if(!BST) 77 printf("要删除的元素未找到\n"); 78 else if(X < BST->Data) 79 BST->Left = Delete(X, BST->Left); 80 else if(X > BST->Data) 81 BST->Right = Delete(X, BST->Right); 82 else{ 83 if(BST->Left && BST->Right){ 84 Tmp = FindMin(BST->Right); 85 BST->Data = Tmp->Data; 86 BST = Delete(BST->Data, BST->Right); 87 free(Tmp); 88 }else{ 89 Tmp = BST; 90 if(!BST->Left) 91 BST = BST->Right; 92 if(!BST->Right) 93 BST = BST->Left; 94 free(Tmp); 95 } 96 } 97 return BST; 98 }
平衡二叉树基本操作:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct AVLNode *AVLTree; 5 struct AVLNode{ 6 AVLTree Left; 7 AVLTree Right; 8 ElementType Data; 9 int Height; 10 }; 11 BinTree BST; 12 13 int Max(int a, int b) 14 { 15 return a > b ? a : b; 16 } 17 18 AVLTree SingleLeftRotation(AVLTree A) 19 { //A必须有一个左子节点B 20 //将A与B做单左旋,更新A与B的高度,返回新的根节点B 21 AVLTree B = A->Left; 22 B->Right = A; 23 A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1; 24 B->Height = Max(GetHeight(B->Left), A->Height) + 1; 25 return B; 26 } 27 28 AVLTree DoubleLeftRightRotation(AVLTree A) 29 { //A必须有一个左子节点B,且B必须有一个右子节点C 30 //将A、B与C做两次单旋,返回新的根节点C 31 A->Left = SingleRightRotation(A->Left) //B与C做右单旋,C被返回 32 return SingleLeftRotation(A); //A与C做左单旋,C被返回 33 } 34 35 AVLTree Insert(AVLTree T, ElementType X) 36 { 37 if(!T){ //插入空树,新建包含一个结点的树 38 T = (AVLTree)malloc(sizeof(struct AVLNode)); 39 T->Data = X; 40 T->Height = 0; 41 T->Left = T->Right = NULL; 42 } 43 44 else if(X < T->Data){ //插入T的左子树 45 T->Left = Insert(T->Left, X); 46 //如果需要左旋 47 if(GetHeight(T->Left) - GetHeight(T->Right) == 2) 48 if(X < T->Left->Data) //X在“左左上” 49 T = SingleLeftRotation(T); //左单旋 50 else //X在“左右”上,LR旋 51 T = DoubleLeftRightRotation(T); 52 } 53 54 else if(X > T->Data){ 55 T->Right = Insert(T->Right, X); 56 if(GetHeight(T->Left) - GetHeight(T->Right) == 2) 57 if(X > T->Right->Data) 58 T = SingleRightRotation(T); 59 else 60 T = DoubleRightLeftRotation(T); 61 } 62 63 //else X == T->Data ,无需插入 64 65 //更新树高 66 T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1; 67 68 return T; 69 }