To take the data structure test in the past two days, write the binary sorting tree.

There are three operations in the binary sorting tree: insert, search, delete, and traverse;

First, we should remind you to use a two-dimensional pointer for operations such as insertion and deletion to change the tree. Otherwise, we cannot change it. Instead, we need to traverse (print) and search (the search here is because it is a service for deletion, therefore, a two-dimensional pointer is used. In fact, pure search is not required.) You do not need to change the value of the two-dimensional pointer.

Insert: place a small value on the left and a large value on the right. If the values are equal, do not perform recursive processing.

Search: if the search value is equal to the current node, the current node is returned. If the search value is smaller than the current node, it is found in the left subtree of the current node. If it is larger than the current node, search in the current right subtree. If the difference cannot be found, null is returned;

Delete: This is the most headache for me. I started to change it based on the data structure, but I was always looking for something that was not a pointer problem. I didn't get any results for one night, finally, I can see a document to write down the idea of deleting nodes. The main idea is as follows:

There are two cases in total. Assume that the node P to be deleted has a left subtree and a left subtree (including the one where P has no children ); when there is a left subtree, find the rightmost one in the left subtree of P (that is, the largest one in the left subtree of P) R, replace the value of P with the value of R, so that the original value of P is overwritten, so that P does not exist, so there is another node R. What should I do, of course it is deleted, but R may have a left subtree. If R has a left subtree, replace the r seat with the left subtree of R. In another case, if there is no left subtree, use the right subtree of P to replace his position, then you can delete the original node (P also applies when there are no children );

Traversal: You can directly traverse in the middle order;

# Include <stdio. h> # include <stdlib. h> # include <conio. h> typedef struct node {int data; struct node * LC, * RC;} node, * link; void insert (link * l, int N) {If (* l = NULL) {(* L) = new node; // If the insert position is found, the newly applied node (* l) -> lc = (* l)-> rc = NULL; (* l)-> DATA = N;} else {If (n = (* l)-> data) // if n is equal to the value of the current node, return; else if (n <(* l)-> data) is not inserted) // if n is smaller than the current node value, insert (& (* l)-> LC, n) to the left subtree of the current node ); else // if n is greater than the current node Value Insert (& (* l)-> RC, n);} link * search (link * l, int X) to the right subtree of the current node) {link P; If (* l) {If (* l)-> DATA = x) // If a node is found, return l of the current node; if (x <(* l)-> data) // If X is smaller than the current node value, return search (& (* l)-> LC, x) in the left subtree of the current node; else // If X is greater than the value of the current node, return search (& (* l)-> RC, x);} return NULL;} void Delete (link * P) in the right subtree of the current node) {If (* P)-> Lc) // if there is a left subtree {link pre = (* P)-> lC; link R = (* P)-> lC; while (R -> RC) // find the rightmost node in the left subtree pre = R, R = r-> RC; (* P)-> DATA = r-> data; // use the value of the rightmost node in the left subtree to overwrite the node to be deleted if (pre! = R) // If P left subtree has right node pre-> rc = r-> lC; else // If P left subtree does not have right node (* P) -> lc = r-> lC; free (r); // release node} else // if there is no left subtree {link q = (* P ); * P = (* P)-> RC; free (Q); q = NULL ;}} void print (link L) {If (l) // In-order traversal {print (L-> Lc); printf ("% d", L-> data); print (L-> RC) ;}} int main () {link L = NULL; int n, x; while (1) {printf ("1 insert 2 middle order traversal 3 search 4 Delete \ n"); char ch; ch = getch (); If (CH = '1') // insert {printf ("Enter N:"); scan F ("% d", & N); insert (& L, n);} else if (CH = '2 ') // In-order traversal {If (L = NULL) printf ("no node \ n"); elseprint (l);} else if (CH = '3 ') // query {If (L = NULL) printf ("no node \ n"); else {printf ("Enter the number of queries X :"); scanf ("% d", & X); Link * P; P = search (& L, x); If (p) printf ("search successful \ n "); elseprintf ("No % d node \ n", x) ;}} else if (CH = '4 ') // Delete {printf ("Enter the number X:"); scanf ("% d", & X); Link * P; P = search (& L, x); // check whether this node is available if (! P) printf ("No such number"); else {Delete (p); // Delete printf ("deleted successfully \ n ");}} printf ("\ NOK"); getch (); System ("CLS");} return 0 ;} // +-+ * 3 # * X #/1 # X #5 ##