二叉查找树的实现

二叉查找树的实现

1、头文件

(二叉查找树.h)

#pragma once
/*
* 二叉查找树ADT代码实现
*/

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

struct Tree* makeEmpty(struct Tree* t);

struct Tree* find(int x, struct Tree* t);
struct Tree* findMin(struct Tree* t);
struct Tree* findMax(struct Tree* t);

struct Tree* insert(int x, struct Tree* t);
struct Tree* myDelete(int x, struct Tree* t);

void printTree(struct Tree* t);

struct Tree
{
	int element;
	struct Tree* left;
	struct Tree* right;
};

2、main函数

(main.c)

#include "二叉查找树.h"

int main()
{
	struct Tree* min;
	struct Tree* max;
	struct Tree* node;
	struct Tree* root;
	root = insert(6, NULL);
	insert(4, root);
	insert(3, root);
	insert(2, root);
	insert(1, root);
	insert(8, root);
	printTree(root);

	//查找关键字为4的节点
	node = find(4, root);
	if (NULL == node) {
		printf("not find!\n");
	}
	else {
		printf("\nfind node is:%d\n", node->element);
	}

	//查找key最小的节点
	min = findMin(root);
	printf("min is:%d\n", min->element);

	//查找key最大的节点
	max = findMax(root);
	printf("max is:%d\n", max->element);

	//删除节点
	myDelete(2, root);
	printTree(root);

	//清空树
	makeEmpty(root);

	return 0;
}

3、函数实现

3.1 makeEmpty.c

#include "二叉查找树.h"

struct Tree* makeEmpty(struct Tree* t)
{
	if (t != NULL) {
		makeEmpty(t->left);
		makeEmpty(t->right);
		free(t);
	}
	return NULL;
}

3.2 find.c

#include "二叉查找树.h"

struct Tree* find(int x, struct Tree* t)
{
	if (NULL == t) {
		return NULL;
	}
	if (x > t->element) {
		return find(x, t->right);
	}
	else if (x < t->element) {
		return find(x, t->left);
	}
	return t;
}

3.3 findMin.c

#include "二叉查找树.h"

struct Tree* findMin(struct Tree* t)
{
	if (NULL == t) {
		return NULL;
	}
	if (t->left != NULL) {
		return findMin(t->left);
	}
	return t;
}

3.4 findMax.c

#include "二叉查找树.h"

struct Tree* findMax(struct Tree* t)
{
	if (NULL == t) {
		return NULL;
	}
	if (NULL != t->right)
	{
		return findMax(t->right);
	}
	return t;
}

3.5 insert.c

#include "二叉查找树.h"

struct Tree* insert(int x, struct Tree* t)
{
	if (NULL == t) {
		t = malloc(sizeof(struct Tree));
		if (NULL == t) {
			printf("内存空间不足,插入失败\n");
			return NULL;
		}
		t->element = x;
		t->left = NULL;
		t->right = NULL;
	}
	else if (x > t->element) {
		t->right = insert(x, t->right);
	}
	else if (x < t->element) {
		t->left = insert(x, t->left);
	}
	return t;
}

3.6 myDelete.c

#include "二叉查找树.h"

struct Tree* myDelete(int x, struct Tree* t)
{
	struct Tree* temp;
	if (NULL == t) {
		printf("Tree is empty!\n");
	}
	else if (x > t->element) {
		t->right = myDelete(x, t->right);
	}
	else if (x < t->element) {
		t->left = myDelete(x, t->left);
	}
	else if (t->left && t->right) {
		temp = findMin(t->right);
		t->element = temp->element;
		t->right = myDelete(temp->element, t->right);
	}
	else {
		temp = t;
		if (t->left == NULL) {
			t = t->right;
		}
		else if (t->right == NULL) {
			t = t->left;
		}
		free(temp);
	}
	return t;
}

3.7 printTree.c

#include "二叉查找树.h"

//中序遍历打印树中的KEY
void printTree(struct Tree* t)
{
	if (NULL == t) {
		printf("Empty tree!\n");
		return;
	}
	//打印左子树
	if (t->left != NULL) {
		printTree(t->left);
	}
	//打印节点
	printf("%d ", t->element);
	//打印右子树
	if (t->right != NULL) {
		printTree(t->right);
	}
}

4、写在最后

为方便大家学习,我已经将原码上传到了GitHub,欢迎大家下载。链接:link(文件名:二叉查找树)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值