本题要求实现给定二叉搜索树的5种常用操作。
函数接口定义:
BinTree Insert( BinTree BST, ElementType X );
BinTree Delete( BinTree BST, ElementType X );
Position Find( BinTree BST, ElementType X );
Position FindMin( BinTree BST );
Position FindMax( BinTree BST );
其中BinTree
结构定义如下:
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
- 函数
Insert
将X
插入二叉搜索树BST
并返回结果树的根结点指针; - 函数
Delete
将X
从二叉搜索树BST
中删除,并返回结果树的根结点指针;如果X
不在树中,则打印一行Not Found
并返回原树的根结点指针; - 函数
Find
在二叉搜索树BST
中找到X
,返回该结点的指针;如果找不到则返回空指针; - 函数
FindMin
返回二叉搜索树BST
中最小元结点的指针; - 函数
FindMax
返回二叉搜索树BST
中最大元结点的指针。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
void PreorderTraversal( BinTree BT ); /* 先序遍历,由裁判实现,细节不表 */
void InorderTraversal( BinTree BT ); /* 中序遍历,由裁判实现,细节不表 */
BinTree Insert( BinTree BST, ElementType X );
BinTree Delete( BinTree BST, ElementType X );
Position Find( BinTree BST, ElementType X );
Position FindMin( BinTree BST );
Position FindMax( BinTree BST );
int main()
{
BinTree BST, MinP, MaxP, Tmp;
ElementType X;
int N, i;
BST = NULL;
scanf("%d", &N);
for ( i=0; i<N; i++ ) {
scanf("%d", &X);
BST = Insert(BST, X);
}
printf("Preorder:"); PreorderTraversal(BST); printf("\n");
MinP = FindMin(BST);
MaxP = FindMax(BST);
scanf("%d", &N);
for( i=0; i<N; i++ ) {
scanf("%d", &X);
Tmp = Find(BST, X);
if (Tmp == NULL) printf("%d is not found\n", X);
else {
printf("%d is found\n", Tmp->Data);
if (Tmp==MinP) printf("%d is the smallest key\n", Tmp->Data);
if (Tmp==MaxP) printf("%d is the largest key\n", Tmp->Data);
}
}
scanf("%d", &N);
for( i=0; i<N; i++ ) {
scanf("%d", &X);
BST = Delete(BST, X);
}
printf("Inorder:"); InorderTraversal(BST); printf("\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
10
5 8 6 2 4 1 0 10 9 7
5
6 3 10 0 5
5
5 7 0 10 3
输出样例:
Preorder: 5 2 1 0 4 8 6 7 10 9 6 is found 3 is not found 10 is found 10 is the largest key 0 is found 0 is the smallest key 5 is found Not Found Inorder: 1 2 4 6 8 9
main.c:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #include "03_01_PTA.h" 5 #include "stack.h" 6 7 int main() 8 { 9 BinTree BST, MinP, MaxP, Tmp; 10 ElementType X; 11 int N, i; 12 13 BST = NULL; 14 scanf("%d", &N); 15 for(i=0;i<N;i++) { 16 scanf("%d", &X); 17 BST = Insert(BST, X); 18 } 19 printf("Preorder:"); PreorderTraversal(BST); printf("\n"); 20 MinP = FindMin(BST); 21 MaxP = FindMax(BST); 22 scanf("%d", &N); 23 for(i=0;i<N;i++) { 24 scanf("%d", &X); 25 Tmp = Find(BST, X); 26 if(Tmp == NULL) printf("%d is not found\n", X); 27 else { 28 printf("%d is found\n", Tmp->Data); 29 if(Tmp == MinP) printf("%d is the smallest key\n", Tmp->Data); 30 if(Tmp == MaxP) printf("%d is the largest key\n", Tmp->Data); 31 } 32 } 33 scanf("%d", &N); 34 for(i=0;i<N;i++) { 35 scanf("%d", &X); 36 BST = Delete(BST, X); 37 } 38 printf("Inorder:"); InorderTraversal(BST); printf("\n"); 39 40 return 0; 41 } 42 43 void PreorderTraversal(BinTree BT) 44 { 45 BinTree T = BT; 46 Stack S = CreateStack(); 47 while(T || !IsEmpty(S)) { 48 while(T) { 49 printf(" %d", T->Data); 50 StackPush(T, S); 51 T = T->Left; 52 } 53 if(!IsEmpty(S)) { 54 T = StackPop(S); 55 T = T->Right; 56 } 57 } 58 } 59 60 void InorderTraversal(BinTree BT) 61 { 62 BinTree T = BT; 63 Stack S = CreateStack(); 64 while(T || !IsEmpty(S)) { 65 while(T) { 66 StackPush(T, S); 67 T = T->Left; 68 } 69 if(!IsEmpty(S)) { 70 T = StackPop(S); 71 printf(" %d", T->Data); 72 T = T->Right; 73 } 74 } 75 } 76 77 BinTree Insert(BinTree BST, ElementType X) 78 { 79 if(!BST) { 80 BST = (BinTree)malloc(sizeof(struct TNode)); 81 BST->Data = X; 82 BST->Left = BST->Right = NULL; 83 } else { 84 if(X < BST->Data) { 85 BST->Left = Insert(BST->Left, X); 86 } else if(X > BST->Data) { 87 BST->Right = Insert(BST->Right, X); 88 } 89 } 90 return BST; 91 } 92 93 94 BinTree Delete(BinTree BST, ElementType X) 95 { 96 BinTree Tmp; 97 if(!BST) printf("Not Found\n"); 98 else { 99 if(X < BST->Data) 100 BST->Left = Delete(BST->Left, X); /* 左子树递归删除 */ 101 else if(X > BST->Data) 102 BST->Right = Delete(BST->Right, X); /* 右子树递归删除 */ 103 else { /* 找到需要删除的结点 */ 104 if(BST->Left && BST->Right) { /* 被删除的结点有左右子结点 */ 105 Tmp = FindMin(BST->Right); /* 在右子树中找到最小结点填充删除结点 */ 106 BST->Data = Tmp->Data; 107 BST->Right = Delete(BST->Right, BST->Data); /* 递归删除要删除结点的右子树中最小元素 */ 108 } else { /* 被删除结点有一个或没有子结点 */ 109 Tmp = BST; 110 if(!BST->Left) /* 有右孩子或者没有孩子 */ 111 BST = BST->Right; 112 else if(!BST->Right) /* 有左孩子,一定要加else,不然BST可能是NULL */ 113 BST = BST->Left; 114 free(Tmp); /* 如无左右孩子直接删除 */ 115 } 116 } 117 } 118 return BST; 119 } 120 121 Position Find(BinTree BST, ElementType X) 122 { 123 if(!BST) 124 return NULL; 125 if(X == BST->Data) 126 return BST; 127 if(X < BST->Data) 128 return Find(BST->Left, X); 129 if(X > BST->Data) 130 return Find(BST->Right, X); 131 } 132 133 Position FindMin(BinTree BST) 134 { 135 if(BST) { 136 while(BST->Left) { 137 BST = BST->Left; 138 } 139 } 140 return BST; 141 } 142 143 Position FindMax(BinTree BST) 144 { 145 if(BST) { 146 while(BST->Right) { 147 BST = BST->Right; 148 } 149 } 150 return BST; 151 }
03_01_PTA.h
1 #ifndef __03_01_PTA_H_ 2 #define __03_01_PTA_H_ 3 4 typedef int ElementType; 5 typedef struct TNode *Position; 6 typedef Position BinTree; 7 struct TNode { 8 ElementType Data; 9 BinTree Left; 10 BinTree Right; 11 }; 12 13 void PreorderTraversal(BinTree BT); 14 void InorderTraversal(BinTree BT); 15 16 BinTree Insert(BinTree BST, ElementType X); 17 BinTree Delete(BinTree BST, ElementType X); 18 Position Find(BinTree BST, ElementType X); 19 Position FindMin(BinTree BST); 20 Position FindMax(BinTree BST); 21 22 #endif
stack.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "stack.h" 4 5 Stack CreateStack() 6 { 7 Stack s; 8 s = (Stack)malloc(sizeof(struct SNode)); 9 s->Next = NULL; 10 return s; 11 } 12 13 int IsEmpty(Stack S) 14 { 15 return (S->Next == NULL); 16 } 17 18 void StackPush(BinTree X, Stack S) 19 { 20 struct SNode *TmpCell; 21 TmpCell = (Stack)malloc(sizeof(struct SNode)); 22 TmpCell->Data = X; 23 TmpCell->Next = S->Next; 24 S->Next = TmpCell; 25 } 26 27 BinTree StackPop(Stack S) 28 { 29 struct SNode *FirstCell; 30 BinTree TopElem; 31 if(IsEmpty(S)) { 32 printf("Stack Empty"); 33 return NULL; 34 } else { 35 FirstCell = S->Next; 36 S->Next = FirstCell->Next; 37 TopElem = FirstCell->Data; 38 free(FirstCell); 39 return TopElem; 40 } 41 }
stack.h
1 #ifndef __STACK_H_ 2 #define __STACK_H_ 3 4 #include "03_01_PTA.h" 5 6 //typedef BinTree ElementType; 7 typedef struct SNode *Stack; 8 struct SNode { 9 BinTree Data; 10 Stack Next; 11 }; 12 13 Stack CreateStack(); 14 int IsEmpty(Stack S); 15 void StackPush(BinTree X, Stack S); 16 BinTree StackPop(Stack S); 17 18 #endif
Makefile
SOURCE_FILE = stack.c 03_01_PTA.c STACK_SOURCE_FILE = stack_test.c stack.c #all: 03_01 stack_test #stack_test: $(STACK_SOURCE_FILE) # gcc $(STACK_SOURCE_FILE) -o stack_test -g -Wall 03_01:$(SOURCE_FILE) gcc $(SOURCE_FILE) -o 03_01 -g -Wall clean: rm -f stack_test 03_01