Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct Node {
- int key;
- Node* right;
- Node* left;
- Node() {
- key = 0;
- left = NULL;
- right = NULL;
- }
- Node(int x) {
- key = x;
- left = NULL;
- right = NULL;
- }
- };
- struct Tree {
- Node* root;
- Tree() {
- root = NULL;
- }
- };
- void insertkey(int x, Node*& y) {
- if (y == NULL) {
- y = new Node;
- y->key = x;
- y->right = NULL;
- y->left = NULL;
- }
- else if (x > y->key) {
- insertkey(x, y->right);
- }
- else if (x < y->key) {
- insertkey(x, y->left);
- }
- }
- Node* rightDelete(int x, Node* y) {
- if (y == NULL) {
- return NULL;
- }
- else if (x < y->key) {
- y->left = rightDelete(x, y->left);
- return y;
- }
- else if (x > y->key) {
- y->right = rightDelete(x, y->right);
- return y;
- }
- if (y->left == NULL) {
- return y->right;
- }
- else if (y->right == NULL) {
- return y->left;
- }
- else {
- int tmp = findMin(y->right)->key;
- y->key = tmp;
- y->right = rightDelete(tmp, y->right);
- return y;
- }
- }
- Node* findMin(Node* y) {
- while (y->left != NULL) {
- y = y->left;
- }
- return y;
- }
- void straightleftbypass(Node *&t, ofstream& out) {
- if (t != NULL) {
- out << t->key << endl;
- straightleftbypass(t->left, out);
- straightleftbypass(t->right, out);
- }
- }
- int main() {
- ifstream in("input.txt");
- ofstream out("output.txt");
- Node* tree = NULL;
- int num = 0;
- int delnum = 0;
- in >> delnum;
- if (in.is_open()) {
- while (in >> num) {
- insertkey(num, tree);
- }
- in.close();
- }
- rightDelete(delnum, tree);
- straightleftbypass(tree, out);
- out.close();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement