Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Data {
- int id;
- char name[];
- }Data;
- typedef struct Node{
- Data data;
- struct Node *left, *right;
- }Node;
- typedef Node Tree;
- Node * newNode(int id){
- Node* node = malloc(sizeof(Node));
- node->data.id = id;
- node->left = node->right = NULL;
- return node;
- }
- Node * addRight(Node *root,int id){
- if(root) {
- Node* new = newNode(id);
- root->right = new;
- return new;
- }
- return NULL;
- }
- Node * addLeft(Node *root,int id){
- if(root) {
- Node* new = newNode(id);
- root->left = new;
- return new;
- }
- return NULL;
- }
- // START OF TRAVERSING FUNCTIONS.
- void preTravers(Tree *root){
- if(root) {
- printf("[%d]\n", root->data.id);
- preTravers(root->left);
- preTravers(root->right);
- }
- }
- void postTravers(Tree *root){
- if(root) {
- postTravers(root->left);
- postTravers(root->right);
- printf("[%d]\n", root->data.id);
- }
- }
- void inTravers(Tree *root){
- if(root) {
- inTravers(root->left);
- printf("[%d]\n", root->data.id);
- inTravers(root->right);
- }
- }
- // END OF TRAVERSING FUNCTIONS.
- // START SHOWING FUNCTIONS.
- void inShow(Tree *root,int currentDepth){
- if(root) {
- inShow(root->right,currentDepth+1);
- int i; for(i=0;i<currentDepth;i++) printf("\t");
- printf("%3d::%p ", root->data.id,root);
- inShow(root->left,currentDepth+1);
- }else
- printf("\n");
- }
- // END SHOWING FUNCTIONS.
- // START OF INSERTING IN BST FUNCTION.
- void insert(Tree *root,int id){
- Node * node = newNode(id);
- if(root) {
- if(id < root->data.id ){
- // GO LEFT
- // if there is no left node
- if(!root->left)
- root->left = node;
- else // there is a node insert to it
- insert(root->left,id);
- }else{
- // GO RIGHT
- // if there is no right node
- if(!root->right)
- root->right = node;
- else
- insert(root->right,id);
- }
- }
- // return node;
- }
- // END OF INSERTING IN BST FUNCTION.
- // START OF SEARCHING IN BST FUNCTION.
- Node * search (Tree *root,int id){
- if (!root)
- return NULL;
- if (root->data.id == id)
- return root;
- if (id < root->data.id)
- return search(root->left,id);
- else
- return search(root->right,id);
- }
- // END OF SEARCHING IN BST FUNCTION.
- // START OF DELETING FROM BST FUNCTION.
- void delete(Tree *root,Node *parent,Node *node){
- if(root) {
- if(root==node){ // WE ARE AT THE NODE BEGIN DELETE OPERATION
- if(!node->left && !node->right) { // Check if there are no children
- (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL);
- }else if(!node->left && node->right) { // check if there no left child for node "only right"
- (parent->left == node) ? (parent->left = node->right) : (parent->right=node->right);
- }else if(node->left && !node->right){ // check if there no right child for node "only left"
- (parent->left == node) ? (parent->left = node->left) : (parent->right=node->left );
- }
- free(node);
- }else{
- // TRYING TO REACH THE NODE
- parent = root;
- if(node->data.id < root->data.id)
- root = root->left;
- else
- root = root->right;
- delete(root,parent,node);
- }
- }
- }
- // END OF DELETING FROM BST FUNCTION.
- int main(){
- system("clear");
- printf("[NUMBER VALUE]::[ADDRESS OF NODE]\n");
- printf("\n====================================================\n");
- Tree *root = newNode(80);
- /*Tree *node1 = addLeft(root,2);
- Tree *node2 = addRight(root,10);
- addLeft(node1,1);
- addRight(addRight(node1,4),5);
- addLeft(node2,9);
- addRight(addLeft(addRight(node2,100),50),60);*/
- // preTravers(root);
- // postTravers(root);
- // inTravers(root);
- insert(root,100);
- insert(root,70);
- insert(root,50);
- insert(root,200);
- insert(root,150);
- insert(root,60);
- insert(root,90);
- insert(root,95);
- inShow(root,0);
- printf("\n====================================================\n\n");
- delete(root,root,search(root,95));
- delete(root,root,search(root,50));
- delete(root,root,search(root,90));
- delete(root,root,search(root,70));
- inShow(root,0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment