Mark 一个类...一个很简单的二叉查找树...
2012-01-02 16:46
155 查看
#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 != 4000 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
相关文章推荐
- bash 批量判断 创建目录是否存在 很简单一个小Demo mark
- 一个很简单但又很奇异的C现象
- 一个比较详细的二叉查找树
- 一个很简单的测试体重的java—applet小程序
- android一个很简单很简单的音乐播放器
- 3年前的一个小项目经验,分享给菜鸟兄弟们(公文收发小软件:小技能 DeleteMark)
- URL重写很简单,只要下载一个URLRewriter.dll
- 分享自己编写的一个很简单的网站访问跟踪程序
- 一个很简单的弹窗
- 毁掉一个孩子很简单,原生家庭究竟有多重要
- 单片机控制继电器电路 程序就很简单了 做一个口的输出就可以了
- 一个很简单的java回调的例子
- 解决 visualmark.vim 碰到的一个BUG
- 程序员面试金典: 9.4树与图 4.5实现一个函数,检查一颗二叉树是否为二叉查找树
- AJAX与JSP结合实例,一个很简单实用的例子:身份证号码检验
- 【Java】给定一个有序整数数组,元素各不相同且按照升序排列,编写一个算法,创建一个高度最小的二叉查找树
- 用springmail写发送邮件服务和一个困扰我很久但是很简单的问题
- 一个很简单的淡入淡出相册 (转)
- 这其实是一个很简单的代码。。。。糊涂
- linux脚本的编写和执行,很简单的一个用法!