BST树

二叉查找树(BST树)的构建、查找、插入、删除操作

BST(二叉搜索/排序树)类模板的实现

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>

typedef int Key;

typedef struct BstNode {
	BstNode* left;
	BstNode* right;
	BstNode* parents;
	Key val;
}BstNode;

typedef struct 
{
	BstNode* head;
	int size;
}Bstree;

BstNode* BuyNode(BstNode* pa = NULL)//创建
{
	BstNode* s = (BstNode*)malloc(sizeof(BstNode));
	if (s == NULL)
		exit(1);
	memset(s, 0, sizeof(BstNode));
	s->parents = pa;
	return s;
}

void InitBstree(Bstree &root)//初始化
{
	root.head = BuyNode();
	root.size = 0;
}

void Free(BstNode* p)//释放
{
	free(p);
}


void DestroyBstree(BstNode* node)
{
	if (node)
	{
		DestroyBstree(node->left);
		DestroyBstree(node->right);
		Free(node);
	}
}
void Destroy(Bstree& root)//销毁
{
	DestroyBstree(root.head->parents);
	root.size = 0;
}

BstNode* First(BstNode* ptr)//Min
{
	while (ptr != NULL&&ptr->left != NULL)
		ptr = ptr->left;
	return ptr;
}

BstNode* Last(BstNode* ptr)//Max
{
	while (ptr != NULL&&ptr->right != NULL)
		ptr = ptr->right;
	return ptr;
}

BstNode* Next(Bstree& root, BstNode *ptr)//后一个结点
{
	if (ptr == NULL || ptr == root.head)
		return NULL;
	if (ptr->right != NULL)
		return First(ptr->right);
	else
	{
		BstNode* pa = ptr->parents;
		while (pa != root.head&&pa->left != ptr)
		{
			ptr = pa;
			pa = pa->parents;
		}
		if (pa == root.head)
			pa = NULL;
		return pa;
	}
}

BstNode* Prev(Bstree& root, BstNode* ptr)//前一个
{
	if (ptr == NULL || ptr == root.head)
		return NULL;
	if (ptr->left != NULL)
		return Last(ptr->left);
	else
	{
		BstNode* pa = ptr->parents;
		while (pa != root.head&&pa->right != ptr)
		{
			ptr = pa;
			pa = pa->parents;
		}
		if (pa == root.head)
			pa = NULL;
		return pa;
	}
}

BstNode* FindVal(Bstree & root, Key val)//循环查找
{
	BstNode* p = root.head->parents;
	while (p != NULL&&p->val != val)
		p = val < p->val ? p->left : p->right;
	return p;
}


BstNode* Search(BstNode* p, Key val)
{
	if (p == NULL || p->val == val)
		return p;
	else if (val < p->val)
		return Search(p->left, val);
	else return Search(p->right, val);
}
BstNode* SearchVal(Bstree& root, Key val)//递归查找
{
	return Search(root.head->parents, val);
}


bool InterBST(Bstree &root, Key val)//插入元素
{
	BstNode* pa = root.head;
	BstNode* p = root.head->parents;
	while (p != NULL&&p->val != val)
	{
		pa = p;
		p = (val < p->val ? p->left : p->right);
	}
	if (p != NULL)
		return false;
	p = BuyNode(pa);
	p->val = val;
	if (pa == root.head)
	{
		root.head->left = p;
		root.head->right = p;
		root.head->parents = p;
	}
	else {
		if (p->val < pa->val)
		{
			pa->left = p;
			if (p->val < root.head->left->val)
				root.head->left = p;
			else {
				pa->right = p;
				if (p->val > root.head->left->val)
					root.head->right = p;
			}
		}
	}
	root.size++;
	return true;
}

bool RemoveBST(Bstree &root, Key val)//删除某值
{
	BstNode* pa = root.head;
	BstNode* p = root.head->parents;
	while (p != NULL || p->val != val)
	{
		pa = p;
		p = val < p->val ? p->left : p->right;
	}
	if (p == NULL) return false;
	if (p->left != NULL&&p->right != NULL)
	{
		BstNode* nt = Next(root, p->right);
		p->val = nt->val;
		p = nt;
	}
	BstNode* child = p->left != NULL ? p->left : p->right;
	if (child != NULL)child->parents = pa;
	if (pa == root.head)
		root.head->parents = child;
	else
	{
		if (p == pa->left)
			pa->left = child;
		else
			pa->right = child;
	}
	Free(p);
	root.size -= 1;
	return true;
}

//void NiceInOrder(Bstree& root)
//{
//	for (BstNode *p = First(root.head)->parents;
//	p != NULL; p = Next(root, p))
//		printf("%d ", p->val);
//	printf("\n");
//}
//void ResNiceInOrder(Bstree& root)
//{
//	for(BstNode *p = Last(root.head)->parents;
//	p != NULL; p = Prev(root, p))
//		printf("%d ", p->val);
//	printf("\n");
//}

void Inordr(BstNode* node)
{
	if (node)
	{
		Inordr(node->left);
		printf("%d ", node->val);
		Inordr(node->right);
	}
}
void inordr(Bstree* root)//中序遍历
{
	if (root)
		Inordr(root->head);
	printf("\n");
}

int main()
{
	Bstree* root = (Bstree*)malloc(sizeof(Bstree));
	InitBstree(*root);
	int arr[] = { 9,5,3,4,1,7,2,8,0 };
	int n = sizeof(arr) / sizeof(int);
	for (int i = 0; i < n; i++)
	{
		InterBST(*root, arr[i]);
	}
	inordr(root);
	printf("max:%d\n", Last(root->head)->val);
	printf("min:%d\n", First(root->head)->val);
	return 0;
}
#include <iostream>

using namespace std;

template<typename T>
class BST {
private:
	struct Node
	{
		T val;
		Node* left;
		Node* right;
		Node(T val)//使用数值构建Node
		{
			this->val = val;
			this->left = this->right = NULL;
		}
		Node(Node* node)//使用已有Node构建新的Node
		{
			this->val = node->val;
			this->left = node->left;
			this->right = node->right;
		}
	};
	Node* root;
	int size;
	Node* insert(Node* node, T val)//插入
	{
		if (node == NULL) {
			size++;
			return new Node(val);
		}
		else
		{
			if (val == node->val)
			{
				node->val = val;
				return node;
			}
			else if (val < node->val)
			{
				node->left = insert(node->left, val);
				return node;
			}
			else {
				node->right = insert(node->right, val);
				return node;
			}
		}
	}

	Node* maxNode(Node* node)//获取最大值,Last
	{
		if (node->right == NULL)
			return node;
		return maxNode(node->right);
	}

	Node* minNode(Node* node)//获取最小值,First
	{
		if (node->left == NULL)
			return node;
		return minNode(node->left);
	}

	Node* deleteMin(Node* node)//返回删除最小值后BST树的值
	{
		if (node->left == NULL)
		{
			Node* rightNode = node->right;
			delete node;
			size--;
			return rightNode;
		}
		else {
			node->left = deleteMin(node->left);
			return node;
		}
	}

	Node* deleteMax(Node* node)//返回删除最大值后BST树的根
	{
		if (node->right == NULL)
		{
			Node* leftNode = node->left;
			delete node;
			size--;
			return leftNode;
		}
		else {
			node->right = deleteMax(node->right);
			return node;
		}
	}
	Node* remove(Node* node, T val)//删除
	{
		if (node == NULL)
			return node;
		else
		{
			if (val < node->val)
			{
				node->left = remove(node->left,val);
				return node;
			}
			else if (val>node->val)
			{
				node->right = remove(node->right,val);
				return node;
			}
			else {
				if (node->right == NULL) {
					Node* left = node->left;
					delete node;
					size--;
					return left;
				}
				else if (node->right == NULL) {
					Node* right = node->right;
					delete node;
					size--;
					return right;
				}
				else {
					Node* deleteNode = node;
					Node* newTree = new Node(minNode(node->right));
					this->size++;
					newTree->right = deleteMin(node->right);
					newTree->left = node->left;
					delete node;
					size--;
					return newTree;
				}
			}
		}
	}
	void inorder(Node* node)//中序遍历
	{
		if (node)
		{
			inorder(node->left);
			cout << node->val << " ";
			inorder(node->right);
		}
	}
	void destroy(Node* node)//销毁
	{
		if (node)
		{
			destroy(node->left);
			destroy(node->right);
			delete node;
			size--;
		}
	}
public:
	//在public中调用私有方法
	BST()
	{
		root = NULL;
		size = 0;
	}
	~BST()
	{
		destroy(root);
	}
	int GetSize()
	{
		return size;
	}
	bool IsEmpty()
	{
		return size == 0;
	}
	void insert(T val)
	{
		root = insert(root, val);
	}
	T maxVal()
	{
			Node* maxVal = maxNode(root);
			return maxVal->val;	
	}
	T minVal()
	{
			Node* minVal = minNode(root);
			return minVal->val;
	}
	void deleteMax()
	{
		if (root)
			root = deleteMax(root);
	}
	void deleteMin()
	{
		if (root)
			root = deleteMin(root);
	}
	void remove(T val)
	{
		if (root)
			root = remove(root, val);
	}
	void inorder()
	{
		inorder(root);
		cout << endl;
	}
};

