Last active
August 1, 2018 09:38
-
-
Save vinitshahdeo/0957439a267d28664cade39cd740783e to your computer and use it in GitHub Desktop.
Trees - Data Structures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Node* findMin(Node* root) | |
{ | |
while(root->left!=NULL) | |
root=root->left; | |
return root; | |
} | |
Node * deleteNode(Node *root, int x) | |
{ | |
if(root==NULL) | |
return root; | |
else if(root->data>x) | |
{ | |
root->left=deleteNode(root->left,x); | |
} | |
else if(root->data<x) | |
{ | |
root->right=deleteNode(root->right,x); | |
} | |
else | |
{ | |
if(root->left==NULL && root->right==NULL) | |
{ | |
delete root; | |
root=NULL; | |
} | |
else if(root->left==NULL) | |
{ | |
Node *temp=root; | |
root=temp->right; | |
delete temp; | |
} | |
else if(root->right==NULL) | |
{ | |
Node *temp=root; | |
root=root->left; | |
delete temp; | |
} | |
else | |
{ | |
Node *temp=findMin(root->right); | |
root->data=temp->data; | |
root->right=deleteNode(root->right,temp->data); | |
} | |
} | |
return root; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct node* insert(struct node* node, int data) | |
{ | |
if(node==NULL) | |
{ | |
node=newNode(data); | |
} | |
else if(node->data<data) | |
{ | |
node->right=insert(node->right,data); | |
} | |
else if(node->data>data) | |
{ | |
node->left=insert(node->left,data); | |
} | |
return node; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment