Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <ctime>
- #include <queue>
- using namespace std;
- template<class T>
- int depth_of_tree( T* root ){
- if( root == 0 )
- return 0;
- int left = 0;
- int right = 0;
- if( root->left != nullptr ) {
- left = depth_of_tree(root->left);
- } else {
- left = -1;
- }
- if( root->right != nullptr ) {
- right = depth_of_tree(root->right);
- } else {
- right = -1;
- }
- int max = left > right ? left : right;
- return max + 1;
- }
- struct DecartTree{
- int key;
- int prior;
- DecartTree* left;
- DecartTree* right;
- DecartTree():
- key(0),
- prior(0),
- left( nullptr ),
- right( nullptr )
- {}
- };
- struct NativeTree{
- int key;
- NativeTree* left;
- NativeTree* right;
- void add( int data );
- NativeTree():
- key(0),
- left( nullptr ),
- right( nullptr )
- {}
- };
- template <class T>
- void BuildTree( T* root, int K ) {
- if( root->key <= K ) {
- if(root->right == 0) {
- root->right = new T;
- root->right->key = K;
- } else {
- BuildTree( root->right, K );
- }
- } else {
- if( root->left == 0 ) {
- root->left = new T;
- root->left->key = K;
- } else {
- BuildTree( root->left, K );
- }
- }
- }
- template <class T>
- void deletetree( T* root ) {
- if( root == nullptr )
- return;
- deletetree( root->left );
- deletetree( root->right );
- delete root;
- };
- void split( DecartTree* root, DecartTree*& left, DecartTree*& right, int key){
- if( root == nullptr ) {
- left = nullptr;
- right = nullptr;
- } else {
- if (root->key < key) {
- split(root->right, root->right, right, key);
- left = root;
- } else {
- split(root->left, left, root->left, key);
- right = root;
- }
- }
- }
- void insert(DecartTree*& root, DecartTree* current)
- {
- if (root == nullptr)
- {
- root = current;
- return;
- }
- if (root->prior > current->prior)
- {
- if (current->key < root->key) {
- insert(root->left, current);
- return;
- } else {
- insert(root->right, current);
- return;
- }
- }
- split(root, current->left, current->right, current->key);
- root = current;
- }
- int main() {
- int n = 0;
- int xkey = 0;
- int prior = 0;
- DecartTree* root = new DecartTree;
- NativeTree* tree = new NativeTree;
- DecartTree* current = new DecartTree;
- cin >> n;
- cin >> xkey >> prior;
- tree->key = prior;
- root->key = xkey;
- root->prior = prior;
- for (int i = 1; i < n; i++){
- cin >> xkey >> prior;
- current->key = xkey;
- current->prior = prior;
- insert(root, current);
- BuildTree(tree, prior);
- }
- cout << depth_of_tree(tree) <<" "<< depth_of_tree(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement