Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <math.h>
- #include <string>
- #include <algorithm>
- #include <set>
- using namespace std;
- struct node
- {
- int data;
- node* left;
- node* right;
- };
- node* newnode(int data)
- {
- node* newnode = new node;
- newnode->data = data;
- newnode->left = NULL;
- newnode->right = NULL;
- return(newnode);
- }
- void addright(node* node, int data)
- {
- node->right = newnode(data);
- }
- void addleft(node* node, int data)
- {
- node->left = newnode(data);
- }
- int j = 1;
- void addtreesme(node* node, int i, int data[])
- {
- if (j <= i)
- if (node->left == NULL)
- {
- addleft(node, data[j]);
- j++;
- }
- if (j <= i)
- if (node->right == NULL)
- {
- addright(node, data[j]);
- j++;
- }
- if (j <= i)
- addtreesme(node->left, j, data);
- }
- void addtreesset(node* node, int data)
- {
- if (node->data < data)
- if (node->right != NULL)
- addtreesset(node->right, data);
- else
- addright(node, data);
- else
- if (node->data > data)
- if (node->left != NULL)
- addtreesset(node->left, data);
- else
- addleft(node, data);
- }
- void print(node* node)
- {
- cout << node->data << ' ';
- if (node->left != NULL)
- {
- cout << endl;
- print(node->left);
- }
- if (node->right != NULL)
- print(node->right);
- }
- node* findleft(node* root)
- {
- if (root != NULL)
- {
- if (root->left != NULL)
- findleft(root->left);
- else
- return root;
- }
- else
- return root;
- }
- node* findperent(node* child, int v)
- {
- node* root = new node;
- root = child;
- if (v > root->data)
- if (root->right != NULL)
- if (root->right->data == v)
- return root;
- else
- return findperent(root->right, v);
- else
- return NULL;
- else
- if (root->left != NULL)
- if (root->left->data == v)
- return root;
- else
- return findperent(root->left, v);
- else
- if (root-> data == v)
- return root;
- else
- return NULL;
- }
- node* deletenode(node* root, int v)
- {
- node* perent = new node;
- node* child = new node;
- node* prom = new node;
- node* prom2 = new node;
- int i = 0;
- if (root->data != v)
- {
- perent = findperent(root, v);
- if (perent == NULL)
- {
- return root;
- }
- else
- if (perent->left != NULL&&perent->left->data == v)
- if (perent->left->left == NULL&&perent->left->right != NULL)
- {
- child = perent->left;
- perent->left = child->right;
- }
- else
- if (perent->left->left != NULL&&perent->left->right == NULL)
- {
- child = perent->left;
- perent->left = child->left;
- }
- else
- if (perent->left->left == NULL&&perent->left->right == NULL)
- {
- child = perent->left;
- perent->left = NULL;
- }
- else
- {
- child = perent->left;
- prom = findleft(child->right);
- prom2 = findperent(root, prom->data);
- if (prom2->left != NULL&&prom2->left->data == prom->data)
- prom2->left = NULL;
- else
- prom2->right = NULL;
- perent->left = prom;
- prom->left = child->left;
- prom->right = child->right;
- }
- else
- if (perent->right != NULL&&perent->right->data == v)
- if (perent->right->left == NULL&&perent->right->right != NULL)
- {
- child = perent->right;
- perent->right = child->right;
- }
- else
- if (perent->right->left != NULL&&perent->right->right == NULL)
- {
- child = perent->right;
- perent->right = child->left;
- }
- else
- if (perent->right->left == NULL&&perent->right->right == NULL)
- {
- child = perent->right;
- perent->right = NULL;
- }
- else
- {
- child = perent->right;
- prom = findleft(child->right);
- prom2 = findperent(root, prom->data);
- if (prom2->left != NULL&&prom2->left->data == prom->data)
- prom2->left = NULL;
- else
- prom2->right = NULL;
- perent->right = prom;
- prom->left = child->left;
- prom->right = child->right;
- }
- else
- return root;
- }
- else
- {
- if (root->left == NULL&& root->right != NULL)
- root = root->right;
- else
- if (root->left != NULL&& root->right == NULL)
- root = root->right;
- else
- if (root->left != NULL&& root->right != NULL)
- {
- child = root;
- prom = findleft(child->right);
- prom2 = findperent(root, prom->data);
- if (prom2->left != NULL&&prom2->left->data == prom->data)
- prom2->left = NULL;
- else
- prom2->right = NULL;
- prom->left = child->left;
- prom->right = child->right;
- root = prom;
- }
- else
- {
- delete root;
- i++;
- }
- }
- delete child;
- if (i == 0)
- return root;
- else
- return NULL;
- }
- node* find(node* root, int data)
- {
- if (root->data = data)
- return root;
- if (root == NULL)
- return NULL;
- if (root->data < data)
- return find(root->left, data);
- else
- return find(root->right, data);
- }
- using namespace ::std;
- int main()
- {
- int data[4] = { 0,9,6,11 };
- node* t = new node;
- t = newnode(0);
- addtreesset(t, data[1]);
- addtreesset(t, data[2]);
- addtreesset(t, data[3]);
- addtreesset(t, -2);
- t=deletenode(t, 0);
- print(t);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement