二叉查找树的实现——C++

二叉查找树是比较基础的数据结构,可实现排序、二分查找等操作。

但是由于生成方式的局限性(不控制平衡),当先后插入的关键字有序时,便会形成单支,严重影响效率和性能。(后续要实现的目标当然就是自平衡树了,比如AVL、红黑树)

话不多说,直接贴源码。

 

头文件(BinarySearchTree.h):

/***********************
 * BinarySearchTree.h
 **********************/

#pragma once

//节点定义.
typedef struct _BinaryTreeNode
{
	int data;
	struct _BinaryTreeNode *leftChild, *rightChild;
}BinaryTreeNode;


//二叉查找树类.
class BinarySearchTree
{
private:
	BinaryTreeNode* m_pRootNode;	//根节点

public:
	BinarySearchTree();
	~BinarySearchTree();

	//添加节点.
	bool AddNode(int data = 0);

	//前序遍历.
	void PreOrderTraverse();
	//中序遍历.
	void InOrderTraverse();
	//后序遍历.
	void PostOrderTraverse();		

private:

	//插入节点,递归.
	void InsertNode(BinaryTreeNode* toNode, BinaryTreeNode* srcNode);
	
	//递归实现前序遍历.
	void PreOrder(BinaryTreeNode* node);
	//递归实现中序遍历.
	void InOrder(BinaryTreeNode* node);
	//递归实现后序遍历.
	void PostOrder(BinaryTreeNode* node);

	//打印节点.
	void PrintNode(BinaryTreeNode* node);
	//创建新节点.
	BinaryTreeNode* CreateNewNode(int data);

	//删除所有节点,在析构函数中调用.
	void DeleteAllNodes();
	//递归删除节点.
	void DeleteNode(BinaryTreeNode* node);
};


 

实现文件(BinarySearchTree.cpp):

/**
 * BinarySearchTree.cpp
 */

#include "BinarySearchTree.h"
#include <stdio.h>

BinarySearchTree::BinarySearchTree()
	:m_pRootNode(NULL)
{
}

BinarySearchTree::~BinarySearchTree()
{
	this->DeleteAllNodes();
}


bool BinarySearchTree::AddNode(int data/* = 0*/)
{
	BinaryTreeNode* newNode = this->CreateNewNode(data);
	if(!newNode)
		return false;

	if(!m_pRootNode)
	{
		m_pRootNode = newNode;
		return true;
	}

	this->InsertNode(m_pRootNode, newNode);
	return true;
}
void BinarySearchTree::InsertNode(BinaryTreeNode* toNode, BinaryTreeNode* srcNode)
{
	if(!toNode || !srcNode)
		return;

	if(srcNode->data <= toNode->data)
	{
		if(!toNode->leftChild)
		{
			toNode->leftChild = srcNode;
		}
		else
		{
			this->InsertNode(toNode->leftChild, srcNode);
		}
	}
	else
	{
		if(!toNode->rightChild)
		{
			toNode->rightChild = srcNode;
		}
		else
		{
			this->InsertNode(toNode->rightChild, srcNode);
		}
	}
}


void BinarySearchTree::PreOrderTraverse()
{
	this->PreOrder(m_pRootNode);
}
void BinarySearchTree::PreOrder(BinaryTreeNode* node)
{
	if(!node)
	{
		return;
	}

	this->PreOrder(node->leftChild);
	this->PrintNode(node);
	this->PreOrder(node->rightChild);
}


void BinarySearchTree::InOrderTraverse()
{
	this->InOrder(m_pRootNode);
}
void BinarySearchTree::InOrder(BinaryTreeNode* node)
{
	if(!node)
		return;

	this->PrintNode(node);
	this->InOrder(node->leftChild);
	this->InOrder(node->rightChild);
}


void BinarySearchTree::PostOrderTraverse()
{
	this->PostOrder(m_pRootNode);
}
void BinarySearchTree::PostOrder(BinaryTreeNode* node)
{
	if(!node)
		return;

	this->PostOrder(node->rightChild);
	this->PrintNode(node);
	this->PostOrder(node->leftChild);
}


BinaryTreeNode* BinarySearchTree::CreateNewNode(int data/* = 0*/)
{
	BinaryTreeNode* newNode = new BinaryTreeNode();

	if(newNode)
		newNode->data = data;

	return newNode;
}


void BinarySearchTree::PrintNode(BinaryTreeNode* node)
{
	if(!node)
		return;

	printf("%d ", node->data);
}

void BinarySearchTree::DeleteAllNodes()
{
	this->DeleteNode(m_pRootNode);
}
void BinarySearchTree::DeleteNode(BinaryTreeNode* node)
{
	if(!node)
		return;

	//自底向上.
	this->DeleteNode(node->leftChild);
	this->DeleteNode(node->rightChild);
	delete node;
}


 

测试文件(TestBST.cpp)

/**
 * TestBST.cpp
 */
#include "BinarySearchTree.h"

#include <iostream>
#include <time.h>

using namespace std;

bool testBST(int nodeNum)
{
	BinarySearchTree* pBST = new BinarySearchTree();

	//以当前时间为随机种子.
	srand((unsigned int)time(0));

	//添加指定个数的元素.
	cout<<"Insert: "<<endl;
	int tmpData;
	for(int i =0; i<nodeNum; ++i)
	{
		tmpData = rand();
		cout<<tmpData<<' ';

		if(!pBST->AddNode(tmpData))
		{
			return false;
		}
	}

	//遍历.
	cout<<endl<<endl<<"PreOrder: "<<endl;
	pBST->PreOrderTraverse();
	cout<<endl<<endl<<"InOrder:"<<endl;
	pBST->InOrderTraverse();
	cout<<endl<<endl<<"PostOrder:"<<endl;
	pBST->PostOrderTraverse();

	//清理.
	delete pBST;
	return true;
}

int main()
{
	if(!testBST(10))
	{
		//cout<<endl<<"测试失败."<<endl;
	}

	return 0;
}


 

运行结果:

 

=============================End===================================

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值