Advertisement
Alan468

BinaryTree

Apr 26th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.43 KB | None | 0 0
  1. /// <title>Drzewa Binarne</title>
  2. /// <author>Alan Biegun</author>
  3. /// <date>25-04-2016 14:45:20</date>
  4. /// <informations>Dodatkowe strony</informations>
  5. /// <site>http://visualgo.net/bst</site>
  6. /// Create -> Empty , Instert i wklej to
  7. /// 55, 69, 44, 12, 2, 3, 4, 8, 7, 6, 98, 45, 74 ,18 ,57 ,52 ,53 ,66 ,99 ,88 ,77 ,11, 22, 54 ,21 ,91 ,23, 24
  8.  
  9. #include <conio.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12.  
  13. struct Tree {
  14.     int iValue;
  15.     Tree *tLeft, *tRight;
  16. };
  17.  
  18. Tree *NewTree(int iNewValue) {
  19.     Tree *newTree = (Tree *)malloc(sizeof(Tree));
  20.     newTree->iValue = iNewValue;
  21.     newTree->tLeft = NULL;
  22.     newTree->tRight = NULL;
  23.     return newTree;
  24. }
  25.  
  26. Tree *Insert(Tree *tTree, int iNewValue) {
  27.     if (tTree == NULL) {
  28.         tTree = NewTree(iNewValue);
  29.         return tTree;
  30.     }else if (iNewValue <= tTree->iValue) {
  31.         tTree->tLeft = Insert(tTree->tLeft, iNewValue);
  32.     }
  33.     else{
  34.         tTree->tRight = Insert(tTree->tRight, iNewValue);
  35.     }
  36.     return tTree;
  37. }
  38.  
  39. bool SearchForValue(Tree *tTree, int iValue) {
  40.     if (tTree == NULL) {
  41.         return false;
  42.     }
  43.     else if (tTree->iValue == iValue) {
  44.         return true;
  45.     }
  46.     else if (iValue <= tTree->iValue) {
  47.         return SearchForValue(tTree->tLeft, iValue);
  48.     }
  49.     else {
  50.         return SearchForValue(tTree->tRight, iValue);
  51.     }
  52. }
  53.  
  54. Tree *FindMin(Tree* tTree){
  55.     while (tTree->tLeft != NULL)
  56.         tTree = tTree->tLeft;
  57.     return tTree;
  58. }
  59.  
  60. Tree *FindMax(Tree* tTree) {
  61.     while (tTree->tRight != NULL)
  62.         tTree = tTree->tRight;
  63.     return tTree;
  64. }
  65.  
  66. Tree *Delete(Tree *tTree, int iValue) {
  67.     if (tTree == NULL)
  68.         return tTree;
  69.     else if (iValue < tTree->iValue)
  70.         tTree->tLeft = Delete(tTree->tLeft, iValue);
  71.     else if (iValue > tTree->iValue)
  72.         tTree->tRight = Delete(tTree->tRight, iValue);
  73.  
  74.     // Wohoo... I found you, Get ready to be deleted   
  75.     else {
  76.         // Case 1:  No child
  77.         if (tTree->tLeft == NULL && tTree->tRight == NULL) {
  78.             free(tTree);
  79.             tTree = NULL;
  80.         }
  81.         //Case 2: One child
  82.         else if (tTree->tLeft == NULL) {
  83.             Tree *temp = tTree;
  84.             tTree = tTree->tRight;
  85.             free(tTree);
  86.         }
  87.         else if (tTree->tRight == NULL) {
  88.             Tree *temp = tTree;
  89.             tTree = tTree->tLeft;
  90.             free(tTree);
  91.         }
  92.         // case 3: 2 children
  93.         else {
  94.             Tree *temp = FindMin(tTree->tRight);
  95.             tTree->iValue = temp->iValue;
  96.             tTree->tRight = Delete(tTree->tRight, temp->iValue);
  97.         }
  98.     }
  99.     return tTree;
  100. }
  101.  
  102. void Delete(Tree *tTree) {
  103.     if (tTree == NULL) return;
  104.  
  105.     Delete(tTree->tLeft);    
  106.     Delete(tTree->tRight);
  107.     delete(tTree);
  108. }
  109.  
  110. void PrintAscendingOrder(Tree *tTree) {
  111.     if (tTree == NULL) return;
  112.  
  113.     PrintAscendingOrder(tTree->tLeft);         // Visit left subtree
  114.     printf("%d ", tTree->iValue);             // Print data
  115.     PrintAscendingOrder(tTree->tRight);      // Visit right subtree
  116. }
  117.  
  118. void PrintDescendingOrder(Tree *tTree) {
  119.     if (tTree == NULL) return;
  120.  
  121.     PrintDescendingOrder(tTree->tRight);      // Visit right subtree
  122.     printf("%d ", tTree->iValue);             // Print data
  123.     PrintDescendingOrder(tTree->tLeft);         // Visit left subtree
  124. }
  125.  
  126. Tree *FillUpTree(Tree *tTree) {
  127.     FILE *fFile = fopen("plik.txt", "r");
  128.    
  129.     int max ,Number;
  130.     fscanf(fFile ,"%d", &max);
  131.  
  132.     for (int i = 0; i < max; i++) {
  133.         fscanf(fFile, "%d", &Number);
  134.         tTree = Insert(tTree, Number);
  135.     }
  136.     return tTree;
  137. }
  138.  
  139. void PrintMax(Tree* tTree) {
  140.     Tree *Max = FindMax(tTree);
  141.     printf("%d\n", Max->iValue);
  142. }
  143.  
  144. void PrintMin(Tree* tTree) {
  145.     Tree *Min = FindMin(tTree);
  146.     printf("%d\n", Min->iValue);
  147. }
  148.  
  149. int Height(Tree* tTree) {
  150.     if (!tTree) return 0;
  151.  
  152.     int left_height = Height(tTree->tLeft);
  153.     int right_height = Height(tTree->tRight);
  154.     return (left_height > right_height) ? left_height + 1 : right_height + 1;
  155. }
  156.  
  157. int main(){
  158.     Tree *tTree = NULL; // Drzewo
  159.  
  160.     tTree = FillUpTree(tTree); // Wczytywanie z pliku listy elementow
  161.  
  162.     printf("\n");
  163.     PrintAscendingOrder(tTree); // Wyswietlenie drzewa w ciągu rosnącym
  164.     printf("\n\n");
  165.     PrintDescendingOrder(tTree); // Wyswietlenie drzewa w ciągu malejącym
  166.     printf("\n\n");
  167.  
  168.     printf("Max: ");
  169.     PrintMax(tTree); // Maksymalny elemenent drzewa
  170.     printf("\n");
  171.  
  172.     printf("Min: ");
  173.     PrintMin(tTree); // Minimalny element drzewa
  174.     printf("\n");
  175.    
  176.     printf("Wysokosc: %d\n", Height(tTree));    // Wysokosc drzewa
  177.  
  178.     int number; // Szukanie danego elementu w drzewie
  179.  
  180.     printf("Enter number be searched: ");
  181.     scanf("%d", &number);
  182.  
  183.     if (SearchForValue(tTree, number) == true)
  184.         printf("Found\n");
  185.     else
  186.         printf("Not Found\n");
  187.  
  188.     printf("\n");
  189.  
  190.     Delete(tTree); // Usowanie drzewa
  191.  
  192.     _getch();
  193.     return 0;
  194. }
  195. //AB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement