mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Add deletion function
This commit is contained in:
parent
7698ff7b91
commit
e34368fcb6
@ -1,7 +1,3 @@
|
|||||||
/**********************
|
|
||||||
author: shrutisheoran
|
|
||||||
***********************/
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
@ -18,6 +14,8 @@ int max(int a, int b) {
|
|||||||
return a > b ? a : b;
|
return a > b ? a : b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a new Node
|
||||||
|
|
||||||
node* createNode(int data) {
|
node* createNode(int data) {
|
||||||
node *nn = new node();
|
node *nn = new node();
|
||||||
nn->data = data;
|
nn->data = data;
|
||||||
@ -28,6 +26,7 @@ node* createNode(int data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns height of tree
|
// Returns height of tree
|
||||||
|
|
||||||
int height(node *root) {
|
int height(node *root) {
|
||||||
if(root==NULL)
|
if(root==NULL)
|
||||||
return 0;
|
return 0;
|
||||||
@ -35,13 +34,13 @@ int height(node *root) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns difference between height of left and right subtree
|
// Returns difference between height of left and right subtree
|
||||||
|
|
||||||
int getBalance(node *root) {
|
int getBalance(node *root) {
|
||||||
return height(root->left) - height(root->right);
|
return height(root->left) - height(root->right);
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************
|
// Returns Node after Right Rotation
|
||||||
Returns Node after Right Rotation
|
|
||||||
*************************************/
|
|
||||||
node* rightRotate(node *root) {
|
node* rightRotate(node *root) {
|
||||||
node *t = root->left;
|
node *t = root->left;
|
||||||
node *u = t->right;
|
node *u = t->right;
|
||||||
@ -50,9 +49,8 @@ node* rightRotate(node *root) {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************
|
// Returns Node after Left Rotation
|
||||||
Returns Node after Left Rotation
|
|
||||||
*************************************/
|
|
||||||
node* leftRotate(node *root) {
|
node* leftRotate(node *root) {
|
||||||
node *t = root->right;
|
node *t = root->right;
|
||||||
node *u = t->left;
|
node *u = t->left;
|
||||||
@ -61,9 +59,16 @@ node* leftRotate(node *root) {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************
|
// Returns node with minimum value in the tree
|
||||||
Balanced Insertion
|
|
||||||
************************/
|
node* minValue(node* root) {
|
||||||
|
if(root->left==NULL)
|
||||||
|
return root;
|
||||||
|
return minValue(root->left);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Balanced Insertion
|
||||||
|
|
||||||
node* insert(node* root, int item) {
|
node* insert(node* root, int item) {
|
||||||
node *nn = createNode(item);
|
node *nn = createNode(item);
|
||||||
if(root == NULL)
|
if(root == NULL)
|
||||||
@ -86,9 +91,42 @@ node* insert(node* root, int item) {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************
|
// Balanced Deletion
|
||||||
LevelOrder (Breadth First Search)
|
|
||||||
*************************************/
|
node* deleteNode(node *root, int key) {
|
||||||
|
if(root == NULL)
|
||||||
|
return root;
|
||||||
|
if(key < root->data)
|
||||||
|
root->left = deleteNode(root->left, key);
|
||||||
|
else if(key > root->data)
|
||||||
|
root->right = deleteNode(root->right, key);
|
||||||
|
|
||||||
|
else {
|
||||||
|
// Node to be deleted is leaf node or have only one Child
|
||||||
|
if(!root->right) {
|
||||||
|
node* temp = root->left;
|
||||||
|
delete(root);
|
||||||
|
root = NULL;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
else if(!root->left) {
|
||||||
|
node* temp = root->right;
|
||||||
|
delete(root);
|
||||||
|
root = NULL;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
// Node to be deleted have both left and right subtrees
|
||||||
|
node *temp = minValue(root->right);
|
||||||
|
root->data = temp->data;
|
||||||
|
root->right = deleteNode(root->right, temp->data);
|
||||||
|
}
|
||||||
|
// Balancing Tree after deletion
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// LevelOrder (Breadth First Search)
|
||||||
|
|
||||||
void levelOrder(node* root) {
|
void levelOrder(node* root) {
|
||||||
queue<node*> q;
|
queue<node*> q;
|
||||||
q.push(root);
|
q.push(root);
|
||||||
@ -104,12 +142,18 @@ void levelOrder(node* root) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
node *root = NULL;
|
|
||||||
// Testing AVL Tree
|
// Testing AVL Tree
|
||||||
|
node *root = NULL;
|
||||||
int i;
|
int i;
|
||||||
for(i = 1 ; i <= 7 ; i++)
|
for(i = 1 ; i <= 7 ; i++)
|
||||||
root = insert(root, i);
|
root = insert(root, i);
|
||||||
cout<<"LevelOrder: ";
|
cout<<"LevelOrder: ";
|
||||||
levelOrder(root);
|
levelOrder(root);
|
||||||
|
root = deleteNode(root, 1); // Deleting key with value 1
|
||||||
|
cout<<"\nLevelOrder: ";
|
||||||
|
levelOrder(root);
|
||||||
|
root = deleteNode(root, 4); // Deletin key with value 4
|
||||||
|
cout<<"\nLevelOrder: ";
|
||||||
|
levelOrder(root);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user