算法第四版3-2Binary Search Trees With C++ Implementation
2016-10-06 19:57
489 查看
http://algs4.cs.princeton.edu/32bst/
/* Oct/6/2016 binary search tree method: 1.total() calculate the size of tree 2.size() calculate the size of subtree 3.get(key) return the value of the key 4.put(key,value) add node(key,value) 5.floor(key) return largest element's key which no more than k 6.select(m) return the key of the m'th element 7.rank(key) return the priority of certain key 8.del(key) delete the element of key 9.print print all elements of tree */ #include<iostream> #include<string> using namespace std; class node { public: int key, value; node *l, *r;//node *a *b,not node* a,b int N; node() { l = r = NULL; } node(int k, int v) { key = k, value = v; l = r = NULL; N = 1; }// creat new node int getN() { return N; //valid only when this is not NULL } }; class bst :public node { private: node* root; int size(node* x) { if (x == NULL) return 0; //when x==NULL,object does not exist, illegal to call class method else return (*x).getN(); } void print(node* x) { if (x->l != NULL) print(x->l); cout << x->key << " - " << x->value << endl; if (x->r != NULL) print(x->r); } void put(node* pa, node* x, int k, int v) { if (x == NULL) { //x = new node(k, v); if (x != root) { if (pa->key > k) pa->l = new node(k, v); //choose left or right by key else pa->r = new node(k, v); } else { root = new node(k, v); } return; } else if (x->key > k) put(x, x->l, k, v); else if (x->key < k) put(x, x->r, k, v); else { x->value = v; return; } (*x).N = size(x->l) + size(x->r) + 1; } //recurrsive complement node* floor(node* x, int k) { if (x == NULL) return NULL; if (x->key > k) return floor(x->l, k); else if (x->key == k) return x; else { node *temp = floor(x->r, k); if (temp == NULL) return x; else return temp; //save the value of latest right seaarch } } node* select(node* x, int m) { while (x != NULL) { if (size(x->l) > m) x = x->l; else if (size(x->l) == m) return x; else { m = m - size(x->l) - 1; x = x->r; } } } int rank(node* x, int k) { int ans = 0; while (x->key != k) { if (x->key < k) { ans = ans + size(x->l) + 1; x = x->r; } else { x = x->l; } } if (x->l != NULL) ans += size(x->l); return ans; } void delmin() { root = delmin(root); //use root=delmin(root),not delmin(root).To avoid error when root is the min element } node* delmin(node* x) { if (x->l == NULL) return x->r; x->l = delmin(x->l); x->N = size(x->l) + size(x->r) + 1; //use size method to avoid NULL pointer error return x; } node* del(node* x, int k) { if (x == NULL) return NULL; if (x->key < k) x->r = del(x->r, k); else if (x->key > k) x->l = del(x->l, k); else { if (x->l == NULL) return x->r; if (x->r == NULL) return x->l; //deal with leftchild/rightchild=NULL as long as use lc/rc node* t = x; //remember x's post point_to x = min(t->r); //change x's point_to x->r = delmin(t->r); x->l = t->l; } x->N = size(x->l) + size(x->r) + 1; return x; } node* min(node* x) { node* temp = x; while (temp->l != NULL) { temp = temp->l; } return temp; } public: bst() { root = NULL; } //int total() { // return root->N; //} int size() { return size(root); } int get(int k) { node* t = root; while (t != NULL) { if (t->key > k) t = t->l; else if (t->key < k) t = t->r; else return t->value; } return NULL; } void put(int k, int v) { put(root, root, k, v); return; } //largest element which no more than k int floor(int k) { node* x = floor(root, k); if (x == NULL) return NULL; else return x->key; } // order to key int select(int m) { return select(root, m)->key; } // key to order int rank(int k) { node* temp = root; return rank(temp, k); } void del(int k) { root = del(root, k); } void print() { node* temp = root; print(root); } }; int main() { int a, b; bst tree; string T; while (cin >> T) { if (T == "put") { cin >> a >> b; tree.put(a, b); } if (T == "get") { cin >> a; cout << tree.get(a) << endl; } if (T == "floor") { cin >> a; cout << tree.floor(a) << endl; } if (T == "select") { cin >> a; cout << tree.select(a - 1) << endl; } if (T == "rank") { cin >> a; cout << tree.rank(a) + 1 << endl; } if (T == "delete") { cin >> a; tree.del(a); } if (T == "print") { tree.print(); } } return 0; }
相关文章推荐
- Binary Search Tree With C++ Implementation(OOP)
- [C++]LeetCode: 53 Unique Binary Search Trees
- [leetcode-95]Unique Binary Search Trees II(c++)
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
- [C++]LeetCode: 92 Unique Binary Search Trees II
- leetcode 96 c++. Unique Binary Search Trees
- LeetCode.96(95) Unique Binary Search Trees(fibonacci的经典改进算法)&& II
- LeetCode算法题目:Unique Binary Search Trees
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
- Nicolas Lehuen’s pytst is a C++ ternary search tree implementation with a Python interface
- 【LeetCode 96】Unique Binary Search Trees (C++)
- LeetCode 96 — Unique Binary Search Trees(C++ Java Python)
- [leetcode-96]Unique Binary Search Trees(c++)
- leetcode_c++:树:Unique Binary Search Trees II(095)
- 算法学习笔记之——priority queue、heapsort、symbol table、binary search trees
- 算法分析与设计第五周习题:95. Unique Binary Search Trees II
- DP (3) -- Count Numbers with Unique Digits,Decode Ways,Unique Binary Search Trees I, II
- c++之STL(13) STL 算法 - 查找算法(6)binary_search(b,e,v) binary_search(b,e,v,p) includes(b,e,sb,se) include
- 数据结构:Binary and other trees(数据结构,算法及应用(C++叙事描述语言)文章8章)
- C++ STL 算法:查找算法(6) binary_search、includes