BTS搜索树c++实现

tree.h

#ifndef  _TREE_
#define _TREE_
#include
#include
using std::vector;
using std::cout;
template
class Node
{
public:
	Node():left(NULL),right(NULL),father(NULL),value(0){}
	Node(Node* l,Node* r,Node* f,T v):left(l),right(r),father(f),value(v){}
	Node(T v):left(NULL), right(NULL), father(NULL), value(v){}
	~Node()
	{
		this->father = NULL;
		this->left = NULL;
		this->right = NULL;
	}
	Node* left;
	Node* right;
	Node* father;
	T value;
};

template
class Tree
{
public:
	Tree():root(NULL),size(0){}
	Tree(const vector&vec);
	~Tree() {};
	void Prevprint(Node* root) const;
	void Inprint(Node* root) const;
	void Postprint(Node* root) const;
	Node* GetRoot() const;
	Node* search(Node*root, T key) const;
	Node* Minivalue(Node* root) const;
	Node* Maxvalue(Node* root) const;
	Node* successor(Node* ptr) const;
	Node* Find(T  key) const;
	void Delete(T key);
	void Insert(T key); 
private:
	Node* root;
	int size;
};

template
Tree::Tree(const vector&vec) {
	size = vec.size();
	root = new Node(vec[0]);
	Node* cur = root;
	Node* cur1 = root;
	for (int i = 1; i < vec.size(); i++) {
		Node* node = new Node(vec[i]);
		while (cur != NULL) {
			if (vec[i] <= cur->value) {
				cur1 = cur;
				cur = cur->left;
			}
			else {
				cur1 = cur;
				cur = cur->right;
			}
		}
		if (cur1->value >= vec[i]) {
			cur1->left = node;
			node->father = cur1;
			cur = root;
		}
		else {
			cur1->right = node;
			node->father = cur1;
			cur = root;
		}
	}
}
template
Node* Tree::GetRoot() const{
	return this->root;
}

template
void Tree::Prevprint(Node* root) const{
	if (root != NULL) {
		cout << root->value << " ";
		Prevprint(root->left);
		Prevprint(root->right);
	}
}

template
void Tree::Inprint(Node* root) const{
	if (root != NULL) {
		Inprint(root->left);
		cout << root->value << " ";
		Inprint(root->right);
	}
}

template
void Tree::Postprint(Node* root) const{
	if (root != NULL) {
		Postprint(root->left);
		Postprint(root->right);
		cout << root->value << " ";
	}
}

template
Node* Tree::search(Node* root, T key) const{
	if (root == NULL || root->value == key)
		return root;
	if (root->value > key)
		return search(root->left, key);
	if (root->value < key)
		return search(root->right, key);
}

template
Node* Tree::Minivalue(Node* root) const {
	while (root->left != NULL) {
		root = root->left;
	}
	return root;
}

template
Node* Tree::Maxvalue(Node* root) const {
	while (root != NULL) {
		root = root->right;
	}
	return cur;
}

template
Node* Tree::successor(Node* ptr) const {
	if (ptr->right != NULL)
		return Minivalue(ptr->right);
	Node* cur = ptr->father;
	while (cur != NULL&&cur->right == ptr) {
		ptr = cur;
		cur = cur->father;
	}
	return cur;
}

template
void Tree::Insert(T key) {
	Node* cur = this->root;
	Node* cur1 = this->root;
	while (cur != NULL) {
		if (key <= cur->value) {
			cur1 = cur;
			cur = cur->left;
		}
		else {
			cur1 = cur;
			cur = cur->right;
		}
	}
	if (cur1->value >= key) {
		cur1->left = new Node(key);
		cur1->left->father = cur1;
	}
	else {
		cur1->right = new Node(key);
		cur1->right->father = cur1;
	}
}

template
Node*  Tree::Find(T key) const{
	Node* cur = this->root;
	while (cur != NULL) {
		if (key > cur->value) {
			cur = cur->right;
			continue;
		}
		if (key < cur->value) {
			cur = cur->left;
			continue;
		}
		else
			return cur;
	}
	return cur;
}

template
void Tree::Delete(T key) {
	Node* cur = Find(key);
	if (cur != NULL) {
		if (cur->left == NULL&&cur->right == NULL) {
 			delete cur;
			return;
		}
		if (cur->left == NULL&&cur->right != NULL) {
			if (cur->father->value >= cur->value) {
				cur->father->left = cur->right;
				cur->right->father = cur->father;
				delete cur;
				return;
			}
			if (cur->father->value <= cur->value) {
				cur->father->right = cur->right;
				cur->right->father = cur->father;
				delete cur;
				return;
			}
		}
		if (cur->left != NULL&&cur->right == NULL) {
			if (cur->father->value >= cur->value) {
				cur->father->left = cur->left;
				cur->left->father = cur->father;
				delete cur;
				return;
			}
		}
		if (cur->left != NULL&&cur->right != NULL) {
			Node* min = Minivalue(cur->right);
			if (min == cur->right) {
				cur->father->right = min;
				min->left = cur->left;
				delete cur;
				return;
			}
			else {
				min->father->left = min->right;
				min->right->father = min->father;
				if (cur->father->value >= cur->value) {
					cur->father->left = min;
					min->father = cur->father;
					min->right = cur->right;
					delete cur;
					return;
				}
				if (cur->father->value <= cur->value) {
					cur->father->right = min;
					min->father = cur->father;
					min->right = cur->right;
					delete cur;
					return;
				}
			}
		}
	}
}
#endif // !_TREE_

test.cpp

#include"tree.h"
using std::cin;
int main()
{
	vector vec;
	int i;
	while (cin >> i) {
		vec.push_back(i);
	}
	Tree tree(vec);
	int p = 23;
	tree.Insert(100);
	tree.Delete(100);
	tree.Prevprint(tree.GetRoot());
	system("pause");
}

 

你可能感兴趣的:(BTS搜索树c++实现)