二叉排序树基本操作的实现:建立二叉排序树,插入删除数据(C语言)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct BTree{
	int date;
	BTree *lchild;
	BTree *rchild;
}BTree;
//插入数据 
int BSTInsert(BTree *&p,int key){
	
	if(p==NULL){
		p=(BTree*)malloc(sizeof(BTree));
		p->date=key;
		p->lchild=NULL;
		p->rchild=NULL;
		return 1;
	}else {
		if(key==p->date) return 0;
		else if(key<p->date){
			return BSTInsert(p->lchild,key);
		}else if(key>p->date){
		    return BSTInsert(p->rchild,key); 
		} 
	}
} 

int BSTDelete(BTree *&p,int key){
	BTree *x,*y;
	x=p;
	y=NULL;
	int flag=-1;
	while(x->date!=key){
		if(key>x->date){
			y=x;//记录父节点
			x=x->rchild; 
			flag=1;
		}else{
			y=x;
			x=x->lchild;
			flag=0;
		}	
	}
	if(x->date!=key){//如果未找到则返回两个空指针 
		x=NULL;
		y=NULL; 
		printf("不存在此元素");
		return 0;
	}
	if(x!=NULL){
		if(x->lchild!=NULL&&x->rchild!=NULL){
	     	BTree *t=x;
			y=x;
			x=x->lchild;//向左走一步
			while(x->rchild!=NULL){
				y=x;
				x=x->rchild;//向右走到头 
			} 
			t->date=x->date;
			if(x->lchild!=NULL) y->rchild=x->lchild;
			else y->rchild=NULL;
			free(x); 
			return 1;	
		}else if(x->rchild!=NULL&&x->lchild==NULL){
		       if(y->lchild=x) y->lchild=x->rchild;	
		       else if(y->rchild=x) y->rchild=x->rchild;
		       free(x);
		       return 1;
		}else if(x->lchild!=NULL&&x->rchild==NULL){
		    if(y->lchild=x) y->lchild=x->lchild;	
		    else if(y->rchild=x) y->rchild=x->lchild;
		    free(x);
		    return 1;
		}
		else if(x->lchild==NULL&&x->rchild==NULL){
			if(y->lchild=x) y->lchild=NULL;	
		    else if(y->rchild=x) y->rchild=NULL;
		    free(x);
		    return 1;
		}
	}
} 
void createBTree(BTree *&p,int t[],int length){
	for(int i=0;i<length;i++){
		int j=BSTInsert(p,t[i]);
		//printf("%d  ",j);
	}
}

int main(){
	int temp[]={5,1,8,0,3,7,9,2,4};
	BTree *p=NULL;
	createBTree(p,temp,9);
	printf("%d  ",p->date);
	printf("%d  ",p->lchild->date);
	printf("%d  ",p->rchild->date);
	int r=BSTDelete(p,5);//删除5 
	printf("\n结果:%d",r);
	printf("\n%d  ",p->date);
	r=BSTDelete(p,1);//删除5 
	printf("\n结果:%d",r);
	printf("\n%d  ",p->lchild->date);
}

代码运行结果:

  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你回答这个问题。下面是一个基于C语言二叉排序树的创建函数CreateBSTree实现: ```c #include <stdio.h> #include <stdlib.h> typedef struct BSTreeNode { int data; struct BSTreeNode* left; struct BSTreeNode* right; } BSTreeNode; // 在二叉排序树插入一个节点 void insert(BSTreeNode** root, int data) { if (*root == NULL) { *root = (BSTreeNode*)malloc(sizeof(BSTreeNode)); (*root)->data = data; (*root)->left = NULL; (*root)->right = NULL; } else if (data < (*root)->data) { insert(&((*root)->left), data); } else if (data > (*root)->data) { insert(&((*root)->right), data); } } // 建立二叉排序树 BSTreeNode* CreateBSTree(int* arr, int n) { BSTreeNode* root = NULL; int i; for (i = 0; i < n; i++) { insert(&root, arr[i]); } return root; } // 打印二叉排序树 void printBSTree(BSTreeNode* root) { if (root != NULL) { printf("%d ", root->data); printBSTree(root->left); printBSTree(root->right); } } int main() { int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; int n = sizeof(arr) / sizeof(arr[0]); BSTreeNode* root = CreateBSTree(arr, n); printBSTree(root); return 0; } ``` 在这个实现中,我们定义了一个`BSTreeNode`结构体,它包含了一个`int`类型的数据和两个指向左右子节点的指针。我们还定义了一个`insert`函数,它用于向二叉排序树插入一个节点。在`CreateBSTree`函数中,我们遍历输入的数组,然后对于每个元素调用`insert`函数来插入二叉排序树中。最后,我们返回指向根节点的指针。 在这个例子中,我们输入了一个整数数组,但你可以根据需要更改输入方式。同时,该代码还包括一个`printBSTree`函数用于输出二叉排序树中的节点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值