二叉排序树

//BSTSearch.h

//Binary Search Tree

#ifndef BSTSEARCH_H
#define BSTSEARCH_H

#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
#define ERROR 2
#define NOT_FOUND 3

#pragma pack( push )
#pragma pack( 4 )

struct Node
{
	int iValue;
	struct Node* pLeft;
	struct Node* pRight;
	struct Node* pParent;
};
typedef struct Node Node;

typedef struct
{
	struct Node* pRoot;
}Header;

#pragma pack( pop )

Node* BSTSearch( Node* pNode, int iValue );
int BSTDel( Header* pHeader, int iValue );
Node* CreateNode( int iValue );
Header* CreateBSTTree();
void PrintNode( Node* pNode );

#endif


//BSTSearch.cpp

#include "BSTSearch.h"

Node* BSTSearch( Node* pNode, int iValue )
{
	if( !pNode )
		return NULL;

	if( pNode->iValue == iValue )
		return pNode;
	else if( iValue < pNode->iValue )
	{
		BSTSearch( pNode->pLeft, iValue );
	}
	else
	{
		BSTSearch( pNode->pRight, iValue );
	}

}

bool BSTInsert( Header* pHeader, int iValue )
{
	if( !pHeader || !pHeader->pRoot )
		return false;

	if( BSTSearch( pHeader->pRoot, iValue ) )
	{//已找到无需再安装
		return false;
	}

	Node* pCur = pHeader->pRoot;
	Node* pPrior = NULL;

	while( pCur )
	{
		pPrior = pCur;

		if( pCur->iValue > iValue )
		{
			pCur = pCur->pLeft;
		}
		else
		{
			pCur = pCur->pRight;
		}
	}

	if( pPrior->iValue > iValue )
	{
		pPrior->pLeft = CreateNode( iValue );
		pPrior->pLeft->pParent = pPrior;
	}
	else
	{
		pPrior->pRight = CreateNode( iValue );
		pPrior->pRight->pParent = pPrior;
	}

	return true;
}

int BSTDel( Header* pHeader, int iValue )
{
	if( !pHeader )//error
		return ERROR;

	Node* pNode = BSTSearch( pHeader->pRoot, iValue );

	if( !pNode )
	{//未找到
		return NOT_FOUND; 
	}

	Node* pParent = NULL;

	if( !pNode->pLeft && !pNode->pRight )
	{//叶子结点
		pParent = pNode->pParent;

		if( !pParent )
		{//只有一个结点的树
			free( pNode );
			pHeader->pRoot = NULL;

			return true;
		}

		if( pParent->pLeft == pNode )
		{
			pParent->pLeft = NULL;
		}
		else
		{
			pParent->pRight = NULL;
		}

		free( pNode );
	}
	else if( (!pNode->pLeft && pNode->pRight) || (pNode->pLeft && !pNode->pRight) )
	{//只有左或右子树
		if( pNode->pLeft )
		{
			pParent = pNode->pParent;

			if( !pParent )
			{
				pHeader->pRoot = pNode->pLeft;
				pNode->pLeft->pParent = NULL;

				free( pNode );

				return true;
			}

			if( pParent->pLeft == pNode )
			{
				pParent->pLeft = pNode->pLeft;
				pNode->pParent = pParent->pLeft;
			}
			else
			{
				pParent->pRight = pNode->pLeft;
				pNode->pParent = pParent->pRight;
			}

		}
		else
		{//右子树
			pParent = pNode->pParent;

			if( !pParent )
			{
				pHeader->pRoot = pNode->pRight;
				pNode->pRight->pParent = NULL;

				free( pNode );

				return true;
			}

			if( pParent->pLeft == pNode )
			{
				pParent->pLeft = pNode->pRight;
				pNode->pParent = pParent->pLeft;
			}
			else
			{
				pParent->pRight = pNode->pRight;
				pNode->pParent =pParent->pRight;
			}
		}

		free( pNode );
	}
	else if( pNode->pLeft && pNode->pRight )
	{//左右子树全存在则把左子树作为其父结点的子结点把其右结点作为其左子树的最右下角子嗣结点
		Node* pLeft = pNode->pLeft;
		Node* pRight = pNode->pRight;

		pParent = pNode->pParent;

		if( !pParent )
		{
			pHeader->pRoot = pNode->pLeft;
		}

		Node* pCur = pNode->pLeft;
		Node* pPrior = pCur;
		while( pCur )
		{
			pPrior = pCur;
			pCur = pCur->pRight;
		}

		if( pPrior )
		{
			pPrior->pRight = pRight;
			pRight->pParent = pPrior;
		}
		else
			return false;

	}

	return true;
}

Node* CreateNode( int iValue )
{
	Node* pNode = (Node*)malloc( sizeof( Node ));

	if( !pNode )
		return NULL;

	pNode->iValue = iValue;
	pNode->pLeft = NULL;
	pNode->pRight = NULL;
	pNode->pParent = NULL;

	return pNode;
}

Header* CreateBSTTree()
{
	Header* pHeader = (Header*)malloc( sizeof( Header  ) );

	if( !pHeader )
		return NULL;

	pHeader->pRoot = CreateNode(  3 );

	Node* pCur = pHeader->pRoot;
	pCur->pLeft = CreateNode( 1 );
	pCur->pLeft->pParent = pCur;

	pCur->pLeft->pLeft = CreateNode( 0 );
	pCur->pLeft->pLeft->pParent = pCur->pLeft;

	pCur->pLeft->pRight = CreateNode( 2 );
	pCur->pLeft->pRight->pParent = pCur->pLeft;

	pCur->pRight = CreateNode( 4 );
	pCur->pRight->pParent = pCur;

	pCur->pRight->pRight = CreateNode( 5 );
	pCur->pRight->pRight->pParent = pCur->pRight;

	pCur->pRight->pRight->pRight = CreateNode( 6 );
	pCur->pRight->pRight->pRight->pParent = pCur->pRight->pRight;

	return pHeader;
}

void PrintNode( Node* pNode )
{
	if( pNode )
		printf( "%d\n", pNode->iValue );
}

int main( int argc, char* argv[] )
{
	Header* pHeader = CreateBSTTree();

	PrintNode( BSTSearch( pHeader->pRoot, 3 ));

	int iRes = BSTInsert( pHeader, 7 );

	PrintNode( BSTSearch( pHeader->pRoot, 7 ));

	if( true == BSTDel( pHeader, 3 ) )
	{
		puts( "true" );
		PrintNode( BSTSearch( pHeader->pRoot, 7  ));
	}
	
	PrintNode( BSTSearch( pHeader->pRoot, 3 ));
	PrintNode( BSTSearch( pHeader->pRoot, 4 ));
	PrintNode( BSTSearch( pHeader->pRoot, 5 ));


	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值