二叉查找树

/* binary search tree
 * 1. Any node in the left subtree is less than the root;
 * 2. Any node in the right subtree is greater than the root; 
 * 3. if we traverse a binary search tree in inorder, we get a sorted list.
 */
#include<stdio.h>
#include<stdlib.h>

typedef struct tnode_t
{
	int _data;
	struct tnode_t *_lchild;
	struct tnode_t *_rchild;
}tnode_t;

/* create a binary search tree */
tnode_t *insertBinaryTree(tnode_t *node, int value)
{
	tnode_t *s, *temp1, *temp2;
	
	s = (tnode_t *)malloc(sizeof(tnode_t));
	if(s == NULL) 
	{
		printf("error: malloc\n");
		exit(0);
	}
	s->_data = value;
	s->_lchild = s->_rchild = NULL;
	/* empty tree */
	if(node == NULL)
	{
		node = s;
		return node;
	}
	/* not empty, need to find the place to insert */
	temp1 = node;
	while(temp1 != NULL)
	{
		temp2 = temp1;
		if(value < temp1->_data)
			temp1 = temp1->_lchild;
		else
			temp1 = temp1->_rchild;
	}
	if(value < temp2->_data)
		temp2->_lchild = s;
	else
		temp2->_rchild = s;
	return node;
}

/* recursive */
void inorder(tnode_t *node)
{
	if(node != NULL)
	{
		inorder(node->_lchild);
		printf("%d ", node->_data);
		inorder(node->_rchild);
	}
}

/* non-recursive: left -> root -> right */
void nonRecursive_inorder(tnode_t *node)
{
	tnode_t *stack[100];
	int top;
	
	top = 0;
	if(node != NULL)
	{
		stack[top++] = node;
		node = node->_lchild;
		while(top > 0)
		{
			while(node != NULL)
			{
				stack[top++] = node;
				node = node->_lchild;
			}
			node = stack[--top];
			printf("%d ", node->_data);
			node = node ->_rchild;
			if(node != NULL)
			{
				stack[top++] = node;
				node = node->_rchild;
			}
		}
		
	}
}

/* swap the left and right subtrees of a given binary tree, use recursive algorithm */
tnode_t *swapTree(tnode_t *node)
{
	tnode_t *temp1 = NULL;
	tnode_t *temp2 = NULL;
	if(node != NULL)
	{
		temp1 = swapTree(node->_lchild);
		temp2 = swapTree(node->_rchild);
		node->_rchild = temp1;
		node->_lchild = temp2;
	}
	return node;
}

/* search the key in the given binary search tree
 * if found, return the key pointer, else return NULL
 */
tnode_t *searchTree(tnode_t *node, int key)
{
	while(node != NULL)
	{
		if(node->_data == key)
		{
			printf("find!\n");
			return node;
		}
		if(key < node->_data)
			node = node->_lchild;
		else
			node = node->_rchild;
	}
	printf("not found!\n");
	return NULL;
}

/* ---------------------delete a node from the binary tree------------------------ */
/* get a pointer to the node (node) whose data value is given as well as the pointer to its root (node_root)*/
tnode_t getptr(tnode_t *node, int key, tnode_t **node_root)
{
	tnode_t *temp;
	if(node == NULL)
		return NULL;
	temp = node;
	*node_root = NULL;
	while(temp != NULL)
	{
		if(temp->_data == key)
			return temp;
		else
		{
			*node_root = temp;
			if(temp->_data > key)
				temp = temp->_lchild;
			else
				temp = temp->_rchild;
		}
	}
	return NULL;
}

/*delete a node*/
tnode_t *delBinaryTree(tnode_t *node, int value)
{
	tnode_t *x, *y, *temp;//x is node to be deleted
	x = getptr(node, value, &y);//x是待删除的节点,y是x的双亲
	if(x == NULL)
	{
		printf("Node does not exist\n");
		return node;
	}
	/*this code is for deleting root node*/
	if(x == node)
	{
		temp = x->_lchild;
		y = x->_rchild;
		node = temp;
		while(temp->_rchild != NULL)
			temp = temp->_rchild;
		temp->_rchild = y;
		free(x);
		return node;
	}
	/*this code is for deleting node having both children*/
	if(x->_lchild != NULL && x->_rchild != NULL)
	{
		if(y->child == x)
		{
			temp = x->_lchild;
			y->_lchild = x->_lchild;
			while(temp->_rchild != NULL)
				temp = temp->_rchild;
			temp->_rchild = x->_rchild;
			x->_lchild = NULL;
			x->_rchild = NULL;
		}
		else
		{
			temp = x-<_rchild;
			y->_rchild = x->_rchild;
			while(temp->_lchild != NULL)
				temp = temp->_lchild;
			temp->_lchild = x->_lchild;
			x->_lchild = NULL;
			x->_rchild = NULL;
		}
		free(x);
		return node;
	}
	/*this code is for deleting a node with one child*/
	if(x->_lchild == NULL && x->_rchild != NULL)
	{
		if(y->_lchild == x)
			y->_lchild = x->_rchild;
		else
			y->rchild = x->_rchild;
		x->_rchild = NULL;
		free(x);
		return node;
	}
	if(x->_lchild != NULL && x->_rchild == NULL)
	{
		if(y->_lchild == x)
			y->_lchild = x->_lchild;
		else
			y->_rchild = x->_lchild
		x->_lchild = NULL;
		free(x);
		return node;
	}
	/*this code is for deleting a node with no child*/
	if(x->_lchild == NULL && x->_rchild == NULL)
	{
		if(y->_lchild == x)
			y->_lchild = NULL;
		else
			y->_rchild = NULL;
		free(x);
		return node;
	}
}


main(void)
{
	tnode_t *root = NULL;
	int n, x;
	printf("Enter number of nodes: ");
	scanf("%d", &n);
	printf("Enter data: ");
	while(n--)
	{
		scanf("%d", &x);
		root = insertBinaryTree(root, x);
	}
	/* test recursive inorder traverse*/
	printf("Inorder list: ");
	inorder(root);
	putchar('\n');
	
	/* test non-recursive inorder traverse*/
	//printf("Non-recursive inorder list: ");
	//nonRecursive_inorder(root);
	//putchar('\n');
	
	/* test swap operations */
	root = swapTree(root);
	printf("Swapped inorder list: ");
	inorder(root);
	putchar('\n');
	printf("Enter number to find: ");
	scanf("%d", &x);
	searchTree(root, x);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值