您的位置:首页 > 编程语言 > C语言/C++

算法第四版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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: