Witam! Nie najlepiej radzę sobie z programowaniem więc proszę o cierpliwość
Mam za zadanie napisać funkcję, która znajdzie w drzewie ścieżkę o maksymalnej sumie kluczy i zwróci tę wartość jako wynik. Zostawiam moje nieudolne próby poniżej. Z góry dzięki!
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
typedef int ElType;
typedef struct Tree {
ElType key;
struct Tree *left;
struct Tree *right;
} Tree;
Tree* InsertBST(Tree* t,int k)
{
if (t == NULL) {
Tree* w = (Tree*) malloc(sizeof(Tree));
w->key = k;
w->left = NULL;
w->right = NULL;
return w;
}
if (k <= t->key)
t->left = InsertBST(t->left, k);
else
t->right = InsertBST(t->right, k);
return t;
}
int NumberOfLeaves(Tree* t)
{
if (t == NULL)
return 0;
if (t->left == NULL && t->right == NULL)
return 1;
return NumberOfLeaves(t->left) + NumberOfLeaves(t->right);
}
#define SCREEN_HEIGHT 100
#define SCREEN_WIDTH 80
typedef struct {
int numberOfLines;
char screen[SCREEN_HEIGHT][SCREEN_WIDTH+2]; // 100 linii, ka¿da linia ma 80 znaków plus \n i \0
} TreeDisplay;
/*
#define DOWN 179
#define RIGHT_DOWN 191
#define DOWN_LEFT_AND_RIGHT 193
#define LEFT_DOWN 218
#define DASH 196
*/
#define DOWN '|'
#define RIGHT_DOWN '.'
#define DOWN_LEFT_AND_RIGHT '^'
#define LEFT_DOWN '.'
#define DASH '-'
/* To find code for special characters:
for (i = 0; i < 256; i++) {
printf("%d:%c,\t", i, i);
if (i % 5 == 0 && i > 0)
printf("\n");
}
*/
void WriteSymbolToTreeDisplay(TreeDisplay* td, char c, int row, int col)
{
td->screen[row][col] = c;
if (row+1 > td->numberOfLines)
td->numberOfLines = row+1;
}
void WriteKeyToTreeDisplay(TreeDisplay* td, int key, int row, int col)
{
if (key >= 1000)
td->screen[row][col-1] = td->screen[row][col] = td->screen[row][col+1] = '.'; // za duza wartosc
else if (key >= 100) {
td->screen[row][col-1] = (key / 100) + '0';
td->screen[row][col] = ((key / 10) % 10) + '0';
td->screen[row][col+1] = (key % 10) + '0';
}
else if (key >= 10) {
td->screen[row][col] = (key / 10) + '0';
td->screen[row][col+1] = (key % 10) + '0';
}
else if (key >= 0)
td->screen[row][col] = key + '0';
else {
td->screen[row][col] = '-';
td->screen[row][col+1] = 'X';
}
if (row+1 > td->numberOfLines)
td->numberOfLines = row+1;
}
int UpdateTreeDisplay(TreeDisplay* td, Tree* t, int depth, int left, int right)
// wypisz drzewo t do ekranu td w obszarze: pionowo od depth do SCREEN_HEIGHT, a poziomowo od left do right;
// funkcja zwraca pozimowa pozycje korzenia drzewa
{
if (t == NULL)
return -1;
int left_width = NumberOfLeaves(t->left);
int right_width = NumberOfLeaves(t->right);
if (left_width + right_width == 0) {
int middle = (left+right)/2;
WriteKeyToTreeDisplay(td, t->key, depth, middle);
return middle;
}
if (left_width == 0) {
int middle = UpdateTreeDisplay(td, t->right, depth+2, left, right);
WriteKeyToTreeDisplay(td, t->key, depth, middle);
WriteSymbolToTreeDisplay(td, DOWN, depth+1, middle);
return middle;
}
if (right_width == 0) {
int middle = UpdateTreeDisplay(td, t->left, depth+2, left, right);
WriteKeyToTreeDisplay(td, t->key, depth, middle);
WriteSymbolToTreeDisplay(td, DOWN, depth+1, middle);
return middle;
}
int k = (right - left - 2) / (left_width + right_width);
int l = UpdateTreeDisplay(td, t->left, depth+2, left, left + left_width*k - 1);
int r = UpdateTreeDisplay(td, t->right, depth+2, left + left_width*k + 1, right-1);
int middle = (l+r)/2;
WriteKeyToTreeDisplay(td, t->key, depth, middle);
WriteSymbolToTreeDisplay(td, DOWN_LEFT_AND_RIGHT, depth+1, middle);
WriteSymbolToTreeDisplay(td, LEFT_DOWN, depth+1, l);
WriteSymbolToTreeDisplay(td, RIGHT_DOWN, depth+1, r);
int i;
for (i = l+1; i < middle; i++)
WriteSymbolToTreeDisplay(td, DASH, depth+1, i);
for (i = middle+1; i < r; i++)
WriteSymbolToTreeDisplay(td, DASH, depth+1, i);
return middle;
}
void DisplayTree(Tree* t)
{
TreeDisplay TD;
TD.numberOfLines = 0;
int i, j;
for (i = 0; i < SCREEN_HEIGHT; i++) {
for (j = 0; j < SCREEN_WIDTH; j++)
TD.screen[i][j] = ' '; // spacja
TD.screen[i][SCREEN_WIDTH] = '\n';
TD.screen[i][SCREEN_WIDTH+1] = '\0';
}
UpdateTreeDisplay(&TD, t, 0, 1, SCREEN_WIDTH - 1);
for (i = 0; i < TD.numberOfLines; i++)
printf("%s", TD.screen[i]);
printf("\n");
}
int DisplayTrack(Tree* t, int k) /*Ta funkcja ma wyświetlać klucze na ścieżce od wierzchołka o danym kluczu do korzenia*/
{
if (t == NULL) return NULL;
if (t->left == NULL && t->right==NULL);
printf("%d, ", t->key);
if (k < t->key)
{
t->left = DisplayTrack(t->left, k);
return t;
}
else if (k > t->key)
{
t->right = DisplayTrack(t->right, k);
return t;
}
else if (t->left == NULL) {
Tree* w = t->right;
free(t);
return w;
}
}
int Sciezka(Tree* t) /*Ta funkcja ma znajdować ścieżkę o maksymalnej sumie kluczy i zwracać te sumę*/
{
int k;
if(t == NULL)
return 0;
if(t->left == NULL && t->right == NULL)
return t->key;
if (k < t->key)
{
t->left = Sciezka(t->left);
return t->key;
}
else if (k > t->key)
{
t->right = Sciezka(t->right);
return t->key;
}
}
int main()
{
printf("____________________________________________________________________________\n");
int i;
srand (time(NULL));
Tree* t = NULL;
for (i = 0; i < 20; i++)
t = InsertBST(t, rand() % 1000);
DisplayTree(t);
int x;
do{
printf("\n____________________________________________________________________________\n");
printf("(1) Najwieksza suma kluczy\n");
printf("(2) Wyswietl klucze na sciezce od wierzcholka o danym kluczu do korzenia\n");
printf("(3) Konczy program\n");
printf("Wybierz numer operacji ktora chcesz wykonac: ");
scanf("%d", &x);
if(x==1)
{
};
if(x==2)
{
ElType b;
printf("Podaj klucz elementu: ");
scanf("%d", &b);
printf("Sciezka: ");
t = DisplayTrack(t, b);
}
if(x==3)
{
printf("Zakonczono dzialanie programu\n");
return ;
}
}while(x<4);
return 0;
}