1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
|
#include <stdio.h>
#include <stdlib.h>
struct Knoten
{
struct Knoten *rechts;
struct Knoten *links;
int wert;
};
struct Knoten* ausgeglichen(int MeinArray[], int start, int n)
{
int links, rechts;
struct Knoten* kn;
if(n==0) return NULL;
links=(n-1)/2;
rechts=n-1-links;
kn=(struct Knoten*)malloc(sizeof(struct Knoten));
if(kn==NULL) return NULL; //kein Speicher vorhanden
kn->wert=MeinArray[start+links];
kn->links=ausgeglichen(MeinArray,start,links);
kn->rechts=ausgeglichen(MeinArray,start+links+1,rechts);
return kn;
}
void PrintTree(struct Knoten*kn, int ordnung)
{
int i;
if(kn==NULL) return;
PrintTree(kn->rechts, ordnung+1);
for(i=0;i<ordnung;i++) printf(" ");
printf("%5d\n",kn->wert);
PrintTree(kn->links, ordnung+1);
}
int main(void)
{
int MeinArray[]={6, 7, 8, 9, 12, 18, 22, 33, 40, 41, 45 };
struct Knoten* Wurzel=ausgeglichen(MeinArray,0,sizeof(MeinArray)/sizeof(MeinArray[0]));
PrintTree(Wurzel,0);
getchar();
} |