二叉排序树

         文章转载自:http://student.zjzk.cn/course_ware/data_structure/web/chazhao/chazhao9.3.1.1.htm

1. 二叉排序树的定义

        二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(BinarySearch Tree)。其定义为:

二叉排序树或者是空树,或者是满足下列性质的二叉树:

1. 若它的左子树非空,则左子树上所有节点的值均小于根节点的值

2. 若它的右子树非空,则右子树上所有节点的值均大于根节点的值

3. 左右子树本身又各是一棵二叉排序树       

2. 二叉排序树的插入

1.如果树为空,则申请新节点,令其为根

2.若不为空,则比较关键字key

        (1)二者相等,说明树中已有此关键字,无须插入

        (2)key < root->key,则插入到root的左子树中

        (3)key > root->key,则插入到root的右子树中

直到将 key 作为一个新的叶子结点插入到二叉排序树中,或者直到发现数中已有此关键字为止。

3. 二叉排序树的删除

1. 要删除的节点 p 为叶子,则直接删除

2. 要删除的节点 p 只有一个孩子节点 q,把 q 与 p 的父节点相连,然后删除 p 即可

3. 要删除的节点 p 有两个孩子,需要找到 p 的左子树的最右子树(或者 p 的右子树的最左子树) q,把 q 的 key 赋给 p,然后把 q 的左子树(或者右子树)与 q 的父节点相连,然后删除 q 即可

4. 简单代码测试

binary_sort_tree.h

#ifndef __binary_sort_tree_h__
#define __binary_sort_tree_h__

#ifndef	DLLEXPORT
#define	DLLAPI		__declspec(dllimport)
#else
#define	DLLAPI		__declspec(dllexport)
#endif

//============================================================================//

typedef int		Key;

typedef struct _bst_node {
	Key					_key;
	struct _bst_node	*_left;
	struct _bst_node	*_right;
} BSTNODE, *PBSTNODE;

typedef void	(*visit_func)( PBSTNODE pnode );

//============================================================================//

#ifdef	__cplusplus
extern "C" {
#endif

int		DLLAPI	bst_insert		( PBSTNODE head, Key key );
int		DLLAPI	bst_delete		( PBSTNODE head, Key key );
void	DLLAPI	bst_destroy		( PBSTNODE head, visit_func visit );

void	DLLAPI	bst_pre_order	( PBSTNODE head, visit_func visit );
void	DLLAPI	bst_in_order	( PBSTNODE head, visit_func visit );
void	DLLAPI	bst_post_order	( PBSTNODE head, visit_func visit );

#ifdef	__cplusplus
}
#endif

#endif // __binary_sort_tree_h__


binary_sort_tree.c

#define DLLEXPORT

#include "binary_sort_tree.h"
#include <stdio.h>
#include <malloc.h>

//============================================================================//

PBSTNODE bst_find( PBSTNODE head, Key key )
{
	if( NULL == head )
		return NULL;

	if( key == head->_key )
		return head;
	else if( key < head->_key )
		return bst_find( head->_left, key );
	else
		return bst_find( head->_right, key );
}

//============================================================================//

int DLLAPI bst_insert( PBSTNODE head, Key key )
{
	PBSTNODE	newnode	= NULL;

	if( NULL == head || key == head->_key )
		return 0;

	newnode = (PBSTNODE)malloc( sizeof(BSTNODE) );
	if( NULL == newnode )
		return 0;
	newnode->_key	= key;
	newnode->_left	= NULL;
	newnode->_right = NULL;

	if( key < head->_key ) {
		if( NULL == head->_left )
			head->_left = newnode;
		else
			return bst_insert( head->_left, key );
	}
	else {
		if( NULL == head->_right )
			head->_right = newnode;
		else
			return bst_insert( head->_right, key );
	}

	return 1;
}

int DLLAPI bst_delete( PBSTNODE head, Key key )
{
	PBSTNODE	p	= NULL;
	PBSTNODE	q	= NULL;
	PBSTNODE	t	= NULL;

	if( NULL == head )
		return 0;

	p = bst_find( head, key );
	if( NULL == p )
		return 0;

	if( NULL == p->_left && NULL == p->_right ) {
		free( p );
		p = NULL;
	}
	else if( NULL == p->_left || NULL == p->_right ) {
		if( NULL == p->_left )
			q = p->_right;
		else
			q = p->_left;
		free( p );
		p = q;
	}
	else {
		q = p->_right;
		while( NULL != q->_left )
			q = q->_left;
		p->_key = q->_key;
		if( NULL != q->_right ) {
			t = q->_right;
			free( q );
			q = t;
		}
	}

	return 1;
}

void DLLAPI bst_destroy( PBSTNODE head, visit_func visit )
{
	if( NULL == head )
		return;
	else {
		bst_destroy( head->_left, visit );
		bst_destroy( head->_right, visit );
	}
	if( NULL != visit )
		visit( head );
	free( head );
	head = NULL;
}

//============================================================================//

void DLLAPI bst_pre_order( PBSTNODE head, visit_func visit )
{
	if( NULL == head )
		return;

	if( NULL != visit )
		visit( head );
	bst_pre_order( head->_left, visit );
	bst_pre_order( head->_right, visit );
}

void DLLAPI bst_in_order( PBSTNODE head, visit_func visit )
{
	if( NULL == head )
		return;

	bst_in_order( head->_left, visit );
	if( NULL != visit )
		visit( head );
	bst_in_order( head->_right, visit );
}

void DLLAPI bst_post_order( PBSTNODE head, visit_func visit )
{
	if( NULL == head )
		return;

	bst_post_order( head->_left, visit );
	bst_post_order( head->_right, visit );
	if( NULL != visit )
		visit( head );
}


binary_sort_tree_test.cpp

#include <stdio.h>
#include <malloc.h>
#include "../binary_sort_tree/binary_sort_tree.h"
#pragma comment( lib, "../bin/binary_sort_tree.lib" )

void visit( PBSTNODE node )
{
	if( NULL == node )
		return;
	printf( "%d ", node->_key );
}

int main( void )
{
	PBSTNODE head = (PBSTNODE)malloc( sizeof(BSTNODE) );
	if( NULL == head )
		return 1;
	head->_key		= 34;
	head->_left		= NULL;
	head->_right	= NULL;

	bst_insert( head, 22 );
	bst_insert( head, 23 );
	bst_insert( head, 91 );
	bst_insert( head, 5 );
	bst_insert( head, 77 );
	bst_insert( head, 89 );

	bst_pre_order( head, visit );
	printf( "\r\n" );
	bst_in_order( head, visit );
	printf( "\r\n" );
	bst_post_order( head, visit );
	printf( "\r\n" );

	bst_destroy( head, visit );
	printf( "\r\n" );

	return 0;
}


测试结果:

34 22 5 23 91 77 89
5 22 23 34 77 89 91
5 23 22 89 77 91 34
5 23 22 89 77 91 34

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值