Binary search tree (Binary Search Tree, BST) and its operations to achieve

Copyright: code word is not easy, please indicate the source: https://blog.csdn.net/qq_24309981/article/details/88681602

Theory 1


A specialization of a binary tree

  • Not greater than the left and right is not less than;
  • Common binary search tree operations: query, minimum, maximum, predecessor, successor, insert, delete;
  • Most of the worst run-time binary search tree operation is proportional to the height of the tree;

2, random build binary search tree (Randomly Built Binary Search Tree)

  • Construction of random binary search tree: random insertion key (Comparative inserted by keywords in the order) into an initially empty tree generated tree;
  • There are a n n different keywords construct random binary tree search elements to desired height O ( l g n ) O (LGN) ;

2 code


Binary search tree that grows Below this code built:
Here Insert Picture Description

#include <iostream>
using namespace std;

struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode* parent;
	TreeNode(int x) : val(x), left(NULL), right(NULL), parent(NULL) {}
};

class BST {
private:
	TreeNode* root;
public:
	BST(TreeNode* root){
		this->root = root;
	}
	~BST() {
	}
	
	TreeNode* getRoot() const {
		return root;
	}
	//node既可为一节点,也可为一棵树(但必须是二叉搜索树)
	void insert(TreeNode* node) {
		TreeNode* x = root;
		TreeNode* y = NULL;
		while (x)
		{
			y = x;
			x = node->val < root->val ? x->left : x->right;
		}
		node->parent = y;

		y ? (node->val < y->val ? y->left = node : y->right = node) : root = node;
	}

	void del(TreeNode* node) {
		if (!node->left)
			transplant(node, node->right);
		else if (!node->right)
			transplant(node, node->left);
		else
		{
			TreeNode* y = minimum(node->right);
			if (y != node->right)
			{
				transplant(y, y->right);
				y->right = node->right;
				node->right->parent = y;
			}
			transplant(node, y);
			y->left = node->left;
			node->left->parent = y;
		}
	}
private:
	void transplant(TreeNode* delN, TreeNode* repN) {
		if (repN)
				repN->parent = delN->parent;
		if (!delN->parent)
			root = repN;
		else
			delN == delN->parent->left ? delN->parent->left = repN : delN->parent->right = repN;
	}
public:
	static TreeNode* searchRecursion(TreeNode* node, int key) {
		if (!node || node->val == key)	return node;
		return key < node->val ? searchRecursion(node->left, key) : searchRecursion(node->right, key);
	}

	static TreeNode* searchIteration(TreeNode* node, int key) {
		while (node && node->val != key)
			node = key < node->val ? node->left : node->right;
		return node;
	}

	static TreeNode* minimum(TreeNode* node) {
		if (node)
		{
			while (node->left)
				node = node->left;
		}
		return node;
	}

	static TreeNode* maximum(TreeNode* node) {
		if (node)
		{
			while (node->right)
				node = node->right;
		}
		return node;
	}

	static TreeNode* successor(TreeNode* node) {
		if (node->right)
			return minimum(node->right);
		TreeNode* y = node->parent;
		while (y && node == y->right)
		{
			node = y;
			y = y->parent;
		}
		return y;
	}

	static TreeNode* predecessor(TreeNode* node) {
		if (node->left)
			return maximum(node->left);
		TreeNode* y = node->parent;
		while (y && node == y->left)
		{
			node = y;
			y = y->parent;
		}
		return y;
	}
};


int main()
{
	TreeNode* node1 = new TreeNode(15);
	TreeNode* node2 = new TreeNode(6);
	TreeNode* node3 = new TreeNode(18);
	TreeNode* node4 = new TreeNode(3);
	TreeNode* node5 = new TreeNode(7);
	TreeNode* node6 = new TreeNode(17);
	TreeNode* node7 = new TreeNode(20);
	TreeNode* node8 = new TreeNode(2);
	TreeNode* node9 = new TreeNode(4);
	TreeNode* node10 = new TreeNode(13);
	TreeNode* node11 = new TreeNode(9);
	node1->left = node2;
	node2->parent = node1;

	node1->right = node3;
	node3->parent = node1;

	node2->left = node4;
	node4->parent = node2;

	node2->right = node5;
	node5->parent = node2;

	node3->left = node6;
	node6->parent = node3;

	node3->right = node7;
	node7->parent = node3;

	node4->left = node8;
	node8->parent = node4;

	node4->right = node9;
	node9->parent = node4;

	node5->right = node10;
	node10->parent = node5;

	node10->left = node11;
	node11->parent = node10;

	//TreeNode* res = searchRecursion(node1, 17);
	TreeNode* res = BST::searchIteration(node1, 17);
	if (res)
	{
		cout << res->val << endl;
	}
	
	res = BST::minimum(node1);
	if (res)
	{
		cout << res->val << endl;
	}

	res = BST::maximum(node1);
	if (res)
	{
		cout << res->val << endl;
	}

	res = BST::successor(node11);
	if (res) 
	{
		cout << res->val << endl;
	}

	res = BST::predecessor(node11);
	if (res)
	{
		cout << res->val << endl;
	}

	BST bst(node1);
	TreeNode* testNode = new TreeNode(21);
	bst.insert(testNode);
	res = BST::maximum(node1);
	if (res)
	{
		cout << res->val << endl;
	}

	bst.del(node1);
	res = BST::searchIteration(bst.getRoot(), node1->val);
	if (res)
	{
		cout << res->val << endl;
	}
	else
	{
		cout << "根节点已被删除" << endl;
	}
	system("pause");
    return 0;
}
//输出
17
2
20
13
7
21
根节点已被删除

Guess you like

Origin blog.csdn.net/qq_24309981/article/details/88681602