Establish a binary sort tree and delete a node on the binary sort tree

The algorithm is designed and verified as follows: the erasure function is represented by a binary list structure, and a binary sort tree is established according to the input key sequence, and a node on the binary sort tree is deleted.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int TElemType;
int m,n;
typedef struct BiTNode {//The establishment of tree node 
	TElemType data ;
    struct BiTNode 	*lchild,*rchild;
}BiTNode,*BiTree;
int SearchBST(BiTree T,int key,BiTree f,BiTree &p){
	if(!T){
		p=f;
		return 0;
		
	}else if(key==T->data){
		p=T;
		return 1;
	}else if(key<T->data)
	return SearchBST(T->lchild,key,T,p);
	else return SearchBST(T->rchild,key,T,p);
}
int InsertBST(BiTree &T,TElemType e){
	BiTree p,s;
	
	if(!SearchBST(T,e,NULL,p)){
		s=(BiTree)malloc(sizeof(BiTNode));
		s->data=e;
		s->lchild=s->rchild=NULL;
		if(!p)
		T=s;
		else if(e<p->data)
		p->lchild=s;
		else
		p->rchild=s;
		return 1;
		
		
	}else
	return 0; 
	
   	
   }
int CeateBiTree(BiTree &T)//Create tree 
{
    TElemType ch;
    T=NULL;
    printf("Please input integer sequence to build binary sort tree,End with 0:\n");
    scanf("%d",&ch);
    while(ch!=0){
    //	printf("please enter an integer sequence to build a binary sort tree, ending with 0: \ n");
    	InsertBST(T,ch);
    	printf("Please input integer sequence to build binary sort tree,End with 0:\n");
    	scanf("%d",&ch);
	} 
}
int Delete(BiTree &p){
	BiTree q,s;
	if(!p->rchild){
		q=p;
		p=p->lchild;
		free(q);
		}else if(!p->lchild){
			q=p;
			p=p->rchild;
			free(q);
		}else{
			q=p;
			s=p->lchild;
			while(s->rchild){
				q=s;
				s=s->rchild;
			}
			
			p->data=s->data;
			if(q!=p)
			q->rchild=s->lchild;
			else
			q->lchild=s->lchild;
			Delete(s);
			
		}
		return 1;
	
}
int deleBiTreeData(BiTree &T,TElemType key) {
	int flag=1;
	if(!T)
	return 0;
	else{
		if(key==T->data){
			flag=0;
			return Delete(T);
		}
		else if(key<T->data)
		return deleBiTreeData(T->lchild,key);
		else
		return deleBiTreeData(T->rchild,key);
	}
	if(flag){
		printf("The node value you want to delete does not exist:\n");
	}	
	
}

int  PreOrder(BiTree BT) {//Recursive preorder traversal 
	if(BT){
		if(!(BT->data))
		return 0;
		printf("%3d  ",BT->data);
		PreOrder(BT->lchild) ;
		PreOrder(BT->rchild);
		return 1;
	}
}
int main(){
   BiTree BT;
   TElemType ch;	
   CeateBiTree(BT);
   printf("To create a binary sort tree, first order traversal:\n");
    PreOrder(BT);
   printf("Please enter the node value you want to delete:\n");
   scanf("%d",&ch) ;
  deleBiTreeData(BT, ch);
  printf("First order traversal of binary sort tree after node deletion:\n");
	 PreOrder(BT);
	
	
}

The test results are as follows:

Enter an integer sequence to build a binary sort tree, ending with 0:
45
 Enter an integer sequence to build a binary sort tree, ending with 0:
24
 Enter an integer sequence to build a binary sort tree, ending with 0:
53
 Enter an integer sequence to build a binary sort tree, ending with 0:
12
 Enter an integer sequence to build a binary sort tree, ending with 0:
37
 Enter an integer sequence to build a binary sort tree, ending with 0:
93
 Enter an integer sequence to build a binary sort tree, ending with 0:
0
 To create a binary sort tree, first order traversal:
 45 24 12 37 53 93 please enter the node value you want to delete:
24
 First order traversal of binary sort tree after node deletion:
 45   12   37   53   93

 

Posted by scorpioy on Fri, 25 Oct 2019 08:36:14 -0700