C++ 二叉搜索树

二叉搜索树里比较复杂的就是删除节点的操作,原理网上有很多说得很细,其中我综合借鉴了几个博主的删除操作,才终于整明白了。
test.h

#include<iostream>
#include <vector>
using namespace std;
struct node {
	int val;
	node* left;
	node* right;
	node(int x) :val(x), left(NULL), right(NULL) {};
};
class tree
{
private:
	node * root;
	void inorder(node* cur);
	void preorder(node* cur);
	void postorder(node* cur);
	void remove(node*& troot, int x);
	void destroy(node* &t);
public:
	tree();
	tree(vector<int> a);//提供一个vector<int>数组的初始化
	~tree();
	node* search(int x);
	void push(int x);
	void inorder();
	void preorder();
	void postorder();
	int minval();
	int maxval();
	void remove(int x);
	void destroy();
	bool isempty();
};


test.cpp

#include "stdafx.h"
#include "test.h"

tree::tree() {}

tree::tree(vector<int> a)
{
	for (auto i : a) {
		push(i);
	}
}

tree::~tree() {}

void tree::inorder() { inorder(root); cout << endl; }
void tree::preorder() { preorder(root); cout << endl; }
void tree::postorder() { postorder(root); cout << endl; }

int tree::minval()
{
	node* cur = root;
	while (cur->left) { cur = cur->left; }
	return cur->val;
}

int tree::maxval()
{
	node* cur = root;
	while (cur->right) { cur = cur->right; }
	return cur->val;
}
void tree::remove(int x)
{
	remove(root, x);
}

void tree::destroy()
{
	destroy(root);
}

bool tree::isempty()
{
	if (root == NULL) {
		cout << "Tree is empty." << endl; return true;
	}
	return false;
}

void tree::destroy(node* &root)
{
	if (root == NULL)return;
	if (root->left) { destroy(root->left); }
	if (root->right) { destroy(root->right); }
	delete(root); root = NULL;
	return;
}
void tree::remove(node* &t, int x)
{
	node* cur;
	if (t != NULL) {
		if (x < t->val) { remove(t->left, x); }
		else if (x > t->val) { remove(t->right, x); }
		else if (!t->left || !t->right) {
			t = (t->left) ? t->left : t->right;
		}
		else {
			cur = t->right;
			while (cur->left) {
				cur = cur->left;
			}
			t->val = cur->val;
			remove(t->right, cur->val);
		}
	}
}

void tree::push(int x)
{
	node* t = new node(x);
	if (root == NULL) { root = t; return; }
	node* cur = root;
	node* pre = NULL;
	while (cur != NULL) {
		if (cur->val > x) { pre = cur; cur = cur->left; }
		else { pre = cur; cur = cur->right; }
	}
	if (pre->val > x) { pre->left = t; }
	else { pre->right = t; }
	return;
}

void tree::inorder(node* cur)
{
	if (cur == NULL) { return; }
	inorder(cur->left);
	cout << cur->val << "->";
	inorder(cur->right);
	return;
}

void tree::preorder(node * cur)
{
	if (cur == NULL)return;
	cout << cur->val << "->";
	preorder(cur->left);
	preorder(cur->right);
	return;
}

void tree::postorder(node * cur)
{
	if (cur == NULL)return;
	postorder(cur->left);
	postorder(cur->right);
	cout << cur->val << "->";
	return;
}

node * tree::search(int x)
{
	node* cur = root;
	while (cur != NULL && cur->val != x) {
		if (cur->val > x) { cur = cur->left; }
		else { cur = cur->right; }
	}
	if (cur == NULL)cout << "No such value in the tree." << endl;
	return cur;
}

测试的主函数

#include "stdafx.h"
#include "test.h"

int main()
{
	vector<int> c = { 3,1,5,7,-3 };
	tree a(c);
	a.inorder();
	a.postorder();
	a.preorder();
	for (auto i : c) {
		cout << endl << a.search(i)->val << endl;
	}
	a.inorder();
	a.remove(3);
	a.inorder();
	vector<int> t1 = { 20,10,30,25,22 };
	tree b1(t1);
	b1.inorder();
	b1.remove(20);
	b1.inorder();
	b1.push(-1);
	b1.inorder();
	b1.destroy();
	b1.isempty();
	system("Pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值