Search: 1. Sequential search 2. Binary Search 3. multipart search 4. dynamic search for data tables (Binary sorting tree search, balanced binary tree AVL Tree, B tree, and B + tree) 5. Hash search --------------------------- Sequential search Note: sequential search is applicable to linear tables with sequential storage or link storage. Int sequelsearch (elemtype s [], keytype key, int N) /* Query records with the key keyword in sequence in S [0]-s [n-1 */ /* Returns the subscript sequence number of the record when the query is successful. If the query fails, returns-1 */ { Int I; I = 0; While (I <n & S [I]. Key! = Key) I ++; If (s [I]. Key = Key) Return I; Else return-1; } ---------------------------- Binary Search 1. Recursive Method implementation: Int bsearch (elemtype A [], elemtype X, int low, int High) /* Find the data element x in array a with the next row of low and the upper bound of high */ { Int mid; If (low> high) Return-1; Mid = (low + high)/2; If (x = A [Mid]) return mid; If (x <A [Mid]) Return (bsearch (A, X, low, mid-1 )); Else Return (bsearch (A, X, Mid + 1, high )); } 2. Non-recursive method implementation: Int bsearch (elemtype A [], keytype key, int N) { Int Low, high, mid; Low = 0; high = n-1; While (low <= high) {
Mid = (low + high)/2; If (A [Mid]. Key = Key) return mid; Else If (A [Mid]. Key <key) Low = Mid + 1; Else high = mid-1; } Return -1; } -------------------------- Multipart search Typedef int keytype; Typedef struct { Keytype key; } Elemtype; Typedef struct { Keytype key; Int link; } Indextype; Int indexsequelsearch (indextype ls [], elemtypes [], int M, int L, keytype Key) /* Searches for records with the key keyword in multiple parts. The index table is ls [0]-ls [M-1] */ /* The sequence table is S and the block length is L */ { Int I, J; /* Sequential search in the index table */ I = 0; While (I <M & Key> ls [I]. Key) I ++; If (I> = m) Return-1; Else { /* Search in sequence in the sequence table */
J = ls [I]. links; While (key! = S [J]. Key & J-ls [I]. Link <L) J ++; If (Key = s [J]. Key) return J; Else return-1; } } ---------------------------- Binary sorting tree search 1. Binary sorting tree search algorithm: A. Non-recursive algorithms: Btree * search (btree * B, int X) /* Search for X in Binary Tree B */ { If (B = NULL) Return (null ); Else {
If (B-> DATA = x) Return (B ); If (x <B-> data) Return (search (B-> left )); Else return (search (B-> right )); }
} B. recursive algorithms: Bsnodetype * search (bsnodetype * BT, keytype Key) /* Search for the key element in the binary tree BT */ { Bsnodetype * P; If (bt = NULL) Return (BT ); P = Bt; While (p-> key! = Key) { If (Key <p-> key) P = p-> lchild; Else P = p-> rchild;
If (P = NULL) break; } Return (P ); } 2. Generation of Binary Trees A. The function of inserting a node s into a binary tree B is as follows: Void insert (B, S) Btree * B, * s; { If (B = NULL) B = s; Else if (S-> DATA = B-> data)
Return (); Else if (S-> data <B-> data)
Insert (B-> left, S ); Else if (S-> DATA> B-> data)
Insert (B-> right, S ); } B. Generate a binary tree Void create (btree * B) { Int X; Btree 8 s; B = NULL; Do { Scanf ("% d", & X ); S = (bnode *) Malloc (sizeof (bnode )); S-> DATA = X; S-> left = NULL;
S-> right = NULL; Insert (B, S ); } While (X! =-1 ); } C. delete a node from a binary tree Bsnodetype * Delete (bsnodetype * BT, keytype Key) /* Delete the key-value node in the binary tree where BT is the root node */ { Bsnodetype * P, * q; If (BT-> key = key) { /* Left and right subtree of Bt are empty */
If (BT-> lchild = NULL & BT-> rchild = NULL) {
Free (BT);/* Delete A leaf node */ Return (null ); } Else If (BT-> lchild = NULL)/* The left subtree of Bt is empty */ {
P = Bt-> rchild; Free (BT ); Return (P ); }
Else if (BT-> rchild = NULL)/* The right subtree of Bt is empty */ {
P = Bt-> lchild; Free (BT ); Return (P ); }
Else { P = q = Bt-> rchild;
While (p-> lchild! = NULL) P = p-> lchild;
P-> lchild = Bt-> lchild; Free (BT ); Return (Q );
} } /* Delete the key-value node from the binary tree where Bt-> lchild is the root node */ If (BT-> key & BT-> lchild! = NULL)
BT-> lchild = Delete (BT-> lchild, key ); /* Delete the key-value node from the binary tree where Bt-> rchild is the root node */ If (BT-> key <key & BT-> rchild! = NULL)
BT-> rchild = Delete (BT-> rchild, key ); Return (BT ); } |