#include <iostream>
//#include <cstdlib>
#include <stdlib.h>
class Node {
private:
int data;
Node *left, *right;
public:
static void push(Node *&root, int data) {
Node *p;
if (!root) {
p = new Node();
p->data = data;
p->left = p->right = 0;
root = p;
return;
}
if (data < root->data)
push(root->left, data);
else
push(root->right, data);
}
static bool pop(Node *&root, int &data) {
Node *p;
if (root == 0)
return false;
if (root->left) {
return pop(root->left, data);
}
p = root;
data = p->data;
root = p->right;
delete p;
return true;
}
};
class BinTree {
private:
Node *root;
public:
BinTree() { root = 0; }
void pushNode(int data) { Node::push(root, data); }
bool popNode(int &data) { return Node::pop(root, data); }
};
int main() {
const int n = 10;
const int max = 1000;
const int seed = 31415926;
int data;
BinTree *obj = new BinTree();
srand(seed);
for (int i = 0; i < n; i++) {
data = rand() % max;
obj->pushNode(data);
}
std::cout << "C++(2)" << std::endl;
while (obj->popNode(data))
std::cout << data << std::endl;
delete obj;
return 0;
}
/* end */
I2luY2x1ZGUgPGlvc3RyZWFtPgovLyNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8c3RkbGliLmg+CgpjbGFzcyBOb2RlIHsKcHJpdmF0ZToKICBpbnQgZGF0YTsKICBOb2RlICpsZWZ0LCAqcmlnaHQ7CnB1YmxpYzoKICBzdGF0aWMgdm9pZCBwdXNoKE5vZGUgKiZyb290LCBpbnQgZGF0YSkgewogICAgTm9kZSAqcDsKICAgIGlmICghcm9vdCkgewogICAgICBwID0gbmV3IE5vZGUoKTsKICAgICAgcC0+ZGF0YSA9IGRhdGE7CiAgICAgIHAtPmxlZnQgPSBwLT5yaWdodCA9IDA7CiAgICAgIHJvb3QgPSBwOwogICAgICByZXR1cm47CiAgICB9IAogICAgaWYgKGRhdGEgPCByb290LT5kYXRhKQogICAgICBwdXNoKHJvb3QtPmxlZnQsIGRhdGEpOwogICAgZWxzZQogICAgICBwdXNoKHJvb3QtPnJpZ2h0LCBkYXRhKTsKICB9CiAgc3RhdGljIGJvb2wgcG9wKE5vZGUgKiZyb290LCBp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