B树(二叉搜索树)实现

头文件:

#pragma once
#ifndef BST_H
#define BST_H

namespace CPP {
	template<typename K, typename V>
	class TreeNode
	{
	public:
		K key;
		V val;
		TreeNode* left;
		TreeNode* right;
		TreeNode(K k, V v, TreeNode* left = nullptr, TreeNode* right = nullptr) :
			key(k), val(v), left(left), right(right)
		{}
	};
	//B树,二叉搜索树
	template<typename K, typename V>
	class BST
	{
		using Node = TreeNode<K, V>;	//定义别名
		int count;
		Node* root;

		Node* insert(Node* node, K key, V val)
		{
			if (node == 0)
			{
				count++;
				return new Node(key, val);
			}
			if (key == node->key)	//去重
			{
				return node;
			}
			if (key < node->key)
			{
				node->left = insert(node->left, key, val);
			}
			else
			{
				node->right = insert(node->right, key, val);
			}
			return node;
		}
		Node* findMin(Node* node)
		{
			if (node->left == nullptr)
			{
				return node;
			}
			return findMin(node->left);
		}
		Node* findMax(Node* node)
		{
			if (node->right == nullptr)
			{
				return node;
			}
			return findMax(node->right);
		}
		Node* popMin(Node* node)
		{
			if (node->left == nullptr)
			{
				Node* temp = node->right;
				delete node;
				count--;
				return temp;
			}
			node->left = popMin(node->left);
			return node;
		}
		Node* popMax(Node* node)
		{
			if (node->right == nullptr)
			{
				Node* temp = node->left;
				delete node;
				count--;
				return temp;
			}
			node->right = popMax(node->right);
			return node;
		}
		Node* find(Node* node, K& key)
		{
			if (node == nullptr)
			{
				return nullptr;
			}
			if (key == node->key)
			{
				return node;
			}
			if (key < node->key)
			{
				return find(node->left, key);
			}
			return find(node->right, key);
		}

	public:
		BST()
		{
			count = 0;
			root = nullptr;
		}
		int size()
		{
			return count;
		}
		bool empty()
		{
			return count == 0;
		}
		//插入
		void insert(K key, V val)
		{
			root = insert(root,key, val);
		}
		//找最小值
		Node* findMin()
		{
			return findMin(root);
		}
		//找最大值
		Node* findMax()
		{
			return findMax(root);
		}
		//删除最小值
		void popMin()
		{
			if (root)
			{
				root=popMin(root);
			}
		}
		//删除最大值
		void popMax()
		{
			if (root)
			{
				root = popMax(root);
			}
		}
		//查找数据
		Node* find(K key)
		{
			return find(root,key);
		}
		V operator[](K key)
		{
			return find(root, key)->val;
		}
	};
}



#endif

main:


#include <iostream>
#include <string>
#include "BST.h"
using namespace std;
using namespace CPP;

void test()
{
	BST<int,string> bst;
	bst.insert(5,"Alice");
	bst.insert(10,"Bob");
	bst.insert(12,"Coc");
	bst.insert(12, "DDD");
	cout << bst.findMin()->key << ":" << bst.findMin()->val << endl;
	cout << bst.findMax()->key << ":" << bst.findMax()->val << endl;


	auto n2 = bst.find(12);
	cout << n2->key << ":" << n2->val << endl;
	cout << "bst[10]:"<<bst[10] << endl;
	cout << "____________________" << endl;
	while (!bst.empty())
	{
		auto n3 = bst.findMin();
		cout << n3->key << ":" << n3->val << endl;
		bst.popMin();
	}
	bst.insert(5, "Alice");
	bst.insert(10, "Bob");
	bst.insert(12, "Coc");
	cout << "$$$$$$$$$$$$$$$$$$" << endl;
	while (!bst.empty())
	{
		auto n3 = bst.findMax();
		cout << n3->key << ":" << n3->val << endl;
		bst.popMax();
	}

}

int main()
{
	test();
	return 0;
}



结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值