• 二叉搜索树(BST)demo


    #include <iostream>
    
    using namespace std;
    
    class Node {
    public:
    	Node(int key_):left(NULL),right(NULL),key(key_){}
    	Node* left;
    	Node* right;
    	int key;
    };
    
    class BST {
    public:
    	BST() : root(NULL) {}
    
    	~BST() {
    		clear(root);
    	}
    
    	void clear(Node* node) {
    		if (node == NULL) return;
    		clear(node->left);
    		clear(node->right);
    		delete node;
    	}
    
    	Node* searchNode(int key) {
    		Node* result = root;
    		while(result != NULL) {
    			if (result->key == key) return result;
    			result = key < result->key ? result->left : result->right;
    		}
    		return result;
    	}
    
    	Node* minSubNode(Node* node) {
    		Node* result = node;
    		while(result->left != NULL) result = result->left;
    		return result;
    	}
    
    	void insertNode(Node*& node, Node* new_node) {
    		if (node == NULL) node = new_node;
    		else if (new_node->key < node->key) insertNode(node->left, new_node);
    		else insertNode(node->right, new_node);
    	}
    
    	void insertKey(int key) {
    		Node* new_node = new Node(key);
    		insertNode(root, new_node);
    	}
    
    	Node* parNode(Node* child_node) {
    		if (child_node == root) return NULL;
    		Node* result = root;
    		while (result->left != child_node && result->right != child_node)  
    			result =  child_node->key < result->key ? result->left : result->right;
    		return result;
    	}
    
    	void sliceNode(Node* slice_node) {
    		Node* par = parNode(slice_node);
    		Node* slice_child_node = slice_node->left != NULL ? slice_node->left : slice_node->right;
    		if (par == NULL) {
    			root = slice_child_node;
    		} else {
    			if (par->left == slice_node) par->left = slice_child_node;
    			else par->right = slice_child_node;
    		}
    	}
    
    	void deleteNode(Node* del_node) {
    		if (del_node->left == NULL || del_node->right == NULL) {
    			sliceNode(del_node);
    			delete del_node;
    		} else {
    			Node* suc_node = minSubNode(del_node->left);
    			sliceNode(suc_node);
    			del_node->key = suc_node->key;
    			delete suc_node;
    		}
    	}
    
    	bool deleteKey(int key) {
    		Node* del_node = searchNode(key);
    		if (del_node == NULL) return false;
    		deleteNode(del_node);
    		return true;
    	}
    
    private:
    	Node* root;
    };
    
    


     

  • 相关阅读:
    MT【274】一道漂亮的不等式题
    MT【273】2014新课标压轴题之$ln2$的估计
    MT【272】更大的视野,更好的思路.
    MT【271】一道三角最值问题
    MT【270】含参绝对值函数最大之二
    MT【269】含参函数绝对值最大
    MT【268】投篮第一次很重要
    国内下载Git的连接地址
    django 取model字段的verbose_name值
    Django报错(NoReverseMatch at /admin/)
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206878.html
Copyright © 2020-2023  润新知