二叉查找树ADT的实现(C语言链表实现)

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

//链表类型声明
struct Tree
{
	int element;
	struct Tree *left;
	struct Tree* right;
};

//建立一棵空树(清空树)
struct Tree* makeEmpty(struct Tree* t)
{
	if (t != NULL)
	{
		makeEmpty(t->left);
		makeEmpty(t->right);
		free(t);
	}

	return NULL;
}

//查找树中某个结点
struct Tree* find(int x, struct Tree* t)
{
	if (t == NULL)
	{
		return NULL;
	}

	if (x < t->element)
	{
		return find(x, t->left);
	}
	else if (x > t->element)
	{
		return find(x, t->right);
	}
	else
	{
		return t;
	}
}

//查找树中的最小值(循环实现)
struct Tree* findMin(struct Tree* t)
{
	if (t != NULL)
	{
		while (t->left != NULL)
		{
			t = t->left;
		}

		return t;
	}

	//递归实现
	/*
	if (t == NULL)
	{
		return NULL;
	}
	else if (t->left == NULL)
	{
		return t;
	}
	else
	{
		return findMin(t->left);
	}
	*/
}

//查找树中的最大值(递归实现)
struct Tree* findMax(struct Tree* t)
{
	if (t == NULL)
	{
		return NULL;
	}
	else if (t->right == NULL)
	{
		return t;
	}
	else
	{
		return findMax(t->right);
	}

	//循环实现
	/*
	if (t != NULL)
	{
		while (t->right != NULL)
		{
			t = t->right;
		}
	}

	return t;
	*/
}

//插入某个元素
struct Tree* insert(int x, struct Tree* t)
{
	if (t == NULL)
	{
		t = (struct Tree*)malloc(sizeof(struct Tree));

		if (t == NULL)
		{
			printf("Out of space!\n");
			return NULL;
		}
		else
		{
			t->element = x;
			t->left = NULL;
			t->right = NULL;
		}
	}
	else if (x < t->element)
	{
		t->left = insert(x, t->left);
	}
	else if (x > t->element)
	{
		t->right = insert(x, t->right);
	}

	return t;
}

//删除某个元素
struct Tree* Delete(int x, struct Tree* t)
{
	struct Tree* tmp;

	if (t == NULL)
	{
		printf("Element not found!\n");
		return NULL;
	}
	else if (x < t->element) //左子树方向
	{
		t->left = Delete(x, t->left);
	}
	else if (x > t->element) //右子树方向
	{
		t->right = Delete(x, t->right);
	}
	else if (t->left != NULL && t->right != NULL) //两个儿子
	{
		tmp = findMin(t->right);
		t->element = tmp->element;
		t->right = Delete(t->element, t->right);
	}
	else //0个或一个儿子
	{
		tmp = t;
		if (t->left == NULL)
		{
			t = t->right;
		}
		else if (t->right == NULL)
		{
			t = t->left;
		}

		free(tmp);
	}

	return t;
}

//使用中序遍历打印
void printTree(struct Tree* t)
{
	if (t == NULL)
	{
		printf("Empty tree!\n");
		return;
	}
	
	//打印左子树
	if (t->left != NULL)
	{
		printTree(t->left);
	}

	//打印根结点
	printf("element is %d\n", t->element);

	//打印右子树
	if (t->right != NULL)
	{
		printTree(t->right);
	}
}


//主函数
int main(void)
{
	struct Tree* min;
	struct Tree* max;
	struct Tree* node;

	struct Tree* root;

	//插入某个元素
	root = insert(6, NULL);
	insert(4, root);
	insert(3, root);
	insert(7, root);
	insert(1, root);
	insert(8, root);
	insert(12, root);

	//使用中序遍历打印
	printTree(root);

	//查找树中某个结点
	node = find(8, root);
	printf("find node is:%d\n", node->element);

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

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

	//删除某个元素
	Delete(4, root);
	Delete(3, root);
	Delete(8, root);
	//使用中序遍历打印
	printTree(root);

	//建立一棵空树(清空树)
	makeEmpty(root);

	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
/* * 二节点ADT接口 */ package dsa; public interface BinTreePosition extends Position { //判断是否有父亲(为使代码描述简洁) public boolean hasParent(); //返回当前节点的父节点 public BinTreePosition getParent(); //设置当前节点的父节点 public void setParent(BinTreePosition p); //判断是否为叶子 public boolean isLeaf(); //判断是否为左孩子(为使代码描述简洁) public boolean isLChild(); //判断是否有左孩子(为使代码描述简洁) public boolean hasLChild(); //返回当前节点的左孩子 public BinTreePosition getLChild(); //设置当前节点的左孩子(注意:this.lChild和c.parent都不一定为空) public void setLChild(BinTreePosition c); //判断是否为右孩子(为使代码描述简洁) public boolean isRChild(); //判断是否有右孩子(为使代码描述简洁) public boolean hasRChild(); //返回当前节点的右孩子 public BinTreePosition getRChild(); //设置当前节点的右孩子(注意:this.rChild和c.parent都不一定为空) public void setRChild(BinTreePosition c); //返回当前节点后代元素的数目 public int getSize(); //在孩子发生变化后,更新当前节点及其祖先的规模 public void updateSize(); //返回当前节点的高度 public int getHeight(); //在孩子发生变化后,更新当前节点及其祖先的高度 public void updateHeight(); //返回当前节点的深度 public int getDepth(); //在父亲发生变化后,更新当前节点及其后代的深度 public void updateDepth(); //按照中序遍历的次序,找到当前节点的直接前驱 public BinTreePosition getPrev(); //按照中序遍历的次序,找到当前节点的直接后继 public BinTreePosition getSucc(); //断绝当前节点与其父亲的父子关系 //返回当前节点 public BinTreePosition secede(); //将节点c作为当前节点的左孩子 public BinTreePosition attachL(BinTreePosition c); //将节点c作为当前节点的右孩子 public BinTreePosition attachR(BinTreePosition c); //前序遍历 public Iterator elementsPreorder(); //中序遍历 public Iterator elementsInorder(); //后序遍历 public Iterator elementsPostorder(); //层次遍历 public Iterator elementsLevelorder(); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值