Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. using namespace std;
  4. struct node{
  5.     int data;
  6.     struct node *left;
  7.     struct node *right;
  8. };
  9.  
  10. typedef struct node node;
  11.  
  12. void _init(node **T){
  13.     *T = NULL;
  14. }
  15.  
  16. void insert(int value, node **T){
  17.     if (*T == NULL) {
  18.         *T = (node *) malloc(sizeof(node));
  19.         (*T)->data = value;
  20.         printf ("%d  \n", (*T)->data);
  21.         (*T)->left = NULL;
  22.         (*T)->right = NULL;
  23.     }
  24.     else if (value < (*T)->data)
  25.         insert(value, &(*T)->left);
  26.     else if (value > (*T)->data)
  27.         insert(value, &(*T)->right);
  28. }
  29.  
  30. int search(int value, node **T){
  31.     if (*T == NULL)
  32.         return 0;
  33.     else if (value < (*T)->data)
  34.         search(value, &(*T)->left);
  35.     else if (value > (*T)->data)
  36.         search(value, &(*T)->right);
  37.     return 1;
  38. }
  39.  
  40. node* remove_node(int value, node *T){
  41.     node *save;
  42.     //we need to create a copy to save the info
  43.     if (T = NULL)
  44.         printf("%s \n", "Number not found" );
  45.     //First we check to see if we are at the right point for deletion
  46.     if (T->data == value) {
  47.         //IF we are removing a leaf
  48.         if ( ( T->right == NULL) && ( (T->left == NULL)) ) {
  49.             free(T);//free the node
  50.             return NULL;
  51.         }
  52.         //Removing degree 1 branch, (only one child)
  53.         else if ( ( T->right == NULL) || ( (T->left == NULL)) ){
  54.             if(T->right == NULL) {
  55.                 save = T->left;
  56.                 free(T);
  57.                 return save;
  58.             }
  59.             else {
  60.                 save = T->right;
  61.                 free(T);
  62.                 return save;
  63.             }
  64.         }
  65.  
  66.         //if ( ( (*T)->right != NULL) && ( ((*T)->left != NULL)) ) //two children
  67.  
  68.     }
  69.  
  70.     //If not, find the proper location in the code
  71.     else if (value < T->data)
  72.         remove_node(value, T->left);
  73.     else if (value > T->data)
  74.         remove_node(value, T->right);
  75.  
  76. }
  77.  
  78.  
  79. void print_ordered(node **T){
  80.     if (*T == NULL)
  81.         return;
  82.     print_ordered(&(*T)->left);
  83.     printf("%d    \n", (*T)->data);
  84.     print_ordered(&(*T)->right);
  85. }
  86.  
  87. int main(){
  88.     node *T;
  89.     _init(&T);
  90.  
  91.     //populate tree
  92.     insert(5,&T);
  93.     insert(24,&T);
  94.     insert(19,&T);
  95.     insert(17, &T);
  96.     insert(93, &T);
  97.  
  98.     printf("%s ", "Search Result: ");
  99.     printf("%d   \n",search(5,&T));
  100.     printf("%s \n", "In Order:  ");
  101.     print_ordered(&T);
  102.  
  103.     //remove element
  104.     remove_node(2, T);
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement