算法第四版3-2Binary Search Trees With C++ Implementation
来源:互联网 发布:淘宝网开店 编辑:程序博客网 时间:2024/06/05 20:46
http://algs4.cs.princeton.edu/32bst/
/*Oct/6/2016binary search treemethod:1.total() calculate the size of tree2.size() calculate the size of subtree3.get(key) return the value of the key4.put(key,value) add node(key,value)5.floor(key) return largest element's key which no more than k6.select(m) return the key of the m'th element7.rank(key) return the priority of certain key8.del(key) delete the element of key9.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,bint N;node() {l = r = NULL;}node(int k, int v) {key = k, value = v;l = r = NULL;N = 1;}// creat new nodeint 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 methodelse 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 keyelse 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 complementnode* 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 errorreturn 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/rcnode* t = x; //remember x's post point_tox = min(t->r); //change x's point_tox->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 kint floor(int k) {node* x = floor(root, k);if (x == NULL) return NULL;else return x->key;}// order to keyint select(int m) {return select(root, m)->key;}// key to orderint 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;}
0 0
- 算法第四版3-2Binary Search Trees With C++ Implementation
- Binary Search Tree With C++ Implementation(OOP)
- 【LeetCode with Python】 Unique Binary Search Trees
- Unique Binary Search Trees 2
- LeetCode算法题目:Unique Binary Search Trees
- 算法训练:Unique Binary Search Trees
- [C++]LeetCode: 53 Unique Binary Search Trees
- [leetcode-96]Unique Binary Search Trees(c++)
- leetcode 96 c++. Unique Binary Search Trees
- 【C++】【LeetCode】96. Unique Binary Search Trees
- Binary Search Implementation
- Binary Search Tree implementation
- DP (3) -- Count Numbers with Unique Digits,Decode Ways,Unique Binary Search Trees I, II
- 5.3.2 Unique Binary Search Trees II
- Unique Binary Search Trees
- Unique Binary Search Trees
- Unique Binary Search Trees
- Unique Binary Search Trees
- jquery中事件
- android中xml文件的作用及解释
- LinearRegressionWithSGD 问题
- PAT_A 1119. Pre- and Post-order Traversals (30)
- Poj 2785 4 Values whose Sum is 0【二分】
- 算法第四版3-2Binary Search Trees With C++ Implementation
- Intent概括
- 编写简单的内核模块——Linux操作系统原理与应用(陈莉君第2版13页)
- 使用排序算法对数组进行排序,按升序 注:数组内容只会有大写字母,且可能会出现重复,不允许使用java.util下的任何类
- 优雅编程之这样使用枚举和注解,你就“正常”了(二十九)
- 弱校连萌 2016 国庆训练计划 10,5 Problem A. As Easy As Possible
- Bias和Variance
- 排序的基本概念和分类
- 洛谷 阶乘数码 阶乘之和