Keywords: REST C

# Linear table and its application

## Experiment content:

``` 1. [problem description] a demonstration program is designed to implement the addition operation of any length integer.
2. [basic requirements] realize the storage of long integers by using bidirectional circular list, each node contains one integer. Input and output form: according to China's representation of long integers, each group of four bits is separated by commas.
3. [test data]
(1) 0; 0; expected '0'.
(2) - 23456789; - 76543211; should output "- 100000000".
(3) - 9999999; 1000000000; should output "99990000001".
(4) 100010001; - 100010001; should output "0".
(5) 100010001; - 100010000; output "1".
(6) - 99999999999; - 99999999999; should output "- 19999999998".
(7) 100009999999; 1; should output "1000100000000".
```

## Implementation ideas:

```1. Use a header node to store symbol bits
2. Store the input data with the bidirectional circular list and input the data with the head insertion method: first use getchar() to check whether the input is a positive number or a negative number, then use isdigit() to check whether it is a number, if it is ungetc() to return the number, use dowhile circular input
3. Print data: when to print circularly, first print the first group of numbers and "-" and use% 04 to ensure that 0 is enough.
4. Calculation: addition and subtraction; calculation from the last group of numbers forward
5. A chain table is used to store the calculation results, and the tail interpolation method is used to match the input of the calculation results.
Use a calculation module to include addition and subtraction, and judge the use by judging the positive and negative of two input numbers
```

## Implementation code:

```#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct node{
int data;
struct node *next;
struct node *prior;
}Node,*List;

List InitList();
void InsertList();//The head insertion method is used to store the data input for the convenience of output, and the storage mode conforms to the reading mode
List InputData();//Input data
void PrintList();//Print results from the head
void InsertResult();//Input of the calculation result of the tail interpolation method
void SubList();
List CalList();//Judge addition and subtraction and calculate
void Del();

void Del(List L,Node *p){
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
List InitList(int sign){
List L;
L = (List)malloc(sizeof(Node));
L -> next = L -> prior = L;
L ->data = sign;
return L;
}
void InsertList(List L,int data){
Node *p;
p = (List)malloc(sizeof(Node));
p ->data = data;
p ->next = L;
p ->prior = L ->prior;
L ->prior ->next = p;
L ->prior = p;
}
List InputData(){
int firstnum = 1, data;
char c;
List L;
L =(List)malloc(sizeof(Node));
L ->next = L ->prior = L;
if((c = getchar())=='-'){//Judge whether the first input is negative
L = InitList(-1);
}else{
L = InitList(1);
}
if (isdigit(c))//Is it a number
ungetc(c,stdin);//Return first number
do{
scanf("%d",&data);
InsertList(L,data);
}while((c = getchar())!= '\n');
return L;
}
void PrintList(List L){
Node *p = L;
int firstg = 1;//First set of numbers
if(p->data == -1&&p->next->data != 0){
printf("-");
}
p = p->next;
while(p != L){
if(firstg){
firstg = 0;
printf("%d",p->data);
}else{
printf(",%04d",p->data);
}
p = p->next;
}
printf("\n");
}
void InsertResult(List L,int data){
Node *p;
p = (List)malloc(sizeof(Node));
p ->data = data;
p -> next = L ->next;
p ->prior = L ;
L ->next ->prior = p;
L ->next = p;
}
void AddList(List a,List b,List c)//From back to front{
List pa,pb;
int carry = 0;/**Carry **/
int temp;
/**The first precursor of the loop list is the last*/
pa = a -> prior;
pb = b -> prior;
while((pa!=a)&&(pb!=b)){
temp = pa->data + pb -> data + carry;
if(temp >= 10000){
carry = 1;
temp -= 10000;
}else{
carry = 0;
}
InsertResult(c,temp);
pa = pa ->prior;
pb = pb ->prior;
}
while(pa != a){//pb is over
temp = pa ->data + carry;
if(temp >= 10000){
carry  =1;
temp -= 10000;
}else{
carry = 0;
}
InsertResult(c,temp);
pa = pa ->prior;
}
while(pb != b){//pa's done
temp = pb ->data + carry;
if(temp >= 10000){
carry  = 1;
temp -= 10000;
}else{
carry = 0;
}
InsertResult(c,temp);
pb = pb ->prior;
}
if(carry != 0)//Move forward 1
InsertResult(c,1);
}
void SubList(List a,List b,List c){
List pa, pb, pc;
int borrow = 0,tmp;
pa = a->prior;
pb = b->prior;
while((pa != a) && (pb != b)){                //Judge whether a > b or a < B
if (pa->data >= pb->data + borrow)
borrow = 0;
else
borrow = 1;
pa = pa->prior;
pb = pb->prior;
}
if (pa != a || (pa == a && pb == b && borrow == 0)){    //Judge whether a > b or a < B

// a >= b
c->data = a->data;
}
pa = a->prior;
pb = b->prior;
if(c->data!=b->data){//A>b situation
borrow=0;
while((pa != a) && (pb != b)){        //Import the subtraction between b and a into c
if (pa->data >= pb->data + borrow){   //No borrow
tmp = pa->data - pb->data - borrow;
borrow = 0;
}else{
tmp = 10000 + pa->data - pb->data - borrow;
borrow = 1;
}
InsertResult(c, tmp);
pa = pa->prior;
pb = pb->prior;
}
while(pa != a){                  //Import the rest of a into c
if (pa->data >= borrow){
tmp = pa->data - borrow;
borrow = 0;
}else{
tmp = 10000 - pa->data - borrow;
borrow = 1;
}
InsertResult(c, tmp);
pa = pa->prior;
}
} else{   //c<b
c->data=b->data;
borrow=0;
while((pa != a) && (pb != b)){          //Import a and b into c
if (pb->data >= pa->data + borrow){
tmp = pb->data - pa->data - borrow;
borrow = 0;
}else{
tmp = 10000 + pb->data - pa->data - borrow;
borrow = 1;
}
InsertResult(c, tmp);
pa = pa->prior;
pb = pb->prior;
}
while(pb != b){           //Import b remaining
if (pb->data >= borrow){
tmp = pb->data - borrow;
borrow = 0;
}else{
tmp = 10000 - pb->data - borrow;
borrow = 1;
}
InsertResult(c, tmp);
pb = pb->prior;
}
}
pc = c->next;
while(pc->next !=c && pc->data == 0){     //In order to prevent the head from becoming 0 due to borrow
pc = pc->next;
Del(c, pc->prior);
}
}
List CalList(List a, List b){
List c;
if(a->data * b ->data > 0){
c = InitList(a->data);
}else{
c = InitList(b->data);
SubList(a,b,c);
}
return c;
}
void int main(){
while(1)
{List a,b,c;
printf("First number:\n");
a = InputData();
printf("Second number:\n");
b = InputData();
c = CalList(a,b);
printf("Operation result:");
PrintList(c);
printf("*********************************************\n");
}
return 0;
}
```

