BST的基本代码实现(详细注释)

BST的基本代码实现(详细注释)

用的都是非常朴素的方法,欢迎交流讨论

#include<iostream>
#include<queue>
using namespace std;
typedef struct Bnode {
	Bnode*left, *right;
	int data;
}BSTnode, *BSTree;
void insert(BSTree &root,int val)//改变内容需用指针的引用
{
	if (!root)
	{
		root = new BSTnode;
		root->left = NULL;
		root->right = NULL;
		root->data = val;

	}
	else
	{

		if (val > root->data)//大于当前节点值,放在右边,反之左边
		{
			insert(root->right, val);
		}
		else
			insert(root->left, val);
	}
}
void input(BSTree &root,int n)//初始化BST树
{
	cout << "please cin the number\n";
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		insert(root, x);
	}
}
BSTree find(int key, BSTree root)
{
	if (root)
	{

		if (root->data == key)
			return root;
		if (root->data < key)
			return find(key, root->right);
		if (root->data > key)
			return find(key, root->left);
	}
	else
		return NULL;
}
void layer_tra(BSTree root)//层次遍历
{
	if (!root) return;
	queue<BSTree>q1;
	q1.push(root);
	while (!q1.empty())
	{
		cout << q1.front()->data << ' ';
		if(q1.front()->left)
			q1.push(q1.front()->left);
		if (q1.front()->right)
			q1.push(q1.front()->right);
		q1.pop();
	}
	cout << endl;
}
void delete_node(BSTree &root, int val)
{
	if (root)
	{
		//首先寻找到该节点以及父节点,用isleft来判断左右子树的关系
		BSTnode*pre = NULL;
		BSTnode*now = root;
		bool isleft;
		while (now)
		{
			if (now->data < val)
			{
				pre = now;
				now = now->right;
				isleft = 0;
			}
			else if(now->data>val)
			{
				pre = now;
				now = now->left;
				isleft = 1;
			}

			else
			{
				break;
			}
		}

		//接下来为删除部分
		if (!now)//如果未找到
		{
			cout << "cant find\n";
			return;
		}
		else
		{
			if (now == root)//如果为根节点
			{
				BSTnode*tmp = root->right;//用tmp来存放后继
				if (!tmp)//如果根节点无右子树,直接删除根节点,改变root指向
				{
					tmp = root;
					root = root->left;
					delete tmp;
					return;
					
				}
				BSTnode*parent = NULL;
				while (tmp->left)
				{
					parent = tmp;
					tmp = tmp->left;
				}
				if (!parent)//如果tmp无左子树
				{
					now->data = tmp->data;
					now->right = tmp->right;
					delete tmp;
					return;
				}
				now->data = tmp->data;
				parent->left = NULL;
				delete tmp;
				return;
			}

			//接下来为非根节点
			if (!now->left && !now->right)//叶子节点直接删除
			{
				delete now;
				return;
			}
			if (!now->left&&now->right)//如果当前节点只有右子树
			{
				if (isleft)//如果当前节点为父节点的左子树
				{
					pre->left = now->right;
					delete now;
					return;

				}
				else
				{
					pre->right = now->right;
					delete now;
					return;

				}
			}
			if (now->left && !now->right)//只有左子树
			{

				if (isleft)
				{

					pre->left = now->left;

					delete now;
					return;

				}
				else
				{
					pre->right = now->left;
					delete now;
					return;

				}
			}
			if (now->left&&now->right)//度为2
			{
				BSTnode*tmp = now->right;//用tmp来存放后继
				BSTnode*parent = NULL;
				while (tmp->left)
				{
					parent = tmp;
					tmp = tmp->left;
				}
				if (!parent)//如果tmp无左子树,则直接用tmp与now替换,并删除tmp
				{
					now->data = tmp->data;
					now->right = tmp->right;
					delete tmp;
					return;
				}
				now->data = tmp->data;
				parent->left = NULL;
				delete tmp;
				return;

			}
		}

	}
}
void inorder(BSTree root)
{
	if (root)
	{
		inorder(root->left);
		cout << root->data << ' ';
		inorder(root->right);
	}
}
int main()
{
	BSTree root=NULL;
	BSTree tmp = NULL;
	int n;
	cout << "please inupt n numbers:\n";
	cin >> n;
	input(root, n);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言BST树的基本操作代码: ```c #include <stdio.h> #include <stdlib.h> //定义二叉搜索树的节点结构体 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; //创建一个新的节点 TreeNode* createNode(int val) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } //向二叉搜索树中插入一个节点 TreeNode* insertNode(TreeNode* root, int val) { if (root == NULL) { return createNode(val); } if (val < root->val) { root->left = insertNode(root->left, val); } else if (val > root->val) { root->right = insertNode(root->right, val); } return root; } //在二叉搜索树中查找一个节点 TreeNode* searchNode(TreeNode* root, int val) { if (root == NULL || root->val == val) { return root; } if (val < root->val) { return searchNode(root->left, val); } else { return searchNode(root->right, val); } } //删除二叉搜索树中的一个节点 TreeNode* deleteNode(TreeNode* root, int val) { if (root == NULL) { return root; } if (val < root->val) { root->left = deleteNode(root->left, val); } else if (val > root->val) { root->right = deleteNode(root->right, val); } else { if (root->left == NULL) { TreeNode* temp = root->right; free(root); return temp; } else if (root->right == NULL) { TreeNode* temp = root->left; free(root); return temp; } TreeNode* temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->val = temp->val; root->right = deleteNode(root->right, temp->val); } return root; } //中序遍历二叉搜索树 void inorderTraversal(TreeNode* root) { if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } } int main() { TreeNode* root = NULL; root = insertNode(root, 5); insertNode(root, 3); insertNode(root, 7); insertNode(root, 1); insertNode(root, 9); printf("中序遍历二叉搜索树:"); inorderTraversal(root); printf("\n"); root = deleteNode(root, 5); printf("删除节点5后的中序遍历二叉搜索树:"); inorderTraversal(root); printf("\n"); return 0; } ``` 以上代码实现了二叉搜索树的基本操作,包括创建节点、插入节点、查找节点、删除节点和中序遍历等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值