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:
42:
43:
44:
45:
46:
47:
48:
49:
50:
|
#include <stdio.h>
#include <stdlib.h>
struct Node
{
struct Node *right;
struct Node *left;
int value;
};
void printTree(struct Node* n, int depth)
{
int i;
if(n==NULL) return;
printTree(n->right, depth+1);
for(i=0;i<depth;i++) printf(" ");
printf("%5d\n",n->value);
printTree(n->left, depth+1);
}
void replace(struct Node* n, int old, int new) {
if(n==NULL) return;
replace(n->right, old, new);
if (n->value == old) n->value = new;
replace(n->left, old, new);
}
int main(void)
{
//Baum erstellen
struct Node* root = (struct Node*)malloc(sizeof(struct Node));
root->value = 1;
struct Node* left = (struct Node*)malloc(sizeof(struct Node));
left->value = 2;
struct Node* right = (struct Node*)malloc(sizeof(struct Node));
right->value = 3;
root->left = left;
root->right = right;
struct Node* leftleft = (struct Node*)malloc(sizeof(struct Node));
leftleft->value = 4;
left->left = leftleft;
//Ausgabe zur Kontrolle
printTree(root, 0);
//Ersetzen des Inhalts
replace(root, 2, 5);
replace(root, 3, 7);
printf("\n\n");
//erneute Ausgabe
printTree(root, 0);
} |