# Stack and horse stepping on chessboard

## Experiment content:

```[problem description] to design a demo program for a chess horse to walk across the board.
[basic requirements] place the horse randomly in a square of the Board[0-7] [0-7] of the 8 × 8 chess board, and move the horse according to the rules of walking chess. Each square is required to enter only once and walk through all 64 squares on the chessboard. Compile non recursive program, find out the horse's walking route, and according to the calculated walking route, put the number 1, 2 , 64 successively fill in an 8 × 8 square matrix and output it.
[test data] specify the initial position of the horse.
[implementation tips]
For example, the initial horse is in the second row and the third column. The eight positions that can be walked are shown in the figure below.

0	1	2	3	4	5	6	7
0			8		1
1		7				2
2				H
3		6				3
4			5		4
5
6
7
```

## Implementation ideas:

```1. Stepping on the chessboard is to fill 1 to 64 in the 8 * 8 matrix, requiring two adjacent numbers to be placed in the matrix according to the "horse" movement rule. In this way, from 1 to 64 in the matrix, we can get the walking route of horse stepping on the chessboard. This process is a recursive process of repetitive operations.
2. The walking process of "horse" is actually a process of deep exploration. The root node of "exploration tree" is the initial position of "horse" in the chessboard. Next, "horse" has two ways of walking, so the root node derives two branches. If we go on the same step further, we will get two different paths. If we do this, we will get all possible walking states of "horse".
3. For the program function required by the title, the sequential stack or chain stack can be used to store its data. Because the required storage space is not large, and there is no need to open up a lot of space dynamically, the relatively simple sequential stack is used to store data, which is convenient and simple, while the chain stack is relatively more complex than the sequential stack in implementation.
4. therefore, the construction code is divided into three modules: the starting coordinate function module -- the starting position of the horse on the chessboard; the path function module -- the horse tries in each direction until the complete chessboard is tried; the output horse
The path function module of children walking - output the path of horses walking;
```

## Implementation code:

```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define X 8
#define Y 8
#define STACK_INIT_SIZE 100
#define STACKINCREMENT
typedef int ElemType;
typedef struct {
ElemType *top;
ElemType *base;
int stacksize;
}sqStack;

int chess[X][Y];
int travel();
int nextlocation(int *x,int *y,int count);
void initstack();
void push();
void pop();
void print();

int nextlocation(int *x,int *y,int count){
switch(count){
case 0:
if(*x+2<=X-1 && y-1>=0 && chess[*x+2][*y-1]==0){
*x+=2;
*y-=1;
return 1;
}
break;
case 1:
if( *x+2 <=X-1 && *y+1<=Y-1 && chess[*x+2][*y+1]==0){
*x+=2;
*y+=1;
return 1;
}
break;
case 2:
if( *x+1<=X-1 && *y-2 >=0 && chess[*x+1][*y-2]==0){
*x+=1;
*y+=2;
return 1;
}
break;
case 3:
if(*x+1<=X-1 && *y+2 <= Y-1 && chess[*x+1][*y+2]==0){
*x+=1;
*y+=2;
return 1;
}
break;
case 4:
if(*x-2>=0 && *y-1>=0 && chess[*x-2][*y-1]==0){
*x-=2;
*y+=1;
return 1;
}
case 5:
if(*x-2>=0 && *y+1<=Y-1 && chess[*x-2][*y+1]==0){
*x-=2;
*y+=1;
return 1;
}
break;
case 6:
if( *x-1>=0 && *y-2 >= 0 && chess[*x-1][*y-2]==0){
*x-=1;
*y-=2;
return 1;
}
break;
case 7:
if(*x-1>=0 && *y+2<=Y-1 && chess[*x-1][*y+2]==0){
*x-=1;
*y+=2;
return 1;
}
break;
default:
break;
}
return 0;
}
int travel(int x,int y,int tag)//x. Y is the coordinate, tag is the marker variable, and non recursively makes it a global variable. Each step, tag+{
chess[x][y]=tag;
int x1 = x,y1=y,flag = 0,count = 0;
if(X*Y == tag){
return 1;
}
flag = nextlocation(&x1,&y1,count);
while(0 == flag && count < 7){
count++;
flag = nextlocation(&x1,&y1,count);
}
while(flag){
if(travel(x1,y1,tag+1)){
return 1;
}
x1=x;
y1=y;
count++;
flag=nextlocation(&x1,&y1,count);
}
if(0==flag){
chess[x][y]= 0;
}
}
void print(){
int i,j;
for(i=0;i<X;i++){
for(j = 0;j<Y;j++){
printf("%2d\t",chess[i][j]);
}
printf("\n");
}
printf("\n");
}
void initstack(sqStack *s){
s->base = (ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!s->base){
exit(0);
}
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;

}
void push(sqStack *s,ElemType *e){
if(s->top - s->base >= s->stacksize ){
s->base = (ElemType*)realloc(s->base,(s->stacksize + STACK_INIT_SIZE)*sizeof(ElemType));
if(!s->base){
exit(0);
}
}
*(s->top)++ = e;
}
void pop(sqStack *s,ElemType *e){
if(s->top = s->base){
return;
}
*(--s->top) = e;
}
void int main(){
int i,j;
clock_t start,finish;
start= clock();
for(i=0;i<X;i++){
for(j=0;j<Y;j++){
chess[i][j]=0;
}
}
if(!travel(2,0,1)){
printf("fail");
}
print();
finish=clock();
printf("\ntime is :%d second",(double)(finish-start)/CLOCKS_PER_SEC);
return 0;
}
```

# Hierarchical traversal of binary trees

## Experiment content:

```[problem description] design a demonstration program that can traverse the binary tree of chain storage hierarchically.
[basic requirements] input data from the keyboard, create a binary tree, and then output the hierarchical calendar results.
The binary tree of test data input is shown in Figure 6.8 (b) on page 127 of the textbook (the textbook is Yan Weimin data structure C Language Edition), and the output is ABCDEFG.
```

## Implementation ideas:

```1. Build a binary tree and traverse the input
2. Establish a queue
3. Sequence traversal output:
The root node enters the queue, the root node exits the queue, the left and right sons of the root node enter the queue, and so on
```

## Implementation code:

```/*
[Problem Description: design a demonstration program that can traverse the binary tree of chain storage hierarchically.
[Basic requirements] input data from the keyboard, create a binary tree, and then output the hierarchical calendar results.
[The binary tree of test data input is shown in Figure 6.8 (b) on page 127 of the textbook, and the output is ABCDEFG.
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH  50
typedef char ElemType;
typedef struct {
ElemType *base;
int front;
int rear;
}SqQueue;
void initqueue(SqQueue *a){
a->base=(ElemType *)malloc(sizeof(ElemType)* MAX_LENGTH);
a->front=a->rear=0;
}
int SqQueueempty(SqQueue *a){
if(a->rear==a->front) return 1;
else return 0;
}
void enqueue(SqQueue *a, ElemType e){
if((a->rear+1)%MAX_LENGTH==a->front) printf("full now!\n");
else{
a->base[a->rear]=e;
a->rear=(a->rear+1)%MAX_LENGTH;

}

}
void dequeue(SqQueue *a, ElemType *e){
if(a->rear==a->front) printf("NULL!\n");
else{
*e=a->base[a->front];
a->front=(a->front+1)%MAX_LENGTH;
}

}
/**Establishment of binary tree*/
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree *T){//Building binary tree and traversing input

char c;
scanf("%c",&c);
if(c == ' '){
*T = NULL;
}else{
*T = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->data=c;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
void visit(char c,int level){
printf("%c,Be located%d layer\n",c,level);
}
void PreOrderTraversal(BiTree T,int level){
if(T)
{
visit(T->data,level);
PreOrderTraversal(T->lchild,level+1);
PreOrderTraversal(T->rchild,level+1);
}
}
void LevelQrderTraversal(BiTree T){
SqQueue q;
BiTree B;
initqueue(&q);
B = T;
enqueue(&q,B);
while(!SqQueueempty(&q)){
dequeue(&q,&B);
printf("%c",B->data);
if(B->lchild)
enqueue(&q,B->lchild);}
if(B->rchild)
enqueue(&q,B->rchild);
}
}

void int main(){
int level = 1;
BiTree T = NULL;
CreateBiTree(&T);
PreOrderTraversal(T,level);
LevelQrderTraversal(T);
return 0;
}
```

# Storage and traversal of Graphs

## Experiment content:

```[problem description] according to the user's input, an undirected graph stored by adjacency matrix is established, then it is transformed into adjacency table storage, and finally the forest is generated by depth first search.
[basic requirements]
1. Write a subroutine to create adjacency matrix. It enters data from the keyboard.
2. Write a subroutine to convert the adjacency matrix into adjacency table.
3. Traversing the adjacency list by depth first search, the forest formed is stored by binary list.
4. Print the forest with the concave method.
[test data] create the undirected figure G3 on page P159 of the textbook. For the output, see Figure 7.15 (c) on page 171 of the textbook
```

## Implementation ideas:

```1. If the adjacency matrix structure stores the graph, when there are more nodes but more edges, the storage space is wasted. In the adjacency table structure, the head node chain can be used to store all the nodes of the graph, and each head node is the starting point of the chain table with the head node as the starting node's edge. The node structure of each linked table is the position of the node in the head node linked list and its next node. The key points of graph related storage are: the structure of the head node list and the edge list. The head node should contain the value of the node and the link list pointer of the edge list.
3. Depth first traversal can start from the initial point of the graph, access the initial point, and then start from the adjacency point of v which is not visited in turn, until all the vertices in the graph that have path communication with v are visited; if there are still vertices not visited at this time, start from another vertex which is not visited, repeat the above process until all the points are visited. This is a recursive process. Therefore, in the process of depth first traversal, depth first search function must be called recursively. In the depth first search function, a flag array must be set to mark whether the node is accessed.
4. The specific process shall be: visit the initial point Vi first, and mark that it has been visited. In this case, a pointer p pointing to the edge node is defined, and a while() loop is established. The control condition is that the object indicated by the pointer is not empty. When the adjacent point of Vi is not accessed, the recursive call depth takes precedence over the traversal function. Then point the p pointer to the next edge node. In this way, the depth first traversal of the graph can be completed.
```

## Implementation code:

```#include <string.h>
#include <malloc.h>
#include <conio.h>
#define MAX_VERTEX_NUM 30
#define OK 1
int visited[30];
typedef int InfoType;
typedef struct ArcNode{ //arc
struct ArcNode *nextarc;
}ArcNode;
int data;
ArcNode *firstarc;
typedef struct{//chart
int vexnum,arcnum;
int kind;
}ALGraph;
typedef struct QNode{
int data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
void CreateDG(ALGraph G){
int k,i,v1;
printf("\n Please enter the number of nodes: ");
scanf("%d",&G.vexnum);
printf("Please enter the number of arcs: ");
scanf("%d",&G.arcnum);
G.vertices[i].data=i;
G.vertices[i].firstarc=NULL;
}
for(k=1;k<=G.vexnum;k++) //Input edge {
int v2;
scanf("%d",&v2);
printf("Please enter and%d Node number of nodes connected: ",k);
scanf("%d",&v1);
ArcNode *p;
p=(ArcNode*)malloc(sizeof(ArcNode));
if(!p) exit(-1);
p->nextarc=NULL;
G.vertices[k].firstarc=p;
for(int i=1;i<v2;i++){
int m;
printf("Please enter and%d Node number of nodes connected: ",k);
scanf("%d",&m);
ArcNode *q;
q=(ArcNode *)malloc(sizeof(ArcNode));//Dynamic pointer
if(!q) exit(-1);
q->nextarc=NULL;
p->nextarc=q;
p=q;
//free(q);
}
//free(p);
}
}
void DFS (ALGraph G,int v ){//Deep search
visited[v]=1;
printf("%d  ",G.vertices[v].data);
ArcNode *x;
x=(ArcNode*)malloc(sizeof(ArcNode));
if(!x) exit(-1);
x=G.vertices[v].firstarc;
int w;
for (;x;x=x->nextarc){
if(visited[w]==0)
DFS(G,w);
}
}
void DFSB (ALGraph G,int v){//Edge set of depth search
visited[v]=1;
ArcNode *y;
y=(ArcNode*)malloc(sizeof(ArcNode));
if(!y) exit(-1);
y=G.vertices[v].firstarc;
int u=G.vertices[v].data;
int w;
for(;y;y=y->nextarc){
if(visited[w]==0){
printf("%d--->%d\n",u,w);
DFSB(G,w);
}
}
}
void InitQueue (LinkQueue &Q){//Create an empty queue
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(-1);
Q.front->next=NULL;
}
void EnQueue (LinkQueue &Q,int e){//Enter the team
QNode *p;
p=(QNode*)malloc(sizeof(QNode));
if(!p) exit(-1);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
//free(p);
}
int DeQueue (LinkQueue &Q,int &e){//Team out
if(Q.front==Q.rear)
return -1;
QNode *p;
p=(QNode*)malloc(sizeof(QNode));
if(!p) exit(-1);
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
return e;
}
int QueueEmpty (LinkQueue Q){//Judge whether the queue is empty
if(Q.front==Q.rear)
return 1;
return 0;
}
void int main(){
int i;
ALGraph G;
CreateDG(G);
int x;
printf("Please enter the number of nodes:");
scanf("%d",&x);
for(int j=1;j<=x;j++){
printf("%d  ",G.vertices[j].data);
ArcNode *p;
p=(ArcNode*)malloc(sizeof(ArcNode));
if(!p) exit(-1);
p=G.vertices[j].firstarc;
while(p){
p=p->nextarc;
}
printf("\n");;
}
printf("Please enter the serial number of the first node to access:\n");
int n;
scanf("%d",&n);
for( i=0;i<30;i++)
visited[i]=0;
printf("Deep search:\n");
DFS(G,n);
for( i=0;i<30;i++)
visited[i]=0;
printf("\n");
printf("Edge set:\n");
DFSB(G,n);
system("pause");
return 0;
}
```

# Operation algorithm of dynamic lookup table

## Implementation content:

```[problem description] search, insert and delete the binary sort tree.
[basic requirements]
1. Define the nodes of binary sort tree.
2. Write a subroutine to search the binary sort tree recursively.
3. Insert elements into the binary sort tree. In this process, the previous search subroutine is used. If the same keyword is found, FALSE is returned. Otherwise, the element is inserted, and the result is still a binary sort tree, and TRUE is returned.
4. Using the algorithm of inserting elements, a binary sorting tree is generated.
5. Write a subroutine to delete the key elements in the binary sorting tree. e
[test data]
Create the binary sorting tree in Figure 9.7 (a) on page P227 of the textbook.
```

## Implementation ideas:

```1. Search idea: first find the root node. If the data of the root node is equal to the key value, return the root node and return TRUE. Otherwise, if the key value is greater than the root node, query its right subtree. If it is smaller than the root node, query its left subtree.
2. When there is no data element with key equal to key in binary sort tree T, insert key and return TRUE, otherwise return FALSE and insert recursively.
```

## Implementation code:

```#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0

/* Node structure definition of binary sort tree */
typedef struct BiTNode{
int data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
int SearchBST( BiTree T, int key, BiTree f, BiTree *p ){
/* Recursive search for the existence of key in binary sort tree T */
/* Pointer f points to the parent of T and its initial call value is NULL */
/* If the lookup succeeds, pointer p points to the data element node and returns TRUE */
/* Otherwise, pointer p points to the last node accessed on the lookup path and returns FALSE */
if( !T ){
*p = f;		//This is the only place where f is used.
return FALSE;
}else{
if( key == T->data ){
*p = T;
return TRUE;
}else if( key > T->data )
return SearchBST( T->rchild, key, T, p );		/* Continue searching in the right subtree */
else
return SearchBST( T->lchild, key, T, p );		/* Continue searching in the left subtree */
}
}
int InsertBST( BiTree *T, int key ){
/* When there is no data element with key equal to key in binary sort tree T */
/* Insert key and return TRUE, otherwise return FALSE */
/* Lookup function not called, recursively inserted */
if( !(*T) ){									/* The tree is empty. */
(*T) = (BiTree)malloc(sizeof(BiTNode));	/* Pay attention to this position, reallocate the space, * T is empty, indicating that no space has been allocated */
(*T)->data = key;
(*T)->lchild = (*T)->rchild = NULL;
return TRUE;
}
if( key == (*T)->data )
return FALSE;
if( key > (*T)->data )
return InsertBST( &((*T)->rchild), key );		/* Insert right child */
else
return InsertBST( &((*T)->lchild), key );		/* Insert left child */
}
void order(BiTree t){//Sequential output
if(t == NULL)
return ;
order(t->lchild);
printf("%d ", t->data);
order(t->rchild);
}
int DeleteBST(BiTree *T, int key){
/* If there is a data element with key equal to key in binary sort tree T, delete the data element node */
/* And return TRUE; otherwise return FALSE */
if( !(*T))
return FALSE;	/* There is no data element with key equal to key */
else{
if( key == (*T)->data )
Delete(T);
else if( key < (*T)->data)
return DeleteBST(&(*T)->lchild, key);
else
return DeleteBST(&(*T)->rchild, key);
}
}
int Delete(BiTree *p){
/* Remove node p from the binary sort tree and reconnect its left or right subtree */
BiTree q, s;
if(  !(*p)->lchild && !(*p)->rchild )	/* p Is a leaf node */
*p = NULL;
else if( !(*p)->lchild ){	/* Left subtree is empty, reconnect right subtree */
q = *p;
*p = (*p)->rchild;
free(q);
}
else if( !(*p)->rchild ){	/* Right subtree is empty, reconnect left subtree */
q = *p;
*p = (*p)->lchild;		/* Not quite understanding */
free(q);
}
else{						/* Left and right subtrees are not empty */
q = *p;
s = (*p)->lchild;
while(s->rchild){		/* Turn left, then right to the end*/
q = s;
s = s->rchild;
}
(*p)->data = s->data;
if( q != *p )				/* Determine whether to execute the above while loop */
q->rchild = s->lchild;	/* Execute the above while loop and reconnect the right subtree */
else
q->lchild = s->lchild;	/* The above while loop is not executed, and the left subtree is reconnected */
free(s);
}
return TRUE;
}
void main(){
int i,result,SearchNumber,DeletNumber;
int a[10] = {3,12,24,37,45,53,61,78,90,100};
BiTree T = NULL;
BiTree f, *p;
for( i = 0; i < 10; i++ )
InsertBST(&T, a[i]);
printf("Middle order traversal binary sort tree:\n");
order(T);
printf("\n");
printf("Enter the number you want to find:\tresult = 1 To find out, result = 0 I can't find it.\n");
scanf("%d",&SearchNumber);
result = SearchBST(T,SearchNumber,f, &p);
printf("result = %d\n",result);
printf("Please enter the number to delete:\n");
scanf("%d",&DeletNumber);
printf("The binary sort tree traversed by the deleted middle order is:\n");
DeleteBST(&T,DeletNumber);
order(T);
printf("\n");
}
```
Published 1 original article, praised 0 and visited 3

Posted by ericburnard on Tue, 11 Feb 2020 23:51:45 -0800