#ifndef BINARYSEARCHTREE_H_INCLUDED #define BINARYSEARCHTREE_H_INCLUDED /** * File BinarySearchTree.h * the Header file of binary search tree **/ #include <stack> #include <utility> using namespace std; template <typename Entry> class BinarySearchTree { public: BinarySearchTree() { this->root = NULL; this->size = 0; } BinarySearchTree(const BinarySearchTree& rhs) { this->root = NULL; this->size = rhs.size; root = clone(rhs.root); } ~BinarySearchTree() { clear(); } const BinarySearchTree& operator=(const BinarySearchTree& rhs) { if (this != &rhs) { clear(); root = clone(rhs.root); this->size = rhs.size; } return *this; } /** * return the size of the binary search tree */ int getSize() { return this->size; } /** * if the tree is empty, return tree **/ bool empty() const { return root == NULL; } /** * insert x into the tree * if x is alrealdy in the tree, then the method is undefined **/ void insert(const Entry& x) { insert(root, x); } /** * remove x from the tree * if the x is not in the tree, then the method is undefined **/ void remove(const Entry& x) { search_remove(root, x); } /** * clear all nodes in the tree **/ void clear() { clear(root); } /** * search x in the tree * if x is not in the tree, return -1, else return 1 **/ int search(Entry x) { BinaryNode* node = recursiveSearch(root, x); if (node != NULL) return 0; else if (node == NULL) { return -1; } return 0; } /** * preorder traversal **/ void preorder(void(*visit)(Entry& )) { recursivePreorder(root, visit); } /** * inorder traversal **/ void inorder(void (*visit)(Entry& )) { recursiveInorder(root, visit); } /** * inorder traversal */ void postorder(void (*visit)(Entry& )) { recursivePostorder(root, visit); } /** * preorder not recursive **/ void NRpreorder(void (*visit)(Entry& )) { NRpreorder(root, visit); } /** * inorder not recursive **/ void NRinorder(void (*visit)(Entry& )) { NRinorder(root, visit); } /** * postorder not recursive **/ void NRpostorder(void (*visit)(Entry& )) { NRpostorder(root, visit); } private: struct BinaryNode { Entry data; BinaryNode* left; BinaryNode* right; BinaryNode() { left = NULL; right = NULL; } BinaryNode(const Entry& data, BinaryNode* left = NULL, BinaryNode* right = NULL) { this->data = data; this->left = NULL; this->right = NULL; } }; BinaryNode* root; int size; BinaryNode* recursiveSearch(BinaryNode* & subRoot, Entry& x) { if (subRoot == NULL || subRoot->data == x) return subRoot; if (x > subRoot->data) return recursiveSearch(subRoot->right, x); else if (x < subRoot->data) return recursiveSearch(subRoot->left, x); } void insert(BinaryNode* & subRoot, const Entry& x) { if (subRoot == NULL) { subRoot = new BinaryNode(x); size++; } else if (x > subRoot->data) { insert(subRoot->right, x); } else if (x < subRoot->data) { insert(subRoot->left, x); } else { ; // do nothing, the entry is duplicate; } } void search_remove(BinaryNode* & subRoot, const Entry& x) { if (subRoot == NULL) return; // the remove value is not found if (x > subRoot->data) search_remove(subRoot->right, x); else if (x < subRoot->data) search_remove(subRoot->left, x); else if (x == subRoot->data) remove(subRoot, x); } void remove(BinaryNode* & subRoot, const Entry& x) { if (subRoot == NULL) return; // the value x is not found BinaryNode* toDelete = subRoot; if (subRoot->right == NULL) { subRoot = subRoot->left; } else if (subRoot->left == NULL) { subRoot = subRoot->right; } else { toDelete = subRoot->left; BinaryNode* parent = subRoot; while (toDelete->right != NULL) { parent = toDelete; toDelete = toDelete->right; } subRoot->data = toDelete->data; if (parent == subRoot) { subRoot->left = toDelete->left; } else { parent->right = toDelete->left; } } delete toDelete; toDelete = NULL; size--; } void clear(BinaryNode* & subRoot) { if (subRoot != NULL) { // 一直爬到树的底部进行删除 clear(subRoot->left); clear(subRoot->right); delete subRoot; } subRoot = NULL; } // copy the whole tree BinaryNode* clone(BinaryNode* subRoot) const { if (subRoot == NULL) return NULL; else { return new BinaryNode(subRoot->data, clone(subRoot->left), clone(subRoot->right)); } } BinaryNode* findMin(BinaryNode* subRoot) const { if (subRoot == NULL) return NULL; else if (subRoot->left == NULL) return subRoot; else return findMin(subRoot->left); } BinaryNode* findMax(BinaryNode* subRoot) const { if (subRoot != NULL) { while (subRoot->right != NULL) subRoot = subRoot->right; } return subRoot; } void recursivePreorder(BinaryNode* subRoot, void (*visit)(Entry& )) { if (subRoot != NULL) { (*visit)(subRoot->data); recursivePreorder(subRoot->left, visit); recursivePreorder(subRoot->right, visit); } } void recursiveInorder(BinaryNode* subRoot, void (*visit)(Entry& )) { if (subRoot != NULL) { recursiveInorder(subRoot->left, visit); (*visit)(subRoot->data); recursiveInorder(subRoot->right, visit); } } void recursivePostorder(BinaryNode* subRoot, void (*visit)(Entry& )) { if (subRoot != NULL) { recursivePostorder(subRoot->left, visit); recursivePostorder(subRoot->right, visit); (*visit)(subRoot->data); } } void NRpreorder(BinaryNode* subRoot, void (*visit)(Entry& )) { stack<BinaryNode* > nodeStack; while (subRoot != NULL || !nodeStack.empty()) { if (subRoot != NULL) { (*visit)(subRoot->data); nodeStack.push(subRoot); subRoot = subRoot->next; } else { BinaryNode* topNode = nodeStack.top(); nodeStack.pop(); subRoot = topNode->right; } } } void NRinorder(BinaryNode* subRoot, void (*visit)(Entry& )) { stack<BinaryNode* > nodeStack; while (subRoot != NULL || !nodeStack.empty()) { if (subRoot != NULL) { nodeStack.push(subRoot); subRoot = subRoot->left; } else { BinaryNode* topNode = nodeStack.top(); nodeStack.pop(); (*visit)(topNode); subRoot = topNode->right; } } } void NRpostorder(BinaryNode* subRoot, void (*visit)(Entry& )) { stack<pair<BinaryNode*, bool> > nodeStack; // node pointer, flag while (subRoot != NULL || !nodeStack.empty()) { if (subRoot != NULL) { nodeStack.push(make_pair(subRoot, false)); subRoot = subRoot->left; } else { pair<BinaryNode*, bool> topPair = nodeStack.top(); BinaryNode* topNode = topPair.first; nodeStack.pop(); if (topPair.second == true) { (*visit)(topNode->data); //topPair.second = true; //nodeStack.pop(); } else { topPair.second = true; nodeStack.push(topPair); subRoot = topNode->right; } } } } }; #endif // BINARYSEARCHTREE_H_INCLUDED
Mark 一个类...一个很简单的二叉查找树...
最新推荐文章于 2012-01-07 01:08:14 发布