二叉查找树的算法实现
2012-07-26 15:12
375 查看
二叉查找树主要包含以下算法:search,insert,delete,max,min,successor和predecessor。下面的程序是在vc6.0下实现的。其中,关于insert和search提高了迭代和递归两个版本,display使用了递归版本,其余算法都只提供了迭代版本。 #include <iostream> using namespace std; template<typename type> class BinarySortTree; template<typename type> class Node{ public: Node(type d):data(d), left(0), right(0), parent(0){} friend BinarySortTree<type>; private: type data; Node *left; Node *right; Node *parent; }; template<typename type> class BinarySortTree{ public: BinarySortTree():root(0){} Node<type> *searchIteration(type data){ if (root == NULL) return NULL; Node<type> *p = root; while (p) { if (p->data == data) return p; else if (p->data > data) p = p->left; else p = p->right; } return NULL; } Node<type> *searchRecursion(int data){ return searchRecursion(root, data); } Node<type> *predecessor(Node<type> *p){ Node<type> *q; if (p->left) { q = p->left; while (q->right) q = q->right; }else{ q = p; while (q == q->parent->left) q = q->parent; } return q; } Node<type> *successor(Node<type> *p){ Node<type> *q; if (p->right) { q = p->right; while (q->left) q = q->left; }else{ q = p; while (q == q->parent->right) q = q->parent; } return q; } Node<type> *min(){ Node<type> *minPtr = root; if (root) { while (minPtr->left) minPtr = minPtr->left; return minPtr; } return NULL; } Node<type> *max(){ Node<type> *maxPtr; if (root) { while (maxPtr->right) maxPtr = maxPtr->right; return maxPtr; } return NULL; } void insertIteration(type data){ Node<type> *p = root; Node<type> *parent = NULL; while (p) { parent = p; if (p->data > data) p = p->left; else p = p->right; } Node<type> *newNode = new Node<type>(data); if (parent == NULL) root = newNode; else { newNode->parent = parent; if (parent->data > data) parent->left = newNode; else parent->right = newNode; } } void insertRecursion(type data){ Node<type> *newNode = new Node<type>(data); insertRecursion(NULL, root, newNode); } Node<type> *del(type data){ Node<type> *z = searchIteration(data); if (z == NULL) return NULL; Node<type> *y; if (z->left == NULL || z->right == NULL) y = z; else y = successor(z); Node<type> *x; if (y->left) x = y->left; else x = y->right; if (x) x->parent = y->parent; if (y->parent == NULL) root = x; else{ if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if (y != z) z->data = y->data; return y; } void display(){ displayOneNode(root, 3); } private: void displayOneNode(Node<type> *root, int layer){ if (root == NULL) return; displayOneNode(root->left, layer + 5); for (int i = 0; i < layer; i ++) cout << " "; cout << root->data << endl; displayOneNode(root->right, layer + 5); } Node<type> *searchRecursion(Node<type> *root, type data){ if (root == NULL) return NULL; if (root->data == data) return root; if (root->data > data) return searchRecursion(root->left, data); else return searchRecursion(root->right, data); } void insertRecursion(Node<type> *parent, Node<type> *cur, Node<type> *newNode){ if (cur == NULL) { if (parent == NULL) root = newNode; else{ newNode->parent = parent; if (parent->data > newNode->data) parent->left = newNode; else parent->right = newNode; } return; } if (cur->data > newNode->data) insertRecursion(cur, cur->left, newNode); else insertRecursion(cur, cur->right, newNode); } private: Node<type> *root; }; void main(){ BinarySortTree<int> binaryTree; int arr[] = {15, 5, 16, 3, 12, 20, 10, 13, 18, 23, 6, 7}; for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i ++) binaryTree.insertIteration(arr[i]); cout << "**************" << endl; binaryTree.display(); int delData[] = {13, 16, 5}; for (i = 0; i < sizeof(delData)/sizeof(delData[0]); i ++) { Node<int> *node = binaryTree.del(delData[i]); if (node) { cout << "delete " << delData[i] << " success" << endl; cout << "******after delete********" << endl; binaryTree.display(); binaryTree.insertIteration(delData[i]); cout << "**************" << endl; binaryTree.display(); } } //test recursion algorithm BinarySortTree<int> binaryTree2; cout << "test insertRecursion" << endl; for (i = 0; i < sizeof(arr)/sizeof(arr[0]); i ++) binaryTree2.insertRecursion(arr[i]); binaryTree2.display(); } |
相关文章推荐
- 关于二叉查找树中的某个节点的前趋和后继的算法(Python实现)
- 算法学习 - 平衡二叉查找树实现(AVL树)
- 算法导论——(2)二叉查找树的实现
- 数据结构C#实现-二叉查找树的创建,查找,以及各种递归(非递归)遍历算法
- 算法学习 - 平衡二叉查找树实现(AVL树)
- 【算法导论】二叉查找树的操作C++实现
- 二分查找,二叉查找树(二叉排序树)的基本思想以及算法实现
- 二叉查找树相关算法实现(算法导论12章)
- 二叉查找树(二叉排序树、有序二叉树)算法分析及实现
- 二叉查找树——算法导论第12章简易代码实现~
- 二叉查找树(AVL)插入算法Java实现
- [算法导论]二叉查找树的实现 @ Python
- js代码实现二叉查找树的算法
- 算法练习之二叉查找树 C++实现
- 【算法设计-二叉搜索树】二叉查找树的操作与实现
- 算法 3.2节 二叉查找树的实现
- 算法(第四版)学习笔记之java实现二叉查找树
- 循环冗余校验CRC的算法分析和程序实现[转]
- WHU-1551-Pairs(莫队算法+分块实现)
- 多模式匹配算法:AC自动机的C++实现