Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <iterator>
- using namespace std;
- int maxPath;
- struct Node
- {
- int countOfLeaves;
- int maxHeight;
- int maxPathsRoot;
- int maxPathVert;
- int key;
- Node * left;
- Node * right;
- Node(int k): left(NULL), right(NULL), key(k), countOfLeaves(0), maxHeight(0), maxPathsRoot(0),maxPathVert(0) {}
- Node(int k, Node * l, Node * r): left(l), right(r), key(k) {}
- Node(): left(NULL), right(NULL), countOfLeaves(0), maxHeight(0), maxPathsRoot(0),maxPathVert(0) {}
- };
- Node * root;
- bool start = true;
- int numberToDelete = 0;
- void addNode(int key)
- {
- if (start)
- {
- start = false;
- root->key = key;
- return;
- }
- Node * curr = root;
- while (true)
- {
- if (curr->key == key) return;
- if (curr->left != NULL && key < curr->key)
- {
- curr = curr->left;
- continue;
- }
- if (curr->right != NULL && key > curr->key)
- {
- curr = curr->right;
- continue;
- }
- if (key < curr->key)
- {
- Node * leave = new Node(key);
- curr->left = leave;
- return;
- }
- if (key > curr->key)
- {
- Node * leave = new Node(key);
- curr->right = leave;
- return;
- }
- }
- }
- void printTree(Node * root)
- {
- //cout << "Key: " << root->key << " MaxPath : " << root -> maxPathVert << " MaxRoot: " << root ->maxPathsRoot << endl;
- //cout << "Key: " << root->key << endl;
- cout << root->key << endl;
- if (root->left != NULL)
- printTree(root ->left);
- if (root->right != NULL)
- printTree(root -> right);
- }
- void fillHeight(Node * root)
- {
- int l = -1;
- int r = -1;
- int countL = 0; int countR = 0;
- if (root -> left != NULL)
- {
- fillHeight(root->left);
- l = (root -> left) -> maxHeight;
- countL = (root -> left) -> countOfLeaves;
- }
- if (root -> right != NULL)
- {
- fillHeight(root->right);
- r = (root -> right) -> maxHeight;
- countR = (root -> right) -> countOfLeaves;
- }
- if (root -> right == NULL && root->left ==NULL)
- {
- root -> countOfLeaves = 1;
- }
- root -> maxHeight = max(l, r) + 1;
- if (l == root -> maxHeight - 1)
- root ->countOfLeaves += countL;
- if (r == root -> maxHeight - 1)
- root ->countOfLeaves += countR;
- if (maxPath < l + r + 2)
- {
- maxPath = l + r + 2;
- }
- }
- void findMaxRoots(Node * root)
- {
- int l = -1;
- int r = -1;
- int countL = 1; int countR = 1;
- if (root -> left != NULL)
- {
- findMaxRoots(root->left);
- l = (root -> left) -> maxHeight;
- countL = (root -> left) -> countOfLeaves;
- }
- if (root -> right != NULL)
- {
- findMaxRoots(root->right);
- r = (root -> right) -> maxHeight;
- countR = (root -> right) -> countOfLeaves;
- }
- if (l + r + 2 == maxPath)
- {
- root -> maxPathsRoot = countL * countR;
- return;
- }
- root ->maxPathsRoot = 0;
- }
- void findPathsfromFather(Node * root)
- {
- int l = -1;
- int r = -1;
- int countL = 1; int countR = 1;
- if (root -> left != NULL)
- {
- //findPathsfromFather(root->left);
- l = (root -> left) -> maxHeight;
- countL = (root -> left) -> countOfLeaves;
- }
- if (root -> right != NULL)
- {
- //findPathsfromFather(root->right);
- r = (root -> right) -> maxHeight;
- countR = (root -> right) -> countOfLeaves;
- }
- if (l > r)
- {
- (root -> left)-> maxPathVert = root -> maxPathsRoot + root -> maxPathVert;
- if (r != -1) (root -> right)-> maxPathVert = root -> maxPathsRoot;
- }
- if (l < r)
- {
- (root -> right)-> maxPathVert = root -> maxPathsRoot + root -> maxPathVert;
- if (l != -1) (root -> left)-> maxPathVert = root -> maxPathsRoot;
- }
- if (l == r && l != -1)
- {
- (root -> right)-> maxPathVert = root -> maxPathsRoot + (root ->maxPathVert * countR) / (countL + countR);
- (root -> left)-> maxPathVert = root -> maxPathsRoot + (root ->maxPathVert * countL) / (countL + countR);
- }
- if (root -> left != NULL)
- {
- findPathsfromFather(root->left);
- }
- if (root -> right != NULL)
- {
- findPathsfromFather(root->right);
- }
- }
- int countGoodVert(Node * elem)
- {
- if (elem == NULL) return 0;
- int ans = 0;
- if ((elem ->maxPathsRoot + elem -> maxPathVert) % 2 == 0
- && elem ->maxPathsRoot + elem ->maxPathVert >0)
- ans = 1;
- return ans + countGoodVert( elem -> left) + countGoodVert(elem -> right);
- }
- void deleteNode(Node * elem)
- {
- int countOfChildren = 0;
- if (elem ->left != NULL)
- countOfChildren++;
- if (elem ->right != NULL)
- countOfChildren++;
- if (countOfChildren == 0)
- {
- if (root == elem)
- {
- root = NULL;
- return;
- }
- Node * pre = root;
- while (true)
- {
- if (pre->left == elem) break;
- if (pre->right == elem) break;
- if (elem->key > pre-> key)
- {
- pre = pre-> right;
- continue;
- }
- if (elem->key < pre-> key)
- {
- pre = pre-> left;
- continue;
- }
- }
- if (pre->left == elem) pre->left = NULL;
- else pre->right = NULL;
- return;
- }
- if (countOfChildren == 1)
- {
- Node * post;
- if (elem->left == NULL)
- post = elem -> right;
- else post = elem -> left;
- int k = elem -> key;
- elem-> key = post->key;
- post->key = k;
- Node * x = elem -> left;
- elem -> left = post->left;
- post ->left = x;
- x = elem -> right;
- elem-> right = post->right;
- post->right = x;
- return;
- }
- Node * curr = elem;
- curr = curr ->right;
- if (curr->left == NULL)
- {
- int k = elem->key;
- elem->key = curr->key;
- curr->key = k;
- elem->right = curr->right;
- return;
- }
- Node * pre = curr;
- curr = curr->left;
- while (curr->left != NULL)
- {
- curr = curr->left;
- pre = pre->left;
- }
- int k = elem->key;
- elem->key = curr->key;
- curr->key = k;
- pre->left = curr->right;
- }
- /*void deleteNode(Node * root)
- {
- Node * curr = root;
- if (curr -> right == NULL)
- {
- if (curr ->parent != NULL) curr->parent = NULL;
- return;
- }
- curr = curr ->right;
- if (curr->left == NULL)
- {
- int k = root->key;
- root->key = curr->key;
- curr->key = k;
- root->right = curr->right;
- return;
- }
- Node * pre = curr;
- curr = curr->left;
- while (curr->left != NULL)
- {
- curr = curr->left;
- pre = pre->left;
- }
- int k = root->key;
- root->key = curr->key;
- curr->key = k;
- pre->left = curr->right;
- }
- */
- void findToDelete(Node * elem)
- {
- if (elem == NULL) return;
- findToDelete(elem -> left);
- if ((elem ->maxPathsRoot + elem -> maxPathVert) % 2 == 0
- && elem ->maxPathsRoot + elem ->maxPathVert >0)
- numberToDelete--;
- if (numberToDelete == 0)
- {
- deleteNode(elem);
- numberToDelete = -10000;
- }
- findToDelete(elem -> right);
- }
- int main()
- {
- freopen("tst.in", "r", stdin);
- freopen("tst.out", "w", stdout);
- root = new Node();
- int x;
- while (scanf("%d", &x) != -1)
- {
- addNode(x);
- }
- fillHeight(root);
- if (maxPath == 0)
- {
- printTree(root);
- return 0;
- }
- findMaxRoots(root);
- findPathsfromFather(root);
- int goodVert = countGoodVert(root);
- if (goodVert % 2 == 0)
- {
- printTree(root);
- return 0;
- }
- numberToDelete = (goodVert + 1) / 2;
- findToDelete(root);
- printTree(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement