二叉搜索树的操作集

Posted onlyblues

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉搜索树的操作集相关的知识,希望对你有一定的参考价值。

二叉搜索树的操作集

本题要求实现给定二叉搜索树的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

 

解题思路

  AC代码:

 1 BinTree Insert(BinTree BST, ElementType X) {
 2     struct TNode *tmp = (struct TNode*)malloc(sizeof(struct TNode));
 3     tmp->Data = X;
 4     tmp->Left = tmp->Right = NULL;
 5     
 6     if (BST == NULL) return tmp;
 7     struct TNode *last = BST, *t = BST;
 8     while (t) {
 9         last = t;
10         if (X < t->Data) t = t->Left;
11         else t = t->Right;
12     }
13     
14     if (X < last->Data) last->Left = tmp;
15     else last->Right = tmp;
16     
17     return BST;
18 }
19 
20 BinTree Delete(BinTree BST, ElementType X) {
21     if (BST == NULL) {
22         puts("Not Found");
23         return NULL;
24     }
25     
26     if (X < BST->Data) BST->Left = Delete(BST->Left, X);
27     else if (X > BST->Data) BST->Right = Delete(BST->Right, X);
28     else {
29         if (BST->Left && BST->Right) {
30             struct TNode *tmp = FindMin(BST->Right);
31             BST->Data = tmp->Data;
32             BST->Right = Delete(BST->Right, tmp->Data);
33         }
34         else {
35             struct TNode *tmp = BST;
36             if (BST->Left) BST = BST->Left;
37             else BST = BST->Right;
38             free(tmp);
39         }
40     }
41     
42     return BST;
43 }
44 
45 Position Find(BinTree BST, ElementType X) {
46     while (BST) {
47         if (X < BST->Data) BST = BST->Left;
48         else if (X > BST->Data) BST = BST->Right;
49         else return BST;
50     }
51     return NULL;
52 }
53 
54 Position FindMin(BinTree BST) {
55     struct TNode *last = BST;
56     while (BST) {
57         last = BST;
58         BST = BST->Left;
59     }
60     
61     return last;
62 }
63 
64 Position FindMax(BinTree BST) {
65     struct TNode *last = BST;
66     while (BST) {
67         last = BST;
68         BST = BST->Right;
69     }
70     
71     return last;
72 }

  其中Insert函数可以改为递归代码。

 1 BinTree Insert(BinTree BST, ElementType X) {
 2     if (BST == NULL) {
 3         BST = (struct TNode*)malloc(sizeof(struct TNode));
 4         BST->Data = X;
 5         BST->Left = BST->Right = NULL;
 6     }
 7     else if (X < BST->Data) {
 8         BST->Left = Insert(BST->Left, X);
 9     }
10     else if (X > BST->Data) {
11         BST->Right = Insert(BST->Right, X);
12     }
13     return BST;
14 }

  补充省略的代码,完整程序如下:

  1 #include <stdio.h>
  2 #include <stdlib.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 int main()
 23 {
 24     BinTree BST, MinP, MaxP, Tmp;
 25     ElementType X;
 26     int N, i;
 27 
 28     BST = NULL;
 29     scanf("%d", &N);
 30     for ( i=0; i<N; i++ ) {
 31         scanf("%d", &X);
 32         BST = Insert(BST, X);
 33     }
 34     printf("Preorder:"); PreorderTraversal(BST); printf("\\n");
 35     MinP = FindMin(BST);
 36     MaxP = FindMax(BST);
 37     scanf("%d", &N);
 38     for( i=0; i<N; i++ ) {
 39         scanf("%d", &X);
 40         Tmp = Find(BST, X);
 41         if (Tmp == NULL) printf("%d is not found\\n", X);
 42         else {
 43             printf("%d is found\\n", Tmp->Data);
 44             if (Tmp==MinP) printf("%d is the smallest key\\n", Tmp->Data);
 45             if (Tmp==MaxP) printf("%d is the largest key\\n", Tmp->Data);
 46         }
 47     }
 48     scanf("%d", &N);
 49     for( i=0; i<N; i++ ) {
 50         scanf("%d", &X);
 51         BST = Delete(BST, X);
 52     }
 53     printf("Inorder:"); InorderTraversal(BST); printf("\\n");
 54 
 55     return 0;
 56 }
 57 
 58 BinTree Insert(BinTree BST, ElementType X) {
 59     struct TNode *tmp = (struct TNode*)malloc(sizeof(struct TNode));
 60     tmp->Data = X;
 61     tmp->Left = tmp->Right = NULL;
 62     
 63     if (BST == NULL) return tmp;
 64     struct TNode *last = BST, *t = BST;
 65     while (t) {
 66         last = t;
 67         if (X < t->Data) t = t->Left;
 68         else t = t->Right;
 69     }
 70     
 71     if (X < last->Data) last->Left = tmp;
 72     else last->Right = tmp;
 73     
 74     return BST;
 75 }
 76 
 77 BinTree Delete(BinTree BST, ElementType X) {
 78     if (BST == NULL) {
 79         puts("Not Found");
 80         return NULL;
 81     }
 82     
 83     if (X < BST->Data) BST->Left = Delete(BST->Left, X);
 84     else if (X > BST->Data) BST->Right = Delete(BST->Right, X);
 85     else {
 86         if (BST->Left && BST->Right) {
 87             struct TNode *tmp = FindMin(BST->Right);
 88             BST->Data = tmp->Data;
 89             BST->Right = Delete(BST->Right, tmp->Data);
 90         }
 91         else {
 92             struct TNode *tmp = BST;
 93             if (BST->Left) BST = BST->Left;
 94             else BST = BST->Right;
 95             free(tmp);
 96         }
 97     }
 98     
 99     return BST;
100 }
101 
102 Position Find(BinTree BST, ElementType X) {
103     while (BST) {
104         if (X < BST->Data) BST = BST->Left;
105         else if (X > BST->Data) BST = BST->Right;
106         else return BST;
107     }
108     return NULL;
109 }
110 
111 Position FindMin(BinTree BST) {
112     struct TNode *last = BST;
113     while (BST) {
114         last = BST;
115         BST = BST->Left;
116     }
117     
118     return last;
119 }
120 
121 Position FindMax(BinTree BST) {
122     struct TNode *last = BST;
123     while (BST) {
124         last = BST;
125         BST = BST->Right;
126     }
127     
128     return last;
129 }
130 
131 void PreorderTraversal(BinTree BST) {
132     if (BST) {
133         printf(" %d", BST->Data);
134         PreorderTraversal(BST->Left);
135         PreorderTraversal(BST->Right);
136     }
137 }
138 
139 void InorderTraversal(BinTree BST) {
140     if (BST) {
141         InorderTraversal(BST->Left);
142         printf(" %d", BST->Data);
143         InorderTraversal(BST->Right);
144     }
145

以上是关于二叉搜索树的操作集的主要内容,如果未能解决你的问题,请参考以下文章

二叉搜索树的操作集(30 分)

二叉搜索树的操作集

04-树7 二叉搜索树的操作集 (30分)

04-树7 二叉搜索树的操作集(30 分)

数据结构 04-树7 二叉搜索树的操作集 (30 分)

数据结构树 —— 编程作业 08 :二叉搜索树的操作集