I recently studied the introduction to algorithms in the section about the Binary Search Tree and the red/black tree. Although the content of the red/black tree has not been completely digested and absorbed, writing a blog is a review and reflection on all the content.
1. Binary Search Tree
The binary search tree is a binary tree. For any node, the data in its left son is smaller than that in the root node, and the data in the right node is larger than that in the root node.
For example
In the search issue, although hash tables can provide O (1) Time in good cases, but it is not good for data distribution, or a small amount of data may cause aggregation or waste of space. However, when using the search tree, the average retrieval efficiency is lnn.
The following describes some specific operations of the algorithms implemented in introduction to algorithms. In fact, the basic operations are the same as binary trees, including insertion, deletion, and traversal. However, because of the special tree structure, some processing is required after the operation.
#ifndef BINARY_SEARCH_TREE_H#define BINARY_SEARCH_TREE_H#include <assert.h>#include <stack>#include <iostream>namespace DataStructure{
// Template Node
template <typename T>class TreeNode{public:T data;TreeNode *left,*right;TreeNode *parent;TreeNode(){parent = left = right = NULL;}TreeNode(T _data){data = _data;parent = left = right = NULL;}};template<typename T>class BinarySearchTree{public:typedef TreeNode<T> NodeType;public:BinarySearchTree(){m_pRoot = NULL;}/** Add a element into tree*/TreeNode<T>* insert(T _element);/** Find a element*/TreeNode<T>* search(T _element) const;/** delete an element*/bool delete_element(TreeNode<T> *_pElement);/**search the element interactively*/TreeNode<T>* interative_search(T data);/**the minimum element of the tree*/TreeNode<T>* minimum_element(TreeNode<T> *)const;/**the maximum element*/TreeNode<T>* maximum_element(TreeNode<T> *)const;/**tree successor */TreeNode<T>* successor(TreeNode<T>* pElement) const;/**tree predecessor */TreeNode<T>* predecessor(TreeNode<T> *pElement)const;/**iterate all elements*/void print_ordered_elements()const;/*** delete one node from the tree*/private:TreeNode<T> * m_pRoot;};/** Add a element into tree*/template<typename T>TreeNode<T>* BinarySearchTree<T>::insert(T _element){if(m_pRoot == NULL){m_pRoot = new TreeNode<T>;m_pRoot->data = _element;m_pRoot->left = m_pRoot->right = NULL;return m_pRoot;}else{TreeNode<T> *pNode = m_pRoot;while(pNode != NULL){if( _element > pNode->data ){//insert into right subtree//pNode = pNode->right;//make the current element be the right son.if(pNode->right == NULL){TreeNode<T> *pNewElement = new TreeNode<T>;pNewElement->data = _element;pNewElement->left = NULL;pNewElement->right = NULL;pNewElement->parent = pNode;pNode->right = pNewElement;return pNewElement;}else{pNode = pNode->right;}}else if(_element < pNode->data ){//insert into left subtree//pNode = pNode->left;if(pNode->left == NULL){TreeNode<T> *pNewElement = new TreeNode<T>;pNewElement->data = _element;pNewElement->left = NULL;pNewElement->right = NULL;pNewElement->parent = pNode;pNode->left = pNewElement;return pNewElement;}else{pNode = pNode->left;}}else{return pNode;}}}return NULL;}/** Find an element* if there is no such element return NULL*/template<typename T>TreeNode<T>* BinarySearchTree<T>::search(T _element) const{TreeNode<T> *pElement = m_pRoot;while(pElement != NULL){//search the leftif(_element < pElement->data){pElement = pElement->left;}else if(_element > pElement->data){pElement = pElement->right;}else{return pElement;}}return NULL;}/** delete an element*/template<typename T>bool BinarySearchTree<T>::delete_element(TreeNode<T> *_pElement){//assert//assert(_pElement != NULL && m_pRoot != NULL);if(_pElement == NULL || m_pRoot == NULL){return false;}TreeNode<T> *realDelete = NULL;TreeNode<T> *pSon = NULL;if(_pElement ->left == NULL || _pElement->right == NULL){realDelete = _pElement;}else{realDelete = successor(_pElement);}if(realDelete->left != NULL){pSon = realDelete->left;}else{pSon = realDelete->right;}if(pSon != NULL){pSon->parent = realDelete->parent;}if(realDelete ->parent != NULL){if(realDelete->parent->left == realDelete){realDelete->parent->left = pSon;}else{realDelete->parent->right = pSon;}if(_pElement != realDelete){_pElement->data = realDelete->data;delete realDelete;}}else{delete m_pRoot;m_pRoot = NULL;}return true;//this is what age}/**the minimum element of the tree*/template<typename T>TreeNode<T>* BinarySearchTree<T>::minimum_element(TreeNode<T> *pCNode)const{TreeNode<T> *pNode = pCNode;while(pNode->left != NULL){pNode = pNode->left;}return pNode;}/**the maximum element*/template<typename T>TreeNode<T>* BinarySearchTree<T>::maximum_element(TreeNode<T> *pCNode)const{TreeNode<T> *pNode = pCNode;while(pNode->right != NULL){pNode = pNode->right;}return pNode;}//print template<typename T>void BinarySearchTree<T>::print_ordered_elements()const{std::stack<TreeNode<T>*> elements;TreeNode<T>* tmp = m_pRoot;elements.push(m_pRoot);std::cout<<"All Elements\n";while(!elements.empty()){while(tmp != NULL){tmp = tmp ->left;elements.push(tmp);}elements.pop();if(!elements.empty()){tmp = elements.top();elements.pop();std::cout<<tmp->data<<std::endl;tmp = tmp->right;elements.push(tmp);}}}/**tree successor */template<typename T>TreeNode<T>* BinarySearchTree<T>::successor(TreeNode<T>* pElement) const{if(pElement->right != NULL){return minimum_element(pElement->right);}TreeNode<T>* pNode = pElement->parent;while(pNode != NULL && pNode ->right == pElement){pElement = pNode;pNode = pNode->parent;}return pNode;}/**tree predecessor */template<typename T>TreeNode<T>* BinarySearchTree<T>::predecessor(TreeNode<T> *pElement)const{if(pElement->left != NULL){return maximum_element(pElement->left);}TreeNode<T>* pNode = pElement->parent;while(pNode != NULL && pNode ->left == pElement){pElement = pNode;pNode = pNode->parent;}return pNode;}}//namespace DataStructure#endif //BINARY_SEARCH_TREE_H