int main()
{
	int arr[] = { 9,5,3,4,1,7,2,8,0 };
	int n = sizeof(arr) / sizeof(int);
	BST<int> bst;
	for (int i = 0; i < n; i++)
	{
		bst.insert(arr[i]);
	}
	bst.inorder();
	cout << "max: " << bst.maxVal() << endl;
	cout << "min: " << bst.minVal() << endl;
	cout << "删除的节点是:value=4" << endl;
	bst.remove(4);
	bst.inorder();
	return 0;
}

对比一下感觉C++也挺好用的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是我重新设计的C语言版本的BST模块: ``` // bst.h // 定义二叉搜索节点类型 typedef struct bst_node { int key; struct bst_node* left; struct bst_node* right; } BSTNode; // 创建一个新的节点 BSTNode* create_node(int key); // 插入一个节点,返回根节点 BSTNode* insert_node(BSTNode* root, int key); // 删除一个节点,返回根节点 BSTNode* delete_node(BSTNode* root, int key); // 查找一个节点,返回指向该节点的指针 BSTNode** search_node(BSTNode** root, int key); // 遍历二叉搜索,并将节点的key值存储到数组中 void traverse_bst(BSTNode* root, int* arr, int* index); // 释放二叉搜索的内存 void free_bst(BSTNode* root); ``` ```c // bst.c #include "bst.h" #include <stdlib.h> // 创建一个新的节点 BSTNode* create_node(int key) { BSTNode* node = (BSTNode*)malloc(sizeof(BSTNode)); node->key = key; node->left = NULL; node->right = NULL; return node; } // 插入一个节点,返回根节点 BSTNode* insert_node(BSTNode* root, int key) { if (root == NULL) { return create_node(key); } if (key < root->key) { root->left = insert_node(root->left, key); } else if (key > root->key) { root->right = insert_node(root->right, key); } return root; } // 删除一个节点,返回根节点 BSTNode* delete_node(BSTNode* root, int key) { if (root == NULL) { return NULL; } if (key < root->key) { root->left = delete_node(root->left, key); } else if (key > root->key) { root->right = delete_node(root->right, key); } else { if (root->left == NULL) { BSTNode* temp = root->right; free(root); return temp; } else if (root->right == NULL) { BSTNode* temp = root->left; free(root); return temp; } BSTNode* temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->key = temp->key; root->right = delete_node(root->right, temp->key); } return root; } // 查找一个节点,返回指向该节点的指针 BSTNode** search_node(BSTNode** root, int key) { if (*root == NULL || (*root)->key == key) { return root; } if (key < (*root)->key) { return search_node(&((*root)->left), key); } else { return search_node(&((*root)->right), key); } } // 遍历二叉搜索,并将节点的key值存储到数组中 void traverse_bst(BSTNode* root, int* arr, int* index) { if (root == NULL) { return; } traverse_bst(root->left, arr, index); arr[*index] = root->key; (*index)++; traverse_bst(root->right, arr, index); } // 释放二叉搜索的内存 void free_bst(BSTNode* root) { if (root == NULL) { return; } free_bst(root->left); free_bst(root->right); free(root); } ``` 这个BST模块包含了创建节点、插入节点、删除节点、查找节点、遍历和释放内存等基本操作。在使用时,可以通过调用这些函数来实现对BST的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值