Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <title>Drzewa Binarne</title>
- /// <author>Alan Biegun</author>
- /// <date>25-04-2016 14:45:20</date>
- /// <informations>Dodatkowe strony</informations>
- /// <site>http://visualgo.net/bst</site>
- /// Create -> Empty , Instert i wklej to
- /// 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
- #include <conio.h>
- #include <stdlib.h>
- #include <stdio.h>
- struct Tree {
- int iValue;
- Tree *tLeft, *tRight;
- };
- Tree *NewTree(int iNewValue) {
- Tree *newTree = (Tree *)malloc(sizeof(Tree));
- newTree->iValue = iNewValue;
- newTree->tLeft = NULL;
- newTree->tRight = NULL;
- return newTree;
- }
- Tree *Insert(Tree *tTree, int iNewValue) {
- if (tTree == NULL) {
- tTree = NewTree(iNewValue);
- return tTree;
- }else if (iNewValue <= tTree->iValue) {
- tTree->tLeft = Insert(tTree->tLeft, iNewValue);
- }
- else{
- tTree->tRight = Insert(tTree->tRight, iNewValue);
- }
- return tTree;
- }
- bool SearchForValue(Tree *tTree, int iValue) {
- if (tTree == NULL) {
- return false;
- }
- else if (tTree->iValue == iValue) {
- return true;
- }
- else if (iValue <= tTree->iValue) {
- return SearchForValue(tTree->tLeft, iValue);
- }
- else {
- return SearchForValue(tTree->tRight, iValue);
- }
- }
- Tree *FindMin(Tree* tTree){
- while (tTree->tLeft != NULL)
- tTree = tTree->tLeft;
- return tTree;
- }
- Tree *FindMax(Tree* tTree) {
- while (tTree->tRight != NULL)
- tTree = tTree->tRight;
- return tTree;
- }
- Tree *Delete(Tree *tTree, int iValue) {
- if (tTree == NULL)
- return tTree;
- else if (iValue < tTree->iValue)
- tTree->tLeft = Delete(tTree->tLeft, iValue);
- else if (iValue > tTree->iValue)
- tTree->tRight = Delete(tTree->tRight, iValue);
- // Wohoo... I found you, Get ready to be deleted
- else {
- // Case 1: No child
- if (tTree->tLeft == NULL && tTree->tRight == NULL) {
- free(tTree);
- tTree = NULL;
- }
- //Case 2: One child
- else if (tTree->tLeft == NULL) {
- Tree *temp = tTree;
- tTree = tTree->tRight;
- free(tTree);
- }
- else if (tTree->tRight == NULL) {
- Tree *temp = tTree;
- tTree = tTree->tLeft;
- free(tTree);
- }
- // case 3: 2 children
- else {
- Tree *temp = FindMin(tTree->tRight);
- tTree->iValue = temp->iValue;
- tTree->tRight = Delete(tTree->tRight, temp->iValue);
- }
- }
- return tTree;
- }
- void Delete(Tree *tTree) {
- if (tTree == NULL) return;
- Delete(tTree->tLeft);
- Delete(tTree->tRight);
- delete(tTree);
- }
- void PrintAscendingOrder(Tree *tTree) {
- if (tTree == NULL) return;
- PrintAscendingOrder(tTree->tLeft); // Visit left subtree
- printf("%d ", tTree->iValue); // Print data
- PrintAscendingOrder(tTree->tRight); // Visit right subtree
- }
- void PrintDescendingOrder(Tree *tTree) {
- if (tTree == NULL) return;
- PrintDescendingOrder(tTree->tRight); // Visit right subtree
- printf("%d ", tTree->iValue); // Print data
- PrintDescendingOrder(tTree->tLeft); // Visit left subtree
- }
- Tree *FillUpTree(Tree *tTree) {
- FILE *fFile = fopen("plik.txt", "r");
- int max ,Number;
- fscanf(fFile ,"%d", &max);
- for (int i = 0; i < max; i++) {
- fscanf(fFile, "%d", &Number);
- tTree = Insert(tTree, Number);
- }
- return tTree;
- }
- void PrintMax(Tree* tTree) {
- Tree *Max = FindMax(tTree);
- printf("%d\n", Max->iValue);
- }
- void PrintMin(Tree* tTree) {
- Tree *Min = FindMin(tTree);
- printf("%d\n", Min->iValue);
- }
- int Height(Tree* tTree) {
- if (!tTree) return 0;
- int left_height = Height(tTree->tLeft);
- int right_height = Height(tTree->tRight);
- return (left_height > right_height) ? left_height + 1 : right_height + 1;
- }
- int main(){
- Tree *tTree = NULL; // Drzewo
- tTree = FillUpTree(tTree); // Wczytywanie z pliku listy elementow
- printf("\n");
- PrintAscendingOrder(tTree); // Wyswietlenie drzewa w ciągu rosnącym
- printf("\n\n");
- PrintDescendingOrder(tTree); // Wyswietlenie drzewa w ciągu malejącym
- printf("\n\n");
- printf("Max: ");
- PrintMax(tTree); // Maksymalny elemenent drzewa
- printf("\n");
- printf("Min: ");
- PrintMin(tTree); // Minimalny element drzewa
- printf("\n");
- printf("Wysokosc: %d\n", Height(tTree)); // Wysokosc drzewa
- int number; // Szukanie danego elementu w drzewie
- printf("Enter number be searched: ");
- scanf("%d", &number);
- if (SearchForValue(tTree, number) == true)
- printf("Found\n");
- else
- printf("Not Found\n");
- printf("\n");
- Delete(tTree); // Usowanie drzewa
- _getch();
- return 0;
- }
- //AB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement