7-28 搜索树判断 (25分)& 搜索树基本操作集
使用正常序列和镜像序列构造出的搜索树是一样的,所以insert函数用一个正常的即可。
镜像先序遍历函数和镜像后序遍历函数需要另外编写。
1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 int k = 0; 7 int f = 0; 8 int n; 9 int b[1001];//存放先序遍历得到的序列,与a[]序列进行对比,判断是否相同 10 typedef struct node 11 { 12 int data; 13 struct node* left; 14 struct node* right; 15 }*tree; 16 tree Insert(tree T, int x) 17 { 18 if (NULL == T) 19 { 20 T = new struct node; 21 T->data = x; 22 T->left = T->right = NULL; 23 return T; 24 } 25 else if (x >= T->data) 26 { 27 T->right = Insert(T->right, x); 28 return T; 29 } 30 else if (x < T->data) 31 { 32 T->left = Insert(T->left, x); 33 return T; 34 } 35 } 36 void pre_trvl(tree T) 37 { 38 if (!T) return; 39 else 40 { 41 b[k++] = T->data; 42 //cout << T->data << endl; 43 pre_trvl(T->left); 44 pre_trvl(T->right); 45 } 46 } 47 void pre_trvl1(tree T)//镜像先序 48 { 49 if (!T) return; 50 else 51 { 52 b[k++] = T->data; 53 //cout << T->data << endl; 54 pre_trvl1(T->right); 55 pre_trvl1(T->left); 56 } 57 }void post_trvl(tree T) 58 { 59 if (!T)return; 60 else 61 { 62 post_trvl(T->left); 63 post_trvl(T->right); 64 if(f==n-1)//控制输出格式,最后面不能有多余空格 65 cout << T->data ; 66 else 67 cout << T->data << " "; 68 f++; 69 } 70 } 71 void post_trvl1(tree T)//镜像后序 72 { 73 if (!T)return; 74 else 75 { 76 post_trvl1(T->right); 77 post_trvl1(T->left); 78 if(f==n-1) 79 cout << T->data ; 80 else 81 cout << T->data << " "; 82 f++; 83 } 84 } 85 int main() 86 { 87 int a[1001];//存放给定序列 88 cin >> n; 89 tree T = NULL; 90 for (int i = 0; i < n; i++) 91 { 92 cin >> a[i]; 93 } 94 if (a[1] < a[0]) 95 { 96 for (int i = 0; i < n; i++) 97 { 98 T = Insert(T, a[i]); 99 } 100 pre_trvl(T); 101 } 102 else 103 { 104 for (int i = 0; i < n; i++) 105 { 106 T = Insert(T, a[i]); 107 } 108 pre_trvl1(T);//插入函数相同,但先序遍历函数不同 109 } 110 int flag = 0; 111 for (int i = 0; i < n; i++) 112 { 113 if (a[i] != b[i]) 114 flag = 1; 115 } 116 if (flag == 1) 117 { 118 cout << "NO" << endl; 119 } 120 else 121 { 122 cout << "YES" << endl; 123 if (a[1] < a[0]) 124 post_trvl(T); 125 else 126 post_trvl1(T); 127 } 128 return 0; 129 }
操作集:6-12 二叉搜索树的操作集 (30分)
1 BinTree Insert( BinTree BST, ElementType X ) 2 { 3 if(!BST) 4 { 5 BinTree p = (BinTree)malloc(sizeof(struct TNode)); 6 p->Data = X; 7 p->Left = NULL; 8 p->Right = NULL; 9 BST=p; 10 return BST; 11 } 12 if(X < BST->Data) 13 { 14 BST->Left=Insert(BST->Left,X); 15 } 16 else if(X > BST->Data) 17 { 18 BST->Right=Insert(BST->Right,X); 19 } 20 return BST; 21 } 22 23 24 BinTree Delete( BinTree BST, ElementType x ) 25 { 26 if(!BST) 27 { 28 printf("Not Found\n"); 29 return NULL; 30 } 31 if(BST->Data == x) 32 { 33 if(!BST->Left && !BST->Right) 34 { 35 BST = NULL; 36 } 37 else if(BST->Left && !BST->Right) 38 { 39 BST = BST->Left; 40 } 41 else if(BST->Right && !BST->Left) 42 { 43 BST = BST->Right; 44 } 45 else if(BST->Right && BST->Left) 46 { 47 BinTree temp = FindMin(BST->Right); 48 Delete(BST->Right,temp->Data); 49 BST->Data = temp->Data; 50 } 51 return BST; 52 } 53 if(x > BST->Data) 54 { 55 BST->Right = Delete(BST->Right, x); 56 return BST; 57 } 58 if(x < BST->Data) 59 { 60 BST->Left = Delete(BST->Left, x); 61 return BST; 62 } 63 } 64 65 Position Find( BinTree BST, ElementType X ) 66 { 67 if(!BST) 68 return NULL; 69 70 if(X == BST->Data) return BST; 71 if(X<BST->Data) return Find( BST->Left, X ); 72 if(X>BST->Data) return Find( BST->Right, X ); 73 } 74 75 Position FindMin( BinTree BST ) 76 { 77 if(!BST)return NULL; 78 if(BST->Left) 79 return FindMin( BST->Left ); 80 else return BST; 81 82 } 83 84 Position FindMax( BinTree BST ) 85 { 86 if(!BST) return NULL; 87 if(BST->Right) 88 return FindMax( BST->Right ); 89 else return BST; 90 }