Skip to content
Snippets Groups Projects
Commit 6ac649ea authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Beispielprogramme 29.1.2018

parent acc2a744
Branches
No related tags found
No related merge requests found
......@@ -7,37 +7,57 @@ typedef struct node
struct node *left, *right;
} node;
void insert (node **root, int content)
{
if (*root == NULL)
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
if (root == NULL)
root = p;
else
{
node *save_root = root;
if (content < root->content)
{
root = root->left;
insert (content);
save_root->left = root;
}
else if (content > (*root)->content)
insert (&(*root)->right, content);
else
insert (&(*root)->left, content);
{
root = root->right;
insert (content);
save_root->right = root;
}
root = save_root;
}
}
void output (node *root)
{
if (root)
{
if (root->left)
output (root->left);
printf ("%d\n", root->content);
if (root->right)
output (root->right);
}
else
printf ("root is NULL\n");
}
int main (void)
{
node *root = NULL;
for (int i = 0; i < 30; i++)
insert (&root, rand () % 1000 + 1);
insert (7);
insert (137);
insert (3);
insert (1117);
insert (42);
insert (13);
output (root);
return 0;
}
......@@ -7,68 +7,55 @@ typedef struct node
struct node *left, *right;
} node;
void insert (node **root, int content)
{
if (*root == NULL)
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
}
else if (content > (*root)->content)
insert (&(*root)->right, content);
if (root == NULL)
root = p;
else
insert (&(*root)->left, content);
}
void output (node *root)
{
if (root)
node *save_root = root;
if (content < root->content)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
root = root->left;
insert (content);
save_root->left = root;
}
int output_tree_layer (node *root, int depth, int current_depth)
{
int result = 0;
if (root)
{
if (output_tree_layer (root->left, depth, current_depth + 1))
result = 1;
if (depth == current_depth)
else
{
printf ("%d ", root->content);
result = 1;
root = root->right;
insert (content);
save_root->right = root;
}
if (output_tree_layer (root->right, depth, current_depth + 1))
result = 1;
root = save_root;
}
return result;
}
void output_tree (node *root)
void output (node *root)
{
if (root)
{
int depth = 0;
while (output_tree_layer (root, depth, 0))
{
printf ("\n");
depth++;
}
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
else
printf ("root is NULL\n");
}
int main (void)
{
node *root = NULL;
for (int i = 0; i < 30; i++)
insert (&root, rand () % 1000 + 1);
output_tree (root);
insert (7);
insert (137);
insert (3);
insert (1117);
insert (42);
insert (13);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
if (root == NULL)
root = p;
else
{
node *save_root = root;
if (content < root->content)
{
root = root->left;
insert (content);
save_root->left = root;
}
else
{
root = root->right;
insert (content);
save_root->right = root;
}
root = save_root;
}
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int main (void)
{
insert (7);
insert (137);
insert (3);
insert (1117);
insert (42);
insert (13);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
node *root = NULL;
node *insert (node *root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
if (root == NULL)
return p;
else
{
if (content < root->content)
{
root->left = insert (root->left, content);
return root;
}
else
{
root->right = insert (root->right, content);
return root;
}
}
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int main (void)
{
root = insert (root, 7);
root = insert (root, 137);
root = insert (root, 3);
root = insert (root, 1117);
root = insert (root, 42);
root = insert (root, 13);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
node *root = NULL;
node *insert (node *root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
if (root == NULL)
return p;
else
{
if (content < root->content)
{
root->left = insert (root->left, content);
return root;
}
else
{
root->right = insert (root->right, content);
return root;
}
}
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int main (void)
{
insert (root, 7);
insert (root, 137);
insert (root, 3);
insert (root, 1117);
insert (root, 42);
insert (root, 13);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
if (*root == NULL)
*root = p;
else
{
if (content < root->content)
insert (&root->left, content);
else
insert (&root->right, content);
}
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 1117);
insert (&root, 42);
insert (&root, 13);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
if (*root == NULL)
*root = p;
else
{
if (content < (*root)->content)
insert (&(*root)->left, content);
else
insert (&(*root)->right, content);
}
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 1117);
insert (&root, 42);
insert (&root, 13);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node *root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
root = p;
}
void output (node *root)
{
if (root)
printf ("%d\n", root->content);
else
printf ("root is NULL\n");
}
int main (void)
{
node *root = NULL;
insert (root, 7);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
root = p;
}
void output (void)
{
if (root)
printf ("%d\n", root->content);
else
printf ("root is NULL\n");
}
int main (void)
{
insert (7);
output ();
return 0;
}
......@@ -7,25 +7,31 @@ typedef struct node
struct node *left, *right;
} node;
void insert (node **root, int content)
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
if (root == NULL)
root = p;
}
void output (node *root)
void output (void)
{
if (root)
printf ("%d\n", root->content);
else
printf ("root is NULL\n");
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
output (root);
insert (7);
insert (137);
insert (3);
output ();
return 0;
}
......@@ -7,28 +7,38 @@ typedef struct node
struct node *left, *right;
} node;
void insert (node **root, int content)
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
if (root == NULL)
root = p;
else
{
if (content > root->content)
root->right = p;
else
root->left = p;
}
}
void output (node *root)
void output (void)
{
if (root)
printf ("%d\n", root->content);
else
printf ("root is NULL\n");
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
insert (7);
insert (137);
insert (3);
output ();
return 0;
}
......@@ -7,43 +7,44 @@ typedef struct node
struct node *left, *right;
} node;
void insert (node **root, int content)
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
if (*root == NULL)
*root = p;
else if (content > (*root)->content)
{
if ((*root)->right == NULL)
(*root)->right = p;
else
/* ??? */;
}
if (root == NULL)
root = p;
else
{
if ((*root)->left == NULL)
(*root)->left = p;
if (content > root->content)
root->right = p;
else
/* ??? */;
root->left = p;
}
}
void output (node *root)
void output (void)
{
if (root)
{
if (root->left)
printf ("%d\n", root->left->content);
printf ("%d\n", root->content);
if (root->right)
printf ("%d\n", root->right->content);
}
else
printf ("root is NULL\n");
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
insert (7);
insert (137);
insert (3);
output ();
return 0;
}
......@@ -7,47 +7,65 @@ typedef struct node
struct node *left, *right;
} node;
void insert (node **root, int content)
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
if (*root == NULL)
*root = p;
else if (content > (*root)->content)
{
if ((*root)->right == NULL)
(*root)->right = p;
if (root == NULL)
root = p;
else
/* ??? */;
{
node *save_root = root;
if (content > root->content)
{
root = root->left;
insert (content);
}
else
{
if ((*root)->left == NULL)
(*root)->left = p;
else
/* ??? */;
root = root->right;
insert (content);
}
root = save_root;
}
}
void output (node *root)
void output (void)
{
if (root)
{
output (root->left);
if (root->left)
{
node *save_root = root;
root = root->left;
output ();
root = save_root;
}
printf ("%d\n", root->content);
output (root->right);
if (root->right)
{
node *save_root = root;
root = root->right;
output ();
root = save_root;
}
}
else
printf ("root is NULL\n");
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
insert (7);
insert (137);
insert (3);
insert (1117);
insert (42);
insert (13);
output ();
return 0;
}
......@@ -7,39 +7,67 @@ typedef struct node
struct node *left, *right;
} node;
void insert (node **root, int content)
{
if (*root == NULL)
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
if (root == NULL)
root = p;
else
{
node *save_root = root;
if (content > root->content)
{
root = root->left;
insert (content);
save_root->left = root;
}
else if (content > (*root)->content)
insert ((*root)->right, content);
else
insert ((*root)->left, content);
{
root = root->right;
insert (content);
save_root->right = root;
}
root = save_root;
}
}
void output (node *root)
void output (void)
{
if (root)
{
output (root->left);
if (root->left)
{
node *save_root = root;
root = root->left;
output ();
root = save_root;
}
printf ("%d\n", root->content);
output (root->right);
if (root->right)
{
node *save_root = root;
root = root->right;
output ();
root = save_root;
}
}
else
printf ("root is NULL\n");
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
insert (7);
insert (137);
insert (3);
insert (1117);
insert (42);
insert (13);
output ();
return 0;
}
......@@ -7,39 +7,67 @@ typedef struct node
struct node *left, *right;
} node;
void insert (node **root, int content)
{
if (*root == NULL)
node *root = NULL;
void insert (int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
if (root == NULL)
root = p;
else
{
node *save_root = root;
if (content < root->content)
{
root = root->left;
insert (content);
save_root->left = root;
}
else if (content > (*root)->content)
insert (&(*root)->right, content);
else
insert (&(*root)->left, content);
{
root = root->right;
insert (content);
save_root->right = root;
}
root = save_root;
}
}
void output (node *root)
void output (void)
{
if (root)
{
output (root->left);
if (root->left)
{
node *save_root = root;
root = root->left;
output ();
root = save_root;
}
printf ("%d\n", root->content);
output (root->right);
if (root->right)
{
node *save_root = root;
root = root->right;
output ();
root = save_root;
}
}
else
printf ("root is NULL\n");
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
insert (7);
insert (137);
insert (3);
insert (1117);
insert (42);
insert (13);
output ();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment